1   package com.atlassian.core.filters;
2   
3   import javax.servlet.*;
4   import javax.servlet.http.HttpServletRequest;
5   import javax.servlet.http.HttpServletResponse;
6   import java.io.IOException;
7   
8   /**
9    * Provides default implementations of {@link #init(FilterConfig)} and {@link #destroy()},
10   * and a doFilter method that casts to HttpServletRequest and HttpServletResponse.
11   *
12   * @since 4.0
13   */
14  public abstract class AbstractHttpFilter implements Filter
15  {
16      /**
17       * Default implementation which does nothing.
18       */
19      public void init(FilterConfig filterConfig) throws ServletException
20      {
21      }
22  
23      /**
24       * If this is an HTTP request, delegates to {@link #doFilter(HttpServletRequest, HttpServletResponse, FilterChain)}.
25       * Does nothing if the request and response are not of the HTTP variety.
26       */
27      public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
28      {
29          if (request instanceof HttpServletRequest && response instanceof HttpServletResponse)
30          {
31              doFilter((HttpServletRequest) request, (HttpServletResponse) response, filterChain);
32              return;
33          }
34          filterChain.doFilter(request, response);
35      }
36  
37      /**
38       * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
39       */
40      protected abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException;
41  
42      /**
43       * Default implementation which does nothing.
44       */
45      public void destroy()
46      {
47      }
48  }