Package org.apache.shiro.web.servlet
Class AdviceFilter
-
- All Implemented Interfaces:
javax.servlet.Filter,org.apache.shiro.util.Nameable
- Direct Known Subclasses:
LogoutFilter,PathMatchingFilter
public abstract class AdviceFilter extends OncePerRequestFilter
A Servlet Filter that enables AOP-style "around" advice for a ServletRequest viapreHandle,postHandle, andafterCompletionhooks.- Since:
- 0.9
-
-
Field Summary
-
Fields inherited from class org.apache.shiro.web.servlet.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
-
Fields inherited from class org.apache.shiro.web.servlet.AbstractFilter
filterConfig
-
-
Constructor Summary
Constructors Constructor Description AdviceFilter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterCompletion(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 thedoFilterInternalimplementation.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, shouldNotFilter
-
Methods inherited from class org.apache.shiro.web.servlet.NameableFilter
getName, setName, toStringBuilder
-
Methods inherited from class org.apache.shiro.web.servlet.AbstractFilter
destroy, getFilterConfig, getInitParam, init, onFilterConfigSet, setFilterConfig
-
Methods inherited from class org.apache.shiro.web.servlet.ServletContextSupport
getContextAttribute, getContextInitParam, getServletContext, removeContextAttribute, setContextAttribute, setServletContext, toString
-
-
-
-
Method Detail
-
preHandle
protected boolean preHandle(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws ExceptionReturnstrueif 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 ExceptionAllows '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 ExceptionCalled 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 ExceptionActually 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, IOExceptionExecutes cleanup logic in thefinallycode block in thedoFilterInternalimplementation. 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
-
-