001/*
002 * Copyright 2002-2012 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;
020import javax.servlet.Filter;
021import javax.servlet.FilterChain;
022import javax.servlet.Servlet;
023import javax.servlet.ServletException;
024import javax.servlet.ServletRequest;
025import javax.servlet.ServletResponse;
026
027import org.springframework.util.Assert;
028
029/**
030 * Implementation of the {@link javax.servlet.FilterConfig} interface which
031 * simply passes the call through to a given Filter/FilterChain combination
032 * (indicating the next Filter in the chain along with the FilterChain that it is
033 * supposed to work on) or to a given Servlet (indicating the end of the chain).
034 *
035 * @author Juergen Hoeller
036 * @since 2.0.3
037 * @see javax.servlet.Filter
038 * @see javax.servlet.Servlet
039 * @see MockFilterChain
040 */
041public class PassThroughFilterChain implements FilterChain {
042
043        private Filter filter;
044
045        private FilterChain nextFilterChain;
046
047        private Servlet servlet;
048
049
050        /**
051         * Create a new PassThroughFilterChain that delegates to the given Filter,
052         * calling it with the given FilterChain.
053         * @param filter the Filter to delegate to
054         * @param nextFilterChain the FilterChain to use for that next Filter
055         */
056        public PassThroughFilterChain(Filter filter, FilterChain nextFilterChain) {
057                Assert.notNull(filter, "Filter must not be null");
058                Assert.notNull(nextFilterChain, "'FilterChain must not be null");
059                this.filter = filter;
060                this.nextFilterChain = nextFilterChain;
061        }
062
063        /**
064         * Create a new PassThroughFilterChain that delegates to the given Servlet.
065         * @param servlet the Servlet to delegate to
066         */
067        public PassThroughFilterChain(Servlet servlet) {
068                Assert.notNull(servlet, "Servlet must not be null");
069                this.servlet = servlet;
070        }
071
072
073        /**
074         * Pass the call on to the Filter/Servlet.
075         */
076        @Override
077        public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
078                if (this.filter != null) {
079                        this.filter.doFilter(request, response, this.nextFilterChain);
080                }
081                else {
082                        this.servlet.service(request, response);
083                }
084        }
085
086}