.getParameter(DownloadStream.CONTENT_DISPOSITION); if (contentDisposition == null) { contentDisposition = "attachment; " + DownloadStream .getContentDispositionFilename(stream.getFileName()); stream.setParameter(DownloadStream.CONTENT_DISPOSITION, contentDisposition); stream.setContentType("application/octet-stream;charset=UTF-8"); stream.writeResponse(request, response); return true;
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); 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()); int bufferSize = getBufferSize(); if (bufferSize <= 0 || bufferSize > Constants.MAX_BUFFER_SIZE) { bufferSize = Constants.DEFAULT_BUFFER_SIZE; tryToCloseStream(out);
@Override public DownloadStream getStream() { try { final DownloadStream ds = new DownloadStream( new FileInputStream(sourceFile), getMIMEType(), getFilename()); ds.setParameter("Content-Length", String.valueOf(sourceFile.length())); ds.setBufferSize(getBufferSize()); ds.setCacheTime(getCacheTime()); return ds; } catch (final FileNotFoundException e) { throw new RuntimeException( "File not found: " + sourceFile.getName(), e); } }
/** * Creates a new instance of DownloadStream. */ public DownloadStream(InputStream stream, String contentType, String fileName) { setStream(stream); setContentType(contentType); setFileName(fileName); }
@Override public DownloadStream getStream() { final DownloadStream ds = new DownloadStream( getAssociatedClass().getResourceAsStream(resourceName), getMIMEType(), getFilename()); ds.setBufferSize(getBufferSize()); ds.setCacheTime(getCacheTime()); return ds; }
/** * Set DownloadStream-Parameter "Content-Disposition" to atachment, * therefore the Stream is downloaded and is not parsed as for example * "normal" xml. */ @Override public DownloadStream getStream() { DownloadStream ds = super.getStream(); ds.setParameter("Content-Disposition", "attachment; filename=\"" + getFilename() + "\""); return ds; }
@Override public DownloadStream getStream() { ByteArrayInputStream in = new ByteArrayInputStream( attachment.getBody()); DownloadStream ds = new DownloadStream(in, attachment.getMIMEType(), attachment.getFileName()); // Need a file download POPUP ds.setParameter("Content-Disposition", "attachment; filename="+attachment.getFileName()); return ds; } };
ds.setCacheTime(getCacheTime()); String fileName = URLEncoder.encode(retrievalName, "UTF8"); ds.setParameter("Content-Disposition", "attachment; filename*=\"utf-8'" + fileName + "\""); return ds;
@Override public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path) throws IOException { Matcher matcher = Pattern.compile("(\\d+)(/.*)?").matcher(path); if (!matcher.matches()) { return super.handleConnectorRequest(request, response, path); } DownloadStream stream; VaadinSession session = getSession(); session.lock(); try { List<URLReference> sources = getState().sources; int sourceIndex = Integer.parseInt(matcher.group(1)); if (sourceIndex < 0 || sourceIndex >= sources.size()) { getLogger().log(Level.WARNING, "Requested source index {0} is out of bounds", sourceIndex); return false; } URLReference reference = sources.get(sourceIndex); ConnectorResource resource = (ConnectorResource) ResourceReference .getResource(reference); stream = resource.getStream(); } finally { session.unlock(); } stream.writeResponse(request, response); return true; }
OutputStream os = new FileOutputStream(filePath, false); byte[] buffer = new byte[1024]; int bytesRead; //read from is to buffer try { DownloadStream stream = resource.getStream(); while(true) { bytesRead = stream.getStream().read(buffer); if(bytesRead == -1) break; os.write(buffer, 0, bytesRead); } os.flush(); os.close(); } catch (IOException e) { e.printStackTrace(); }
/** * Set DownloadStream-Parameter "Content-Disposition" to atachment, * therefore the Stream is downloaded and is not parsed as for example * "normal" xml. */ @Override public DownloadStream getStream() { DownloadStream ds = super.getStream(); ds.setParameter("Content-Disposition", "attachment; filename=\"" + getFilename() + "\""); return ds; }
@Override public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path) throws IOException { DownloadStream stream = null; String[] parts = path.split("/", 2); String key = parts[0]; VaadinSession session = getSession(); session.lock(); try { ConnectorResource resource = (ConnectorResource) getResource(key); if (resource == null) { return false; } stream = resource.getStream(); } finally { session.unlock(); } stream.writeResponse(request, response); return true; }
@Override public DownloadStream getStream() { final StreamSource ss = getStreamSource(); if (ss == null) { return null; } final DownloadStream ds = new DownloadStream(ss.getStream(), getMIMEType(), getFilename()); ds.setBufferSize(getBufferSize()); ds.setCacheTime(getCacheTime()); return ds; }
inStream = res.getStream().getStream();
String contentDisposition = stream.getParameter(DownloadStream.CONTENT_DISPOSITION); if (contentDisposition == null) { DownloadStream.getContentDispositionFilename(stream.getFileName()); stream.setParameter(DownloadStream.CONTENT_DISPOSITION, contentDisposition); stream.setContentType("application/octet-stream;charset=UTF-8"); } else { if (StringUtils.isNotEmpty(stream.getContentType())) { stream.setContentType(stream.getContentType() + ";charset=UTF-8\""); } else { stream.setContentType(";charset=UTF-8\""); stream.writeResponse(request, response); return true;
/** * Set DownloadStream-Parameter "Content-Disposition" to atachment, * therefore the Stream is downloaded and is not parsed as for example * "normal" xml. */ @Override public DownloadStream getStream() { DownloadStream ds = super.getStream(); ds.setParameter("Content-Disposition", "attachment; filename=\"" + getFilename() + "\""); return ds; }
stream.writeResponse(request, response); return true;
if (stream.getParameter("Content-Disposition") == null) { stream.setParameter("Content-Disposition", "attachment; filename=\"" + stream.getFileName() + "\""); stream.setContentType("application/octet-stream;charset=UTF-8"); session.unlock(); stream.writeResponse(request, response); return true;
@Override public DownloadStream getStream() { final DownloadStream stream = super.getStream(); if (StringUtils.isNotBlank(getFilename())) { String encodedFilename = EncodeUtil.rfc5987Encode(getFilename()); stream.setParameter("Content-Disposition", String.format("attachment;filename=\"%s\"; filename*=utf-8''%s", encodedFilename, encodedFilename)); } return stream; }
@Test public void testContentDispositionHeaderIsSetCorrectly() throws Exception { // GIVEN String encodedFileName = EncodeUtil.rfc5987Encode(fileName); action = new DownloadBinaryAction<DownloadBinaryActionDefinition>(definition, item); DownloadStreamResource downloadStreamResource = action.getStreamResource(inputStream, fileName); String expectedContentDispositionHeaderValue = String.format("attachment;filename=\"%s\"; filename*=utf-8''%<s", encodedFileName); VaadinRequest vaadinRequest = mock(VaadinRequest.class); VaadinResponse vaadinResponse = mock(VaadinResponse.class); // WHEN downloadStreamResource.getStream().writeResponse(vaadinRequest, vaadinResponse); // THEN // At least as of 7.6 Vaadin may set content-disposition header twice with the same value verify(vaadinResponse, atLeastOnce()).setHeader("Content-Disposition", expectedContentDispositionHeaderValue); }