Class AdviceFilter

All Implemented Interfaces:
javax.servlet.Filter, org.apache.shiro.lang.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 via preHandle, postHandle, and afterCompletion hooks.
Since:
0.9
  • Constructor Details

  • Method Details

    • preHandle

      protected boolean preHandle(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws Exception
      Returns true if the filter chain should be allowed to continue, false otherwise. It is called before the chain is actually consulted/executed.

      The default implementation returns true always and exists as a template method for subclasses.

      Parameters:
      request - the incoming ServletRequest
      response - 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(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, if executeChain throws an exception, this method will never be called. Be aware of this when implementing logic. Most resource 'cleanup' behavior is often done in the afterCompletion(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 ServletRequest
      response - 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 a finally block even if preHandle returns false 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 ServletRequest
      response - the outgoing ServletResponse
      exception - any exception thrown during preHandle, executeChain, or postHandle execution, or null if 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 calling chain.doFilter(request,response);.

      Can be overridden by subclasses for custom logic.

      Parameters:
      request - the incoming ServletRequest
      response - the outgoing ServletResponse
      chain - 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
      Actually implements the chain execution logic, utilizing pre, post, and after advice hooks.
      Specified by:
      doFilterInternal in class OncePerRequestFilter
      Parameters:
      request - the incoming ServletRequest
      response - the outgoing ServletResponse
      chain - the filter chain to execute
      Throws:
      javax.servlet.ServletException - if a servlet-related error occurs
      IOException - 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 the finally code block in the doFilterInternal(request, response, filterChain) implementation.

      This implementation specifically calls afterCompletion as well as handles any exceptions properly.

      Parameters:
      request - the incoming ServletRequest
      response - the outgoing ServletResponse
      existing - any exception that might have occurred while executing the FilterChain or pre or post advice, or null if the pre/chain/post execution did not throw an Exception.
      Throws:
      javax.servlet.ServletException - if any exception other than an IOException is thrown.
      IOException - if the pre/chain/post execution throw an IOException