Rakesh API

org.rakeshv.filters
Class CachingResponseWrapper

java.lang.Object
  extended by javax.servlet.ServletResponseWrapper
      extended by javax.servlet.http.HttpServletResponseWrapper
          extended by org.rakeshv.filters.CachingResponseWrapper
All Implemented Interfaces:
javax.servlet.http.HttpServletResponse, javax.servlet.ServletResponse

public class CachingResponseWrapper
extends javax.servlet.http.HttpServletResponseWrapper

A HttpServletResponseWrapper class that is used to create a cached output stream. This class over-rides appropriate methods in the parent class to create cached OutputStreams and PrintWriters.

© Copyright 2005, Rakesh Vidyadharan

Version:
$Id: CachingResponseWrapper.java,v 1.4 2005/10/16 02:19:57 rakesh Exp $
Author:
Rakesh Vidyadharan 15th September 2005

Field Summary
protected  java.io.File cacheFile
          The file to which the cached contents are to be written.
private  CachingFilter.FileProperties fileProperties
          The CachingFilter.FileProperties that is to be modified if the content properties for the response are modified by objects down the filter chain.
protected  javax.servlet.http.HttpServletResponse servletResponse
          Original response
protected  javax.servlet.ServletOutputStream stream
          The ServletOutputStream that has been returned by getOutputStream(), if any.
protected  java.io.PrintWriter writer
          The PrintWriter that has been returned by getWriter(), if any.
 
Fields inherited from interface javax.servlet.http.HttpServletResponse
SC_ACCEPTED, SC_BAD_GATEWAY, SC_BAD_REQUEST, SC_CONFLICT, SC_CONTINUE, SC_CREATED, SC_EXPECTATION_FAILED, SC_FORBIDDEN, SC_FOUND, SC_GATEWAY_TIMEOUT, SC_GONE, SC_HTTP_VERSION_NOT_SUPPORTED, SC_INTERNAL_SERVER_ERROR, SC_LENGTH_REQUIRED, SC_METHOD_NOT_ALLOWED, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY, SC_MULTIPLE_CHOICES, SC_NO_CONTENT, SC_NON_AUTHORITATIVE_INFORMATION, SC_NOT_ACCEPTABLE, SC_NOT_FOUND, SC_NOT_IMPLEMENTED, SC_NOT_MODIFIED, SC_OK, SC_PARTIAL_CONTENT, SC_PAYMENT_REQUIRED, SC_PRECONDITION_FAILED, SC_PROXY_AUTHENTICATION_REQUIRED, SC_REQUEST_ENTITY_TOO_LARGE, SC_REQUEST_TIMEOUT, SC_REQUEST_URI_TOO_LONG, SC_REQUESTED_RANGE_NOT_SATISFIABLE, SC_RESET_CONTENT, SC_SEE_OTHER, SC_SERVICE_UNAVAILABLE, SC_SWITCHING_PROTOCOLS, SC_TEMPORARY_REDIRECT, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY
 
Constructor Summary
CachingResponseWrapper(javax.servlet.http.HttpServletResponse response, java.io.File file, CachingFilter.FileProperties fileProperties)
          Constructs a cached response adaptor wrapping the given response.
 
Method Summary
 void addHeader(java.lang.String name, java.lang.String value)
          Add the specified header to the fileProperties
 void closeCacheFile()
          Closes the cached file in the CachingResponseStream without closing the underlying stream.
 javax.servlet.ServletOutputStream createOutputStream()
          Create a new instance of the CachingResponseWrapper class, that compresses the response.
 void flushBuffer()
          Forces any content in the buffer to be written to the client.
 javax.servlet.ServletOutputStream getOutputStream()
          Returns a ServletOutputStream suitable for writing binary data in the response.
 java.io.PrintWriter getWriter()
          Returns a PrintWriter object that can send character text to the client.
 void setCharacterEncoding(java.lang.String charset)
          Update the property in the fileProperties and then pass along to the super class implementation.
 void setContentType(java.lang.String type)
          Update the property in the fileProperties and then pass along to the super class implementation.
 void setHeader(java.lang.String name, java.lang.String value)
          Add the specified header to the fileProperties
 void setLocale(java.util.Locale locale)
          Update the property in the fileProperties and then pass along to the super class implementation.
 void setStatus(int status)
          If the status is not equal to SC_OK, indicate to the fileProperties that the response should not be cached.
 
Methods inherited from class javax.servlet.http.HttpServletResponseWrapper
addCookie, addDateHeader, addIntHeader, containsHeader, encodeRedirectUrl, encodeRedirectURL, encodeUrl, encodeURL, sendError, sendError, sendRedirect, setDateHeader, setIntHeader, setStatus
 
Methods inherited from class javax.servlet.ServletResponseWrapper
getBufferSize, getCharacterEncoding, getContentType, getLocale, getResponse, isCommitted, reset, resetBuffer, setBufferSize, setContentLength, setResponse
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.ServletResponse
getBufferSize, getCharacterEncoding, getContentType, getLocale, isCommitted, reset, resetBuffer, setBufferSize, setContentLength
 

Field Detail

servletResponse

protected javax.servlet.http.HttpServletResponse servletResponse
Original response


