16
16
17
17
package org .springframework .mock .web ;
18
18
19
+ import java .io .IOException ;
20
+ import java .util .Arrays ;
21
+ import java .util .Iterator ;
22
+
23
+ import javax .servlet .Filter ;
19
24
import javax .servlet .FilterChain ;
25
+ import javax .servlet .FilterConfig ;
26
+ import javax .servlet .Servlet ;
27
+ import javax .servlet .ServletException ;
20
28
import javax .servlet .ServletRequest ;
21
29
import javax .servlet .ServletResponse ;
22
30
23
31
import org .springframework .util .Assert ;
32
+ import org .springframework .util .ObjectUtils ;
24
33
25
34
/**
26
35
* Mock implementation of the {@link javax.servlet.FilterChain} interface.
29
38
* custom {@link javax.servlet.Filter} implementations.
30
39
*
31
40
* @author Juergen Hoeller
41
+ * @author Rob Winch
42
+ *
32
43
* @since 2.0.3
33
44
* @see MockFilterConfig
34
45
* @see PassThroughFilterChain
@@ -39,16 +50,69 @@ public class MockFilterChain implements FilterChain {
39
50
40
51
private ServletResponse response ;
41
52
53
+ private final Iterator <Filter > iterator ;
54
+
55
+
56
+ /**
57
+ * Register a single do-nothing {@link Filter} implementation. The first
58
+ * invocation saves the request and response. Subsequent invocations raise
59
+ * an {@link IllegalStateException}.
60
+ */
61
+ public MockFilterChain () {
62
+ this .iterator = null ;
63
+ }
64
+
65
+ /**
66
+ * Create a FilterChain with a {@link Servlet} but without filters.
67
+ *
68
+ * @param servlet the {@link Servlet} to use in this {@link FilterChain}
69
+ * @since 3.2
70
+ */
71
+ public MockFilterChain (Servlet servlet ) {
72
+ this (new ServletFilterProxy (servlet ));
73
+ }
74
+
75
+ /**
76
+ * Create a FilterChain with one or more {@link Filter} instances and a {@link Servlet}.
77
+ *
78
+ * @param servlet the {@link Servlet} to use in this {@link FilterChain}
79
+ * @param filters the {@link Filter}'s to use in this {@link FilterChain}
80
+ * @since 3.2
81
+ */
82
+ public MockFilterChain (Servlet servlet , Filter ... filters ) {
83
+ this (ObjectUtils .addObjectToArray (filters , new ServletFilterProxy (servlet )));
84
+ }
85
+
86
+ /**
87
+ * Create a {@link FilterChain} with one or more {@link Filter} instances.
88
+ *
89
+ * @param filters the {@link Filter}'s to use in this {@link FilterChain}
90
+ * @since 3.2
91
+ */
92
+ private MockFilterChain (Filter ... filters ) {
93
+ Assert .notNull (filters , "filters cannot be null" );
94
+ Assert .notEmpty (filters , "filters cannot be empty" );
95
+ Assert .noNullElements (filters , "filters cannot contain null values" );
96
+ this .iterator = Arrays .asList (filters ).iterator ();
97
+ }
42
98
43
99
/**
44
- * Records the request and response.
100
+ * Invoke registered {@link Filter}s and/or {@link Servlet} also saving the
101
+ * request and response.
45
102
*/
46
- public void doFilter (ServletRequest request , ServletResponse response ) {
103
+ public void doFilter (ServletRequest request , ServletResponse response ) throws IOException , ServletException {
47
104
Assert .notNull (request , "Request must not be null" );
48
105
Assert .notNull (response , "Response must not be null" );
106
+
49
107
if (this .request != null ) {
50
- throw new IllegalStateException ("This FilterChain has already been called!" );
108
+ throw new IllegalStateException ("This FilterChain has already been called!" );
51
109
}
110
+
111
+ if ((this .iterator != null ) && (this .iterator .hasNext ())) {
112
+ Filter nextFilter = this .iterator .next ();
113
+ nextFilter .doFilter (request , response , this );
114
+ }
115
+
52
116
this .request = request ;
53
117
this .response = response ;
54
118
}
@@ -67,4 +131,35 @@ public ServletResponse getResponse() {
67
131
return this .response ;
68
132
}
69
133
134
+
135
+ /**
136
+ * A filter that simply delegates to a Servlet.
137
+ */
138
+ private static class ServletFilterProxy implements Filter {
139
+
140
+ private final Servlet delegateServlet ;
141
+
142
+ private ServletFilterProxy (Servlet servlet ) {
143
+ Assert .notNull (servlet , "servlet cannot be null" );
144
+ this .delegateServlet = servlet ;
145
+ }
146
+
147
+ public void doFilter (ServletRequest request , ServletResponse response , FilterChain chain )
148
+ throws IOException , ServletException {
149
+
150
+ this .delegateServlet .service (request , response );
151
+ }
152
+
153
+ public void init (FilterConfig filterConfig ) throws ServletException {
154
+ }
155
+
156
+ public void destroy () {
157
+ }
158
+
159
+ @ Override
160
+ public String toString () {
161
+ return this .delegateServlet .toString ();
162
+ }
163
+ }
164
+
70
165
}
0 commit comments