Rakesh API

org.rakeshv.filters
Class CompressionResponseWrapper

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

public class CompressionResponseWrapper
extends javax.servlet.http.HttpServletResponseWrapper

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

Note: - It is REQUIRED that you set the value of the compressionType instance variable using the mutator method setCompressionType( int ) if the compression is to work.

© Copyright 2003, Rakesh Vidyadharan

Version:
$Id: CompressionResponseWrapper.java,v 1.5 2005/09/17 03:53:05 rakesh Exp $
Author:
Rakesh Vidyadharan 2nd September 2003

Field Summary
protected  int compressionType
          An integer that indicates the type of compression that is to be used.
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
CompressionResponseWrapper(javax.servlet.http.HttpServletResponse response)
          Constructs a compressed response adaptor wrapping the given response.
 
Method Summary
 javax.servlet.ServletOutputStream createOutputStream()
          Create a new instance of the CompressionResponseWrapper class, that compresses the response.
 void finish()
          Finishes writing compressed data to the CompressionResponseStream output stream without closing the underlying stream.
 void flushBuffer()
          Forces any content in the buffer to be written to the client.
 int getCompressionType()
          Returns compressionType.
 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 setCompressionType(int compressionType)
          Set compressionType.
 
Methods inherited from class javax.servlet.http.HttpServletResponseWrapper
addCookie, addDateHeader, addHeader, addIntHeader, containsHeader, encodeRedirectUrl, encodeRedirectURL, encodeUrl, encodeURL, sendError, sendError, sendRedirect, setDateHeader, setHeader, setIntHeader, setStatus, setStatus
 
Methods inherited from class javax.servlet.ServletResponseWrapper
getBufferSize, getCharacterEncoding, getContentType, getLocale, getResponse, isCommitted, reset, resetBuffer, setBufferSize, setCharacterEncoding, setContentLength, setContentType, setLocale, 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, setCharacterEncoding, setContentLength, setContentType, setLocale
 

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.


compressionType

protected int compressionType
An integer that indicates the type of compression that is to be used. The different compression algorithms supported are the ones supported in the java.util.zip package.

Constructor Detail

CompressionResponseWrapper

public CompressionResponseWrapper(javax.servlet.http.HttpServletResponse response)
                           throws java.lang.IllegalArgumentException
Constructs a compressed response adaptor wrapping the given response. Calls the super-class constructor, and set the servletResponse reference.

Parameters:
response - - The response that is to be compressed.
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 CompressionResponseWrapper 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 compressed 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 CompressionResponseStream 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 compressed 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 CompressionResponseStream 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 compressed response stream.
java.lang.IllegalStateException - - If getWriter() has already been called on this instance.

finish

public void finish()
            throws java.io.IOException
Finishes writing compressed data to the CompressionResponseStream output stream without closing the underlying stream.

Note: This method must be invoked by to properly finish the output if multiple filters are added in succession to the same output stream. This is generally the case, and hence this method must be called in order for the response to be completely sent back to the client.

Throws:
java.io.IOException - - If errors are encountered while writing to the stream. An exception will be thrown if this method is invoked after the stream has been closed.

getCompressionType

public final int getCompressionType()
Returns compressionType.

Returns:
int - The value/reference of/to compressionType.

setCompressionType

public final void setCompressionType(int compressionType)
Set compressionType.

Parameters:
compressionType - - The value to set.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan