changeset 1433:cc4e46818377

Finally fixed bug with images not loading from time to time! (hopefully)
author robcast
date Thu, 05 Nov 2015 20:19:38 +0100
parents 9963ffc5c3c4
children dcf933a2bd7e
files common/src/main/java/digilib/conf/DigilibConfiguration.java common/src/main/java/digilib/image/ImageLoaderDocuImage.java servlet/src/main/java/digilib/servlet/ServletOps.java servlet3/src/main/java/digilib/servlet/AsyncServletWorker.java servlet3/src/main/java/digilib/servlet/Scaler.java
diffstat 5 files changed, 37 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/common/src/main/java/digilib/conf/DigilibConfiguration.java	Tue Nov 03 11:25:14 2015 +0100
+++ b/common/src/main/java/digilib/conf/DigilibConfiguration.java	Thu Nov 05 20:19:38 2015 +0100
@@ -57,7 +57,7 @@
 
     /** digilib version */
     public static String getClassVersion() {
-        return "2.3.3";
+        return "2.3.4";
     }
 
     /* non-static getVersion for Java inheritance */
--- a/common/src/main/java/digilib/image/ImageLoaderDocuImage.java	Tue Nov 03 11:25:14 2015 +0100
+++ b/common/src/main/java/digilib/image/ImageLoaderDocuImage.java	Thu Nov 05 20:19:38 2015 +0100
@@ -481,8 +481,22 @@
         } catch (IOException e) {
             logger.error("Error writing image:", e);
             throw new FileOpException("Error writing image!", e);
+        } finally {
+        	if (writer != null) {
+        		writer.dispose();
+        	}
+        	if (imgout != null) {
+        		/* 
+        		 * ImageOutputStream likes to keep ServletOutputStream and close it when disposed.
+        		 * Thanks to Tom Van Wietmarschen's mail to tomcat-users on July 4, 2008!
+        		 */
+        		try {
+					imgout.close();
+				} catch (IOException e) {
+					logger.error("Error closing ImageOutputStream!", e);
+				}
+        	}
         }
-        // TODO: should we: finally { writer.dispose(); }
     }
 
     /* 
--- a/servlet/src/main/java/digilib/servlet/ServletOps.java	Tue Nov 03 11:25:14 2015 +0100
+++ b/servlet/src/main/java/digilib/servlet/ServletOps.java	Thu Nov 05 20:19:38 2015 +0100
@@ -319,11 +319,11 @@
     	}
         //logger.debug("sending to response: ("+ headersToString(response) + ") committed=" + response.isCommitted());
         logger.debug("sending to response. committed=" + response.isCommitted());
-        // TODO: should we erase or replace old last-modified header?
         try {
-            OutputStream outstream = response.getOutputStream();
-            // setup output -- if mime type is set use that otherwise
-            // if source is JPG then dest will be JPG else it's PNG
+            /*
+             *  determine content-type: if mime type is set use that otherwise
+             *  if source is JPG then dest will be JPG else it's PNG
+             */
             if (mimeType == null) {
                 mimeType = img.getMimetype();
                 if (mimeType == null) {
@@ -338,8 +338,20 @@
             } else {
                 mimeType = "image/png";
             }
-            // write the image
+            // set the content type
             response.setContentType(mimeType);
+            String respType = response.getContentType();
+            if (! mimeType.equals(respType)) {
+            	// this shouldn't happen
+            	logger.error("Crap! ServletResponse lost content type! ct="+respType);
+            	// TODO: would this help?
+            	response.getOutputStream().close();
+            	return;
+            }
+            /*
+             * write the image
+             */
+            OutputStream outstream = response.getOutputStream();
             img.writeImage(mimeType, outstream);
         } catch (IOException e) {
             throw new ServletException("Error sending image:", e);
--- a/servlet3/src/main/java/digilib/servlet/AsyncServletWorker.java	Tue Nov 03 11:25:14 2015 +0100
+++ b/servlet3/src/main/java/digilib/servlet/AsyncServletWorker.java	Thu Nov 05 20:19:38 2015 +0100
@@ -113,11 +113,6 @@
              *  send the image
              */
             HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
-            if (response.isCommitted()) {
-            	logger.error("Crap! ServletResponse is already committed! Aborting.");
-            	// what now?
-            	return;
-            }
             ServletOps.sendImage(img, mt, response, logger);
             
             logger.debug("Job done in: "
--- a/servlet3/src/main/java/digilib/servlet/Scaler.java	Tue Nov 03 11:25:14 2015 +0100
+++ b/servlet3/src/main/java/digilib/servlet/Scaler.java	Thu Nov 05 20:19:38 2015 +0100
@@ -229,7 +229,10 @@
         accountlog.debug("request: " + request.getQueryString());
         logger.debug("request: " + request.getQueryString());
         //logger.debug("headers: " + ServletOps.headersToString(request));
-        //logger.debug("response committed=" + response.isCommitted());
+        //logger.debug("processRequest response committed=" + response.isCommitted());
+        if (response.isCommitted()) {
+        	logger.error("Crap: response committed before we got a chance!");
+        }
         final long startTime = System.currentTimeMillis();
 
         // parse request