-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
MockMvcFilterDecorator.java
185 lines (156 loc) · 5.52 KB
/
MockMvcFilterDecorator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.servlet.setup;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.lang.Nullable;
import org.springframework.mock.web.MockFilterConfig;
import org.springframework.util.Assert;
import org.springframework.web.util.UrlPathHelper;
/**
* A Filter that invokes a delegate {@link Filter} only if the request URL
* matches the pattern it is mapped to using pattern matching as defined in the
* Servlet spec.
*
* @author Rob Winch
* @since 3.2
*/
final class MockMvcFilterDecorator implements Filter {
private static final String EXTENSION_MAPPING_PATTERN = "*.";
private static final String PATH_MAPPING_PATTERN = "/*";
private final Filter delegate;
@Nullable
private final Map<String, String> initParams;
@Nullable
private final EnumSet<DispatcherType> dispatcherTypes;
private final boolean hasPatterns;
/** Patterns that require an exact match, e.g. "/test" */
private final List<String> exactMatches = new ArrayList<>();
/** Patterns that require the URL to have a specific prefix, e.g. "/test/*" */
private final List<String> startsWithMatches = new ArrayList<>();
/** Patterns that require the request URL to have a specific suffix, e.g. "*.html" */
private final List<String> endsWithMatches = new ArrayList<>();
/**
* Create instance with URL patterns only.
* <p>Note: when this constructor is used, the Filter is not initialized.
*/
public MockMvcFilterDecorator(Filter delegate, String[] urlPatterns) {
this(delegate, null, null, urlPatterns);
}
/**
* Create instance with init parameters to initialize the filter with,
* as well as dispatcher types and URL patterns to match.
*/
public MockMvcFilterDecorator(
Filter delegate, @Nullable Map<String, String> initParams,
@Nullable EnumSet<DispatcherType> dispatcherTypes, String... urlPatterns) {
Assert.notNull(delegate, "filter cannot be null");
Assert.notNull(urlPatterns, "urlPatterns cannot be null");
this.delegate = delegate;
this.initParams = initParams;
this.dispatcherTypes = dispatcherTypes;
this.hasPatterns = (urlPatterns.length != 0);
for (String urlPattern : urlPatterns) {
addUrlPattern(urlPattern);
}
}
private void addUrlPattern(String urlPattern) {
Assert.notNull(urlPattern, "Found null URL Pattern");
if (urlPattern.startsWith(EXTENSION_MAPPING_PATTERN)) {
this.endsWithMatches.add(urlPattern.substring(1));
}
else if (urlPattern.equals(PATH_MAPPING_PATTERN)) {
this.startsWithMatches.add("");
}
else if (urlPattern.endsWith(PATH_MAPPING_PATTERN)) {
this.startsWithMatches.add(urlPattern.substring(0, urlPattern.length() - 1));
this.exactMatches.add(urlPattern.substring(0, urlPattern.length() - 2));
}
else {
if (urlPattern.isEmpty()) {
urlPattern = "/";
}
this.exactMatches.add(urlPattern);
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestPath = UrlPathHelper.defaultInstance.getPathWithinApplication(httpRequest);
if (matchDispatcherType(httpRequest.getDispatcherType()) && matchRequestPath(requestPath)) {
this.delegate.doFilter(request, response, filterChain);
}
else {
filterChain.doFilter(request, response);
}
}
private boolean matchDispatcherType(DispatcherType dispatcherType) {
return (this.dispatcherTypes == null ||
this.dispatcherTypes.stream().anyMatch(type -> type == dispatcherType));
}
private boolean matchRequestPath(String requestPath) {
if (!this.hasPatterns) {
return true;
}
for (String pattern : this.exactMatches) {
if (pattern.equals(requestPath)) {
return true;
}
}
if (!requestPath.startsWith("/")) {
return false;
}
for (String pattern : this.endsWithMatches) {
if (requestPath.endsWith(pattern)) {
return true;
}
}
for (String pattern : this.startsWithMatches) {
if (requestPath.startsWith(pattern)) {
return true;
}
}
return false;
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.delegate.init(filterConfig);
}
@Override
public void destroy() {
this.delegate.destroy();
}
public void initIfRequired(@Nullable ServletContext servletContext) throws ServletException {
if (this.initParams != null) {
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
this.initParams.forEach(filterConfig::addInitParameter);
this.delegate.init(filterConfig);
}
}
}