Author: vgritsenko
Date: Wed Feb 23 14:48:09 2005
New Revision: 155099

URL: http://svn.apache.org/viewcvs?view=rev&rev=155099
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/trunk/src/java/org/apache/cocoon/reading/ImageReader.java
    cocoon/trunk/src/java/org/apache/cocoon/reading/ResourceReader.java

Modified: cocoon/trunk/src/java/org/apache/cocoon/reading/ImageReader.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/reading/ImageReader.java?view=diff&r1=155098&r2=155099
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/reading/ImageReader.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/reading/ImageReader.java Wed Feb 23 
14:48:09 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.
@@ -61,13 +61,13 @@
  *          of the image is kept.
  *     </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>
@@ -90,27 +90,23 @@
  * @version CVS $Id$
  */
 final public class ImageReader extends ResourceReader {
+    private static final boolean GRAYSCALE_DEFAULT = false;
+    private static final boolean ENLARGE_DEFAULT = true;
+    private static final boolean FIT_DEFAULT = false;
 
     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 boolean fitUniform;
-    private final static String FIT_DEFAULT = "false";
+    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);
@@ -123,7 +119,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;
@@ -136,26 +131,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;
-        }   
+        if (par.getParameterAsBoolean("grayscale", GRAYSCALE_DEFAULT)) {
+            this.grayscaleFilter = new 
ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
+        }
 
-        String enlargePar = par.getParameter("allow-enlarging", 
ENLARGE_DEFAULT);
-        enlarge = BooleanUtils.toBoolean(enlargePar);
+        this.enlarge = par.getParameterAsBoolean("allow-enlarging", 
ENLARGE_DEFAULT);
+        this.fitUniform = par.getParameterAsBoolean("fit-uniform", 
FIT_DEFAULT);
 
-        String fitUniformPar = par.getParameter("fit-uniform", FIT_DEFAULT);
-        fitUniform = BooleanUtils.toBoolean(fitUniformPar);
+        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
@@ -211,16 +216,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
@@ -270,15 +272,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);
@@ -287,7 +283,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
@@ -311,8 +308,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]
@@ -326,7 +323,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
      *
@@ -351,8 +348,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/trunk/src/java/org/apache/cocoon/reading/ResourceReader.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/reading/ResourceReader.java?view=diff&r1=155098&r2=155099
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/reading/ResourceReader.java 
(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/reading/ResourceReader.java Wed Feb 
23 14:48:09 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 {


Reply via email to