FileDocCategorySizeDatePackage
CompressionServletResponseWrapper.javaAPI DocApache Tomcat 6.0.147117Fri Jul 20 04:20:32 BST 2007compressionFilters

CompressionServletResponseWrapper

public class CompressionServletResponseWrapper extends HttpServletResponseWrapper
Implementation of HttpServletResponseWrapper that works with the CompressionServletResponseStream implementation..
author
Amy Roh
author
Dmitri Valdin
version
$Revision: 500668 $, $Date: 2007-01-28 00:07:51 +0100 (dim., 28 janv. 2007) $

Fields Summary
protected HttpServletResponse
origResponse
Original response
protected static final String
info
Descriptive information about this Response implementation.
protected ServletOutputStream
stream
The ServletOutputStream that has been returned by getOutputStream(), if any.
protected PrintWriter
writer
The PrintWriter that has been returned by getWriter(), if any.
protected int
threshold
The threshold number to compress
private int
debug
Debug level
protected String
contentType
Content type
Constructors Summary
public CompressionServletResponseWrapper(HttpServletResponse response)
Calls the parent constructor which creates a ServletResponse adaptor wrapping the given response object.

        super(response);
        origResponse = response;
        if (debug > 1) {
            System.out.println("CompressionServletResponseWrapper constructor gets called");
        }
    
Methods Summary
public javax.servlet.ServletOutputStreamcreateOutputStream()
Create and return a ServletOutputStream to write the content associated with this Response.

exception
IOException if an input/output error occurs

        if (debug > 1) {
            System.out.println("createOutputStream gets called");
        }

        CompressionResponseStream stream = new CompressionResponseStream(origResponse);
        stream.setDebugLevel(debug);
        stream.setBuffer(threshold);

        return stream;

    
public voidfinishResponse()
Finish a response.

        try {
            if (writer != null) {
                writer.close();
            } else {
                if (stream != null)
                    stream.close();
            }
        } catch (IOException e) {
        }
    
public voidflushBuffer()
Flush the buffer and commit this response.

exception
IOException if an input/output error occurs

        if (debug > 1) {
            System.out.println("flush buffer @ CompressionServletResponseWrapper");
        }
        ((CompressionResponseStream)stream).flush();

    
public javax.servlet.ServletOutputStreamgetOutputStream()
Return the servlet output stream associated with this Response.

exception
IllegalStateException if getWriter has already been called for this response
exception
IOException if an input/output error occurs


        if (writer != null)
            throw new IllegalStateException("getWriter() has already been called for this response");

        if (stream == null)
            stream = createOutputStream();
        if (debug > 1) {
            System.out.println("stream is set to "+stream+" in getOutputStream");
        }

        return (stream);

    
public java.io.PrintWritergetWriter()
Return the writer associated with this Response.

exception
IllegalStateException if getOutputStream has already been called for this response
exception
IOException if an input/output error occurs


        if (writer != null)
            return (writer);

        if (stream != null)
            throw new IllegalStateException("getOutputStream() has already been called for this response");

        stream = createOutputStream();
        if (debug > 1) {
            System.out.println("stream is set to "+stream+" in getWriter");
        }
        //String charset = getCharsetFromContentType(contentType);
        String charEnc = origResponse.getCharacterEncoding();
        if (debug > 1) {
            System.out.println("character encoding is " + charEnc);
        }
        // HttpServletResponse.getCharacterEncoding() shouldn't return null
        // according the spec, so feel free to remove that "if"
        if (charEnc != null) {
            writer = new PrintWriter(new OutputStreamWriter(stream, charEnc));
        } else {
            writer = new PrintWriter(stream);
        }
        
        return (writer);

    
public voidsetCompressionThreshold(int threshold)
Set threshold number

        if (debug > 1) {
            System.out.println("setCompressionThreshold to " + threshold);
        }
        this.threshold = threshold;
    
public voidsetContentLength(int length)

    
public voidsetContentType(java.lang.String contentType)
Set content type


    // --------------------------------------------------------- Public Methods


            
        
        if (debug > 1) {
            System.out.println("setContentType to "+contentType);
        }
        this.contentType = contentType;
        origResponse.setContentType(contentType);
    
public voidsetDebugLevel(int debug)
Set debug level

        this.debug = debug;