Save This Page
Home » cactus-1.8.0-src » org.apache.cactus.sample.servlet.util » [javadoc | source]
    1   /* 
    2    * ========================================================================
    3    * 
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    * 
   11    *   http://www.apache.org/licenses/LICENSE-2.0
   12    * 
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    * 
   19    * ========================================================================
   20    */
   21   package org.apache.cactus.sample.servlet.util;
   22   
   23   import java.io.ByteArrayOutputStream;
   24   import java.io.PrintWriter;
   25   
   26   import javax.servlet.ServletOutputStream;
   27   import javax.servlet.http.HttpServletResponse;
   28   import javax.servlet.http.HttpServletResponseWrapper;
   29   
   30   /**
   31    * Wrapper around a <code>HttpServletResponse</code> that we use to easily
   32    * write filters that manipulate the output stream. Indeed, we cannot pass
   33    * the output stream of our filter direectly to the next filter in the chain
   34    * because then we won't be able to write to it (the response will have been
   35    * committed). Instead, we pass this wrapper class and then copy its data
   36    * to our filter output stream.
   37    *
   38    * Note: This code was adapted from the Filter tutorial found
   39    * {@link <a href="http://www.orionserver.com/tutorials/filters/lesson3/">
   40    * here</a>}
   41    *
   42    * @version $Id: GenericResponseWrapper.java 238816 2004-02-29 16:36:46Z vmassol $
   43    *
   44    * @see FilterServletOutputStream
   45    */
   46   public class GenericResponseWrapper extends HttpServletResponseWrapper
   47   {
   48       /**
   49        * Holder for the output data
   50        */
   51       private ByteArrayOutputStream output;
   52   
   53       /**
   54        * Save the content length so that we can query it at a later time
   55        * (otherwise it would not be possible as
   56        * <code>HttpServletResponseWrapper</code> does not have a method to get
   57        * the content length).
   58        */
   59       private int contentLength;
   60   
   61       /**
   62        * Save the content type so that we can query it at a later time
   63        * (otherwise it would not be possible as
   64        * <code>HttpServletResponseWrapper</code> does not have a method to get
   65        * the content type).
   66        */
   67       private String contentType;
   68   
   69       // Constructors ----------------------------------------------------------
   70   
   71       /**
   72        * @param theResponse the wrapped response object
   73        */
   74       public GenericResponseWrapper(HttpServletResponse theResponse)
   75       {
   76           super(theResponse);
   77           this.output = new ByteArrayOutputStream();
   78       }
   79   
   80       // New methods -----------------------------------------------------------
   81   
   82       /**
   83        * @return the data sent to the output stream
   84        */
   85       public byte[] getData()
   86       {
   87           return output.toByteArray();
   88       }
   89   
   90       // Overridden methods ----------------------------------------------------
   91   
   92       /**
   93        * @see HttpServletResponseWrapper#getOutputStream()
   94        */
   95       public ServletOutputStream getOutputStream()
   96       {
   97           return new FilterServletOutputStream(this.output);
   98       }
   99   
  100       /**
  101        * @see HttpServletResponseWrapper#setContentLength(int)
  102        */
  103       public void setContentLength(int theLength)
  104       {
  105           this.contentLength = theLength;
  106           super.setContentLength(theLength);
  107       }
  108   
  109       /**
  110        * @see HttpServletResponseWrapper#getContentLength()
  111        */
  112       public int getContentLength()
  113       {
  114           return this.contentLength;
  115       }
  116   
  117       /**
  118        * @see HttpServletResponseWrapper#setContentType(String)
  119        */
  120       public void setContentType(String theType)
  121       {
  122           this.contentType = theType;
  123           super.setContentType(theType);
  124       }
  125   
  126       /**
  127        * @see HttpServletResponseWrapper#getContentType()
  128        */
  129       public String getContentType()
  130       {
  131           return this.contentType;
  132       }
  133   
  134       /**
  135        * @see HttpServletResponseWrapper#getWriter()
  136        */
  137       public PrintWriter getWriter()
  138       {
  139           return new PrintWriter(getOutputStream(), true);
  140       }
  141   }

Save This Page
Home » cactus-1.8.0-src » org.apache.cactus.sample.servlet.util » [javadoc | source]