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 */ 019package org.apache.shiro.web.servlet; 020 021import org.slf4j.Logger; 022import org.slf4j.LoggerFactory; 023 024import javax.servlet.Filter; 025import javax.servlet.FilterChain; 026import javax.servlet.ServletException; 027import javax.servlet.ServletRequest; 028import javax.servlet.ServletResponse; 029import java.io.IOException; 030import java.util.List; 031 032/** 033 * A proxied filter chain is a {@link FilterChain} instance that proxies an original {@link FilterChain} as well 034 * as a {@link List List} of other {@link Filter Filter}s that might need to execute prior to the final wrapped 035 * original chain. It allows a list of filters to execute before continuing the original (proxied) 036 * {@code FilterChain} instance. 037 * 038 * @since 0.9 039 */ 040public class ProxiedFilterChain implements FilterChain { 041 042 //TODO - complete JavaDoc 043 044 private static final Logger LOGGER = LoggerFactory.getLogger(ProxiedFilterChain.class); 045 046 private FilterChain orig; 047 private List<Filter> filters; 048 private int index; 049 050 public ProxiedFilterChain(FilterChain orig, List<Filter> filters) { 051 if (orig == null) { 052 throw new NullPointerException("original FilterChain cannot be null."); 053 } 054 this.orig = orig; 055 this.filters = filters; 056 this.index = 0; 057 } 058 059 public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { 060 if (this.filters == null || this.filters.size() == this.index) { 061 //we've reached the end of the wrapped chain, so invoke the original one: 062 if (LOGGER.isTraceEnabled()) { 063 LOGGER.trace("Invoking original filter chain."); 064 } 065 this.orig.doFilter(request, response); 066 } else { 067 if (LOGGER.isTraceEnabled()) { 068 LOGGER.trace("Invoking wrapped filter at index [" + this.index + "]"); 069 } 070 this.filters.get(this.index++).doFilter(request, response, this); 071 } 072 } 073}