org.weborganic.bastille.cache.filter
Class StaticCachingFilter

java.lang.Object
  extended by org.weborganic.bastille.cache.filter.CachingFilterBase
      extended by org.weborganic.bastille.cache.filter.StaticCachingFilter
All Implemented Interfaces:
javax.servlet.Filter, CachingFilter

public final class StaticCachingFilter
extends CachingFilterBase
implements CachingFilter

A caching filter for static resources such as scripts, images, styles, etc..

This filter is designed to be used for URIs which map directly to a file on the server as it will check whether the file has been changed and update the cache accordingly. These resources are typically served by the default Servlet on the Web container.

GZip compression

This filter will automatically compress resources such as styles and scripts while leaving images uncompressed. This is done based on the media type of the resource.

If this use agent does not support GZip encoding, the resource is served uncompressed.

Cache Key

The key for each resource is the path component of the URI. The scheme, host port, query string and fragment are ignored. The HTTP method is also ignored, so that GET and POST methods are equivalent.


Field Summary
static String DEFAULT_CACHE_CONTROL
          The default cache control to use.
static String DEFAULT_CACHE_NAME
          The default of the cache to use.
static long DEFAULT_FILESIZE_THRESHOLD
          The default file size threshold.
 
Constructor Summary
StaticCachingFilter()
           
 
Method Summary
 String calculateKey(javax.servlet.http.HttpServletRequest req)
          Returns the key for this cache.
 void destroy()
           
 CacheManager getCacheManager()
          Returns the CacheManager for this caching filter.
 String getDefaultCacheName()
          Returns the default name for the cache managed by the filter.
 CachedResource getResource(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, javax.servlet.FilterChain chain)
          Get the requested resource either from the cache or by invoking the page directly.
 void init(javax.servlet.FilterConfig config)
          Initialises blockingCache to use.
 boolean isCacheable(javax.servlet.http.HttpServletRequest req)
          Always return true unless the "berlioz-cache" parameter is set to "false" or the file is too large or does not exist.
 void writeResponse(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, CachedResource resource)
          Writes the response from a PageInfo object.
 
Methods inherited from class org.weborganic.bastille.cache.filter.CachingFilterBase
doFilter, doFilter, writeContent
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CACHE_NAME

public static final String DEFAULT_CACHE_NAME
The default of the cache to use.

See Also:
Constant Field Values

DEFAULT_CACHE_CONTROL

public static final String DEFAULT_CACHE_CONTROL
The default cache control to use.

See Also:
Constant Field Values

DEFAULT_FILESIZE_THRESHOLD

public static final long DEFAULT_FILESIZE_THRESHOLD
The default file size threshold.

Beyond this files are not cached but served directly.

See Also:
Constant Field Values
Constructor Detail

StaticCachingFilter

public StaticCachingFilter()
Method Detail

getCacheManager

public CacheManager getCacheManager()
Description copied from interface: CachingFilter
Returns the CacheManager for this caching filter.

Specified by:
getCacheManager in interface CachingFilter
Returns:
the CacheManager to be used

getDefaultCacheName

public String getDefaultCacheName()
Description copied from interface: CachingFilter
Returns the default name for the cache managed by the filter.

This method must not return null.

Specified by:
getDefaultCacheName in interface CachingFilter
Returns:
the default name for the cache manage by the filter.

init

public void init(javax.servlet.FilterConfig config)
          throws CacheException
Description copied from class: CachingFilterBase
Initialises blockingCache to use.

The BlockingCache created by this method does not have a lock timeout set.

A timeout can be appled using blockingCache.setTimeoutMillis(int timeout) and takes effect immediately for all new requests

Specified by:
init in interface javax.servlet.Filter
Overrides:
init in class CachingFilterBase
Parameters:
config - this filter's configuration.
Throws:
CacheException - The most likely cause is that a cache has not been configured in ehcache's configuration file ehcache.xml for the filter name

destroy

public void destroy()
Specified by:
destroy in interface javax.servlet.Filter

getResource

public CachedResource getResource(javax.servlet.http.HttpServletRequest req,
                                  javax.servlet.http.HttpServletResponse res,
                                  javax.servlet.FilterChain chain)
                           throws IOException,
                                  javax.servlet.ServletException,
                                  CacheException
Get the requested resource either from the cache or by invoking the page directly. Returns a cached resource from the specified request and response.

This method may invoke the rest of the filter chain.

Specified by:
getResource in interface CachingFilter
Parameters:
req - The HTTP servlet request.
res - The HTTP servlet response.
chain - The filter chain.
Returns:
the corresponding cached resource
Throws:
IOException - Should an IO error occur
javax.servlet.ServletException - For general errors or errors while invoking the filetr chain.
CacheException - For caching-specific error.

isCacheable

public boolean isCacheable(javax.servlet.http.HttpServletRequest req)
Always return true unless the "berlioz-cache" parameter is set to "false" or the file is too large or does not exist. Indicates whether the resource from the specified request can return a cached copy.

This method may be used to determine whether of not this filter should be used for the resource. Implementations may use this method to decide whether to bypass the filter and directly invoke the rest of the filter chain.

Specified by:
isCacheable in interface CachingFilter
Parameters:
req - The HTTP servlet request.
Returns:
true if the caching filter can use a cache; false to invoke the rest of the filter chain.

writeResponse

public void writeResponse(javax.servlet.http.HttpServletRequest req,
                          javax.servlet.http.HttpServletResponse res,
                          CachedResource resource)
                   throws IOException,
                          javax.servlet.ServletException
Writes the response from a PageInfo object.

This method actually performs the conditional GET and returns 304 if not modified, short-circuiting the normal response.

Specified by:
writeResponse in interface CachingFilter
Parameters:
req - The HTTP servlet request.
res - the HTTP servlet response.
resource - The cached resource build previously.
Throws:
IOException - For I/O errors only
javax.servlet.ServletException - For all other errors.

calculateKey

public String calculateKey(javax.servlet.http.HttpServletRequest req)
Returns the key for this cache.

Specified by:
calculateKey in interface CachingFilter
Parameters:
req - the HTTP Servlet request.
Returns:
the request URI.