Merge branch '6.0.x'

This commit is contained in:
rstoyanchev 2023-07-11 11:17:41 +01:00
commit 39e74d89e1
10 changed files with 51 additions and 55 deletions

View File

@ -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.
@ -17,6 +17,7 @@
package org.springframework.web.util.pattern;
import org.springframework.http.server.PathContainer;
import org.springframework.util.StringUtils;
/**
* 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
* path elements around separator boundaries and verifying the structure at each

View File

@ -112,10 +112,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);
}
/**

View File

@ -30,9 +30,9 @@ import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.observation.ServerRequestObservationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
/**
* Abstract base class for URL-mapped
@ -217,8 +217,9 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
Object resolvedHandler = handler;
// Parse path pattern
urlPath = prependLeadingSlash(urlPath);
PathPattern pattern = getPathPatternParser().parse(urlPath);
PathPatternParser parser = getPathPatternParser();
urlPath = parser.initFullPathPattern(urlPath);
PathPattern pattern = parser.parse(urlPath);
if (this.handlerMap.containsKey(pattern)) {
Object existingHandler = this.handlerMap.get(pattern);
if (existingHandler != null && existingHandler != resolvedHandler) {
@ -246,14 +247,4 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
return (handler instanceof String ? "'" + handler + "'" : handler.toString());
}
private static String prependLeadingSlash(String pattern) {
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
return "/" + pattern;
}
else {
return pattern;
}
}
}

View File

@ -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");
* 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.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;
@ -86,8 +85,9 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
public void registerHandlers(Map<String, ResourceWebHandler> handlerMap) {
this.handlerMap.clear();
handlerMap.forEach((rawPattern, resourceWebHandler) -> {
rawPattern = prependLeadingSlash(rawPattern);
PathPattern pattern = PathPatternParser.defaultInstance.parse(rawPattern);
PathPatternParser parser = PathPatternParser.defaultInstance;
rawPattern = parser.initFullPathPattern(rawPattern);
PathPattern pattern = parser.parse(rawPattern);
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;
}
}
}

View File

@ -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");
* 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();
}
List<PathPattern> result = new ArrayList<>(patterns.length);
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;
}

View File

@ -112,10 +112,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);
}
/**

View File

@ -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 {

View File

@ -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;
}

View File

@ -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;

View File

@ -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);