View Javadoc

1   package com.atlassian.plugin.servlet.filter;
2   
3   import java.io.IOException;
4   import java.util.Iterator;
5   
6   import javax.servlet.Filter;
7   import javax.servlet.FilterChain;
8   import javax.servlet.ServletException;
9   import javax.servlet.ServletRequest;
10  import javax.servlet.ServletResponse;
11  
12  /**
13   * This FilterChain passes control from the first Filter in an iterator to the last.  When the last iterator
14   * calls the chain.doFilter(request, response) method, the supplied "parent" chain has control returned to it.
15   * 
16   * @since 2.1.0
17   */
18  public final class IteratingFilterChain implements FilterChain
19  {
20      private final Iterator<Filter> iterator;
21      private final FilterChain chain;
22  
23      /**
24       * Create a new IteratingFilterChain which iterates over the Filters in the supplied Iterator and then returns
25       * control to the main FilterChain.
26       * 
27       * @param iterator Iterator over the Filters to apply
28       * @param chain FilterChain to return control to after the last Filter in the iterator has called the
29       *              chain.doFilter(request, response) method. 
30       */
31      public IteratingFilterChain(Iterator<Filter> iterator, FilterChain chain)
32      {
33          this.iterator = iterator;
34          this.chain = chain;
35      }
36  
37      public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException
38      {
39          if (iterator.hasNext())
40          {
41              Filter filter = iterator.next();
42              filter.doFilter(request, response, this);
43          }
44          else
45          {
46              chain.doFilter(request, response);
47          }
48      }
49  }