]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add content length header for static resources (#11699)
authorHenri Sara <hesara@vaadin.com>
Thu, 25 Apr 2013 08:00:28 +0000 (11:00 +0300)
committerHenri Sara <hesara@vaadin.com>
Thu, 25 Apr 2013 08:00:28 +0000 (11:00 +0300)
Change-Id: Ie307198a05e5c4f1b8bd74ec17185b9086ff0577

server/src/com/vaadin/server/VaadinServlet.java

index 4fde870f74ac7fd5fafacde15f8699fc5937357f..35d5fd7cc1cc412ebdd16da4eef464dce29724aa 100644 (file)
@@ -896,14 +896,32 @@ public class VaadinServlet extends HttpServlet implements Constants {
     protected void writeStaticResourceResponse(HttpServletRequest request,
             HttpServletResponse response, URL resourceUrl) throws IOException {
         // Write the resource to the client.
-        final OutputStream os = response.getOutputStream();
-        final byte buffer[] = new byte[DEFAULT_BUFFER_SIZE];
-        int bytes;
-        InputStream is = resourceUrl.openStream();
-        while ((bytes = is.read(buffer)) >= 0) {
-            os.write(buffer, 0, bytes);
+        URLConnection connection = resourceUrl.openConnection();
+        try {
+            int length = connection.getContentLength();
+            if (length >= 0) {
+                response.setContentLength(length);
+            }
+        } catch (Throwable e) {
+            // This can be ignored, content length header is not required.
+            // Need to close the input stream because of
+            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4257700 to
+            // prevent it from hanging, but that is done below.
+        }
+
+        InputStream is = connection.getInputStream();
+        try {
+            final OutputStream os = response.getOutputStream();
+            final byte buffer[] = new byte[DEFAULT_BUFFER_SIZE];
+            int bytes;
+            while ((bytes = is.read(buffer)) >= 0) {
+                os.write(buffer, 0, bytes);
+            }
+        } finally {
+            if (is != null) {
+                is.close();
+            }
         }
-        is.close();
     }
 
     private URL findResourceURL(String filename, ServletContext sc)