View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.web.servlet;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import javax.servlet.*;
25  import java.io.IOException;
26  import java.util.List;
27  
28  /**
29   * A proxied filter chain is a {@link FilterChain} instance that proxies an original {@link FilterChain} as well
30   * as a {@link List List} of other {@link Filter Filter}s that might need to execute prior to the final wrapped
31   * original chain.  It allows a list of filters to execute before continuing the original (proxied)
32   * {@code FilterChain} instance.
33   *
34   * @since 0.9
35   */
36  public class ProxiedFilterChain implements FilterChain {
37  
38      //TODO - complete JavaDoc
39  
40      private static final Logger log = LoggerFactory.getLogger(ProxiedFilterChain.class);
41  
42      private FilterChain orig;
43      private List<Filter> filters;
44      private int index = 0;
45  
46      public ProxiedFilterChain(FilterChain orig, List<Filter> filters) {
47          if (orig == null) {
48              throw new NullPointerException("original FilterChain cannot be null.");
49          }
50          this.orig = orig;
51          this.filters = filters;
52          this.index = 0;
53      }
54  
55      public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
56          if (this.filters == null || this.filters.size() == this.index) {
57              //we've reached the end of the wrapped chain, so invoke the original one:
58              if (log.isTraceEnabled()) {
59                  log.trace("Invoking original filter chain.");
60              }
61              this.orig.doFilter(request, response);
62          } else {
63              if (log.isTraceEnabled()) {
64                  log.trace("Invoking wrapped filter at index [" + this.index + "]");
65              }
66              this.filters.get(this.index++).doFilter(request, response, this);
67          }
68      }
69  }