cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r155087 - in cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading: ImageReader.java ResourceReader.java
Date Wed, 23 Feb 2005 22:17:33 GMT
Author: vgritsenko
Date: Wed Feb 23 14:17:31 2005
New Revision: 155087

URL: http://svn.apache.org/viewcvs?view=rev&rev=155087
Log:
Move response header initialization into the setup phase.
Remove Last-Modified header initialization: this is done in
the environment by request from AbstractProcessingPipeline.

Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ResourceReader.java

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java?view=diff&r1=155086&r2=155087
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ImageReader.java Wed Feb
23 14:17:31 2005
@@ -1,12 +1,12 @@
 /*
  * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -57,13 +57,13 @@
  *         of the image that should be served.
  *     </dd>
  *     <dt>&lt;scale(Red|Green|Blue)&gt;</dt>
- *     <dd>This parameter is optional. When specified it will cause the 
- *         specified color component in the image to be multiplied by the 
+ *     <dd>This parameter is optional. When specified it will cause the
+ *         specified color component in the image to be multiplied by the
  *         specified floating point value.
  *     </dd>
  *     <dt>&lt;offset(Red|Green|Blue)&gt;</dt>
- *     <dd>This parameter is optional. When specified it will cause the 
- *         specified color component in the image to be incremented by the 
+ *     <dd>This parameter is optional. When specified it will cause the
+ *         specified color component in the image to be incremented by the
  *         specified floating point value.
  *     </dd>
  *     <dt>&lt;grayscale&gt;</dt>
@@ -86,24 +86,21 @@
  * @version CVS $Id$
  */
 final public class ImageReader extends ResourceReader {
+    private static final String GRAYSCALE_DEFAULT = "false";
+    private static final boolean ENLARGE_DEFAULT = true;
 
     private int width;
     private int height;
-
     private float[] scaleColor = new float[3];
     private float[] offsetColor = new float[3];
-    private RescaleOp colorFilter = null;
 
     private boolean enlarge;
-    private final static String ENLARGE_DEFAULT = "true";
+    private RescaleOp colorFilter;
+    private ColorConvertOp grayscaleFilter;
 
-    private ColorConvertOp grayscaleFilter = null;
-    private final static String GRAYSCALE_DEFAULT = "false";
 
     public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
-            throws ProcessingException, SAXException, IOException {
-
-        super.setup(resolver, objectModel, src, par);
+    throws ProcessingException, SAXException, IOException {
 
         width = par.getParameterAsInteger("width", 0);
         height = par.getParameterAsInteger("height", 0);
@@ -116,7 +113,6 @@
         offsetColor[2] = par.getParameterAsFloat("offsetBlue", 0.0f);
 
         boolean filterColor = false;
-
         for (int i = 0; i < 3; ++i) {
             if (scaleColor[i] != -1.0f) {
                 filterColor = true;
@@ -129,22 +125,36 @@
         }
 
         if (filterColor) {
-            colorFilter = new RescaleOp(scaleColor, offsetColor, null);
-        } else {
-            colorFilter = null;
+            this.colorFilter = new RescaleOp(scaleColor, offsetColor, null);
         }
 
         String grayscalePar = par.getParameter("grayscale", GRAYSCALE_DEFAULT);
-        if (BooleanUtils.toBoolean(grayscalePar)){            
-            grayscaleFilter = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),
null);
-        } else {
-            grayscaleFilter = null;
-        }   
-        String enlargePar = par.getParameter("allow-enlarging", ENLARGE_DEFAULT);
-        enlarge = BooleanUtils.toBoolean(enlargePar);
+        if (BooleanUtils.toBoolean(grayscalePar)) {
+            this.grayscaleFilter = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),
null);
+        }
+
+        this.enlarge = par.getParameterAsBoolean("allow-enlarging", ENLARGE_DEFAULT);
+
+        super.setup(resolver, objectModel, src, par);
+    }
+
+    protected void setupHeaders() {
+        // Reset byte ranges support for dynamic response
+        if (byteRanges && hasTransform()) {
+            byteRanges = false;
+        }
+
+        super.setupHeaders();
+    }
+
+    /**
+     * @return True if image transform is specified
+     */
+    private boolean hasTransform() {
+        return width > 0 || height > 0 || null != colorFilter || null != grayscaleFilter;
     }
 
