com.opensymphony.oscache.web.filter
Class CacheFilter

java.lang.Object
  extended bycom.opensymphony.oscache.web.filter.CacheFilter
All Implemented Interfaces:
javax.servlet.Filter, ICacheGroupsProvider, ICacheKeyProvider

public class CacheFilter
extends Object
implements javax.servlet.Filter, ICacheKeyProvider, ICacheGroupsProvider

CacheFilter is a filter that allows for server-side caching of post-processed servlet content.

It also gives great programatic control over refreshing, flushing and updating the cache.

Version:
$Revision: 1.9 $
Author:
Serge Knystautas, Mike Cannon-Brookes, Lars Torunski

Field Summary
static long EXPIRES_OFF
           
static long EXPIRES_ON
           
static long EXPIRES_TIME
           
static int FRAGMENT_AUTODETECT
           
static int FRAGMENT_NO
           
static int FRAGMENT_YES
           
static String HEADER_ACCEPT_ENCODING
           
static String HEADER_CACHE_CONTROL
           
static String HEADER_CONTENT_ENCODING
           
static String HEADER_CONTENT_TYPE
           
static String HEADER_EXPIRES
           
static String HEADER_IF_MODIFIED_SINCE
           
static String HEADER_LAST_MODIFIED
           
static long LAST_MODIFIED_INITIAL
           
static long LAST_MODIFIED_OFF
           
static long LAST_MODIFIED_ON
           
static int NOCACHE_OFF
           
static int NOCACHE_SESSION_ID_IN_URL
           
 
Constructor Summary
CacheFilter()
           
 
Method Summary
protected  boolean acceptsGZipEncoding(javax.servlet.http.HttpServletRequest request)
          Check if the client browser support gzip compression.
 String[] createCacheGroups(javax.servlet.http.HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache)
          Creates the cache groups for the CacheFilter.
 String createCacheKey(javax.servlet.http.HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache)
          Creates the cache key for the CacheFilter.
 void destroy()
          Filter clean-up
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
          The doFilter call caches the response by wrapping the HttpServletResponse object so that the output stream can be caught.
 void init(javax.servlet.FilterConfig filterConfig)
          Initialize the filter.
protected  boolean isCacheable(CacheHttpServletResponseWrapper cacheResponse)
          isCacheable is a method allowing subclass to decide if a response is cachable or not.
protected  boolean isCacheable(javax.servlet.ServletRequest request)
          isCacheable is a method allowing a subclass to decide if a request is cachable or not.
protected  boolean isFilteredBefore(javax.servlet.ServletRequest request)
          Checks if the request was filtered before, so guarantees to be executed once per request.
protected  boolean isFragment(javax.servlet.http.HttpServletRequest request)
          Checks if the request is a fragment in a page.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEADER_LAST_MODIFIED

public static final String HEADER_LAST_MODIFIED
See Also:
Constant Field Values

HEADER_CONTENT_TYPE

public static final String HEADER_CONTENT_TYPE
See Also:
Constant Field Values

HEADER_CONTENT_ENCODING

public static final String HEADER_CONTENT_ENCODING
See Also:
Constant Field Values

HEADER_EXPIRES

public static final String HEADER_EXPIRES
See Also:
Constant Field Values

HEADER_IF_MODIFIED_SINCE

public static final String HEADER_IF_MODIFIED_SINCE
See Also:
Constant Field Values

HEADER_CACHE_CONTROL

public static final String HEADER_CACHE_CONTROL
See Also:
Constant Field Values

HEADER_ACCEPT_ENCODING

public static final String HEADER_ACCEPT_ENCODING
See Also:
Constant Field Values

FRAGMENT_AUTODETECT

public static final int FRAGMENT_AUTODETECT
See Also:
Constant Field Values

FRAGMENT_NO

public static final int FRAGMENT_NO
See Also:
Constant Field Values

FRAGMENT_YES

public static final int FRAGMENT_YES
See Also:
Constant Field Values

NOCACHE_OFF

public static final int NOCACHE_OFF
See Also:
Constant Field Values

NOCACHE_SESSION_ID_IN_URL

public static final int NOCACHE_SESSION_ID_IN_URL
See Also:
Constant Field Values

LAST_MODIFIED_OFF

public static final long LAST_MODIFIED_OFF
See Also:
Constant Field Values

LAST_MODIFIED_ON

