|
| 1 | +/* |
| 2 | + * Copyright 2002-2012 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.mock.web; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import javax.servlet.Filter; |
| 26 | +import javax.servlet.FilterChain; |
| 27 | +import javax.servlet.FilterConfig; |
| 28 | +import javax.servlet.Servlet; |
| 29 | +import javax.servlet.ServletException; |
| 30 | +import javax.servlet.ServletRequest; |
| 31 | +import javax.servlet.ServletResponse; |
| 32 | + |
| 33 | +import org.springframework.util.Assert; |
| 34 | +import org.springframework.util.ObjectUtils; |
| 35 | + |
| 36 | +/** |
| 37 | + * Mock implementation of the {@link javax.servlet.FilterChain} interface. Used |
| 38 | + * for testing the web framework; also useful for testing custom |
| 39 | + * {@link javax.servlet.Filter} implementations. |
| 40 | + * |
| 41 | + * <p>A {@link MockFilterChain} can be configured with one or more filters and a |
| 42 | + * Servlet to be invoked. When the chain is invoked, it invokes in turn all |
| 43 | + * filters and the Servlet and saves the request and response. Subsequent |
| 44 | + * invocations raise an {@link IllegalStateException} unless {@link #reset()} is |
| 45 | + * called. |
| 46 | + * |
| 47 | + * @author Juergen Hoeller |
| 48 | + * @author Rob Winch |
| 49 | + * @author Rossen Stoyanchev |
| 50 | + * |
| 51 | + * @since 2.0.3 |
| 52 | + * @see MockFilterConfig |
| 53 | + * @see PassThroughFilterChain |
| 54 | + */ |
| 55 | +public class MockFilterChain implements FilterChain { |
| 56 | + |
| 57 | + private ServletRequest request; |
| 58 | + |
| 59 | + private ServletResponse response; |
| 60 | + |
| 61 | + private final List<Filter> filters; |
| 62 | + |
| 63 | + private Iterator<Filter> iterator; |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Register a single do-nothing {@link Filter} implementation. The first |
| 68 | + * invocation saves the request and response. Subsequent invocations raise |
| 69 | + * an {@link IllegalStateException} unless {@link #reset()} is called. |
| 70 | + */ |
| 71 | + public MockFilterChain() { |
| 72 | + this.filters = Collections.emptyList(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Create a FilterChain with a Servlet. |
| 77 | + * |
| 78 | + * @param servlet the Servlet to invoke |
| 79 | + * @since 3.2 |
| 80 | + */ |
| 81 | + public MockFilterChain(Servlet servlet) { |
| 82 | + this.filters = initFilterList(servlet); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Create a {@code FilterChain} with Filter's and a Servlet. |
| 87 | + * |
| 88 | + * @param servlet the {@link Servlet} to invoke in this {@link FilterChain} |
| 89 | + * @param filters the {@link Filter}'s to invoke in this {@link FilterChain} |
| 90 | + * @since 3.2 |
| 91 | + */ |
| 92 | + public MockFilterChain(Servlet servlet, Filter... filters) { |
| 93 | + Assert.notNull(filters, "filters cannot be null"); |
| 94 | + Assert.noNullElements(filters, "filters cannot contain null values"); |
| 95 | + this.filters = initFilterList(servlet, filters); |
| 96 | + } |
| 97 | + |
| 98 | + private static List<Filter> initFilterList(Servlet servlet, Filter... filters) { |
| 99 | + Filter[] allFilters = ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet)); |
| 100 | + return Arrays.asList(allFilters); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Return the request that {@link #doFilter} has been called with. |
| 105 | + */ |
| 106 | + public ServletRequest getRequest() { |
| 107 | + return this.request; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Return the response that {@link #doFilter} has been called with. |
| 112 | + */ |
| 113 | + public ServletResponse getResponse() { |
| 114 | + return this.response; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Invoke registered {@link Filter}s and/or {@link Servlet} also saving the |
| 119 | + * request and response. |
| 120 | + */ |
| 121 | + public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { |
| 122 | + Assert.notNull(request, "Request must not be null"); |
| 123 | + Assert.notNull(response, "Response must not be null"); |
| 124 | + |
| 125 | + if (this.request != null) { |
| 126 | + throw new IllegalStateException("This FilterChain has already been called!"); |
| 127 | + } |
| 128 | + |
| 129 | + if (this.iterator == null) { |
| 130 | + this.iterator = this.filters.iterator(); |
| 131 | + } |
| 132 | + |
| 133 | + if (this.iterator.hasNext()) { |
| 134 | + Filter nextFilter = this.iterator.next(); |
| 135 | + nextFilter.doFilter(request, response, this); |
| 136 | + } |
| 137 | + |
| 138 | + this.request = request; |
| 139 | + this.response = response; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Reset the {@link MockFilterChain} allowing it to be invoked again. |
| 144 | + */ |
| 145 | + public void reset() { |
| 146 | + this.request = null; |
| 147 | + this.response = null; |
| 148 | + this.iterator = null; |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + /** |
| 153 | + * A filter that simply delegates to a Servlet. |
| 154 | + */ |
| 155 | + private static class ServletFilterProxy implements Filter { |
| 156 | + |
| 157 | + private final Servlet delegateServlet; |
| 158 | + |
| 159 | + private ServletFilterProxy(Servlet servlet) { |
| 160 | + Assert.notNull(servlet, "servlet cannot be null"); |
| 161 | + this.delegateServlet = servlet; |
| 162 | + } |
| 163 | + |
| 164 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 165 | + throws IOException, ServletException { |
| 166 | + |
| 167 | + this.delegateServlet.service(request, response); |
| 168 | + } |
| 169 | + |
| 170 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 171 | + } |
| 172 | + |
| 173 | + public void destroy() { |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public String toString() { |
| 178 | + return this.delegateServlet.toString(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments