changeset 803:225054177f67 jquery

remove all ServletOutputStream.flush(). (stupid me ;-)
author robcast
date Sat, 19 Feb 2011 13:03:31 +0100
parents 12f790cb30de
children 1b1728926534
files servlet/src/digilib/servlet/Scaler.java servlet/src/digilib/servlet/ServletOps.java
diffstat 2 files changed, 27 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/servlet/Scaler.java	Fri Feb 18 23:13:26 2011 +0100
+++ b/servlet/src/digilib/servlet/Scaler.java	Sat Feb 19 13:03:31 2011 +0100
@@ -30,7 +30,7 @@
 public class Scaler extends HttpServlet {
 
     /** digilib servlet version (for all components) */
-    public static final String version = "1.8.4a";
+    public static final String version = "1.8.4a2";
 
     /** servlet error codes */
     public static enum Error {UNKNOWN, AUTH, FILE, IMAGE};
@@ -309,6 +309,11 @@
                 img = this.errorImgFile;
                 status = HttpServletResponse.SC_BAD_REQUEST;
             }
+            if (response.isCommitted()) {
+                // response already committed
+                logger.error("Unable to send error: " + msg);
+                return;
+            }
             if (type == ErrMsg.TEXT) {
                 ServletOps.htmlMessage(msg, response);
             } else if (type == ErrMsg.CODE) {
--- a/servlet/src/digilib/servlet/ServletOps.java	Fri Feb 18 23:13:26 2011 +0100
+++ b/servlet/src/digilib/servlet/ServletOps.java	Sat Feb 19 13:03:31 2011 +0100
@@ -222,6 +222,10 @@
     public static void sendFile(File f, String mt, String name, HttpServletResponse response, Logger logger)
             throws ImageOpException, ServletException {
         logger.debug("sendRawFile(" + mt + ", " + f + ")");
+    	if (response.isCommitted()) {
+        	logger.warn("sendFile: response already committed!");
+        	//return;
+    	}
         if (mt == null) {
             // auto-detect mime-type
             mt = FileOps.mimeForFile(f);
@@ -238,22 +242,30 @@
             }
             response.addHeader("Content-Disposition", "attachment; filename=\""+name+"\"");
         }
+        FileInputStream inFile = null;
         try {
-            FileInputStream inFile = new FileInputStream(f);
+            inFile = new FileInputStream(f);
             OutputStream outStream = response.getOutputStream();
+            // TODO: should we set content length? 
+            // see http://www.prozesse-und-systeme.de/servletFlush.html
             response.setContentLength( (int) f.length());
             byte dataBuffer[] = new byte[4096];
             int len;
             while ((len = inFile.read(dataBuffer)) != -1) {
                 // copy out file
                 outStream.write(dataBuffer, 0, len);
-                outStream.flush();
             }
-            response.flushBuffer();
-            inFile.close();
         } catch (IOException e) {
             logger.error("Error sending file:", e);
             throw new ServletException("Error sending file:", e);
+        } finally {
+            try {
+                if (inFile != null) {
+                    inFile.close();
+                }
+            } catch (IOException e) {
+                // nothing to do
+            }
         }
     }
 
@@ -285,6 +297,10 @@
     public static void sendImage(DocuImage img, String mimeType,
             HttpServletResponse response, Logger logger) throws ImageOpException,
             ServletException {
+    	if (response.isCommitted()) {
+        	logger.warn("sendImage: response already committed!");
+        	//return;
+    	}
         try {
             OutputStream outstream = response.getOutputStream();
             // setup output -- if mime type is set use that otherwise
@@ -301,13 +317,11 @@
             // write the image
             response.setContentType(mimeType);
             img.writeImage(mimeType, outstream);
-            outstream.flush();
         } catch (IOException e) {
             logger.error("Error sending image:", e);
             throw new ServletException("Error sending image:", e);
-        } finally {
-            img.dispose();
         }
+        // TODO: should we: finally { img.dispose(); }
     }
 
 }
\ No newline at end of file