Package org.apache.shiro.web.servlet
Class AdviceFilter
- All Implemented Interfaces:
javax.servlet.Filter,org.apache.shiro.lang.util.Nameable
- Direct Known Subclasses:
LogoutFilter,PathMatchingFilter
A Servlet Filter that enables AOP-style "around" advice for a ServletRequest via
preHandle,
postHandle,
and afterCompletion
hooks.- Since:
- 0.9
-
Field Summary
Fields inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
ALREADY_FILTERED_SUFFIXFields inherited from class org.apache.shiro.web.servlet.AbstractFilter
filterConfig -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidafterCompletion(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Exception exception) Called in all cases in afinallyblock even ifpreHandlereturnsfalseor if an exception is thrown during filter chain processing.protected voidcleanup(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Exception existing) Executes cleanup logic in thefinallycode block in thedoFilterInternal(request, response, filterChain)implementation.voiddoFilterInternal(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) protected voidexecuteChain(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) Actually executes the specified filter chain by callingchain.doFilter(request,response);.protected voidpostHandle(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) Allows 'post' advice logic to be called, but only if no exception occurs during filter chain execution.protected booleanpreHandle(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) Returnstrueif the filter chain should be allowed to continue,falseotherwise.Methods inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
doFilter, getAlreadyFilteredAttributeName, isEnabled, isEnabled, isFilterOncePerRequest, setEnabled, setFilterOncePerRequest, shouldNotFilterMethods inherited from class org.apache.shiro.web.servlet.NameableFilter
getName, setName, toStringBuilderMethods inherited from class org.apache.shiro.web.servlet.AbstractFilter
destroy, getFilterConfig, getInitParam, init, onFilterConfigSet, setFilterConfigMethods inherited from class org.apache.shiro.web.servlet.ServletContextSupport
getContextAttribute, getContextInitParam, getServletContext, removeContextAttribute, setContextAttribute, setServletContext, toString
-
Constructor Details
-
AdviceFilter
public AdviceFilter()
-
-
Method Details
-
preHandle
protected boolean preHandle(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws Exception Returnstrueif the filter chain should be allowed to continue,falseotherwise. It is called before the chain is actually consulted/executed. The default implementation returnstruealways and exists as a template method for subclasses.- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponse- Returns:
trueif the filter chain should be allowed to continue,falseotherwise.- Throws:
Exception- if there is any error.
-
postHandle
protected void postHandle(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws Exception Allows 'post' advice logic to be called, but only if no exception occurs during filter chain execution. That is, ifexecuteChainthrows an exception, this method will never be called. Be aware of this when implementing logic. Most resource 'cleanup' behavior is often done in theafterCompletion(request,response,exception)implementation, which is guaranteed to be called for every request, even when the chain processing throws an Exception. The default implementation does nothing (no-op) and exists as a template method for subclasses.- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponse- Throws:
Exception- if an error occurs.
-
afterCompletion
public void afterCompletion(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Exception exception) throws Exception Called in all cases in afinallyblock even ifpreHandlereturnsfalseor if an exception is thrown during filter chain processing. Can be used for resource cleanup if so desired. The default implementation does nothing (no-op) and exists as a template method for subclasses.- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponseexception- any exception thrown duringpreHandle,executeChain, orpostHandleexecution, ornullif no exception was thrown (i.e. the chain processed successfully).- Throws:
Exception- if an error occurs.
-
executeChain
protected void executeChain(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws Exception Actually executes the specified filter chain by callingchain.doFilter(request,response);. Can be overridden by subclasses for custom logic.- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponsechain- the filter chain to execute- Throws:
Exception- if there is any error executing the chain.
-
doFilterInternal
public void doFilterInternal(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain) throws javax.servlet.ServletException, IOException - Specified by:
doFilterInternalin classOncePerRequestFilter- Parameters:
request- the incoming ServletRequestresponse- the outgoing ServletResponsechain- the filter chain to execute- Throws:
javax.servlet.ServletException- if a servlet-related error occursIOException- if an IO error occurs
-
cleanup
protected void cleanup(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Exception existing) throws javax.servlet.ServletException, IOException Executes cleanup logic in thefinallycode block in thedoFilterInternal(request, response, filterChain)implementation. This implementation specifically callsafterCompletionas well as handles any exceptions properly.- Parameters:
request- the incomingServletRequestresponse- the outgoingServletResponseexisting- any exception that might have occurred while executing theFilterChainor pre or post advice, ornullif the pre/chain/post execution did not throw anException.- Throws:
javax.servlet.ServletException- if any exception other than anIOExceptionis thrown.IOException- if the pre/chain/post execution throw anIOException
-