Skip to content

Commit ac82b5c

Browse files
committed
Encapsulate full path initialization
1 parent 921635b commit ac82b5c

File tree

9 files changed

+46
-57
lines changed

9 files changed

+46
-57
lines changed

spring-web/src/main/java/org/springframework/web/util/pattern/PathPatternParser.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import org.apache.commons.logging.LogFactory;
2121

2222
import org.springframework.http.server.PathContainer;
23+
import org.springframework.util.StringUtils;
2324

2425
/**
2526
* Parser for URI path patterns producing {@link PathPattern} instances that can
@@ -103,6 +104,17 @@ public PathContainer.Options getPathOptions() {
103104
}
104105

105106

107+
/**
108+
* Prepare the given pattern for use in matching to full URL paths.
109+
* <p>By default, prepend a leading slash if needed for non-empty patterns.
110+
* @param pattern the pattern to initialize
111+
* @return the updated pattern
112+
* @since 5.2.25
113+
*/
114+
public String initFullPathPattern(String pattern) {
115+
return (StringUtils.hasLength(pattern) && !pattern.startsWith("/") ? "/" + pattern : pattern);
116+
}
117+
106118
/**
107119
* Process the path pattern content, a character at a time, breaking it into
108120
* path elements around separator boundaries and verifying the structure at each

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,10 +109,9 @@ public static RequestPredicate methods(HttpMethod... httpMethods) {
109109
*/
110110
public static RequestPredicate path(String pattern) {
111111
Assert.notNull(pattern, "'pattern' must not be null");
112-
if (!pattern.isEmpty() && !pattern.startsWith("/")) {
113-
pattern = "/" + pattern;
114-
}
115-
return pathPredicates(PathPatternParser.defaultInstance).apply(pattern);
112+
PathPatternParser parser = PathPatternParser.defaultInstance;
113+
pattern = parser.initFullPathPattern(pattern);
114+
return pathPredicates(parser).apply(pattern);
116115
}
117116

118117
/**

spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,9 +28,9 @@
2828
import org.springframework.http.server.PathContainer;
2929
import org.springframework.lang.Nullable;
3030
import org.springframework.util.Assert;
31-
import org.springframework.util.StringUtils;
3231
import org.springframework.web.server.ServerWebExchange;
3332
import org.springframework.web.util.pattern.PathPattern;
33+
import org.springframework.web.util.pattern.PathPatternParser;
3434

3535
/**
3636
* Abstract base class for URL-mapped
@@ -185,8 +185,9 @@ protected void registerHandler(String urlPath, Object handler) throws BeansExcep
185185
Object resolvedHandler = handler;
186186

187187
// Parse path pattern
188-
urlPath = prependLeadingSlash(urlPath);
189-
PathPattern pattern = getPathPatternParser().parse(urlPath);
188+
PathPatternParser parser = getPathPatternParser();
189+
urlPath = parser.initFullPathPattern(urlPath);
190+
PathPattern pattern = parser.parse(urlPath);
190191
if (this.handlerMap.containsKey(pattern)) {
191192
Object existingHandler = this.handlerMap.get(pattern);
192193
if (existingHandler != null && existingHandler != resolvedHandler) {
@@ -215,14 +216,4 @@ private String getHandlerDescription(Object handler) {
215216
return (handler instanceof String ? "'" + handler + "'" : handler.toString());
216217
}
217218

218-
219-
private static String prependLeadingSlash(String pattern) {
220-
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
221-
return "/" + pattern;
222-
}
223-
else {
224-
return pattern;
225-
}
226-
}
227-
228219
}

spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,6 @@
3535
import org.springframework.http.server.PathContainer;
3636
import org.springframework.http.server.reactive.ServerHttpRequest;
3737
import org.springframework.lang.Nullable;
38-
import org.springframework.util.StringUtils;
3938
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
4039
import org.springframework.web.server.ServerWebExchange;
4140
import org.springframework.web.util.pattern.PathPattern;
@@ -86,8 +85,9 @@ public Map<PathPattern, ResourceWebHandler> getHandlerMap() {
8685
public void registerHandlers(Map<String, ResourceWebHandler> handlerMap) {
8786
this.handlerMap.clear();
8887
handlerMap.forEach((rawPattern, resourceWebHandler) -> {
89-
rawPattern = prependLeadingSlash(rawPattern);
90-
PathPattern pattern = PathPatternParser.defaultInstance.parse(rawPattern);
88+
PathPatternParser parser = PathPatternParser.defaultInstance;
89+
rawPattern = parser.initFullPathPattern(rawPattern);
90+
PathPattern pattern = parser.parse(rawPattern);
9191
this.handlerMap.put(pattern, resourceWebHandler);
9292
});
9393
}
@@ -173,14 +173,4 @@ private Mono<String> resolveResourceUrl(ServerWebExchange exchange, PathContaine
173173
});
174174
}
175175

176-
177-
private static String prependLeadingSlash(String pattern) {
178-
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
179-
return "/" + pattern;
180-
}
181-
else {
182-
return pattern;
183-
}
184-
}
185-
186176
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -564,11 +564,9 @@ private static List<PathPattern> parse(String[] patterns, PathPatternParser pars
564564
return Collections.emptyList();
565565
}
566566
List<PathPattern> result = new ArrayList<>(patterns.length);
567-
for (String path : patterns) {
568-
if (StringUtils.hasText(path) && !path.startsWith("/")) {
569-
path = "/" + path;
570-
}
571-
result.add(parser.parse(path));
567+
for (String pattern : patterns) {
568+
pattern = parser.initFullPathPattern(pattern);
569+
result.add(parser.parse(pattern));
572570
}
573571
return result;
574572
}

spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -107,10 +107,9 @@ public static RequestPredicate methods(HttpMethod... httpMethods) {
107107
*/
108108
public static RequestPredicate path(String pattern) {
109109
Assert.notNull(pattern, "'pattern' must not be null");
110-
if (!pattern.isEmpty() && !pattern.startsWith("/")) {
111-
pattern = "/" + pattern;
112-
}
113-
return pathPredicates(PathPatternParser.defaultInstance).apply(pattern);
110+
PathPatternParser parser = PathPatternParser.defaultInstance;
111+
pattern = parser.initFullPathPattern(pattern);
112+
return pathPredicates(parser).apply(pattern);
114113
}
115114