public static final long LAST_MODIFIED_ON
See Also:
Constant Field Values

LAST_MODIFIED_INITIAL

public static final long LAST_MODIFIED_INITIAL
See Also:
Constant Field Values

EXPIRES_OFF

public static final long EXPIRES_OFF
See Also:
Constant Field Values

EXPIRES_ON

public static final long EXPIRES_ON
See Also:
Constant Field Values

EXPIRES_TIME

public static final long EXPIRES_TIME
See Also:
Constant Field Values
Constructor Detail

CacheFilter

public CacheFilter()
Method Detail

destroy

public void destroy()
Filter clean-up

Specified by:
destroy in interface javax.servlet.Filter

doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain chain)
              throws javax.servlet.ServletException,
                     IOException
The doFilter call caches the response by wrapping the HttpServletResponse object so that the output stream can be caught. This works by splitting off the output stream into two with the SplitServletOutputStream class. One stream gets written out to the response as normal, the other is fed into a byte array inside a ResponseContent object.

Specified by:
doFilter in interface javax.servlet.Filter
Parameters:
request - The servlet request
response - The servlet response
chain - The filter chain
Throws:
javax.servlet.ServletException - IOException
IOException

init

public void init(javax.servlet.FilterConfig filterConfig)
Initialize the filter. This retrieves a ServletCacheAdministrator instance and configures the filter based on any initialization parameters.

The supported initialization parameters are:


createCacheKey

public String createCacheKey(javax.servlet.http.HttpServletRequest httpRequest,
                             ServletCacheAdministrator scAdmin,
                             Cache cache)
Description copied from interface: ICacheKeyProvider
Creates the cache key for the CacheFilter.

Specified by:
createCacheKey in interface ICacheKeyProvider
Parameters:
httpRequest - the http request.
scAdmin - the ServletCacheAdministrator of the cache
cache - the cache of the ServletCacheAdministrator
Returns:
the cache key
See Also:
ICacheKeyProvider.createCacheKey(javax.servlet.http.HttpServletRequest, ServletCacheAdministrator, Cache)

createCacheGroups

public String[] createCacheGroups(javax.servlet.http.HttpServletRequest httpRequest,
                                  ServletCacheAdministrator scAdmin,
                                  Cache cache)
Description copied from interface: ICacheGroupsProvider
Creates the cache groups for the CacheFilter.

Specified by:
createCacheGroups in interface ICacheGroupsProvider
Parameters:
httpRequest - the http request.
scAdmin - the ServletCacheAdministrator of the cache
cache - the cache of the ServletCacheAdministrator
Returns:
the cache key
See Also:
ICacheGroupsProvider.createCacheGroups(javax.servlet.http.HttpServletRequest, ServletCacheAdministrator, Cache)

isFragment

protected boolean isFragment(javax.servlet.http.HttpServletRequest request)
Checks if the request is a fragment in a page. According to Java Servlet API 2.2 (8.2.1 Dispatching Requests, Included Request Parameters), when a servlet is being used from within an include, the attribute javax.servlet.include.request_uri is set. According to Java Servlet API 2.3 this is excepted for servlets obtained by using the getNamedDispatcher method.

Parameters:
request - the to be handled request
Returns:
true if the request is a fragment in a page

isFilteredBefore

protected boolean isFilteredBefore(javax.servlet.ServletRequest request)
Checks if the request was filtered before, so guarantees to be executed once per request. You can override this methods to define a more specific behaviour.

Parameters:
request - checks if the request was filtered before.
Returns:
true if it is the first execution

isCacheable

protected boolean isCacheable(javax.servlet.ServletRequest request)
isCacheable is a method allowing a subclass to decide if a request is cachable or not.

Parameters:
request - The servlet request
Returns:
Returns a boolean indicating if the request can be cached or not.

isCacheable

protected boolean isCacheable(CacheHttpServletResponseWrapper cacheResponse)
isCacheable is a method allowing subclass to decide if a response is cachable or not.

Parameters:
cacheResponse - The HTTP servlet response
Returns:
Returns a boolean indicating if the response can be cached or not.

acceptsGZipEncoding

protected boolean acceptsGZipEncoding(javax.servlet.http.HttpServletRequest request)
Check if the client browser support gzip compression.

Parameters:
request - the http request
Returns:
true if client browser supports GZIP

OSCache Project Page