001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.mock.web; 018 019import java.io.IOException; 020 021import javax.servlet.Filter; 022import javax.servlet.FilterChain; 023import javax.servlet.Servlet; 024import javax.servlet.ServletException; 025import javax.servlet.ServletRequest; 026import javax.servlet.ServletResponse; 027 028import org.springframework.lang.Nullable; 029import org.springframework.util.Assert; 030 031/** 032 * Implementation of the {@link javax.servlet.FilterConfig} interface which 033 * simply passes the call through to a given Filter/FilterChain combination 034 * (indicating the next Filter in the chain along with the FilterChain that it is 035 * supposed to work on) or to a given Servlet (indicating the end of the chain). 036 * 037 * @author Juergen Hoeller 038 * @since 2.0.3 039 * @see javax.servlet.Filter 040 * @see javax.servlet.Servlet 041 * @see MockFilterChain 042 */ 043public class PassThroughFilterChain implements FilterChain { 044 045 @Nullable 046 private Filter filter; 047 048 @Nullable 049 private FilterChain nextFilterChain; 050 051 @Nullable 052 private Servlet servlet; 053 054 055 /** 056 * Create a new PassThroughFilterChain that delegates to the given Filter, 057 * calling it with the given FilterChain. 058 * @param filter the Filter to delegate to 059 * @param nextFilterChain the FilterChain to use for that next Filter 060 */ 061 public PassThroughFilterChain(Filter filter, FilterChain nextFilterChain) { 062 Assert.notNull(filter, "Filter must not be null"); 063 Assert.notNull(nextFilterChain, "'FilterChain must not be null"); 064 this.filter = filter; 065 this.nextFilterChain = nextFilterChain; 066 } 067 068 /** 069 * Create a new PassThroughFilterChain that delegates to the given Servlet. 070 * @param servlet the Servlet to delegate to 071 */ 072 public PassThroughFilterChain(Servlet servlet) { 073 Assert.notNull(servlet, "Servlet must not be null"); 074 this.servlet = servlet; 075 } 076 077 078 /** 079 * Pass the call on to the Filter/Servlet. 080 */ 081 @Override 082 public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException { 083 if (this.filter != null) { 084 this.filter.doFilter(request, response, this.nextFilterChain); 085 } 086 else { 087 Assert.state(this.servlet != null, "Neither a Filter not a Servlet set"); 088 this.servlet.service(request, response); 089 } 090 } 091 092}