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