stream

protected javax.servlet.ServletOutputStream stream
The ServletOutputStream that has been returned by getOutputStream(), if any.


writer

protected java.io.PrintWriter writer
The PrintWriter that has been returned by getWriter(), if any.


cacheFile

protected java.io.File cacheFile
The file to which the cached contents are to be written.


fileProperties

private CachingFilter.FileProperties fileProperties
The CachingFilter.FileProperties that is to be modified if the content properties for the response are modified by objects down the filter chain.

Constructor Detail

CachingResponseWrapper

public CachingResponseWrapper(javax.servlet.http.HttpServletResponse response,
                              java.io.File file,
                              CachingFilter.FileProperties fileProperties)
                       throws java.lang.IllegalArgumentException
Constructs a cached response adaptor wrapping the given response. Calls the super-class constructor, and set the servletResponse reference.

Parameters:
response - The response that is to be cached.
file - The file to which the contents are to be cached.
fileProperties - The FileProperties that will be passed along to the CachingResponseStream.
Throws:
java.lang.IllegalArgumentException - If the specified response is null.
Method Detail

createOutputStream

public javax.servlet.ServletOutputStream createOutputStream()
                                                     throws java.io.IOException
Create a new instance of the CachingResponseWrapper class, that compresses the response.

Returns:
ServletOutputStream - The appropriate instance of the ServletOutputStream that wraps a DeflaterOutputStream .
Throws:
java.io.IOException - If exceptions are encountered while creating the cached response stream.

flushBuffer

public void flushBuffer()
                 throws java.io.IOException
Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.

Specified by:
flushBuffer in interface javax.servlet.ServletResponse
Overrides:
flushBuffer in class javax.servlet.ServletResponseWrapper
Throws:
java.io.IOException

getOutputStream

public javax.servlet.ServletOutputStream getOutputStream()
                                                  throws java.io.IOException
Returns a ServletOutputStream suitable for writing binary data in the response. If a Writer has not already been created for the response (through a call to getWriter() method) a new instance of the CachingResponseStream is created, and the instance variable stream set to the new instance created. If the stream has already been initialised, then the reference is returned.

Specified by:
getOutputStream in interface javax.servlet.ServletResponse
Overrides:
getOutputStream in class javax.servlet.ServletResponseWrapper
Throws:
java.io.IOException - If errors are encountered while creating the cached response stream.
java.lang.IllegalStateException - If getWriter() has already been called on this instance.

getWriter

public java.io.PrintWriter getWriter()
                              throws java.io.IOException
Returns a PrintWriter object that can send character text to the client. The PrintWriter is created on top of the appropriate CachingResponseStream class. The character encoding used is the one specified in the charset= property of the setContentType( java.lang.String ) method, which must be called before calling this method for the charset to take effect.

If necessary, the MIME type of the response is modified to reflect the character encoding used.

Either this method or getOutputStream() may be called to write the body, not both.

Specified by:
getWriter in interface javax.servlet.ServletResponse
Overrides:
getWriter in class javax.servlet.ServletResponseWrapper
Throws:
java.io.IOException - If errors are encountered while creating the cached response stream.
java.lang.IllegalStateException - If getWriter() has already been called on this instance.

closeCacheFile

public void closeCacheFile()
                    throws java.io.IOException
Closes the cached file in the CachingResponseStream without closing the underlying stream.

Note: This method must be invoked by to ensure that the cache file that was generated is properly closed after the actual response was generated.

Throws:
java.io.IOException - - If errors are encountered while closing the file.

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
Add the specified header to the fileProperties

Specified by:
addHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
addHeader in class javax.servlet.http.HttpServletResponseWrapper
Parameters:
name - The name of the header
value - The value of the header

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
Add the specified header to the fileProperties

Specified by:
setHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
setHeader in class javax.servlet.http.HttpServletResponseWrapper
Parameters:
name - The name of the header
value - The value of the header

setCharacterEncoding

public void setCharacterEncoding(java.lang.String charset)
Update the property in the fileProperties and then pass along to the super class implementation.

Specified by:
setCharacterEncoding in interface javax.servlet.ServletResponse
Overrides:
setCharacterEncoding in class javax.servlet.ServletResponseWrapper
Parameters:
charset - A String specifying only the character set defined by IANA Character Sets (http://www.iana.org/assignments/character-sets)

setContentType

public void setContentType(java.lang.String type)
Update the property in the fileProperties and then pass along to the super class implementation.

Specified by:
setContentType in interface javax.servlet.ServletResponse
Overrides:
setContentType in class javax.servlet.ServletResponseWrapper
Parameters:
type - A String specifying the MIME type of the content

setLocale

public void setLocale(java.util.Locale locale)
Update the property in the fileProperties and then pass along to the super class implementation.

Specified by:
setLocale in interface javax.servlet.ServletResponse
Overrides:
setLocale in class javax.servlet.ServletResponseWrapper
Parameters:
locale - The locale of the response

setStatus

public void setStatus(int status)
If the status is not equal to SC_OK, indicate to the fileProperties that the response should not be cached.

Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse
Overrides:
setStatus in class javax.servlet.http.HttpServletResponseWrapper
Parameters:
status - The status code to set for the response.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan