Clover coverage report -
Coverage timestamp: do jan 22 2004 21:12:32 CET
file stats: LOC: 213   Methods: 16
NCLOC: 94   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CacheHttpServletResponseWrapper.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * Copyright (c) 2002-2003 by OpenSymphony
 3   
  * All rights reserved.
 4   
  */
 5   
 package com.opensymphony.oscache.web.filter;
 6   
 
 7   
 import org.apache.commons.logging.Log;
 8   
 import org.apache.commons.logging.LogFactory;
 9   
 
 10   
 import java.io.IOException;
 11   
 import java.io.PrintWriter;
 12   
 
 13   
 import java.util.Locale;
 14   
 
 15   
 import javax.servlet.ServletOutputStream;
 16   
 import javax.servlet.http.HttpServletResponse;
 17   
 import javax.servlet.http.HttpServletResponseWrapper;
 18   
 
 19   
 /**
 20   
  * CacheServletResponse is a serialized representation of a response
 21   
  *
 22   
  * @author  <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
 23   
  * @version $Revision: 1.3 $
 24   
  */
 25   
 public class CacheHttpServletResponseWrapper extends HttpServletResponseWrapper {
 26   
     private final Log log = LogFactory.getLog(this.getClass());
 27   
 
 28   
     /**
 29   
      * We cache the printWriter so we can maintain a single instance
 30   
      * of it no matter how many times it is requested.
 31   
      */
 32   
     private PrintWriter cachedWriter;
 33   
     private ResponseContent result = null;
 34   
     private SplitServletOutputStream cacheOut = null;
 35   
     private int status = SC_OK;
 36   
 
 37   
     /**
 38   
      * Constructor
 39   
      *
 40   
      * @param response The servlet response
 41   
      */
 42  0
     public CacheHttpServletResponseWrapper(HttpServletResponse response) {
 43  0
         super(response);
 44  0
         result = new ResponseContent();
 45   
     }
 46   
 
 47   
     /**
 48   
      * Get a response content
 49   
      *
 50   
      * @return The content
 51   
      */
 52  0
     public ResponseContent getContent() {
 53   
         //Create the byte array
 54  0
         result.commit();
 55   
 
 56   
         //Return the result from this response
 57  0
         return result;
 58   
     }
 59   
 
 60   
     /**
 61   
      * Set the content type
 62   
      *
 63   
      * @param value The content type
 64   
      */
 65  0
     public void setContentType(String value) {
 66  0
         super.setContentType(value);
 67  0
         result.setContentType(value);
 68   
     }
 69   
 
 70   
     /**
 71   
      * Set the date of a header
 72   
      *
 73   
      * @param name The header name
 74   
      * @param value The date
 75   
      */
 76  0
     public void setDateHeader(String name, long value) {
 77  0
         if (log.isDebugEnabled()) {
 78  0
             log.debug("dateheader: " + name + ": " + value);
 79   
         }
 80   
 
 81  0
         super.setDateHeader(name, value);
 82   
     }
 83   
 
 84   
     /**
 85   
      * Set a header field
 86   
      *
 87   
      * @param name The header name
 88   
      * @param value The header value
 89   
      */
 90  0
     public void setHeader(String name, String value) {
 91  0
         if (log.isDebugEnabled()) {
 92  0
             log.debug("header: " + name + ": " + value);
 93   
         }
 94   
 
 95  0
         super.setHeader(name, value);
 96   
     }
 97   
 
 98   
     /**
 99   
      * Set the int value of the header
 100   
      *
 101   
      * @param name The header name
 102   
      * @param value The int value
 103   
      */
 104  0
     public void setIntHeader(String name, int value) {
 105  0
         if (log.isDebugEnabled()) {
 106  0
             log.debug("intheader: " + name + ": " + value);
 107   
         }
 108   
 
 109  0
         super.setIntHeader(name, value);
 110   
     }
 111   
 
 112   
     /**
 113   
      * We override this so we can catch the response status. Only
 114   
      * responses with a status of 200 (<code>SC_OK</code>) will
 115   
      * be cached.
 116   
      */
 117  0
     public void setStatus(int status) {
 118  0
         super.setStatus(status);
 119  0
         this.status = status;
 120   
     }
 121   
 
 122   
     /**
 123   
      * We override this so we can catch the response status. Only
 124   
      * responses with a status of 200 (<code>SC_OK</code>) will
 125   
      * be cached.
 126   
      */
 127  0
     public void sendError(int status, String string) throws IOException {
 128  0
         super.sendError(status, string);
 129  0
         this.status = status;
 130   
     }
 131   
 
 132   
     /**
 133   
      * We override this so we can catch the response status. Only
 134   
      * responses with a status of 200 (<code>SC_OK</code>) will
 135   
      * be cached.
 136   
      */
 137  0
     public void sendError(int status) throws IOException {
 138  0
         super.sendError(status);
 139  0
         this.status = status;
 140   
     }
 141   
 
 142   
     /**
 143   
      * We override this so we can catch the response status. Only
 144   
      * responses with a status of 200 (<code>SC_OK</code>) will
 145   
      * be cached.
 146   
      */
 147  0
     public void setStatus(int status, String string) {
 148  0
         super.setStatus(status, string);
 149  0
         this.status = status;
 150   
     }
 151   
 
 152  0
     public void sendRedirect(String location) throws IOException {
 153  0
         this.status = SC_MOVED_TEMPORARILY;
 154  0
         super.sendRedirect(location);
 155   
     }
 156   
 
 157   
     /**
 158   
      * Retrieves the captured HttpResponse status.
 159   
      */
 160  0
     public int getStatus() {
 161  0
         return status;
 162   
     }
 163   
 
 164   
     /**
 165   
      * Set the locale
 166   
      *
 167   
      * @param value The locale
 168   
      */
 169  0
     public void setLocale(Locale value) {
 170  0
         super.setLocale(value);
 171  0
         result.setLocale(value);
 172   
     }
 173   
 
 174   
     /**
 175   
      * Get an output stream
 176   
      *
 177   
      * @throws IOException
 178   
      */
 179  0
     public ServletOutputStream getOutputStream() throws IOException {
 180   
         // Pass this faked servlet output stream that captures what is sent
 181  0
         if (cacheOut == null) {
 182  0
             cacheOut = new SplitServletOutputStream(result.getOutputStream(), super.getOutputStream());
 183   
         }
 184   
 
 185  0
         return cacheOut;
 186   
     }
 187   
 
 188   
     /**
 189   
      * Get a print writer
 190   
      *
 191   
      * @throws IOException
 192   
      */
 193  0
     public PrintWriter getWriter() throws IOException {
 194  0
         if (cachedWriter == null) {
 195  0
             cachedWriter = new PrintWriter(getOutputStream());
 196   
         }
 197   
 
 198  0
         return cachedWriter;
 199   
     }
 200   
 
 201  0
     public void flushBuffer() throws IOException {
 202  0
         super.flushBuffer();
 203   
 
 204  0
         if (cacheOut != null) {
 205  0
             cacheOut.flush();
 206   
         }
 207   
 
 208  0
         if (cachedWriter != null) {
 209  0
             cachedWriter.flush();
 210   
         }
 211   
     }
 212   
 }
 213