Skip to content

Instantly share code, notes, and snippets.

@heruan
Created June 16, 2017 11:02
Show Gist options
  • Save heruan/8dc57d715ce4607b178a9e332a499cdd to your computer and use it in GitHub Desktop.
Save heruan/8dc57d715ce4607b178a9e332a499cdd to your computer and use it in GitHub Desktop.
Vaadin Resource for FileDownload which consumes OutputStream
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.function.Consumer;
import javax.servlet.http.HttpServletResponse;
import com.vaadin.server.Constants;
import com.vaadin.server.DownloadStream;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinResponse;
@SuppressWarnings("serial")
public class DownloadStreamWriter extends DownloadStream {
private final Consumer<OutputStream> streamWriter;
public DownloadStreamWriter(Consumer<OutputStream> streamWriter, String contentType, String fileName) {
super(null, contentType, fileName);
this.streamWriter = streamWriter;
}
@Override
public void writeResponse(VaadinRequest request, VaadinResponse response) throws IOException {
if (this.getParameter("Location") != null) {
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", this.getParameter("Location"));
return;
}
OutputStream out = null;
try {
// Sets content type
response.setContentType(this.getContentType());
// Sets cache headers
response.setCacheTime(this.getCacheTime());
// Copy download stream parameters directly
// to HTTP headers.
final Iterator<String> i = this.getParameterNames();
if (i != null) {
while (i.hasNext()) {
final String param = i.next();
response.setHeader(param, this.getParameter(param));
}
}
// Content-Disposition: attachment generally forces download
String contentDisposition = this.getParameter(CONTENT_DISPOSITION);
if (contentDisposition == null) {
contentDisposition = getContentDispositionFilename(this.getFileName());
}
response.setHeader(CONTENT_DISPOSITION, contentDisposition);
int bufferSize = this.getBufferSize();
if (bufferSize <= 0 || bufferSize > Constants.MAX_BUFFER_SIZE) {
bufferSize = Constants.DEFAULT_BUFFER_SIZE;
}
out = response.getOutputStream();
this.streamWriter.accept(out);
} finally {
tryToCloseStream(out);
}
}
/**
* Helper method that tries to close an output stream and ignores any exceptions.
*
* @param out the output stream to close, <code>null</code> is also supported
*/
static void tryToCloseStream(OutputStream out) {
try {
// try to close output stream (e.g. file handle)
if (out != null) {
out.close();
}
} catch (IOException e1) {
// NOP
}
}
}
import com.vaadin.server.ConnectorResource;
import com.vaadin.server.DownloadStream;
import com.vaadin.util.FileTypeResolver;
@SuppressWarnings("serial")
public class StreamWriterResource implements ConnectorResource {
private final Consumer<OutputStream> streamWriter;
private final String filename;
private final String mimeType;
public StreamWriterResource(Consumer<OutputStream> streamWriter, String filename) {
this.streamWriter = streamWriter;
this.filename = filename;
this.mimeType = FileTypeResolver.getMIMEType(filename);
}
@Override
public String getMIMEType() {
return this.mimeType;
}
@Override
public DownloadStream getStream() {
return new DownloadStreamWriter(this.streamWriter, this.mimeType, this.filename);
}
@Override
public String getFilename() {
return this.filename;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment