org.weborganic.bastille.cache.util
Class CachedResponseWrapper

java.lang.Object
  extended by javax.servlet.ServletResponseWrapper
      extended by javax.servlet.http.HttpServletResponseWrapper
          extended by org.weborganic.bastille.cache.util.CachedResponseWrapper
All Implemented Interfaces:
Serializable, javax.servlet.http.HttpServletResponse, javax.servlet.ServletResponse

public final class CachedResponseWrapper
extends javax.servlet.http.HttpServletResponseWrapper
implements Serializable

Provides a wrapper for use with the caching filters.

This is the response that is supplied to the filters and servlets in the filter chain.

See Also:
Serialized Form

Nested Class Summary
static class CachedResponseWrapper.FilterOutputStream
          A custom ServletOutputStream for this wrapper.
 
Field Summary
 
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
CachedResponseWrapper(javax.servlet.http.HttpServletResponse res)
          Creates a cached response wrapper.
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
           
 void addDateHeader(String name, long date)
           
 void addHeader(String name, String value)
           
 void addIntHeader(String name, int value)
           
 void adjustVaryAcceptEncoding(boolean add)
          Adds the "Vary: Accept-Encoding" header to this response.
 void flush()
          Flushes all the streams for this response.
 void flushBuffer()
          Flushes buffer and commits response to client.
 List<HttpHeader<? extends Serializable>> getAllHeaders()
           
 int getContentLength()
           
 String getContentType()
           
 Collection<javax.servlet.http.Cookie> getCookies()
           
 long getDateHeader(String name)
           
 String getHeader(String name)
           
 List<Serializable> getHeaderValues(String name)
           
 javax.servlet.ServletOutputStream getOutputStream()
           
 int getStatus()
           
 PrintWriter getWriter()
           
 void reset()
          Resets the response.
 void sendError(int code)
          Send the error.
 void sendError(int code, String string)
          Send the error.
 void sendRedirect(String url)
          Send the redirect.
 void setContentLength(int length)
           
 void setContentType(String type)
           
 void setDateHeader(String name, long date)
           
 void setHeader(String name, String value)
           
 void setIntHeader(String name, int value)
           
 void setStatus(int status)
           
 void setStatus(int code, String msg)
           
 byte[] toByteArray()
           
 
Methods inherited from class javax.servlet.http.HttpServletResponseWrapper
containsHeader, encodeRedirectUrl, encodeRedirectURL, encodeUrl, encodeURL, getHeaderNames, getHeaders
 
Methods inherited from class javax.servlet.ServletResponseWrapper
getBufferSize, getCharacterEncoding, getLocale, getResponse, isCommitted, isWrapperFor, isWrapperFor, resetBuffer, setBufferSize, setCharacterEncoding, setLocale, setResponse
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.ServletResponse
getBufferSize, getCharacterEncoding, getLocale, isCommitted, resetBuffer, setBufferSize, setCharacterEncoding, setLocale
 

Constructor Detail

CachedResponseWrapper

public CachedResponseWrapper(javax.servlet.http.HttpServletResponse res)
Creates a cached response wrapper.

Parameters:
res - The HTTP response
Method Detail

getOutputStream

public javax.servlet.ServletOutputStream getOutputStream()
Specified by:
getOutputStream in interface javax.servlet.ServletResponse
Overrides:
getOutputStream in class javax.servlet.ServletResponseWrapper

getWriter

public PrintWriter getWriter()
                      throws IOException
Specified by:
getWriter in interface javax.servlet.ServletResponse
Overrides:
getWriter in class javax.servlet.ServletResponseWrapper
Returns:
The print writer wrapping the underlying stream.
Throws:
IOException - If thrown while creating a new PrintWriter instance.

setStatus

public void setStatus(int status)
Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse
Overrides:
setStatus in class javax.servlet.http.HttpServletResponseWrapper

sendError

public void sendError(int code,
                      String string)
               throws IOException
Send the error.

If the response is not OK, most of the logic is bypassed and the error is sent raw and the content is not cached.

Specified by:
sendError in interface javax.servlet.http.HttpServletResponse
Overrides:
sendError in class javax.servlet.http.HttpServletResponseWrapper
Throws:
IOException

sendError

public void sendError(int code)
               throws IOException
Send the error.

If the response is not OK, most of the logic is bypassed and the error is sent raw and the content is not cached.

Specified by:
sendError in interface javax.servlet.http.HttpServletResponse
Overrides:
sendError in class javax.servlet.http.HttpServletResponseWrapper
Throws:
IOException

