@Test public void contentDispositionFilenameIsUtf8Encoded() throws IOException { VaadinResponse response = mock(VaadinResponse.class); stream.writeResponse(mock(VaadinRequest.class), response); String encodedFileName = URLEncoder.encode(filename, "utf-8"); verify(response).setHeader(eq(DownloadStream.CONTENT_DISPOSITION), contains(String.format("filename=\"%s\";", encodedFileName))); verify(response) .setHeader( eq(DownloadStream.CONTENT_DISPOSITION), contains(String.format("filename*=utf-8''%s", encodedFileName))); }
public void writeResponse(VaadinRequest request, VaadinResponse response) throws IOException { if (getParameter("Location") != null) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", getParameter("Location")); return; } final InputStream data = getStream(); if (data == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } if (data != null) { OutputStream out = null; try { response.setContentType(getContentType()); response.setCacheTime(getCacheTime()); final Iterator<String> i = getParameterNames(); if (i != null) { while (i.hasNext()) { final String param = i.next(); response.setHeader(param, getParameter(param)); } } String contentDisposition = getParameter(CONTENT_DISPOSITION); if (contentDisposition == null) { contentDisposition = getContentDispositionFilename(getFileName()); } response.setHeader(CONTENT_DISPOSITION, contentDisposition); int bufferSize = getBufferSize(); if (bufferSize <= 0 || bufferSize > Constants.MAX_BUFFER_SIZE) { bufferSize = Constants.DEFAULT_BUFFER_SIZE; } final byte[] buffer = new byte[bufferSize]; int bytesRead = 0; out = response.getOutputStream(); long totalWritten = 0; while ((bytesRead = data.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); totalWritten += bytesRead; if (totalWritten >= buffer.length) { out.flush(); } } } finally { tryToCloseStream(out); tryToCloseStream(data); } } }
DownloadStream implements Serializable { public void writeResponse(VaadinRequest request, VaadinResponse response) throws IOException { if (getParameter("Location") != null) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", getParameter("Location")); return; } final InputStream data = getStream(); if (data == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } if (data != null) { OutputStream out = null; try { response.setContentType(getContentType()); response.setCacheTime(getCacheTime()); final Iterator<String> i = getParameterNames(); if (i != null) { while (i.hasNext()) { final String param = i.next(); response.setHeader(param, getParameter(param)); } } String contentDisposition = getParameter(CONTENT_DISPOSITION); if (contentDisposition == null) { contentDisposition = getContentDispositionFilename(getFileName()); } response.setHeader(CONTENT_DISPOSITION, contentDisposition); int bufferSize = getBufferSize(); if (bufferSize <= 0 || bufferSize > Constants.MAX_BUFFER_SIZE) { bufferSize = Constants.DEFAULT_BUFFER_SIZE; } final byte[] buffer = new byte[bufferSize]; int bytesRead = 0; out = response.getOutputStream(); long totalWritten = 0; while ((bytesRead = data.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); totalWritten += bytesRead; if (totalWritten >= buffer.length) { out.flush(); } } } finally { tryToCloseStream(out); tryToCloseStream(data); } } } }
DownloadStream implements Serializable { public void writeResponse(VaadinRequest request, VaadinResponse response) throws IOException { if (getParameter("Location") != null) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", getParameter("Location")); return; } final InputStream data = getStream(); if (data == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } if (data != null) { OutputStream out = null; try { response.setContentType(getContentType()); response.setCacheTime(getCacheTime()); final Iterator<String> i = getParameterNames(); if (i != null) { while (i.hasNext()) { final String param = i.next(); response.setHeader(param, getParameter(param)); } } String contentDisposition = getParameter(CONTENT_DISPOSITION); if (contentDisposition == null) { contentDisposition = getContentDispositionFilename(getFileName()); } response.setHeader(CONTENT_DISPOSITION, contentDisposition); int bufferSize = getBufferSize(); if (bufferSize <= 0 || bufferSize > Constants.MAX_BUFFER_SIZE) { bufferSize = Constants.DEFAULT_BUFFER_SIZE; } final byte[] buffer = new byte[bufferSize]; int bytesRead = 0; out = response.getOutputStream(); long totalWritten = 0; while ((bytesRead = data.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); totalWritten += bytesRead; if (totalWritten >= buffer.length) { out.flush(); } } } finally { tryToCloseStream(out); tryToCloseStream(data); } } } DownloadStream(InputStream stream, String contentType,
            String fileName); }
DownloadStream implements Serializable { public void writeResponse(VaadinRequest request, VaadinResponse response) throws IOException { if (getParameter("Location") != null) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", getParameter("Location")); return; } final InputStream data = getStream(); if (data == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } if (data != null) { OutputStream out = null; try { response.setContentType(getContentType()); response.setCacheTime(getCacheTime()); final Iterator<String> i = getParameterNames(); if (i != null) { while (i.hasNext()) { final String param = i.next(); response.setHeader(param, getParameter(param)); } } String contentDisposition = getParameter(CONTENT_DISPOSITION); if (contentDisposition == null) { contentDisposition = getContentDispositionFilename(getFileName()); } response.setHeader(CONTENT_DISPOSITION, contentDisposition); int bufferSize = getBufferSize(); if (bufferSize <= 0 || bufferSize > Constants.MAX_BUFFER_SIZE) { bufferSize = Constants.DEFAULT_BUFFER_SIZE; } final byte[] buffer = new byte[bufferSize]; int bytesRead = 0; out = response.getOutputStream(); long totalWritten = 0; while ((bytesRead = data.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); totalWritten += bytesRead; if (totalWritten >= buffer.length) { out.flush(); } } } finally { tryToCloseStream(out); tryToCloseStream(data); } } } DownloadStream(InputStream stream, String contentType,
            String fileName); InputStream getStream(); void setStream(InputStream stream); String getContentType(); void setContentType(String contentType); String getFileName(); void setFileName(String fileName); void setParameter(String name, String value); String getParameter(String name); Iterator<String> getParameterNames(); long getCacheTime(); void setCacheTime(long cacheTime); int getBufferSize(); void setBufferSize(int bufferSize); void writeResponse(VaadinRequest request, VaadinResponse response); static String getContentDispositionFilename(String filename); }
DownloadStream implements Serializable { public void writeResponse(VaadinRequest request, VaadinResponse response) throws IOException { if (getParameter("Location") != null) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", getParameter("Location")); return; } final InputStream data = getStream(); if (data == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); return; } if (data != null) { OutputStream out = null; try { response.setContentType(getContentType()); response.setCacheTime(getCacheTime()); final Iterator<String> i = getParameterNames(); if (i != null) { while (i.hasNext()) { final String param = i.next(); response.setHeader(param, getParameter(param)); } } String contentDisposition = getParameter(CONTENT_DISPOSITION); if (contentDisposition == null) { contentDisposition = getContentDispositionFilename(getFileName()); } response.setHeader(CONTENT_DISPOSITION, contentDisposition); int bufferSize = getBufferSize(); if (bufferSize <= 0 || bufferSize > Constants.MAX_BUFFER_SIZE) { bufferSize = Constants.DEFAULT_BUFFER_SIZE; } final byte[] buffer = new byte[bufferSize]; int bytesRead = 0; out = response.getOutputStream(); long totalWritten = 0; while ((bytesRead = data.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); totalWritten += bytesRead; if (totalWritten >= buffer.length) { out.flush(); } } } finally { tryToCloseStream(out); tryToCloseStream(data); } } } DownloadStream(InputStream stream, String contentType,
            String fileName); InputStream getStream(); void setStream(InputStream stream); String getContentType(); void setContentType(String contentType); String getFileName(); void setFileName(String fileName); void setParameter(String name, String value); String getParameter(String name); Iterator<String> getParameterNames(); long getCacheTime(); void setCacheTime(long cacheTime); int getBufferSize(); void setBufferSize(int bufferSize); void writeResponse(VaadinRequest request, VaadinResponse response); static String getContentDispositionFilename(String filename); static final String CONTENT_DISPOSITION; static final long MAX_CACHETIME; static final long DEFAULT_CACHETIME; }