Skip to content

Commit 8f1ade5

Browse files
committed
Update contribution
Closes gh-35273
1 parent 4d6a921 commit 8f1ade5

File tree

3 files changed

+118
-213
lines changed

3 files changed

+118
-213
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ public InterceptorRegistration addPathPatterns(String... patterns) {
8383
* @since 5.0.3
8484
*/
8585
public InterceptorRegistration addPathPatterns(List<String> patterns) {
86-
this.includePatterns = (this.includePatterns != null ?
87-
this.includePatterns : new ArrayList<>(patterns.size()));
86+
if (this.includePatterns == null) {
87+
this.includePatterns = new ArrayList<>(patterns.size());
88+
}
8889
this.includePatterns.addAll(patterns);
8990
return this;
9091
}
@@ -105,48 +106,50 @@ public InterceptorRegistration excludePathPatterns(String... patterns) {
105106
* @since 5.0.3
106107
*/
107108
public InterceptorRegistration excludePathPatterns(List<String> patterns) {
108-
this.excludePatterns = (this.excludePatterns != null ?
109-
this.excludePatterns : new ArrayList<>(patterns.size()));
109+
if (this.excludePatterns == null) {
110+
this.excludePatterns = new ArrayList<>(patterns.size());
111+
}
110112
this.excludePatterns.addAll(patterns);
111113
return this;
112114
}
113115

114116
/**
115-
* Add HTTP methods the interceptor should be included for.
116-
* <p>Only requests with these HTTP methods will be intercepted.
117-
* @since 7.0.x
117+
* Add HTTP methods for requests the interceptor should be included in.
118+
* @since 7.0
118119
*/
119120
public InterceptorRegistration includeHttpMethods(HttpMethod... httpMethods) {
120121
return includeHttpMethods(Arrays.asList(httpMethods));
121122
}
122123

123124
/**
124125
* List-based variant of {@link #includeHttpMethods(HttpMethod...)}.
125-
* @since 7.0.x
126+
* @since 7.0
126127
*/
127128
public InterceptorRegistration includeHttpMethods(List<HttpMethod> httpMethods) {
128-
this.includeHttpMethods = (this.includeHttpMethods != null ?
129-
this.includeHttpMethods : new ArrayList<>(httpMethods.size()));
129+
if (this.includeHttpMethods == null) {
130+
this.includeHttpMethods = new ArrayList<>(httpMethods.size());
131+
}
130132
this.includeHttpMethods.addAll(httpMethods);
131133
return this;
132134
}
133135

134136
/**
135-
* Add HTTP methods the interceptor should be excluded from.
137+
* Add HTTP methods for requests the interceptor should be excluded from.
136138
* <p>Requests with these HTTP methods will be ignored by the interceptor.
137-
* @since 7.0.x
139+
* @since 7.0
138140
*/
139-
public InterceptorRegistration excludeHttpMethods(HttpMethod... httpMethods){
140-
return this.excludeHttpMethods(Arrays.asList(httpMethods));
141+
public InterceptorRegistration excludeHttpMethods(HttpMethod... httpMethods) {
142+
return excludeHttpMethods(Arrays.asList(httpMethods));
141143
}
142144

143145
/**
144146
* List-based variant of {@link #excludeHttpMethods(HttpMethod...)}.
145-
* @since 7.0.x
147+
* @since 7.0
146148
*/
147-
public InterceptorRegistration excludeHttpMethods(List<HttpMethod> httpMethods){
148-
this.excludeHttpMethods = (this.excludeHttpMethods != null ?
149-
this.excludeHttpMethods : new ArrayList<>(httpMethods.size()));
149+
public InterceptorRegistration excludeHttpMethods(List<HttpMethod> httpMethods) {
150+
if (this.excludeHttpMethods == null) {
151+
this.excludeHttpMethods = new ArrayList<>(httpMethods.size());
152+
}
150153
this.excludeHttpMethods.addAll(httpMethods);
151154
return this;
152155
}
@@ -175,7 +178,7 @@ public InterceptorRegistration pathMatcher(PathMatcher pathMatcher) {
175178
* Specify an order position to be used. Default is 0.
176179
* @since 4.3.23
177180
*/
178-
public InterceptorRegistration order(int order){
181+
public InterceptorRegistration order(int order) {
179182
this.order = order;
180183
return this;
181184
}
@@ -194,27 +197,18 @@ protected int getOrder() {
194197
@SuppressWarnings("removal")
195198
protected Object getInterceptor() {
196199

197-
if (this.includePatterns == null && this.excludePatterns == null && this.includeHttpMethods == null && this.excludeHttpMethods == null) {
200+
if (this.includePatterns == null && this.excludePatterns == null &&
201+
this.includeHttpMethods == null && this.excludeHttpMethods == null) {
202+
198203
return this.interceptor;
199204
}
200205

201-
HttpMethod[] includeMethodsArray = (this.includeHttpMethods != null) ?
202-
this.includeHttpMethods.toArray(new HttpMethod[0]) : null;
203-
204-
HttpMethod[] excludeMethodsArray = (this.excludeHttpMethods != null) ?
205-
this.excludeHttpMethods.toArray(new HttpMethod[0]) : null;
206-
207-
String[] includePattersArray = StringUtils.toStringArray(this.includePatterns);
208-
209-
String[] excludePattersArray = StringUtils.toStringArray(this.excludePatterns);
210-
211-
212206
MappedInterceptor mappedInterceptor = new MappedInterceptor(
213-
includePattersArray,
214-
excludePattersArray,
215-
includeMethodsArray,
216-
excludeMethodsArray,
217-
this.interceptor);
207+
StringUtils.toStringArray(this.includePatterns),
208+
StringUtils.toStringArray(this.excludePatterns),
209+
(this.includeHttpMethods != null) ? this.includeHttpMethods.toArray(new HttpMethod[0]) : null,
210+
(this.excludeHttpMethods != null) ? this.excludeHttpMethods.toArray(new HttpMethod[0]) : null,
211+
this.interceptor, null);
218212

219213
if (this.pathMatcher != null) {
220214
mappedInterceptor.setPathMatcher(this.pathMatcher);

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MappedInterceptor.java

Lines changed: 43 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -68,66 +68,69 @@ public final class MappedInterceptor implements HandlerInterceptor {
6868

6969
private final PatternAdapter @Nullable [] excludePatterns;
7070

71-
private final MethodAdapter @Nullable [] includeHttpMethods;
71+
private final HttpMethod @Nullable [] includeHttpMethods;
7272

73-
private final MethodAdapter @Nullable [] excludeHttpMethods;
73+
private final HttpMethod @Nullable [] excludeHttpMethods;
7474

7575
private PathMatcher pathMatcher = defaultPathMatcher;
7676

7777
private final HandlerInterceptor interceptor;
7878

7979

8080
/**
81-
* Create an instance with the given include and exclude patterns along with
82-
* the target interceptor for the mappings.
83-
* @param includePatterns patterns to which requests must match, or null to
84-
* match all paths
85-
* @param excludePatterns patterns to which requests must not match
86-
* @param includeHttpMethods http methods to which request must match, or null to match all paths
87-
* @param excludeHttpMethods http methods to which request must not match
81+
* Create an instance with the given include and exclude patterns and HTTP methods.
82+
* @param includePatterns patterns to match, or null to match all paths
83+
* @param excludePatterns patterns for which requests must not match
84+
* @param includeHttpMethods the HTTP methods to match, or null for all methods
85+
* @param excludeHttpMethods the ßHTTP methods to which requests must not match
8886
* @param interceptor the target interceptor
8987
* @param parser a parser to use to pre-parse patterns into {@link PathPattern};
9088
* when not provided, {@link PathPatternParser#defaultInstance} is used.
91-
* @since 5.3
89+
* @since 7.0
9290
*/
93-
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods,
91+
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns,
92+
HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods,
9493
HandlerInterceptor interceptor, @Nullable PathPatternParser parser) {
9594

9695
this.includePatterns = PatternAdapter.initPatterns(includePatterns, parser);
9796
this.excludePatterns = PatternAdapter.initPatterns(excludePatterns, parser);
98-
this.includeHttpMethods = MethodAdapter.initHttpMethods(includeHttpMethods);
99-
this.excludeHttpMethods = MethodAdapter.initHttpMethods(excludeHttpMethods);
97+
this.includeHttpMethods = includeHttpMethods;
98+
this.excludeHttpMethods = excludeHttpMethods;
10099
this.interceptor = interceptor;
101100
}
102101

103-
104102
/**
105-
* Variant of
103+
* Variation of
106104
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
107-
* with include patterns only.
105+
* without HTTP methods.
106+
* @since 5.3
107+
* @deprecated in favor of the constructor variant with HTTP methods
108108
*/
109-
public MappedInterceptor(String @Nullable [] includePatterns, HandlerInterceptor interceptor) {
110-
this(includePatterns, null, null, null, interceptor);
109+
@Deprecated(since = "7.0", forRemoval = true)
110+
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns,
111+
HandlerInterceptor interceptor, @Nullable PathPatternParser parser) {
112+
113+
this(includePatterns, excludePatterns, null, null, interceptor, parser);
111114
}
112115

113116
/**
114117
* Variant of
115118
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
116-
* with include methods only.
119+
* with include patterns only.
117120
*/
118-
public MappedInterceptor(HttpMethod @Nullable [] includeHttpMethods, HandlerInterceptor interceptor) {
119-
this(null, null, includeHttpMethods, null, interceptor);
121+
public MappedInterceptor(String @Nullable [] includePatterns, HandlerInterceptor interceptor) {
122+
this(includePatterns, null, null, null, interceptor, null);
120123
}
121124

122125
/**
123126
* Variant of
124127
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
125128
* without a provided parser.
126129
*/
127-
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods,
130+
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns,
128131
HandlerInterceptor interceptor) {
129132

130-
this(includePatterns, excludePatterns,includeHttpMethods,excludeHttpMethods, interceptor, null);
133+
this(includePatterns, excludePatterns, null, null, interceptor, null);
131134
}
132135

133136
/**
@@ -136,26 +139,18 @@ public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [
136139
* with a {@link WebRequestInterceptor} as the target.
137140
*/
138141
public MappedInterceptor(String @Nullable [] includePatterns, WebRequestInterceptor interceptor) {
139-
this(includePatterns, null,null,null, interceptor);
140-
}
141-
/**
142-
* Variant of
143-
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
144-
* with a {@link WebRequestInterceptor} as the target.
145-
*/
146-
public MappedInterceptor(HttpMethod @Nullable [] includeHttpMethods, WebRequestInterceptor interceptor) {
147-
this(null, null,includeHttpMethods ,null, interceptor);
142+
this(includePatterns, null, interceptor);
148143
}
149144

150145
/**
151146
* Variant of
152-
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[] , HandlerInterceptor, PathPatternParser)}
147+
* {@link #MappedInterceptor(String[], String[], HttpMethod[], HttpMethod[], HandlerInterceptor, PathPatternParser)}
153148
* with a {@link WebRequestInterceptor} as the target.
154149
*/
155-
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns, HttpMethod @Nullable [] includeHttpMethods, HttpMethod @Nullable [] excludeHttpMethods,
150+
public MappedInterceptor(String @Nullable [] includePatterns, String @Nullable [] excludePatterns,
156151
WebRequestInterceptor interceptor) {
157152

158-
this(includePatterns, excludePatterns,includeHttpMethods,excludeHttpMethods, new WebRequestHandlerInterceptorAdapter(interceptor));
153+
this(includePatterns, excludePatterns, null, null, new WebRequestHandlerInterceptorAdapter(interceptor), null);
159154
}
160155

161156

@@ -228,7 +223,7 @@ public PathMatcher getPathMatcher() {
228223
*/
229224
public boolean matches(HttpServletRequest request) {
230225
Object path = ServletRequestPathUtils.getCachedPath(request);
231-
HttpMethod method = HttpMethod.valueOf(request.getMethod());
226+
HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());
232227
if (this.pathMatcher != defaultPathMatcher) {
233228
path = path.toString();
234229
}
@@ -241,47 +236,37 @@ public boolean matches(HttpServletRequest request) {
241236
}
242237
}
243238
if (!ObjectUtils.isEmpty(this.excludeHttpMethods)) {
244-
for (MethodAdapter adapter : this.excludeHttpMethods) {
245-
if (adapter.match(method)){
239+
for (HttpMethod excluded : this.excludeHttpMethods) {
240+
if (excluded == httpMethod) {
246241
return false;
247242
}
248243
}
249244
}
250-
if (ObjectUtils.isEmpty(this.includePatterns) && ObjectUtils.isEmpty(this.includeHttpMethods)) {
251-
return true;
252-
}
253-
if (!ObjectUtils.isEmpty(this.includePatterns) && ObjectUtils.isEmpty(this.includeHttpMethods)) {
245+
if (!ObjectUtils.isEmpty(this.includePatterns)) {
246+
boolean match = false;
254247
for (PatternAdapter adapter : this.includePatterns) {
255248
if (adapter.match(path, isPathContainer, this.pathMatcher)) {
256-
return true;
249+
match = true;
250+
break;
257251
}
258252
}
259-
}
260-
if (!ObjectUtils.isEmpty(this.includeHttpMethods) && ObjectUtils.isEmpty(this.includePatterns)) {
261-
for (MethodAdapter adapter : this.includeHttpMethods) {
262-
if (adapter.match(method)) {
263-
return true;
264-
}
253+
if (!match) {
254+
return false;
265255
}
266256
}
267-
if (!ObjectUtils.isEmpty(this.includePatterns) && !ObjectUtils.isEmpty(this.includeHttpMethods)) {
257+
if (!ObjectUtils.isEmpty(this.includeHttpMethods)) {
268258
boolean match = false;
269-
for (MethodAdapter methodAdapter : this.includeHttpMethods) {
270-
if (methodAdapter.match(method)) {
259+
for (HttpMethod included : this.includeHttpMethods) {
260+
if (included == httpMethod) {
271261
match = true;
272262
break;
273263
}
274264
}
275265
if (!match) {
276266
return false;
277267
}
278-
for (PatternAdapter pathAdapter : this.includePatterns) {
279-
if (pathAdapter.match(path, isPathContainer, pathMatcher)) {
280-
return true;
281-
}
282-
}
283268
}
284-
return false;
269+
return true;
285270
}
286271

287272

@@ -365,40 +350,4 @@ public boolean match(Object path, boolean isPathContainer, PathMatcher pathMatch
365350
}
366351
}
367352

368-
/**
369-
* Adapts {@link HttpMethod} instances for internal matching purposes.
370-
*
371-
* <p>Encapsulates an {@link HttpMethod} and provides matching functionality.
372-
* Also provides a utility method to initialize arrays of {@code MethodAdapter}
373-
* instances from arrays of {@link HttpMethod}.</p>
374-
*
375-
* @since 7.0.x
376-
*/
377-
private static class MethodAdapter {
378-
379-
private final @Nullable HttpMethod httpMethod;
380-
381-
public MethodAdapter(@Nullable HttpMethod httpMethod) {
382-
this.httpMethod = httpMethod;
383-
}
384-
385-
public boolean match(HttpMethod method) {
386-
return this.httpMethod == method;
387-
}
388-
389-
public @Nullable HttpMethod getHttpMethod() {
390-
return this.httpMethod;
391-
}
392-
393-
private static MethodAdapter @Nullable [] initHttpMethods(HttpMethod @Nullable [] methods) {
394-
if (ObjectUtils.isEmpty(methods)) {
395-
return null;
396-
}
397-
return Arrays.stream(methods)
398-
.map(MethodAdapter::new)
399-
.toArray(MethodAdapter[]::new);
400-
}
401-
402-
}
403-
404353
}

0 commit comments

Comments
 (0)