Package org.apache.shiro.web.servlet
Class AdviceFilter
-
- 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
, andafterCompletion
hooks.- 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 void
afterCompletion(ServletRequest request, ServletResponse response, Exception exception)
Called in all cases in afinally
block even ifpreHandle
returnsfalse
or if an exception is thrown during filter chain processing.protected void
cleanup(ServletRequest request, ServletResponse response, Exception existing)
Executes cleanup logic in thefinally
code block in thedoFilterInternal
implementation.void
doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
protected void
executeChain(ServletRequest request, ServletResponse response, FilterChain chain)
Actually executes the specified filter chain by callingchain.doFilter(request,response);
.protected void
postHandle(ServletRequest request, ServletResponse response)
Allows 'post' advice logic to be called, but only if no exception occurs during filter chain execution.protected boolean
preHandle(ServletRequest request, ServletResponse response)
Returnstrue
if the filter chain should be allowed to continue,false
otherwise.-
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
-
-
-
-
Constructor Detail
-
AdviceFilter
public AdviceFilter()
-
-
Method Detail
-
preHandle
protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception
Returnstrue
if the filter chain should be allowed to continue,false
otherwise. It is called before the chain is actually consulted/executed. The default implementation returnstrue
always and exists as a template method for subclasses.- Parameters:
request
- the incoming ServletRequestresponse
- the outgoing ServletResponse- Returns:
true
if the filter chain should be allowed to continue,false
otherwise.- Throws:
Exception
- if there is any error.
-
postHandle
protected void postHandle(ServletRequest request, ServletResponse response) throws Exception
Allows 'post' advice logic to be called, but only if no exception occurs during filter chain execution. That is, ifexecuteChain
throws 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(ServletRequest request, ServletResponse response, Exception exception) throws Exception
Called in all cases in afinally
block even ifpreHandle
returnsfalse
or 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
, orpostHandle
execution, ornull
if no exception was thrown (i.e. the chain processed successfully).- Throws:
Exception
- if an error occurs.
-
executeChain
protected void executeChain(ServletRequest request, ServletResponse response, 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(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException
- Specified by:
doFilterInternal
in classOncePerRequestFilter
- Parameters:
request
- the incoming ServletRequestresponse
- the outgoing ServletResponsechain
- the filter chain to execute- Throws:
ServletException
- if a servlet-related error occursIOException
- if an IO error occurs
-
cleanup
protected void cleanup(ServletRequest request, ServletResponse response, Exception existing) throws ServletException, IOException
Executes cleanup logic in thefinally
code block in thedoFilterInternal
implementation. This implementation specifically callsafterCompletion
as well as handles any exceptions properly.- Parameters:
request
- the incomingServletRequest
response
- the outgoingServletResponse
existing
- any exception that might have occurred while executing theFilterChain
or pre or post advice, ornull
if the pre/chain/post execution did not throw anException
.- Throws:
ServletException
- if any exception other than anIOException
is thrown.IOException
- if the pre/chain/post execution throw anIOException
-
-