Merge branch '6.0.x'
This commit is contained in:
commit
39e74d89e1
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.web.util.pattern;
|
package org.springframework.web.util.pattern;
|
||||||
|
|
||||||
import org.springframework.http.server.PathContainer;
|
import org.springframework.http.server.PathContainer;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parser for URI path patterns producing {@link PathPattern} instances that can
|
* Parser for URI path patterns producing {@link PathPattern} instances that can
|
||||||
|
@ -103,6 +104,17 @@ public class PathPatternParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the given pattern for use in matching to full URL paths.
|
||||||
|
* <p>By default, prepend a leading slash if needed for non-empty patterns.
|
||||||
|
* @param pattern the pattern to initialize
|
||||||
|
* @return the updated pattern
|
||||||
|
* @since 5.2.25
|
||||||
|
*/
|
||||||
|
public String initFullPathPattern(String pattern) {
|
||||||
|
return (StringUtils.hasLength(pattern) && !pattern.startsWith("/") ? "/" + pattern : pattern);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process the path pattern content, a character at a time, breaking it into
|
* Process the path pattern content, a character at a time, breaking it into
|
||||||
* path elements around separator boundaries and verifying the structure at each
|
* path elements around separator boundaries and verifying the structure at each
|
||||||
|
|
|
@ -112,10 +112,9 @@ public abstract class RequestPredicates {
|
||||||
*/
|
*/
|
||||||
public static RequestPredicate path(String pattern) {
|
public static RequestPredicate path(String pattern) {
|
||||||
Assert.notNull(pattern, "'pattern' must not be null");
|
Assert.notNull(pattern, "'pattern' must not be null");
|
||||||
if (!pattern.isEmpty() && !pattern.startsWith("/")) {
|
PathPatternParser parser = PathPatternParser.defaultInstance;
|
||||||
pattern = "/" + pattern;
|
pattern = parser.initFullPathPattern(pattern);
|
||||||
}
|
return pathPredicates(parser).apply(pattern);
|
||||||
return pathPredicates(PathPatternParser.defaultInstance).apply(pattern);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -30,9 +30,9 @@ import org.springframework.http.server.PathContainer;
|
||||||
import org.springframework.http.server.reactive.observation.ServerRequestObservationContext;
|
import org.springframework.http.server.reactive.observation.ServerRequestObservationContext;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import org.springframework.web.util.pattern.PathPattern;
|
import org.springframework.web.util.pattern.PathPattern;
|
||||||
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for URL-mapped
|
* Abstract base class for URL-mapped
|
||||||
|
@ -217,8 +217,9 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||||
Object resolvedHandler = handler;
|
Object resolvedHandler = handler;
|
||||||
|
|
||||||
// Parse path pattern
|
// Parse path pattern
|
||||||
urlPath = prependLeadingSlash(urlPath);
|
PathPatternParser parser = getPathPatternParser();
|
||||||
PathPattern pattern = getPathPatternParser().parse(urlPath);
|
urlPath = parser.initFullPathPattern(urlPath);
|
||||||
|
PathPattern pattern = parser.parse(urlPath);
|
||||||
if (this.handlerMap.containsKey(pattern)) {
|
if (this.handlerMap.containsKey(pattern)) {
|
||||||
Object existingHandler = this.handlerMap.get(pattern);
|
Object existingHandler = this.handlerMap.get(pattern);
|
||||||
if (existingHandler != null && existingHandler != resolvedHandler) {
|
if (existingHandler != null && existingHandler != resolvedHandler) {
|
||||||
|
@ -246,14 +247,4 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
|
||||||
return (handler instanceof String ? "'" + handler + "'" : handler.toString());
|
return (handler instanceof String ? "'" + handler + "'" : handler.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static String prependLeadingSlash(String pattern) {
|
|
||||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
|
||||||
return "/" + pattern;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return pattern;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -35,7 +35,6 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||||
import org.springframework.http.server.PathContainer;
|
import org.springframework.http.server.PathContainer;
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import org.springframework.web.util.pattern.PathPattern;
|
import org.springframework.web.util.pattern.PathPattern;
|
||||||
|
@ -86,8 +85,9 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||||
public void registerHandlers(Map<String, ResourceWebHandler> handlerMap) {
|
public void registerHandlers(Map<String, ResourceWebHandler> handlerMap) {
|
||||||
this.handlerMap.clear();
|
this.handlerMap.clear();
|
||||||
handlerMap.forEach((rawPattern, resourceWebHandler) -> {
|
handlerMap.forEach((rawPattern, resourceWebHandler) -> {
|
||||||
rawPattern = prependLeadingSlash(rawPattern);
|
PathPatternParser parser = PathPatternParser.defaultInstance;
|
||||||
PathPattern pattern = PathPatternParser.defaultInstance.parse(rawPattern);
|
rawPattern = parser.initFullPathPattern(rawPattern);
|
||||||
|
PathPattern pattern = parser.parse(rawPattern);
|
||||||
this.handlerMap.put(pattern, resourceWebHandler);
|
this.handlerMap.put(pattern, resourceWebHandler);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -172,14 +172,4 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static String prependLeadingSlash(String pattern) {
|
|
||||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
|
||||||
return "/" + pattern;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return pattern;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -601,11 +601,9 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
List<PathPattern> result = new ArrayList<>(patterns.length);
|
List<PathPattern> result = new ArrayList<>(patterns.length);
|
||||||
for (String path : patterns) {
|
for (String pattern : patterns) {
|
||||||
if (StringUtils.hasText(path) && !path.startsWith("/")) {
|
pattern = parser.initFullPathPattern(pattern);
|
||||||
path = "/" + path;
|
result.add(parser.parse(pattern));
|
||||||
}
|
|
||||||
result.add(parser.parse(path));
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,10 +112,9 @@ public abstract class RequestPredicates {
|
||||||
*/
|
*/
|
||||||
public static RequestPredicate path(String pattern) {
|
public static RequestPredicate path(String pattern) {
|
||||||
Assert.notNull(pattern, "'pattern' must not be null");
|
Assert.notNull(pattern, "'pattern' must not be null");
|
||||||
if (!pattern.isEmpty() && !pattern.startsWith("/")) {
|
PathPatternParser parser = PathPatternParser.defaultInstance;
|
||||||
pattern = "/" + pattern;
|
pattern = parser.initFullPathPattern(pattern);
|
||||||
}
|
return pathPredicates(parser).apply(pattern);
|
||||||
return pathPredicates(PathPatternParser.defaultInstance).apply(pattern);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -51,6 +51,7 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.HandlerMapping;
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
import org.springframework.web.util.ServletRequestPathUtils;
|
import org.springframework.web.util.ServletRequestPathUtils;
|
||||||
import org.springframework.web.util.UrlPathHelper;
|
import org.springframework.web.util.UrlPathHelper;
|
||||||
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class to get information from the {@code HandlerMapping} that would
|
* Helper class to get information from the {@code HandlerMapping} that would
|
||||||
|
@ -309,10 +310,15 @@ public class HandlerMappingIntrospector
|
||||||
ServletRequestPathUtils.PATH_ATTRIBUTE : UrlPathHelper.PATH_ATTRIBUTE);
|
ServletRequestPathUtils.PATH_ATTRIBUTE : UrlPathHelper.PATH_ATTRIBUTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PathPatternParser getPatternParser() {
|
||||||
|
return this.delegate.getPatternParser();
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public RequestMatchResult match(HttpServletRequest request, String pattern) {
|
public RequestMatchResult match(HttpServletRequest request, String pattern) {
|
||||||
pattern = (StringUtils.hasLength(pattern) && !pattern.startsWith("/") ? "/" + pattern : pattern);
|
pattern = initFullPathPattern(pattern);
|
||||||
Object previousPath = request.getAttribute(this.pathAttributeName);
|
Object previousPath = request.getAttribute(this.pathAttributeName);
|
||||||
request.setAttribute(this.pathAttributeName, this.lookupPath);
|
request.setAttribute(this.pathAttributeName, this.lookupPath);
|
||||||
try {
|
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
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
|
public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
|
||||||
|
|
|
@ -79,11 +79,9 @@ public final class PathPatternsRequestCondition extends AbstractRequestCondition
|
||||||
return EMPTY_PATH_PATTERN;
|
return EMPTY_PATH_PATTERN;
|
||||||
}
|
}
|
||||||
SortedSet<PathPattern> result = new TreeSet<>();
|
SortedSet<PathPattern> result = new TreeSet<>();
|
||||||
for (String path : patterns) {
|
for (String pattern : patterns) {
|
||||||
if (StringUtils.hasText(path) && !path.startsWith("/")) {
|
pattern = parser.initFullPathPattern(pattern);
|
||||||
path = "/" + path;
|
result.add(parser.parse(pattern));
|
||||||
}
|
|
||||||
result.add(parser.parse(path));
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ import org.springframework.util.PathMatcher;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.util.UrlPathHelper;
|
import org.springframework.web.util.UrlPathHelper;
|
||||||
import org.springframework.web.util.pattern.PathPattern;
|
import org.springframework.web.util.pattern.PathPattern;
|
||||||
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A logical disjunction (' || ') request condition that matches a request
|
* 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);
|
Set<String> result = new LinkedHashSet<>(patterns.length);
|
||||||
for (String pattern : patterns) {
|
for (String pattern : patterns) {
|
||||||
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
|
pattern = PathPatternParser.defaultInstance.initFullPathPattern(pattern);
|
||||||
pattern = "/" + pattern;
|
|
||||||
}
|
|
||||||
result.add(pattern);
|
result.add(pattern);
|
||||||
}
|
}
|
||||||
return result;
|
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.mvc.method.RequestMappingInfoHandlerMapping;
|
||||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates instances of {@link org.springframework.web.util.UriComponentsBuilder}
|
* Creates instances of {@link org.springframework.web.util.UriComponentsBuilder}
|
||||||
|
@ -544,10 +545,8 @@ public class MvcUriComponentsBuilder {
|
||||||
String typePath = getClassMapping(controllerType);
|
String typePath = getClassMapping(controllerType);
|
||||||
String methodPath = getMethodMapping(method);
|
String methodPath = getMethodMapping(method);
|
||||||
String path = pathMatcher.combine(typePath, methodPath);
|
String path = pathMatcher.combine(typePath, methodPath);
|
||||||
if (StringUtils.hasLength(path) && !path.startsWith("/")) {
|
path = PathPatternParser.defaultInstance.initFullPathPattern(path);
|
||||||
path = "/" + path;
|
if (!StringUtils.hasText(prefix + path)) {
|
||||||
}
|
|
||||||
else if (!StringUtils.hasText(prefix + path)) {
|
|
||||||
path = "/";
|
path = "/";
|
||||||
}
|
}
|
||||||
builder.path(path);
|
builder.path(path);
|
||||||
|
|
Loading…
Reference in New Issue