001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.web.servlet;
020    
021    import org.slf4j.Logger;
022    import org.slf4j.LoggerFactory;
023    
024    import javax.servlet.*;
025    import java.io.IOException;
026    import java.util.List;
027    
028    /**
029     * A proxied filter chain is a {@link FilterChain} instance that proxies an original {@link FilterChain} as well
030     * as a {@link List List} of other {@link Filter Filter}s that might need to execute prior to the final wrapped
031     * original chain.  It allows a list of filters to execute before continuing the original (proxied)
032     * {@code FilterChain} instance.
033     *
034     * @since 0.9
035     */
036    public class ProxiedFilterChain implements FilterChain {
037    
038        //TODO - complete JavaDoc
039    
040        private static final Logger log = LoggerFactory.getLogger(ProxiedFilterChain.class);
041    
042        private FilterChain orig;
043        private List<Filter> filters;
044        private int index = 0;
045    
046        public ProxiedFilterChain(FilterChain orig, List<Filter> filters) {
047            if (orig == null) {
048                throw new NullPointerException("original FilterChain cannot be null.");
049            }
050            this.orig = orig;
051            this.filters = filters;
052            this.index = 0;
053        }
054    
055        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
056            if (this.filters == null || this.filters.size() == this.index) {
057                //we've reached the end of the wrapped chain, so invoke the original one:
058                if (log.isTraceEnabled()) {
059                    log.trace("Invoking original filter chain.");
060                }
061                this.orig.doFilter(request, response);
062            } else {
063                if (log.isTraceEnabled()) {
064                    log.trace("Invoking wrapped filter at index [" + this.index + "]");
065                }
066                this.filters.get(this.index++).doFilter(request, response, this);
067            }
068        }
069    }