116115
/**
@@ -333,14 +332,14 @@ public interface Visitor {
333332
void method(Set<HttpMethod> methods);
334333

335334
/**
336-
* Receive notification of an path predicate.
335+
* Receive notification of a path predicate.
337336
* @param pattern the path pattern that makes up the predicate
338337
* @see RequestPredicates#path(String)
339338
*/
340339
void path(String pattern);
341340

342341
/**
343-
* Receive notification of an path extension predicate.
342+
* Receive notification of a path extension predicate.
344343
* @param extension the path extension that makes up the predicate
345344
* @see RequestPredicates#pathExtension(String)
346345
*/
@@ -426,11 +425,11 @@ public interface Visitor {
426425
void unknown(RequestPredicate predicate);
427426
}
428427

428+
429429
private static class HttpMethodPredicate implements RequestPredicate {
430430

431431
private final Set<HttpMethod> httpMethods;
432432

433-
434433
public HttpMethodPredicate(HttpMethod httpMethod) {
435434
Assert.notNull(httpMethod, "HttpMethod must not be null");
436435
this.httpMethods = EnumSet.of(httpMethod);
@@ -641,12 +640,14 @@ public String toString() {
641640
}
642641
}
643642

643+
644644
private static class PathExtensionPredicate implements RequestPredicate {
645645

646646
private final Predicate<String> extensionPredicate;
647647

648648
@Nullable
649649
private final String extension;
650+
650651
public PathExtensionPredicate(Predicate<String> extensionPredicate) {
651652
Assert.notNull(extensionPredicate, "Predicate must not be null");
652653
this.extensionPredicate = extensionPredicate;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PatternsRequestCondition.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import org.springframework.util.StringUtils;
3535
import org.springframework.web.servlet.HandlerMapping;
3636
import org.springframework.web.util.UrlPathHelper;
37+
import org.springframework.web.util.pattern.PathPatternParser;
3738

3839
/**
3940
* A logical disjunction (' || ') request condition that matches a request
@@ -142,9 +143,7 @@ private static Set<String> initPatterns(String[] patterns) {
142143
}
143144
Set<String> result = new LinkedHashSet<>(patterns.length);
144145
for (String pattern : patterns) {
145-
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
146-
pattern = "/" + pattern;
147-
}
146+
pattern = PathPatternParser.defaultInstance.initFullPathPattern(pattern);
148147
result.add(pattern);
149148
}
150149
return result;

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,6 +65,7 @@
6565
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
6666
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
6767
import org.springframework.web.util.UriComponentsBuilder;
68+
import org.springframework.web.util.pattern.PathPatternParser;
6869

6970
/**
7071
* Creates instances of {@link org.springframework.web.util.UriComponentsBuilder}
@@ -545,9 +546,7 @@ private static UriComponentsBuilder fromMethodInternal(@Nullable UriComponentsBu
545546
String typePath = getClassMapping(controllerType);
546547
String methodPath = getMethodMapping(method);
547548
String path = pathMatcher.combine(typePath, methodPath);
548-
if (StringUtils.hasLength(path) && !path.startsWith("/")) {
549-
path = "/" + path;
550-
}
549+
path = PathPatternParser.defaultInstance.initFullPathPattern(path);
551550
builder.path(path);
552551

553552
return applyContributors(builder, method, args);

0 commit comments

Comments
 (0)