Encapsulate full path initialization
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* 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.
|
||||
@@ -109,10 +109,9 @@ public abstract class RequestPredicates {
|
||||
*/
|
||||
public static RequestPredicate path(String pattern) {
|
||||
Assert.notNull(pattern, "'pattern' must not be null");
|
||||
if (!pattern.isEmpty() && !pattern.startsWith("/")) {
|
||||
pattern = "/" + pattern;
|
||||
}
|
||||
return pathPredicates(PathPatternParser.defaultInstance).apply(pattern);
|
||||
PathPatternParser parser = PathPatternParser.defaultInstance;
|
||||
pattern = parser.initFullPathPattern(pattern);
|
||||
return pathPredicates(parser).apply(pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -51,6 +51,7 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.util.ServletRequestPathUtils;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
/**
|
||||
* Helper class to get information from the {@code HandlerMapping} that would
|
||||
@@ -309,10 +310,15 @@ public class HandlerMappingIntrospector
|
||||
ServletRequestPathUtils.PATH_ATTRIBUTE : UrlPathHelper.PATH_ATTRIBUTE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PathPatternParser getPatternParser() {
|
||||
return this.delegate.getPatternParser();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public RequestMatchResult match(HttpServletRequest request, String pattern) {
|
||||
pattern = (StringUtils.hasLength(pattern) && !pattern.startsWith("/") ? "/" + pattern : pattern);
|
||||
pattern = initFullPathPattern(pattern);
|
||||
Object previousPath = request.getAttribute(this.pathAttributeName);
|
||||
request.setAttribute(this.pathAttributeName, this.lookupPath);
|
||||
try {
|
||||
@@ -323,6 +329,11 @@ public class HandlerMappingIntrospector
|
||||
}
|
||||
}
|
||||
|
||||
private String initFullPathPattern(String pattern) {
|
||||
PathPatternParser parser = (getPatternParser() != null ? getPatternParser() : PathPatternParser.defaultInstance);
|
||||
return parser.initFullPathPattern(pattern);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
|
||||
|
||||
@@ -79,11 +79,9 @@ public final class PathPatternsRequestCondition extends AbstractRequestCondition
|
||||
return EMPTY_PATH_PATTERN;
|
||||
}
|
||||
SortedSet<PathPattern> result = new TreeSet<>();
|
||||
for (String path : patterns) {
|
||||
if (StringUtils.hasText(path) && !path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
result.add(parser.parse(path));
|
||||
for (String pattern : patterns) {
|
||||
pattern = parser.initFullPathPattern(pattern);
|
||||
result.add(parser.parse(pattern));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
/**
|
||||
* A logical disjunction (' || ') request condition that matches a request
|
||||
@@ -158,9 +159,7 @@ public class PatternsRequestCondition extends AbstractRequestCondition<PatternsR
|
||||
}
|
||||
Set<String> result = new LinkedHashSet<>(patterns.length);
|
||||
for (String pattern : patterns) {
|
||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
||||
pattern = "/" + pattern;
|
||||
}
|
||||
pattern = PathPatternParser.defaultInstance.initFullPathPattern(pattern);
|
||||
result.add(pattern);
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -64,6 +64,7 @@ import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
/**
|
||||
* Creates instances of {@link org.springframework.web.util.UriComponentsBuilder}
|
||||
@@ -544,10 +545,8 @@ public class MvcUriComponentsBuilder {
|
||||
String typePath = getClassMapping(controllerType);
|
||||
String methodPath = getMethodMapping(method);
|
||||
String path = pathMatcher.combine(typePath, methodPath);
|
||||
if (StringUtils.hasLength(path) && !path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
else if (!StringUtils.hasText(prefix + path)) {
|
||||
path = PathPatternParser.defaultInstance.initFullPathPattern(path);
|
||||
if (!StringUtils.hasText(prefix + path)) {
|
||||
path = "/";
|
||||
}
|
||||
builder.path(path);
|
||||
|
||||
Reference in New Issue
Block a user