Clover coverage report -
Coverage timestamp: So Nov 6 2005 14:19:51 CET
file stats: LOC: 333   Methods: 19
NCLOC: 167   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.OutputStreamWriter;
 12    import java.io.PrintWriter;
 13   
 14    import java.util.Locale;
 15   
 16    import javax.servlet.ServletOutputStream;
 17    import javax.servlet.http.HttpServletResponse;
 18    import javax.servlet.http.HttpServletResponseWrapper;
 19   
 20    /**
 21    * CacheServletResponse is a serialized representation of a response
 22    *
 23    * @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
 24    * @version $Revision: 1.4 $
 25    */
 26    public class CacheHttpServletResponseWrapper extends HttpServletResponseWrapper {
 27    private final Log log = LogFactory.getLog(this.getClass());
 28   
 29    /**
 30    * We cache the printWriter so we can maintain a single instance
 31    * of it no matter how many times it is requested.
 32    */
 33    private PrintWriter cachedWriter;
 34    private ResponseContent result = null;
 35    private SplitServletOutputStream cacheOut = null;
 36    private boolean fragment = false;
 37    private int status = SC_OK;
 38    private long expires = CacheFilter.EXPIRES_ON;
 39    private long lastModified = CacheFilter.LAST_MODIFIED_INITIAL;
 40   
 41    /**
 42    * Constructor
 43    *
 44    * @param response The servlet response
 45    */
 46  0 public CacheHttpServletResponseWrapper(HttpServletResponse response) {
 47  0 this(response, false, Long.MAX_VALUE, CacheFilter.EXPIRES_ON, CacheFilter.LAST_MODIFIED_INITIAL);
 48    }
 49   
 50    /**
 51    * Constructor
 52    *
 53    * @param response The servlet response
 54    * @param fragment true if the repsonse indicates that it is a fragement of a page
 55    * @param time the refresh time in millis
 56    * @param lastModified defines if last modified header will be send, @see CacheFilter
 57    * @param expires defines if expires header will be send, @see CacheFilter
 58    */
 59  0 public CacheHttpServletResponseWrapper(HttpServletResponse response, boolean fragment, long time, long lastModified, long expires) {
 60  0 super(response);
 61  0 result = new ResponseContent();
 62  0 this.fragment = fragment;
 63  0 this.expires = expires;
 64  0 this.lastModified = lastModified;
 65   
 66    // only set inital values for last modified and expires, when a complete page is cached
 67  0 if (!fragment) {
 68    // setting a default last modified value based on object creation and remove the millis
 69  0 if (lastModified == CacheFilter.LAST_MODIFIED_INITIAL) {
 70  0 long current = System.currentTimeMillis() / 1000;
 71  0 result.setLastModified(current * 1000);
 72  0 super.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, result.getLastModified());
 73    }
 74    // setting the expires value
 75  0 if (expires == CacheFilter.EXPIRES_TIME) {
 76  0 result.setExpires(result.getLastModified() + time);
 77  0 super.setDateHeader(CacheFilter.HEADER_EXPIRES, result.getExpires());
 78    }
 79    }
 80    }
 81   
 82    /**
 83    * Get a response content
 84    *
 85    * @return The content
 86    */
 87  0 public ResponseContent getContent() {
 88    //Create the byte array
 89  0 result.commit();
 90   
 91    //Return the result from this response
 92  0 return result;
 93    }
 94   
 95    /**
 96    * Set the content type
 97    *
 98    * @param value The content type
 99    */
 100  0 public void setContentType(String value) {
 101  0 if (log.isDebugEnabled()) {
 102  0 log.debug("ContentType: " + value);
 103    }
 104   
 105  0 super.setContentType(value);
 106  0 result.setContentType(value);
 107    }
 108   
 109    /**
 110    * Set the date of a header
 111    *
 112    * @param name The header name
 113    * @param value The date
 114    */
 115  0 public void setDateHeader(String name, long value) {
 116  0 if (log.isDebugEnabled()) {
 117  0 log.debug("dateheader: " + name + ": " + value);
 118    }
 119   
 120    // only set the last modified value, if a complete page is cached
 121  0 if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) {
 122  0 if (!fragment) {
 123  0 result.setLastModified(value);
 124    } // TODO should we return now by fragments to avoid putting the header to the response?
 125    }
 126   
 127    // implement RFC 2616 14.21 Expires (without max-age)
 128  0 if ((expires != CacheFilter.EXPIRES_OFF) && (CacheFilter.HEADER_EXPIRES.equalsIgnoreCase(name))) {
 129  0 if (!fragment) {
 130  0 result.setExpires(value);
 131    } // TODO should we return now by fragments to avoid putting the header to the response?
 132    }
 133   
 134  0 super.setDateHeader(name, value);
 135    }
 136   
 137    /**
 138    * Add the date of a header
 139    *
 140    * @param name The header name
 141    * @param value The date
 142    */
 143  0 public void addDateHeader(String name, long value) {
 144  0 if (log.isDebugEnabled()) {
 145  0 log.debug("dateheader: " + name + ": " + value);
 146    }
 147   
 148    // only set the last modified value, if a complete page is cached
 149  0 if ((lastModified != CacheFilter.LAST_MODIFIED_OFF) && (CacheFilter.HEADER_LAST_MODIFIED.equalsIgnoreCase(name))) {
 150  0 if (!fragment) {
 151  0 result.setLastModified(value);
 152    } // TODO should we return now by fragments to avoid putting the header to the response?
 153    }
 154   
 155    // implement RFC 2616 14.21 Expires (without max-age)
 156  0 if ((expires != CacheFilter.EXPIRES_OFF) && (CacheFilter.HEADER_EXPIRES.equalsIgnoreCase(name))) {
 157  0 if (!fragment) {
 158  0 result.setExpires(value);
 159    } // TODO should we return now by fragments to avoid putting the header to the response?
 160    }
 161   
 162  0 super.addDateHeader(name, value);
 163    }
 164   
 165    /**
 166    * Set a header field
 167    *
 168    * @param name The header name
 169    * @param value The header value
 170    */
 171  0 public void setHeader(String name, String value) {
 172  0 if (log.isDebugEnabled()) {
 173  0 log.debug("header: " + name + ": " + value);
 174    }
 175   
 176  0 if (CacheFilter.HEADER_CONTENT_TYPE.equalsIgnoreCase(name)) {
 177  0 result.setContentType(value);
 178    }
 179   
 180  0 if (CacheFilter.HEADER_CONTENT_ENCODING.equalsIgnoreCase(name)) {
 181  0 result.setContentEncoding(value);
 182    }
 183   
 184  0 super.setHeader(name, value);
 185    }
 186   
 187    /**
 188    * Add a header field
 189    *
 190    * @param name The header name
 191    * @param value The header value
 192    */
 193  0 public void addHeader(String name, String value) {
 194  0 if (log.isDebugEnabled()) {
 195  0 log.debug("header: " + name + ": " + value);
 196    }
 197   
 198  0 if (CacheFilter.HEADER_CONTENT_TYPE.equalsIgnoreCase(name)) {
 199  0 result.setContentType(value);
 200    }
 201   
 202  0 if (CacheFilter.HEADER_CONTENT_ENCODING.equalsIgnoreCase(name)) {
 203  0 result.setContentEncoding(value);
 204    }
 205   
 206  0 super.addHeader(name, value);
 207    }
 208   
 209    /**
 210    * Set the int value of the header
 211    *
 212    * @param name The header name
 213    * @param value The int value
 214    */
 215  0 public void setIntHeader(String name, int value) {
 216  0 if (log.isDebugEnabled()) {
 217  0 log.debug("intheader: " + name + ": " + value);
 218    }
 219   
 220  0 super.setIntHeader(name, value);
 221    }
 222   
 223    /**
 224    * We override this so we can catch the response status. Only
 225    * responses with a status of 200 (<code>SC_OK</code>) will
 226    * be cached.
 227    */
 228  0 public void setStatus(int status) {
 229  0 super.setStatus(status);
 230  0 this.status = status;
 231    }
 232   
 233    /**
 234    * We override this so we can catch the response status. Only
 235    * responses with a status of 200 (<code>SC_OK</code>) will
 236    * be cached.
 237    */
 238  0 public void sendError(int status, String string) throws IOException {
 239  0 super.sendError(status, string);
 240  0 this.status = status;
 241    }
 242   
 243    /**
 244    * We override this so we can catch the response status. Only
 245    * responses with a status of 200 (<code>SC_OK</code>) will
 246    * be cached.
 247    */
 248  0 public void sendError(int status) throws IOException {
 249  0 super.sendError(status);
 250  0 this.status = status;
 251    }
 252   
 253    /**
 254    * We override this so we can catch the response status. Only
 255    * responses with a status of 200 (<code>SC_OK</code>) will
 256    * be cached.
 257    */
 258  0 public void setStatus(int status, String string) {
 259  0 super.setStatus(status, string);
 260  0 this.status = status;
 261    }
 262   
 263    /**
 264    * We override this so we can catch the response status. Only
 265    * responses with a status of 200 (<code>SC_OK</code>) will
 266    * be cached.
 267    */
 268  0 public void sendRedirect(String location) throws IOException {
 269  0 this.status = SC_MOVED_TEMPORARILY;
 270  0 super.sendRedirect(location);
 271    }
 272   
 273    /**
 274    * Retrieves the captured HttpResponse status.
 275    */
 276  0 public int getStatus() {
 277  0 return status;
 278    }
 279   
 280    /**
 281    * Set the locale
 282    *
 283    * @param value The locale
 284    */
 285  0 public void setLocale(Locale value) {
 286  0 super.setLocale(value);
 287  0 result.setLocale(value);
 288    }
 289   
 290    /**
 291    * Get an output stream
 292    *
 293    * @throws IOException
 294    */
 295  0 public ServletOutputStream getOutputStream() throws IOException {
 296    // Pass this faked servlet output stream that captures what is sent
 297  0 if (cacheOut == null) {
 298  0 cacheOut = new SplitServletOutputStream(result.getOutputStream(), super.getOutputStream());
 299    }
 300   
 301  0 return cacheOut;
 302    }
 303   
 304    /**
 305    * Get a print writer
 306    *
 307    * @throws IOException
 308    */
 309  0 public PrintWriter getWriter() throws IOException {
 310  0 if (cachedWriter == null) {
 311  0 String encoding = getCharacterEncoding();
 312  0 if (encoding != null) {
 313  0 cachedWriter = new PrintWriter(new OutputStreamWriter(getOutputStream(), encoding));
 314    } else { // using the default character encoding
 315  0 cachedWriter = new PrintWriter(new OutputStreamWriter(getOutputStream()));
 316    }
 317    }
 318   
 319  0 return cachedWriter;
 320    }
 321   
 322  0 public void flushBuffer() throws IOException {
 323  0 super.flushBuffer();
 324   
 325  0 if (cacheOut != null) {
 326  0 cacheOut.flush();
 327    }
 328   
 329  0 if (cachedWriter != null) {
 330  0 cachedWriter.flush();
 331    }
 332    }
 333    }