FileDocCategorySizeDatePackage
CachingResponseWrapper.javaAPI DocGlassfish v2 API11733Fri May 04 22:35:28 BST 2007com.sun.appserv.web.cache.filter

CachingResponseWrapper

public class CachingResponseWrapper extends HttpServletResponseWrapper
a wrapper to HttpServletResponse to cache the outbound headers and content
see
javax.servlet.http.HttpServletResponseWrapper and
see
javax.servlet.http.HttpServletResponse
see
org.apache.catalina.connector.HttpResponseBase

Fields Summary
int
statusCode
HashMap
headers
The HTTP headers explicitly added via addHeader(), but not including those to be added with setContentLength(), setContentType(), and so on. This collection is keyed by the header name, and the elements are ArrayLists containing the associated values that have been set.
HashMap
dateHeaders
cache all the set/addDateHeader calls
ArrayList
cookies
The set of Cookies associated with this Response.
int
contentLength
String
contentType
Locale
locale
boolean
error
Error flag. True if the response runs into an error. treat the response to be in the error state if the servlet doesn't get the OutpuStream or the Writer.
CachingOutputStreamWrapper
cosw
OutputStream and PrintWriter objects for this response.
PrintWriter
writer
Constructors Summary
public CachingResponseWrapper(HttpServletResponse response)
Constructs a response adaptor wrapping the given response.

throws
java.lang.IllegalArgumentException if the response is null


                         
       
	    super(response);
    
Methods Summary
public voidaddCookie(javax.servlet.http.Cookie cookie)
The default behavior of this method is to call addCookie(Cookie cookie) on the wrapped response object.

        super.addCookie(cookie);

        synchronized (cookies) {
            cookies.add(cookie);
        }
    
public voidaddDateHeader(java.lang.String name, long value)
Add the specified date header to the specified value.

param
name Name of the header to set
param
value Date value to be set

        super.addDateHeader(name, value);

        ArrayList values = (ArrayList) dateHeaders.get(name);
        if (values == null) {
            values = new ArrayList();

            synchronized (dateHeaders) {
                dateHeaders.put(name, values);
            }
        }

        values.add(Long.valueOf(value));
    
public voidaddHeader(java.lang.String name, java.lang.String value)
Add the specified header to the specified value.

param
name Name of the header to set
param
value Value to be set

        super.addHeader(name, value);

        ArrayList values = (ArrayList) headers.get(name);
        if (values == null) {
            values = new ArrayList();

            synchronized (headers) {
                headers.put(name, values);
            }
        }

        values.add(value);
    
public voidaddIntHeader(java.lang.String name, int value)
Add the specified integer header to the specified value.

param
name Name of the header to set
param
value Integer value to be set

        addHeader(name, "" + value);
    
public HttpCacheEntrycacheResponse()
called by doFilter to cache the response that was just sent out

return
the entry with cached response headers and body.

        // create a new entry
        HttpCacheEntry entry = new HttpCacheEntry();
        entry.responseHeaders = headers;
        entry.dateHeaders = dateHeaders;
        entry.cookies = cookies;

        entry.contentLength = contentLength;
        entry.contentType = contentType;
        entry.locale = locale;

        entry.statusCode = statusCode;

        // flush the writer??
        if (writer != null) {
            writer.flush();
        }

        entry.bytes = cosw.getBytes();
        return entry;
    
public voidclear()
clear the contents of this wrapper

        cosw = null;
        writer = null;
    
private CachingOutputStreamWrappercreateCachingOutputStreamWrapper()
Create and return a ServletOutputStream to write the content associated with this Response.

exception
IOException if an input/output error occurs

        return new CachingOutputStreamWrapper();
    
public java.lang.LonggetExpiresDateHeader()
return the Expires: date header value

        return (Long)dateHeaders.get("Expires");
    
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 ("getOutputStream<>getWriter");

        if (cosw == null) {
            cosw = createCachingOutputStreamWrapper();
        }

        error = false;
        return (ServletOutputStream)cosw;
    
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 (cosw != null)
            throw new IllegalStateException ("getWriter<>getOutputStream");

        cosw = createCachingOutputStreamWrapper();

        OutputStreamWriter osw =
            new OutputStreamWriter(cosw, getCharacterEncoding());

        writer = new PrintWriter(osw);

        error = false;
        return (writer);
	
public booleanisError()
has the response been set to error

        return error;
    
public voidsendError(int status)
Send an error response with the specified status and a default message.

param
status HTTP status code to send
exception
IllegalStateException if this response has already been committed
exception
IOException if an input/output error occurs

        super.sendError(status);

        error = true;
    
public voidsendError(int status, java.lang.String message)
Send an error response with the specified status and message.

param
status HTTP status code to send
param
message Corresponding message to send
exception
IllegalStateException if this response has already been committed
exception
IOException if an input/output error occurs

        super.sendError(status, message);

        error = true;
    
public voidsetContentLength(int len)
Set the content length (in bytes) for this Response.

param
len The new content length

        super.setContentLength(len);

        this.contentLength = len;
    
public voidsetContentType(java.lang.String type)
Set the content type for this Response.

param
type The new content type

        super.setContentType(type);

        this.contentType = type;
    
public voidsetDateHeader(java.lang.String name, long value)
Set the specified date header to the specified value.

param
name Name of the header to set
param
value Date value to be set

        super.setDateHeader(name, value);

        ArrayList values = new ArrayList();
        values.add(Long.valueOf(value));

        synchronized (dateHeaders) {
            dateHeaders.put(name, values);
        }
    
public voidsetHeader(java.lang.String name, java.lang.String value)
Set the specified header to the specified value.

param
name Name of the header to set
param
value Value to be set

        super.setHeader(name, value);

        ArrayList values = new ArrayList();
        values.add(value);

        synchronized (headers) {
            headers.put(name, values);
        }
    
public voidsetIntHeader(java.lang.String name, int value)
Set the specified integer header to the specified value.

param
name Name of the header to set
param
value Integer value to be set

        setHeader(name, "" + value);
    
public voidsetLocale(java.util.Locale locale)
Set the Locale that is appropriate for this response, including setting the appropriate character encoding.

param
locale The new locale

        super.setLocale(locale);

        this.locale = locale;
    
public voidsetStatus(int sc)
Set the HTTP status to be returned with this response.

param
sc The new HTTP status

        super.setStatus(sc);

        this.statusCode = sc;