Spring Framework

org.springframework.web.filter
Class OncePerRequestFilter

java.lang.Object
  extended by org.springframework.web.filter.GenericFilterBean
      extended by org.springframework.web.filter.OncePerRequestFilter
All Implemented Interfaces:
javax.servlet.Filter, Aware, BeanNameAware, DisposableBean, InitializingBean, EnvironmentAware, ServletContextAware
Direct Known Subclasses:
AbstractRequestLoggingFilter, CharacterEncodingFilter, HiddenHttpMethodFilter, HttpPutFormContentFilter, MultipartFilter, OpenEntityManagerInViewFilter, OpenPersistenceManagerInViewFilter, OpenSessionInViewFilter, OpenSessionInViewFilter, RequestContextFilter, ShallowEtagHeaderFilter

public abstract class OncePerRequestFilter
extends GenericFilterBean

Filter base class that guarantees to be just executed once per request, on any servlet container. It provides a doFilterInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain) method with HttpServletRequest and HttpServletResponse arguments.

The getAlreadyFilteredAttributeName() method determines how to identify that a request is already filtered. The default implementation is based on the configured name of the concrete filter instance.

Since:
06.12.2003
Author:
Juergen Hoeller, Rossen Stoyanchev

Field Summary
static java.lang.String ALREADY_FILTERED_SUFFIX
          Suffix that gets appended to the filter name for the "already filtered" request attribute.
 
Fields inherited from class org.springframework.web.filter.GenericFilterBean
logger
 
Constructor Summary
OncePerRequestFilter()
           
 
Method Summary
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain filterChain)
          This doFilter implementation stores a request attribute for "already filtered", proceeding without filtering again if the attribute is already there.
protected abstract  void doFilterInternal(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain filterChain)
          Same contract as for doFilter, but guaranteed to be just invoked once per request.
protected  java.lang.String getAlreadyFilteredAttributeName()
          Return the name of the request attribute that identifies that a request is already filtered.
protected  boolean shouldNotFilter(javax.servlet.http.HttpServletRequest request)
          Can be overridden in subclasses for custom filtering control, returning true to avoid filtering of the given request.
 
Methods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALREADY_FILTERED_SUFFIX

public static final java.lang.String ALREADY_FILTERED_SUFFIX
Suffix that gets appended to the filter name for the "already filtered" request attribute.

See Also:
getAlreadyFilteredAttributeName(), Constant Field Values
Constructor Detail

OncePerRequestFilter

public OncePerRequestFilter()
Method Detail

doFilter

public final void doFilter(javax.servlet.ServletRequest request,
                           javax.servlet.ServletResponse response,
                           javax.servlet.FilterChain filterChain)
                    throws javax.servlet.ServletException,
                           java.io.IOException
This doFilter implementation stores a request attribute for "already filtered", proceeding without filtering again if the attribute is already there.

Throws:
javax.servlet.ServletException
java.io.IOException
See Also:
getAlreadyFilteredAttributeName(), shouldNotFilter(javax.servlet.http.HttpServletRequest), doFilterInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)

getAlreadyFilteredAttributeName

protected java.lang.String getAlreadyFilteredAttributeName()
Return the name of the request attribute that identifies that a request is already filtered.

Default implementation takes the configured name of the concrete filter instance and appends ".FILTERED". If the filter is not fully initialized, it falls back to its class name.

See Also:
GenericFilterBean.getFilterName(), ALREADY_FILTERED_SUFFIX

shouldNotFilter

protected boolean shouldNotFilter(javax.servlet.http.HttpServletRequest request)
                           throws javax.servlet.ServletException
Can be overridden in subclasses for custom filtering control, returning true to avoid filtering of the given request.

The default implementation always returns false.

Parameters:
request - current HTTP request
Returns:
whether the given request should not be filtered
Throws:
javax.servlet.ServletException - in case of errors

doFilterInternal

protected abstract void doFilterInternal(javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response,
                                         javax.servlet.FilterChain filterChain)
                                  throws javax.servlet.ServletException,
                                         java.io.IOException
Same contract as for doFilter, but guaranteed to be just invoked once per request. Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.

Throws:
javax.servlet.ServletException
java.io.IOException

Spring Framework