org.springframework.web.filter
abstract public class: OncePerRequestFilter [javadoc |
source]
java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
All Implemented Interfaces:
Filter, DisposableBean, BeanNameAware, InitializingBean, ServletContextAware
Direct Known Subclasses:
ServletContextRequestLoggingFilter, OpenSessionInViewFilter, AbstractRequestLoggingFilter, MultipartFilter, OpenPersistenceManagerInViewFilter, OpenSessionInViewFilter, CharacterEncodingFilter, OpenEntityManagerInViewFilter, CommonsRequestLoggingFilter, Log4jNestedDiagnosticContextFilter, RequestContextFilter
Filter base class that guarantees to be just executed once per request,
on any servlet container. It provides a
#doFilterInternal
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.
- author:
Juergen - Hoeller
- since:
06.12.2003 -
| Field Summary |
|---|
| public static final String | ALREADY_FILTERED_SUFFIX | Suffix that gets appended to the filter name for the
"already filtered" request attribute. |
| Methods from org.springframework.web.filter.GenericFilterBean: |
|---|
|
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setServletContext |
| Method from org.springframework.web.filter.OncePerRequestFilter Detail: |
public final void doFilter(ServletRequest request,
ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
throw new ServletException("OncePerRequestFilter just supports HTTP requests");
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
if (request.getAttribute(alreadyFilteredAttributeName) != null || shouldNotFilter(httpRequest)) {
// Proceed without invoking this filter...
filterChain.doFilter(request, response);
}
else {
// Do invoke this filter...
request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
try {
doFilterInternal(httpRequest, httpResponse, filterChain);
}
finally {
// Remove the "already filtered" request attribute for this request.
request.removeAttribute(alreadyFilteredAttributeName);
}
}
}
This doFilter implementation stores a request attribute for
"already filtered", proceeding without filtering again if the
attribute is already there. |
abstract protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws IOException, ServletException
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. |
protected String getAlreadyFilteredAttributeName() {
String name = getFilterName();
if (name == null) {
name = getClass().getName();
}
return name + ALREADY_FILTERED_SUFFIX;
}
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. |
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
return false;
}
|