sendRedirect

public void sendRedirect(String url)
                  throws IOException
Send the redirect.

If the response is not OK, most of the logic is bypassed and the error is sent raw and the content is not cached.

Specified by:
sendRedirect in interface javax.servlet.http.HttpServletResponse
Overrides:
sendRedirect in class javax.servlet.http.HttpServletResponseWrapper
Throws:
IOException

setStatus

public void setStatus(int code,
                      String msg)
Specified by:
setStatus in interface javax.servlet.http.HttpServletResponse
Overrides:
setStatus in class javax.servlet.http.HttpServletResponseWrapper

setContentLength

public void setContentLength(int length)
Specified by:
setContentLength in interface javax.servlet.ServletResponse
Overrides:
setContentLength in class javax.servlet.ServletResponseWrapper

setContentType

public void setContentType(String type)
Specified by:
setContentType in interface javax.servlet.ServletResponse
Overrides:
setContentType in class javax.servlet.ServletResponseWrapper

getContentType

public String getContentType()
Specified by:
getContentType in interface javax.servlet.ServletResponse
Overrides:
getContentType in class javax.servlet.ServletResponseWrapper

addHeader

public void addHeader(String name,
                      String value)
Specified by:
addHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
addHeader in class javax.servlet.http.HttpServletResponseWrapper

setHeader

public void setHeader(String name,
                      String value)
Specified by:
setHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
setHeader in class javax.servlet.http.HttpServletResponseWrapper

addDateHeader

public void addDateHeader(String name,
                          long date)
Specified by:
addDateHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
addDateHeader in class javax.servlet.http.HttpServletResponseWrapper

setDateHeader

public void setDateHeader(String name,
                          long date)
Specified by:
setDateHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
setDateHeader in class javax.servlet.http.HttpServletResponseWrapper

addIntHeader

public void addIntHeader(String name,
                         int value)
Specified by:
addIntHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
addIntHeader in class javax.servlet.http.HttpServletResponseWrapper

setIntHeader

public void setIntHeader(String name,
                         int value)
Specified by:
setIntHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
setIntHeader in class javax.servlet.http.HttpServletResponseWrapper

addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Specified by:
addCookie in interface javax.servlet.http.HttpServletResponse
Overrides:
addCookie in class javax.servlet.http.HttpServletResponseWrapper

flushBuffer

public void flushBuffer()
                 throws IOException
Flushes buffer and commits response to client.

This method does not flush the buffer of the underlying stream so as to avoid the response to commit the response prematurely.

Specified by:
flushBuffer in interface javax.servlet.ServletResponse
Overrides:
flushBuffer in class javax.servlet.ServletResponseWrapper
Throws:
IOException - if thrown while flushing the underlying buffer.

reset

public void reset()
Resets the response.

Specified by:
reset in interface javax.servlet.ServletResponse
Overrides:
reset in class javax.servlet.ServletResponseWrapper

getStatus

public int getStatus()
Specified by:
getStatus in interface javax.servlet.http.HttpServletResponse
Overrides:
getStatus in class javax.servlet.http.HttpServletResponseWrapper
Returns:
the status code for this response.

getContentLength

public int getContentLength()
Returns:
the content length.

getCookies

public Collection<javax.servlet.http.Cookie> getCookies()
Returns:
all the cookies.

getAllHeaders

public List<HttpHeader<? extends Serializable>> getAllHeaders()
Returns:
All of the headersMap set/added on the response

getHeaderValues

public List<Serializable> getHeaderValues(String name)
Parameters:
name - the name of the header
Returns:
All of the headersMap set/added on the response

getHeader

public String getHeader(String name)
Specified by:
getHeader in interface javax.servlet.http.HttpServletResponse
Overrides:
getHeader in class javax.servlet.http.HttpServletResponseWrapper
Parameters:
name - the name of the header
Returns:
All of the headersMap set/added on the response

getDateHeader

public long getDateHeader(String name)
Parameters:
name - the name of the header
Returns:
the value of the header as a date.

flush

public void flush()
           throws IOException
Flushes all the streams for this response.

Throws:
IOException - if thrown by the underlying output stream or writer

toByteArray

public byte[] toByteArray()
Returns:
the content of the underlying stream as a byte array.

adjustVaryAcceptEncoding

public void adjustVaryAcceptEncoding(boolean add)
Adds the "Vary: Accept-Encoding" header to this response.

Parameters:
add - true to add the "Accept-Encoding" to the Vary header; false to remove it.