Clover coverage report -
Coverage timestamp: Mo Mrz 6 2006 19:30:45 CET
file stats: LOC: 203   Methods: 15
NCLOC: 105   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.2 $
 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    * @return the original (uncached) response, returns null if response is already committed.
 87    */
 88  0 public OutputStream getOutputStream() {
 89  0 return bout;
 90    }
 91   
 92    /**
 93    * Gets the size of this cached content.
 94    *
 95    * @return The size of the content, in bytes. If no content
 96    * exists, this method returns <code>-1</code>.
 97    */
 98  0 public int getSize() {
 99  0 return (content != null) ? content.length : (-1);
 100    }
 101   
 102    /**
 103    * Called once the response has been written in its entirety. This
 104    * method commits the response output stream by converting the output
 105    * stream into a byte array.
 106    */
 107  0 public void commit() {
 108  0 if (bout != null) {
 109  0 content = bout.toByteArray();
 110  0 bout = null;
 111    }
 112    }
 113   
 114    /**
 115    * Writes this cached data out to the supplied <code>ServletResponse</code>.
 116    *
 117    * @param response The servlet response to output the cached content to.
 118    * @throws IOException
 119    */
 120  0 public void writeTo(ServletResponse response) throws IOException {
 121  0 writeTo(response, false, false);
 122    }
 123   
 124    /**
 125    * Writes this cached data out to the supplied <code>ServletResponse</code>.
 126    *
 127    * @param response The servlet response to output the cached content to.
 128    * @param fragment is true if this content a fragment or part of a page
 129    * @param acceptsGZip is true if client browser supports gzip compression
 130    * @throws IOException
 131    */
 132  0 public void writeTo(ServletResponse response, boolean fragment, boolean acceptsGZip) throws IOException {
 133    //Send the content type and data to this response
 134  0 if (contentType != null) {
 135  0 response.setContentType(contentType);
 136    }
 137   
 138  0 if (fragment) {
 139    // Don't support gzip compression if the content is a fragment of a page
 140  0 acceptsGZip = false;
 141    } else {
 142    // add special headers for a complete page
 143  0 if (response instanceof HttpServletResponse) {
 144  0 HttpServletResponse httpResponse = (HttpServletResponse) response;
 145   
 146    // add the last modified header
 147  0 if (lastModified != -1) {
 148  0 httpResponse.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, lastModified);
 149    }
 150   
 151    // add the expires header
 152  0 if (expires != Long.MAX_VALUE) {
 153  0 httpResponse.setDateHeader(CacheFilter.HEADER_EXPIRES, expires);
 154    }
 155    }
 156    }
 157   
 158  0 if (locale != null) {
 159  0 response.setLocale(locale);
 160    }
 161   
 162  0 OutputStream out = new BufferedOutputStream(response.getOutputStream());
 163   
 164  0 if (isContentGZiped()) {
 165  0 if (acceptsGZip) {
 166  0 ((HttpServletResponse) response).addHeader(CacheFilter.HEADER_CONTENT_ENCODING, "gzip");
 167  0 response.setContentLength(content.length);
 168  0 out.write(content);
 169    } else {
 170    // client doesn't support, so we have to uncompress it
 171  0 ByteArrayInputStream bais = new ByteArrayInputStream(content);
 172  0 GZIPInputStream zis = new GZIPInputStream(bais);
 173   
 174  0 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 175  0 int numBytesRead = 0;
 176  0 byte[] tempBytes = new byte[4196];
 177   
 178  0 while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) {
 179  0 baos.write(tempBytes, 0, numBytesRead);
 180    }
 181   
 182  0 byte[] result = baos.toByteArray();
 183   
 184  0 response.setContentLength(result.length);
 185  0 out.write(result);
 186    }
 187    } else {
 188    // the content isn't compressed
 189    // regardless if the client browser supports gzip we will just return the content
 190  0 response.setContentLength(content.length);
 191  0 out.write(content);
 192    }
 193  0 out.flush();
 194    }
 195   
 196   
 197    /**
 198    * @return true if the content is GZIP compressed
 199    */
 200  0 public boolean isContentGZiped() {
 201  0 return "gzip".equals(contentEncoding);
 202    }
 203    }