]> source.dussan.org Git - vaadin-framework.git/commitdiff
#6499 AbstractApplicationServlet#handleDownload() does not close streams in case...
authorLeif Åstrand <leif@vaadin.com>
Fri, 12 Aug 2011 06:47:06 +0000 (06:47 +0000)
committerLeif Åstrand <leif@vaadin.com>
Fri, 12 Aug 2011 06:47:06 +0000 (06:47 +0000)
svn changeset:20333/svn branch:6.7

src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java
src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java
src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java

index b4f194c7f7a0499e0167dd9329194dcfd3eaa0ad..4abb3b6fd282672994083e54a5a37ec063fa810d 100644 (file)
@@ -613,59 +613,67 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet
         final InputStream data = stream.getStream();
         if (data != null) {
 
-            // Sets content type
-            response.setContentType(stream.getContentType());
-
-            // Sets cache headers
-            final long cacheTime = stream.getCacheTime();
-            if (cacheTime <= 0) {
-                response.setProperty("Cache-Control", "no-cache");
-                response.setProperty("Pragma", "no-cache");
-                response.setProperty("Expires", "0");
-            } else {
-                response.setProperty("Cache-Control", "max-age=" + cacheTime
-                        / 1000);
-                response.setProperty("Expires", "" + System.currentTimeMillis()
-                        + cacheTime);
-                // Required to apply caching in some Tomcats
-                response.setProperty("Pragma", "cache");
-            }
+            OutputStream out = null;
+            try {
+
+                // Sets content type
+                response.setContentType(stream.getContentType());
 
-            // 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.setProperty(param, stream.getParameter(param));
+                // Sets cache headers
+                final long cacheTime = stream.getCacheTime();
+                if (cacheTime <= 0) {
+                    response.setProperty("Cache-Control", "no-cache");
+                    response.setProperty("Pragma", "no-cache");
+                    response.setProperty("Expires", "0");
+                } else {
+                    response.setProperty("Cache-Control", "max-age="
+                            + cacheTime / 1000);
+                    response.setProperty("Expires",
+                            "" + System.currentTimeMillis() + cacheTime);
+                    // Required to apply caching in some Tomcats
+                    response.setProperty("Pragma", "cache");
                 }
-            }
 
-            // 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.setProperty("Content-Disposition",
-                        contentDispositionValue);
-            }
+                // 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.setProperty(param, stream.getParameter(param));
+                    }
+                }
 
-            int bufferSize = stream.getBufferSize();
-            if (bufferSize <= 0 || bufferSize > MAX_BUFFER_SIZE) {
-                bufferSize = DEFAULT_BUFFER_SIZE;
-            }
-            final byte[] buffer = new byte[bufferSize];
-            int bytesRead = 0;
+                // 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.setProperty("Content-Disposition",
+                            contentDispositionValue);
+                }
 
-            final OutputStream out = response.getPortletOutputStream();
+                int bufferSize = stream.getBufferSize();
+                if (bufferSize <= 0 || bufferSize > MAX_BUFFER_SIZE) {
+                    bufferSize = DEFAULT_BUFFER_SIZE;
+                }
+                final byte[] buffer = new byte[bufferSize];
+                int bytesRead = 0;
 
-            while ((bytesRead = data.read(buffer)) > 0) {
-                out.write(buffer, 0, bytesRead);
-                out.flush();
+                out = response.getPortletOutputStream();
+
+                while ((bytesRead = data.read(buffer)) > 0) {
+                    out.write(buffer, 0, bytesRead);
+                    out.flush();
+                }
+                out.close();
+            } finally {
+                AbstractCommunicationManager.tryToCloseStream(data);
+                AbstractCommunicationManager.tryToCloseStream(out);
             }
-            out.close();
         }
     }
 
index c394e8ae6a867ad1abec796a46bf479eb95c0cb2..2fcbf3a850f0b76b7828f195096801356d40b3f6 100644 (file)
@@ -887,61 +887,68 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements
         final InputStream data = stream.getStream();
         if (data != null) {
 
-            // Sets content type
-            response.setContentType(stream.getContentType());
-
-            // Sets cache headers
-            final long cacheTime = stream.getCacheTime();
-            if (cacheTime <= 0) {
-                response.setHeader("Cache-Control", "no-cache");
-                response.setHeader("Pragma", "no-cache");
-                response.setDateHeader("Expires", 0);
-            } else {
-                response.setHeader("Cache-Control", "max-age=" + cacheTime
-                        / 1000);
-                response.setDateHeader("Expires", System.currentTimeMillis()
-                        + cacheTime);
-                response.setHeader("Pragma", "cache"); // Required to apply
-                // caching in some
-                // Tomcats
-            }
+            OutputStream out = null;
+            try {
+                // Sets content type
+                response.setContentType(stream.getContentType());
+
+                // Sets cache headers
+                final long cacheTime = stream.getCacheTime();
+                if (cacheTime <= 0) {
+                    response.setHeader("Cache-Control", "no-cache");
+                    response.setHeader("Pragma", "no-cache");
+                    response.setDateHeader("Expires", 0);
+                } else {
+                    response.setHeader("Cache-Control", "max-age=" + cacheTime
+                            / 1000);
+                    response.setDateHeader("Expires",
+                            System.currentTimeMillis() + cacheTime);
+                    response.setHeader("Pragma", "cache"); // Required to apply
+                    // caching in some
+                    // Tomcats
+                }
 
-            // 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));
+                // 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);
-            }
+                // 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 > MAX_BUFFER_SIZE) {
-                bufferSize = DEFAULT_BUFFER_SIZE;
-            }
-            final byte[] buffer = new byte[bufferSize];
-            int bytesRead = 0;
+                int bufferSize = stream.getBufferSize();
+                if (bufferSize <= 0 || bufferSize > MAX_BUFFER_SIZE) {
+                    bufferSize = DEFAULT_BUFFER_SIZE;
+                }
+                final byte[] buffer = new byte[bufferSize];
+                int bytesRead = 0;
 
-            final OutputStream out = response.getOutputStream();
+                out = response.getOutputStream();
 
-            while ((bytesRead = data.read(buffer)) > 0) {
-                out.write(buffer, 0, bytesRead);
-                out.flush();
+                while ((bytesRead = data.read(buffer)) > 0) {
+                    out.write(buffer, 0, bytesRead);
+                    out.flush();
+                }
+                out.close();
+                data.close();
+            } finally {
+                AbstractCommunicationManager.tryToCloseStream(out);
+                AbstractCommunicationManager.tryToCloseStream(data);
             }
-            out.close();
-            data.close();
         }
 
     }
index b566077ef129334ac0ec97dcc9f7d0a1380b22a6..a7302775555e01bd73195f3aa160011c0eb690fd 100644 (file)
@@ -607,7 +607,7 @@ public abstract class AbstractCommunicationManager implements
         return startedEvent.isDisposed();
     }
 
-    private void tryToCloseStream(OutputStream out) {
+    static void tryToCloseStream(OutputStream out) {
         try {
             // try to close output stream (e.g. file handle)
             if (out != null) {
@@ -618,6 +618,17 @@ public abstract class AbstractCommunicationManager implements
         }
     }
 
+    static void tryToCloseStream(InputStream in) {
+        try {
+            // try to close output stream (e.g. file handle)
+            if (in != null) {
+                in.close();
+            }
+        } catch (IOException e1) {
+            // NOP
+        }
+    }
+
     /**
      * Removes any possible path information from the filename and returns the
      * filename. Separators / and \\ are used.