]> source.dussan.org Git - vaadin-framework.git/commitdiff
Move code for writing a DownloadStream to a WrappedResponse
authorLeif Åstrand <leif@vaadin.com>
Wed, 9 Nov 2011 09:09:09 +0000 (11:09 +0200)
committerLeif Åstrand <leif@vaadin.com>
Wed, 9 Nov 2011 09:09:09 +0000 (11:09 +0200)
src/com/vaadin/terminal/DownloadStream.java
src/com/vaadin/terminal/gwt/server/ApplicationResourceHandler.java

index 1460918ca3b09da0a2f5c1bae9e6252ea1c1e3dd..5cebd24001bddc7df62784f299b4192b2f7907ec 100644 (file)
@@ -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
+        }
+    }
+
 }
index a5bec1881d6af3441fdffdd262d0662dcd64d738..9f8bebe60ad7a69148deeafee08558bb82bc4945 100644 (file)
@@ -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);
-            }
-        }
-    }
 }