public abstract class OncePerRequestFilter extends NameableFilter
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.
isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
method and
isEnabled()
property to allow explicit controll over whether the filter executes (or allows passthrough)
for any given request.
NOTE This class was initially borrowed from the Spring framework but has continued modifications.Modifier and Type | Field and Description |
---|---|
static String |
ALREADY_FILTERED_SUFFIX
Suffix that gets appended to the filter name for the "already filtered" request attribute.
|
filterConfig
Constructor and Description |
---|
OncePerRequestFilter() |
Modifier and Type | Method and Description |
---|---|
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 chain)
Same contract as for
doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) ,
but guaranteed to be invoked only once per request. |
protected String |
getAlreadyFilteredAttributeName()
Return name of the request attribute that identifies that a request has already been filtered.
|
boolean |
isEnabled()
Returns
true if this filter should generally* execute for any request,
false if it should let the request/response pass through immediately to the next
element in the FilterChain . |
protected boolean |
isEnabled(ServletRequest request,
ServletResponse response)
Returns
true if this filter should filter the specified request, false if it should let the
request/response pass through immediately to the next element in the FilterChain . |
void |
setEnabled(boolean enabled)
Sets whether or not this filter generally executes for any request.
|
protected boolean |
shouldNotFilter(ServletRequest request)
Deprecated.
in favor of overriding
isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
for custom behavior. This method will be removed in Shiro 2.0. |
getName, setName, toStringBuilder
destroy, getFilterConfig, getInitParam, init, onFilterConfigSet, setFilterConfig
getContextAttribute, getContextInitParam, getServletContext, removeContextAttribute, setContextAttribute, setServletContext, toString
public static final String ALREADY_FILTERED_SUFFIX
public OncePerRequestFilter()
public boolean isEnabled()
true
if this filter should generally* execute for any request,
false
if it should let the request/response pass through immediately to the next
element in the FilterChain
. The default value is true
, as most filters would inherently need
to execute when configured.
* This configuration property is for general configuration for any request that comes through
the filter. The
isEnabled(request,response)
method actually determines whether or not if the filter is enabled based on the current request.true
if this filter should generally execute, false
if it should let the
request/response pass through immediately to the next element in the FilterChain
.public void setEnabled(boolean enabled)
isEnabled()
JavaDoc as to what general execution means.enabled
- whether or not this filter generally executes.public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException
doFilter
implementation stores a request attribute for
"already filtered", proceeding without filtering again if the
attribute is already there.protected boolean isEnabled(ServletRequest request, ServletResponse response) throws ServletException, IOException
true
if this filter should filter the specified request, false
if it should let the
request/response pass through immediately to the next element in the FilterChain
.
This default implementation merely returns the value of isEnabled()
, which is
true
by default (to ensure the filter always executes by default), but it can be overridden by
subclasses for request-specific behavior if necessary. For example, a filter could be enabled or disabled
based on the request path being accessed.
Helpful Hint: if your subclass extends PathMatchingFilter
,
you may wish to instead override the
PathMatchingFilter.isEnabled(request,response,path,pathSpecificConfig)
method if you want to make your enable/disable decision based on any path-specific configuration.request
- the incoming servlet requestresponse
- the outbound servlet responsetrue
if this filter should filter the specified request, false
if it should let the
request/response pass through immediately to the next element in the FilterChain
.IOException
- in the case of any IO errorServletException
- in the case of any errorPathMatchingFilter.isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse, String, Object)
protected String getAlreadyFilteredAttributeName()
name
and appends ".FILTERED
".
If the filter is not fully initialized, it falls back to the implementation's class name.NameableFilter.getName()
,
ALREADY_FILTERED_SUFFIX
@Deprecated protected boolean shouldNotFilter(ServletRequest request) throws ServletException
isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
for custom behavior. This method will be removed in Shiro 2.0.true
to avoid filtering of the given request.
The default implementation always returns false
.
request
- current HTTP requestServletException
- in case of errorsprotected abstract void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException
doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
,
but guaranteed to be invoked only once per request.request
- incoming ServletRequest
response
- outgoing ServletResponse
chain
- the FilterChain
to executeServletException
- if there is a problem processing the requestIOException
- if there is an I/O problem processing the requestCopyright © 2004-2013 The Apache Software Foundation. All Rights Reserved.