ehcache

net.sf.ehcache.constructs.web.filter
Class Filter

java.lang.Object
  extended by net.sf.ehcache.constructs.web.filter.Filter
All Implemented Interfaces:
javax.servlet.Filter
Direct Known Subclasses:
CachingFilter, GzipFilter

public abstract class Filter
extends java.lang.Object
implements javax.servlet.Filter

A generic Filter with most of what we need done.

Participates in the Template Method pattern with Filter.

Version:
$Id: Filter.java 512 2007-07-10 09:18:45Z gregluck $
Author:
Greg Luck

Field Summary
protected  java.lang.String exceptionsToLogDifferently
          The exceptions to log differently, as a comma separated list
protected  java.lang.String exceptionsToLogDifferentlyLevel
          A the level of the exceptions which will be logged differently
protected  javax.servlet.FilterConfig filterConfig
          The filter configuration.
static java.lang.String NO_FILTER
          If a request attribute NO_FILTER is set, then filtering will be skipped
protected  boolean suppressStackTraces
          Most Throwables in Web applications propagate to the user.
 
Constructor Summary
Filter()
           
 
Method Summary
protected  boolean acceptsEncoding(javax.servlet.http.HttpServletRequest request, java.lang.String name)
          Checks if request accepts the named encoding.
protected  boolean acceptsGzipEncoding(javax.servlet.http.HttpServletRequest request)
          Determine whether the user agent accepts GZIP encoding.
 void destroy()
          Destroys the filter.
protected abstract  void doDestroy()
          A template method that performs any Filter specific destruction tasks.
protected abstract  void doFilter(javax.servlet.http.HttpServletRequest httpRequest, javax.servlet.http.HttpServletResponse httpResponse, javax.servlet.FilterChain chain)
          A template method that performs the filtering for a request.
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
          Performs the filtering.
protected abstract  void doInit()
          A template method that performs any Filter specific initialisation tasks.
protected  boolean filterNotDisabled(javax.servlet.http.HttpServletRequest httpRequest)
          Filters can be disabled programmatically by adding a NO_FILTER parameter to the request.
 javax.servlet.FilterConfig getFilterConfig()
          Returns the filter config.
 void init(javax.servlet.FilterConfig config)
          Initialises the filter.
protected  void logRequestHeaders(javax.servlet.http.HttpServletRequest request)
          Logs the request headers, if debug is enabled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_FILTER

public static final java.lang.String NO_FILTER
If a request attribute NO_FILTER is set, then filtering will be skipped

See Also:
Constant Field Values

filterConfig

protected javax.servlet.FilterConfig filterConfig
The filter configuration.


exceptionsToLogDifferently

protected java.lang.String exceptionsToLogDifferently
The exceptions to log differently, as a comma separated list


exceptionsToLogDifferentlyLevel

protected java.lang.String exceptionsToLogDifferentlyLevel
A the level of the exceptions which will be logged differently


suppressStackTraces

protected boolean suppressStackTraces
Most Throwables in Web applications propagate to the user. Usually they are logged where they first happened. Printing the stack trace once a Throwable as propagated to the servlet is sometimes just clutters the log.

This field corresponds to an init-param of the same name. If set to true stack traces will be suppressed.

Constructor Detail

Filter

public Filter()
Method Detail

doFilter

public final void doFilter(javax.servlet.ServletRequest request,
                           javax.servlet.ServletResponse response,
                           javax.servlet.FilterChain chain)
                    throws javax.servlet.ServletException,
                           java.io.IOException
Performs the filtering. This method calls template method doFilter(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,javax.servlet.FilterChain) which does the filtering. This method takes care of error reporting and handling. Errors are reported at Log.warn(Object) level because http tends to produce lots of errors.

Specified by:
doFilter in interface javax.servlet.Filter
Throws:
java.io.IOException - if an IOException occurs during this method it will be rethrown and will not be wrapped
javax.servlet.ServletException

filterNotDisabled

protected boolean filterNotDisabled(javax.servlet.http.HttpServletRequest httpRequest)
Filters can be disabled programmatically by adding a NO_FILTER parameter to the request. This parameter is normally added to make RequestDispatcher include and forwards work.

Parameters:
httpRequest - the request
Returns:
true if NO_FILTER is not set.

init

public final void init(javax.servlet.FilterConfig config)
                throws javax.servlet.ServletException
Initialises the filter. Calls template method doInit() to perform any filter specific initialisation.

Specified by:
init in interface javax.servlet.Filter
Throws:
javax.servlet.ServletException

destroy

public final void destroy()
Destroys the filter. Calls template method doDestroy() to perform any filter specific destruction tasks.

Specified by:
destroy in interface javax.servlet.Filter

acceptsEncoding

protected boolean acceptsEncoding(javax.servlet.http.HttpServletRequest request,
                                  java.lang.String name)
Checks if request accepts the named encoding.


logRequestHeaders

protected void logRequestHeaders(javax.servlet.http.HttpServletRequest request)
Logs the request headers, if debug is enabled.

Parameters:
request -

doDestroy

protected abstract void doDestroy()
A template method that performs any Filter specific destruction tasks. Called from destroy()


doFilter

protected abstract void doFilter(javax.servlet.http.HttpServletRequest httpRequest,
                                 javax.servlet.http.HttpServletResponse httpResponse,
                                 javax.servlet.FilterChain chain)
                          throws java.lang.Throwable
A template method that performs the filtering for a request. Called from doFilter(ServletRequest,ServletResponse,FilterChain).

Throws:
java.lang.Throwable

doInit

protected abstract void doInit()
                        throws java.lang.Exception
A template method that performs any Filter specific initialisation tasks. Called from init(FilterConfig).

Throws:
java.lang.Exception

getFilterConfig

public javax.servlet.FilterConfig getFilterConfig()
Returns the filter config.


acceptsGzipEncoding

protected boolean acceptsGzipEncoding(javax.servlet.http.HttpServletRequest request)
Determine whether the user agent accepts GZIP encoding. This feature is part of HTTP1.1. If a browser accepts GZIP encoding it will advertise this by including in its HTTP header:

Accept-Encoding: gzip

Requests which do not accept GZIP encoding fall into the following categories:

As of September 2004, about 34% of requests coming from the Internet did not accept GZIP encoding.

Parameters:
request -
Returns:
true, if the User Agent request accepts GZIP encoding

ehcache

true