-    /** 
+    /**
      * Returns the affine transform that implements the scaling.
      * The behavior is the following: if both the new width and height values
      * are positive, the image is rescaled according to these new values and
@@ -158,7 +168,7 @@
     private AffineTransform getTransform(double ow, double oh, double nw, double nh) {
         double wm = 1.0d;
         double hm = 1.0d;
-        
+
         if (nw > 0) {
             wm = nw / ow;
             if (nh > 0) {
@@ -187,16 +197,13 @@
     }
 
     protected void processStream(InputStream inputStream) throws IOException, ProcessingException
{
-        if (width > 0 || height > 0 || null != colorFilter || null != grayscaleFilter)
{
+        if (hasTransform()) {
             if (getLogger().isDebugEnabled()) {
                 getLogger().debug("image " + ((width == 0) ? "?" : Integer.toString(width))
                                   + "x"    + ((height == 0) ? "?" : Integer.toString(height))
                                   + " expires: " + expires);
             }
 
-            // since we create the image on the fly
-            response.setHeader("Accept-Ranges", "none");
-
             /*
              * NOTE (SM):
              * Due to Bug Id 4502892 (which is found in *all* JVM implementations from
@@ -246,15 +253,9 @@
                 }
 
                 if (!handleJVMBug()) {
-                    if (getLogger().isDebugEnabled()) {
-                        getLogger().debug( "No need to handle JVM bug" );
-                    }
                     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                     encoder.encode(currentImage);
                 } else {
-                    if (getLogger().isDebugEnabled()) {
-                        getLogger().debug( "Need to handle JVM bug" );
-                    }
                     ByteArrayOutputStream bstream = new ByteArrayOutputStream();
                     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bstream);
                     encoder.encode(currentImage);
@@ -263,7 +264,8 @@
 
                 out.flush();
             } catch (ImageFormatException e) {
-                throw new ProcessingException("Error reading the image. Note that only JPEG
images are currently supported.");
+                throw new ProcessingException("Error reading the image. " +
+                                              "Note that only JPEG images are currently supported.");
             } finally {
               // Bugzilla Bug 25069, close inputStream in finally block
               // this will close inputStream even if processStream throws
@@ -287,8 +289,8 @@
      * parameters
     */
     public Serializable getKey() {
-        return this.inputSource.getURI() 
-                + ':' + this.width 
+        return this.inputSource.getURI()
+                + ':' + this.width
                 + ':' + this.height
                 + ":" + this.scaleColor[0]
                 + ":" + this.scaleColor[1]
@@ -302,7 +304,7 @@
 
     /**
      * Determine if workaround for Bug Id 4502892 is neccessary.
-     * This method assumes that Bug is present if 
+     * This method assumes that Bug is present if
      * java.version is undeterminable, and for java.version
      * 1.1, 1.2, 1.3, all other java.version do not need the Bug handling
      *
@@ -327,8 +329,8 @@
             handleJVMBug = true;
         }
         if (getLogger().isDebugEnabled()) {
-            getLogger().debug( "Running java.version " + String.valueOf(java_version) + 
-              " need to handle JVM bug " + String.valueOf(handleJVMBug) );
+            getLogger().debug("Running java " + String.valueOf(java_version) +
+                              " need to handle JVM bug " + String.valueOf(handleJVMBug));
         }
 
         return handleJVMBug;

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ResourceReader.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ResourceReader.java?view=diff&r1=155086&r2=155087
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ResourceReader.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/reading/ResourceReader.java Wed
Feb 23 14:17:31 2005
@@ -153,6 +153,27 @@
         } catch (SourceException e) {
             throw SourceUtil.handle("Error during resolving of '" + src + "'.", e);
         }
+
+        setupHeaders();
+    }
+
+    /**
+     * Setup the response headers: Accept-Ranges, Expires.
+     */
+    protected void setupHeaders() {
+        // Tell the client whether we support byte range requests or not
+        if (byteRanges) {
+            response.setHeader("Accept-Ranges", "bytes");
+        } else {
+            response.setHeader("Accept-Ranges", "none");
+        }
+
+        if (expires > 0) {
+            response.setDateHeader("Expires", System.currentTimeMillis() + expires);
+        } else if (expires == 0) {
+            // See Bug #14048
+            response.addHeader("Vary", "Host");
+        }
     }
 
     /**
@@ -169,6 +190,13 @@
     }
 
     /**
+     * @return True if byte ranges support is enabled and request has range header.
+     */
+    protected boolean hasRanges() {
+        return this.byteRanges && this.request.getHeader("Range") != null;
+    }
+
+    /**
      * Generate the unique key.
      * This key must be unique inside the space of this component.
      *
@@ -185,7 +213,7 @@
      *         component is currently not cacheable.
      */
     public SourceValidity getValidity() {
-        if (request.getHeader("Range") != null) {
+        if (hasRanges()) {
             // This is a byte range request so we can't use the cache, return null.
             return null;
         } else {
@@ -198,7 +226,7 @@
      *         possible to detect
      */
     public long getLastModified() {
-        if(request.getHeader("Range") != null) {
+        if (hasRanges()) {
             // This is a byte range request so we can't use the cache, return null.
             return 0;
         }
@@ -221,13 +249,6 @@
         byte[] buffer = new byte[bufferSize];
         int length = -1;
 
-        // tell the client whether we support byte range requests or not
-        if(byteRanges) {
-            response.setHeader("Accept-Ranges", "bytes");
-        } else {
-            response.setHeader("Accept-Ranges", "none");
-        }
-
         String ranges = request.getHeader("Range");
 
         ByteRange byteRange;
@@ -265,7 +286,6 @@
             }
 
             response.setHeader("Content-Range", entityRange + "/" + entityLength);
-
             if (response instanceof HttpResponse) {
                 // Response with status 206 (Partial content)
                 ((HttpResponse)response).setStatus(206);
@@ -300,18 +320,6 @@
     public void generate()
     throws IOException, ProcessingException {
         try {
-            if (expires > 0) {
-                response.setDateHeader("Expires", System.currentTimeMillis() + expires);
-            } else if (expires == 0) {
-                // See Bug #14048
-                response.addHeader("Vary", "Host");
-            }
-
-            long lastModified = getLastModified();
-            if (lastModified > 0) {
-                response.setDateHeader("Last-Modified", lastModified);
-            }
-
             InputStream inputStream;
             try {
                 inputStream = inputSource.getInputStream();
@@ -319,9 +327,7 @@
                 throw SourceUtil.handle("Error during resolving of the input stream", e);
             }
 
-            // Bugzilla Bug #25069, close inputStream in finally block.
-            // This will close inputStream even if processStream throws
-            // an exception
+            // Bugzilla Bug #25069: Close inputStream in finally block.
             try {
                 processStream(inputStream);
             } finally {



Mime
View raw message