JSecurity

org.jsecurity.web.servlet
Class OncePerRequestFilter

java.lang.Object
  extended by org.jsecurity.web.servlet.ServletContextSupport
      extended by org.jsecurity.web.servlet.OncePerRequestFilter
All Implemented Interfaces:
Filter, Nameable
Direct Known Subclasses:
AdviceFilter, JSecurityFilter

public abstract class OncePerRequestFilter
extends ServletContextSupport
implements Filter, Nameable

Filter base class that guarantees to be just executed once per request, on any servlet container. It provides a doFilterInternal(javax.servlet.ServletRequest, javax.servlet.ServletResponse, 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.

NOTE This class was borrowed from the Spring framework, and as such, all copyright notices and author names have remained in tact.

Since:
0.1
Author:
Les Hazlewood, Juergen Hoeller

Field Summary
static String ALREADY_FILTERED_SUFFIX
          Suffix that gets appended to the filter name for the "already filtered" request attribute.
protected  FilterConfig filterConfig
          FilterConfig provided by the Servlet container at startup.
 
Constructor Summary
OncePerRequestFilter()
           
 
Method Summary
 void destroy()
          Default no-op implementation that can be overridden by subclasses for custom cleanup behavior.
 void doFilter(ServletRequest request, ServletResponse response, 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(ServletRequest request, ServletResponse response, FilterChain filterChain)
          Same contract as for doFilter, but guaranteed to be just invoked once per request.
protected  String getAlreadyFilteredAttributeName()
          Return name of the request attribute that identifies that a request has already been filtered.
 FilterConfig getFilterConfig()
          Returns the servlet container specified FilterConfig instance provided at startup.
protected  String getName()
          Returns the name of this filter.
 void init(FilterConfig filterConfig)
          Sets the filter's filterConfig and then immediately calls onFilterConfigSet() to trigger any processing a subclass might wish to perform.
protected  void onFilterConfigSet()
          Template method to be overridden by subclasses to perform initialization logic at startup.
 void setFilterConfig(FilterConfig filterConfig)
          Sets the FilterConfig and the ServletContext as attributes of this class for use by subclasses.
 void setName(String name)
          Sets the filter's name.
protected  boolean shouldNotFilter(ServletRequest request)
          Can be overridden in subclasses for custom filtering control, returning true to avoid filtering of the given request.
 
Methods inherited from class org.jsecurity.web.servlet.ServletContextSupport
bind, getAttribute, getContextInitParam, getServletContext, removeAttribute, setAttribute, 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 String ALREADY_FILTERED_SUFFIX
Suffix that gets appended to the filter name for the "already filtered" request attribute.

See Also:
getAlreadyFilteredAttributeName(), Constant Field Values

filterConfig

protected FilterConfig filterConfig
FilterConfig provided by the Servlet container at startup.

Constructor Detail

OncePerRequestFilter

public OncePerRequestFilter()
Method Detail

getFilterConfig

public FilterConfig getFilterConfig()
Returns the servlet container specified FilterConfig instance provided at startup.

Returns:
the servlet container specified FilterConfig instance provided at startup.

setFilterConfig

public void setFilterConfig(FilterConfig filterConfig)
Sets the FilterConfig and the ServletContext as attributes of this class for use by subclasses. That is:

this.filterConfig = filterConfig;
setServletContext(filterConfig.getServletContext());

Parameters:
filterConfig - the FilterConfig instance provided by the Servlet container at startup.

getName

protected String getName()
Returns the name of this filter.

Unless overridden by calling the setName(String) method, this value defaults to the filter name as specified by the servlet container at startup:

this.name = getFilterConfig().getName();

Returns:
the filter name, or null if none available
See Also:
GenericServlet.getServletName(), FilterConfig.getFilterName()

setName

public void setName(String name)
Sets the filter's name.

Unless overridden by calling this method, this value defaults to the filter name as specified by the servlet container at startup:

this.name = getFilterConfig().getName();

Specified by:
setName in interface Nameable
Parameters:
name - the name of the filter.

init

public final void init(FilterConfig filterConfig)
                throws ServletException
Sets the filter's filterConfig and then immediately calls onFilterConfigSet() to trigger any processing a subclass might wish to perform.

Specified by:
init in interface Filter
Parameters:
filterConfig - the servlet container supplied FilterConfig instance.
Throws:
ServletException - if onFilterConfigSet() throws an Exception.

onFilterConfigSet

protected void onFilterConfigSet()
                          throws Exception
Template method to be overridden by subclasses to perform initialization logic at startup. The ServletContext and FilterConfig will be accessible (and non-null) at the time this method is invoked via the getServletContext() and getFilterConfig() methods respectively.

Throws:
Exception - if the subclass has an error upon initialization.

doFilter

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

Specified by:
doFilter in interface Filter
Throws:
ServletException
IOException
See Also:
getAlreadyFilteredAttributeName(), shouldNotFilter(javax.servlet.ServletRequest), doFilterInternal(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)

getAlreadyFilteredAttributeName

protected String getAlreadyFilteredAttributeName()
Return name of the request attribute that identifies that a request has already been filtered.

The default implementation takes the configured name and appends ".FILTERED". If the filter is not fully initialized, it falls back to the implementation's class name.

Returns:
the name of the request attribute that identifies that a request has already been filtered.
See Also:
getName(), ALREADY_FILTERED_SUFFIX

shouldNotFilter

protected boolean shouldNotFilter(ServletRequest request)
                           throws 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:
ServletException - in case of errors

doFilterInternal

protected abstract void doFilterInternal(ServletRequest request,
                                         ServletResponse response,
                                         FilterChain filterChain)
                                  throws ServletException,
                                         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:
ServletException
IOException

destroy

public void destroy()
Default no-op implementation that can be overridden by subclasses for custom cleanup behavior.

Specified by:
destroy in interface Filter

JSecurity

Copyright © 2004-2008 JSecurity.