public abstract class OncePerRequestFilter extends GenericFilterBean
doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain)
method with HttpServletRequest and HttpServletResponse arguments.
In an async scenario a filter may be invoked again in additional threads
as part of an ASYNC dispatch.
Sub-classes may decide whether to be invoked once per request or once per
request thread for as long as the same request is being processed.
See shouldFilterAsyncDispatches()
.
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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ALREADY_FILTERED_SUFFIX
Suffix that gets appended to the filter name for the
"already filtered" request attribute.
|
logger
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(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
Same contract as for
doFilter , but guaranteed to be
just invoked once per request or once per request thread. |
protected java.lang.String |
getAlreadyFilteredAttributeName()
Return the name of the request attribute that identifies that a request
is already filtered.
|
protected boolean |
isAsyncDispatch(HttpServletRequest request)
Whether the request was dispatched to complete processing of results produced
in another thread.
|
protected boolean |
isLastRequestThread(HttpServletRequest request)
Whether this is the last thread processing the request.
|
protected boolean |
shouldFilterAsyncDispatches()
Whether to filter once per request or once per request thread.
|
protected boolean |
shouldNotFilter(HttpServletRequest request)
Can be overridden in subclasses for custom filtering control,
returning
true to avoid filtering of the given request. |
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
public static final java.lang.String ALREADY_FILTERED_SUFFIX
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, java.io.IOException
doFilter
implementation stores a request attribute for
"already filtered", proceeding without filtering again if the
attribute is already there.ServletException
java.io.IOException
getAlreadyFilteredAttributeName()
,
shouldNotFilter(HttpServletRequest)
,
doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain)
protected java.lang.String getAlreadyFilteredAttributeName()
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.
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException
true
to avoid filtering of the given request.
The default implementation always returns false
.
request
- current HTTP requestServletException
- in case of errorsprotected boolean shouldFilterAsyncDispatches()
javax.servlet.DispatcherType.ASYNC
introduced in Servlet 3.0
means a filter can be invoked in more than one thread (and exited) over the
course of a single request. Some filters only need to filter the initial
thread (e.g. request wrapping) while others may need to be invoked at least
once in each additional thread for example for setting up thread locals or
to perform final processing at the very end.
Note that although a filter can be mapped to handle specific dispatcher
types via web.xml
or in Java through the ServletContext
,
servlet containers may enforce different defaults with regards to dispatcher
types. This flag enforces the design intent of the filter.
The default setting is "false", which means the filter will be invoked once only per request and only on the initial request thread. If "true", the filter will also be invoked once only on each additional thread.
WebAsyncManager
protected final boolean isAsyncDispatch(HttpServletRequest request)
javax.servlet.DispatcherType.ASYNC
and can be used by filters that
return "true" from shouldFilterAsyncDispatches()
to detect when
the filter is being invoked subsequently in additional thread(s).WebAsyncManager
protected final boolean isLastRequestThread(HttpServletRequest request)
true
to false
if the method is
invoked before and after delegating to the next filter, since the next filter
or servlet may begin concurrent processing. Therefore this method is most
useful after delegation for final, end-of-request type processing.request
- the current requesttrue
if the response will be committed when the current
thread exits; false
if the response will remain open.WebAsyncManager
protected abstract void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, java.io.IOException
doFilter
, but guaranteed to be
just invoked once per request or once per request thread.
See shouldFilterAsyncDispatches()
for details.
Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
ServletException
java.io.IOException