FileDocCategorySizeDatePackage
CompressionServletResponseWrapper.javaAPI DocGlassfish v2 API9012Fri May 04 22:34:22 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: 1.3 $, $Date: 2007/05/05 05:34:21 $

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();

    
private static java.lang.StringgetCharsetFromContentType(java.lang.String type)
Returns character from content type. This method was taken from tomcat.

author
rajo


        if (type == null) {
            return null;
        }
        int semi = type.indexOf(";");
        if (semi == -1) {
            return null;
        }
        String afterSemi = type.substring(semi + 1);
        int charsetLocation = afterSemi.indexOf("charset=");
        if(charsetLocation == -1) {
            return null;
        } else {
            String afterCharset = afterSemi.substring(charsetLocation + 8);
            String encoding = afterCharset.trim();
            return encoding;
        }
    
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;