Clover coverage report -
Coverage timestamp: So Nov 6 2005 14:19:51 CET
file stats: LOC: 199   Methods: 15
NCLOC: 102   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ResponseContent.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 java.io.*;
 8   
 9    import java.util.Locale;
 10    import java.util.zip.GZIPInputStream;
 11   
 12    import javax.servlet.ServletResponse;
 13    import javax.servlet.http.HttpServletResponse;
 14   
 15    /**
 16    * Holds the servlet response in a byte array so that it can be held
 17    * in the cache (and, since this class is serializable, optionally
 18    * persisted to disk).
 19    *
 20    * @version $Revision: 1.1 $
 21    * @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
 22    */
 23    public class ResponseContent implements Serializable {
 24    private transient ByteArrayOutputStream bout = new ByteArrayOutputStream(1000);
 25    private Locale locale = null;
 26    private String contentEncoding = null;
 27    private String contentType = null;
 28    private byte[] content = null;
 29    private long expires = Long.MAX_VALUE;
 30    private long lastModified = -1;
 31   
 32  0 public String getContentType() {
 33  0 return contentType;
 34    }
 35   
 36    /**
 37    * Set the content type. We capture this so that when we serve this
 38    * data from cache, we can set the correct content type on the response.
 39    */
 40  0 public void setContentType(String value) {
 41  0 contentType = value;
 42    }
 43   
 44  0 public long getLastModified() {
 45  0 return lastModified;
 46    }
 47   
 48  0 public void setLastModified(long value) {
 49  0 lastModified = value;
 50    }
 51   
 52  0 public String getContentEncoding() {
 53  0 return contentEncoding;
 54    }
 55   
 56  0 public void setContentEncoding(String contentEncoding) {
 57  0 this.contentEncoding = contentEncoding;
 58    }
 59   
 60    /**
 61    * Set the Locale. We capture this so that when we serve this data from
 62    * cache, we can set the correct locale on the response.
 63    */
 64  0 public void setLocale(Locale value) {
 65  0 locale = value;
 66    }
 67   
 68    /**
 69    * @return the expires date and time in miliseconds when the content is stale
 70    */
 71  0 public long getExpires() {
 72  0 return expires;
 73    }
 74   
 75    /**
 76    * Sets the expires date and time in miliseconds.
 77    * @param value time in miliseconds when the content will expire
 78    */
 79  0 public void setExpires(long value) {
 80  0 expires = value;
 81    }
 82   
 83    /**
 84    * Get an output stream. This is used by the {@link SplitServletOutputStream}
 85    * to capture the original (uncached) response into a byte array.
 86    */
 87  0 public OutputStream getOutputStream() {
 88  0 return bout;
 89    }
 90   
 91    /**
 92    * Gets the size of this cached content.
 93    *
 94    * @return The size of the content, in bytes. If no content
 95    * exists, this method returns <code>-1</code>.
 96    */
 97  0 public int getSize() {
 98  0 return (content != null) ? content.length : (-1);
 99    }
 100   
 101    /**
 102    * Called once the response has been written in its entirety. This
 103    * method commits the response output stream by converting the output
 104    * stream into a byte array.
 105    */
 106  0 public void commit() {
 107  0 content = bout.toByteArray();
 108    }
 109   
 110    /**
 111    * Writes this cached data out to the supplied <code>ServletResponse</code>.
 112    *
 113    * @param response The servlet response to output the cached content to.
 114    * @throws IOException
 115    */
 116  0 public void writeTo(ServletResponse response) throws IOException {
 117  0 writeTo(response, false, false);
 118    }
 119   
 120    /**
 121    * Writes this cached data out to the supplied <code>ServletResponse</code>.
 122    *
 123    * @param response The servlet response to output the cached content to.
 124    * @param fragment is true if this content a fragment or part of a page
 125    * @param acceptsGZip is true if client browser supports gzip compression
 126    * @throws IOException
 127    */
 128  0 public void writeTo(ServletResponse response, boolean fragment, boolean acceptsGZip) throws IOException {
 129    //Send the content type and data to this response
 130  0 if (contentType != null) {
 131  0 response.setContentType(contentType);
 132    }
 133   
 134  0 if (fragment) {
 135    // Don't support gzip compression if the content is a fragment of a page
 136  0 acceptsGZip = false;
 137    } else {
 138    // add special headers for a complete page
 139  0 if (response instanceof HttpServletResponse) {
 140  0 HttpServletResponse httpResponse = (HttpServletResponse) response;
 141   
 142    // add the last modified header
 143  0 if (lastModified != -1) {
 144  0 httpResponse.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, lastModified);
 145    }
 146   
 147    // add the expires header
 148  0 if (expires != Long.MAX_VALUE) {
 149  0 httpResponse.setDateHeader(CacheFilter.HEADER_EXPIRES, expires);
 150    }
 151    }
 152    }
 153   
 154  0 if (locale != null) {
 155  0 response.setLocale(locale);
 156    }
 157   
 158  0 OutputStream out = new BufferedOutputStream(response.getOutputStream());
 159   
 160  0 if (isContentGZiped()) {
 161  0 if (acceptsGZip) {
 162  0 ((HttpServletResponse) response).addHeader(CacheFilter.HEADER_CONTENT_ENCODING, "gzip");
 163  0 response.setContentLength(content.length);
 164  0 out.write(content);
 165    } else {
 166    // client doesn't support, so we have to uncompress it
 167  0 ByteArrayInputStream bais = new ByteArrayInputStream(content);
 168  0 GZIPInputStream zis = new GZIPInputStream(bais);
 169   
 170  0 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 171  0 int numBytesRead = 0;
 172  0 byte[] tempBytes = new byte[4196];
 173   
 174  0 while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) {
 175  0 baos.write(tempBytes, 0, numBytesRead);
 176    }
 177   
 178  0 byte[] result = baos.toByteArray();
 179   
 180  0 response.setContentLength(result.length);
 181  0 out.write(result);
 182    }
 183    } else {
 184    // the content isn't compressed
 185    // regardless if the client browser supports gzip we will just return the content
 186  0 response.setContentLength(content.length);
 187  0 out.write(content);
 188    }
 189  0 out.flush();
 190    }
 191   
 192   
 193    /**
 194    * @return true if the content is GZIP compressed
 195    */
 196  0 public boolean isContentGZiped() {
 197  0 return "gzip".equals(contentEncoding);
 198    }
 199    }