diff options
author | Leif Åstrand <leif@vaadin.com> | 2011-11-09 11:09:09 +0200 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2011-11-09 11:09:09 +0200 |
commit | fd93e2ae8dabab1184068d578b254653e363ce55 (patch) | |
tree | bfa252237f518e4de9062a6734c98f76f05264b5 /src/com | |
parent | 6d2cd8be1595b22aa84d85e1be9a363fb9998b0c (diff) | |
download | vaadin-framework-fd93e2ae8dabab1184068d578b254653e363ce55.tar.gz vaadin-framework-fd93e2ae8dabab1184068d578b254653e363ce55.zip |
Move code for writing a DownloadStream to a WrappedResponse
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/vaadin/terminal/DownloadStream.java | 94 | ||||
-rw-r--r-- | src/com/vaadin/terminal/gwt/server/ApplicationResourceHandler.java | 75 |
2 files changed, 95 insertions, 74 deletions
diff --git a/src/com/vaadin/terminal/DownloadStream.java b/src/com/vaadin/terminal/DownloadStream.java index 1460918ca3..5cebd24001 100644 --- a/src/com/vaadin/terminal/DownloadStream.java +++ b/src/com/vaadin/terminal/DownloadStream.java @@ -4,12 +4,18 @@ package com.vaadin.terminal; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.Serializable; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import javax.servlet.http.HttpServletResponse; + +import com.vaadin.terminal.gwt.server.Constants; + /** * Downloadable stream. * @@ -203,4 +209,92 @@ public class DownloadStream implements Serializable { this.bufferSize = bufferSize; } + public void writeTo(WrappedResponse response) throws IOException { + if (getParameter("Location") != null) { + response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); + response.setHeader("Location", getParameter("Location")); + return; + } + + // Download from given stream + final InputStream data = getStream(); + if (data != null) { + + OutputStream out = null; + try { + // Sets content type + response.setContentType(getContentType()); + + // Sets cache headers + response.setCacheTime(getCacheTime()); + + // Copy download stream parameters directly + // to HTTP headers. + final Iterator<String> i = getParameterNames(); + if (i != null) { + while (i.hasNext()) { + final String param = i.next(); + response.setHeader(param, getParameter(param)); + } + } + + // suggest local filename from DownloadStream if + // Content-Disposition + // not explicitly set + String contentDispositionValue = getParameter("Content-Disposition"); + if (contentDispositionValue == null) { + contentDispositionValue = "filename=\"" + getFileName() + + "\""; + response.setHeader("Content-Disposition", + contentDispositionValue); + } + + 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) { + // Avoid chunked encoding for small resources + out.flush(); + } + } + } finally { + tryToCloseStream(out); + tryToCloseStream(data); + } + } + } + + static void tryToCloseStream(OutputStream out) { + try { + // try to close output stream (e.g. file handle) + if (out != null) { + out.close(); + } + } catch (IOException e1) { + // NOP + } + } + + static void tryToCloseStream(InputStream in) { + try { + // try to close output stream (e.g. file handle) + if (in != null) { + in.close(); + } + } catch (IOException e1) { + // NOP + } + } + } diff --git a/src/com/vaadin/terminal/gwt/server/ApplicationResourceHandler.java b/src/com/vaadin/terminal/gwt/server/ApplicationResourceHandler.java index a5bec1881d..9f8bebe60a 100644 --- a/src/com/vaadin/terminal/gwt/server/ApplicationResourceHandler.java +++ b/src/com/vaadin/terminal/gwt/server/ApplicationResourceHandler.java @@ -1,14 +1,9 @@ package com.vaadin.terminal.gwt.server; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.servlet.http.HttpServletResponse; - import com.vaadin.Application; import com.vaadin.terminal.ApplicationResource; import com.vaadin.terminal.DownloadStream; @@ -34,7 +29,7 @@ public class ApplicationResourceHandler implements RequestHandler { DownloadStream stream = resource.getStream(); if (stream != null) { stream.setCacheTime(resource.getCacheTime()); - serveResource(stream, response); + stream.writeTo(response); return true; } } @@ -42,72 +37,4 @@ public class ApplicationResourceHandler implements RequestHandler { return false; } - - protected void serveResource(DownloadStream stream, WrappedResponse response) - throws IOException { - if (stream.getParameter("Location") != null) { - response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); - response.setHeader("Location", stream.getParameter("Location")); - return; - } - - // Download from given stream - final InputStream data = stream.getStream(); - if (data != null) { - - OutputStream out = null; - try { - // Sets content type - response.setContentType(stream.getContentType()); - - // Sets cache headers - response.setCacheTime(stream.getCacheTime()); - - // Copy download stream parameters directly - // to HTTP headers. - final Iterator<String> i = stream.getParameterNames(); - if (i != null) { - while (i.hasNext()) { - final String param = i.next(); - response.setHeader(param, stream.getParameter(param)); - } - } - - // suggest local filename from DownloadStream if - // Content-Disposition - // not explicitly set - String contentDispositionValue = stream - .getParameter("Content-Disposition"); - if (contentDispositionValue == null) { - contentDispositionValue = "filename=\"" - + stream.getFileName() + "\""; - response.setHeader("Content-Disposition", - contentDispositionValue); - } - - int bufferSize = stream.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) { - // Avoid chunked encoding for small resources - out.flush(); - } - } - } finally { - AbstractCommunicationManager.tryToCloseStream(out); - AbstractCommunicationManager.tryToCloseStream(data); - } - } - } } |