Remove HttpRequestPathHelper

The use of the undecoded URL path by default and the removal of suffix
pattern matching effectively means HttpRequestPathHelper is no longer
needed.

Issue: SPR-15640, SPR-15639
This commit is contained in:
Rossen Stoyanchev
2017-06-06 21:40:40 -04:00
parent 95196e1aee
commit b65bfdb979
10 changed files with 43 additions and 323 deletions

View File

@@ -18,7 +18,6 @@ package org.springframework.web.reactive.config;
import org.springframework.lang.Nullable;
import org.springframework.util.PathMatcher;
import org.springframework.web.server.support.HttpRequestPathHelper;
import org.springframework.web.util.pattern.ParsingPathMatcher;
/**
@@ -35,8 +34,6 @@ public class PathMatchConfigurer {
private Boolean registeredSuffixPatternMatch;
private HttpRequestPathHelper pathHelper;
private PathMatcher pathMatcher;
@@ -73,15 +70,6 @@ public class PathMatchConfigurer {
return this;
}
/**
* Set a {@code HttpRequestPathHelper} for the resolution of lookup paths.
* <p>Default is {@code HttpRequestPathHelper}.
*/
public PathMatchConfigurer setPathHelper(HttpRequestPathHelper pathHelper) {
this.pathHelper = pathHelper;
return this;
}
/**
* Set the PathMatcher for matching URL paths against registered URL patterns.
* <p>The default is a {@link org.springframework.web.util.pattern.ParsingPathMatcher}.
@@ -106,11 +94,6 @@ public class PathMatchConfigurer {
return this.registeredSuffixPatternMatch;
}
@Nullable
protected HttpRequestPathHelper getPathHelper() {
return this.pathHelper;
}
@Nullable
public PathMatcher getPathMatcher() {
if (this.pathMatcher instanceof ParsingPathMatcher && (this.trailingSlashMatch || this.suffixPatternMatch)) {

View File

@@ -133,16 +133,10 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
if (useTrailingSlashMatch != null) {
mapping.setUseTrailingSlashMatch(useTrailingSlashMatch);
}
HttpRequestPathHelper pathHelper = configurer.getPathHelper();
if (pathHelper != null) {
mapping.setPathHelper(pathHelper);
}
PathMatcher pathMatcher = configurer.getPathMatcher();
if (pathMatcher != null) {
mapping.setPathMatcher(pathMatcher);
}
return mapping;
}
@@ -251,9 +245,6 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
if (pathMatchConfigurer.getPathMatcher() != null) {
handlerMapping.setPathMatcher(pathMatchConfigurer.getPathMatcher());
}
if (pathMatchConfigurer.getPathHelper() != null) {
handlerMapping.setPathHelper(pathMatchConfigurer.getPathHelper());
}
}
else {
handlerMapping = new EmptyHandlerMapping();

View File

@@ -34,7 +34,6 @@ import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.support.HttpRequestPathHelper;
import org.springframework.web.util.pattern.ParsingPathMatcher;
/**
@@ -53,8 +52,6 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport im
private int order = Integer.MAX_VALUE; // default: same as non-Ordered
private HttpRequestPathHelper pathHelper = new HttpRequestPathHelper();
private PathMatcher pathMatcher = new ParsingPathMatcher();
private final UrlBasedCorsConfigurationSource globalCorsConfigSource = new UrlBasedCorsConfigurationSource();
@@ -76,33 +73,6 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport im
return this.order;
}
/**
* Set if the path should be URL-decoded. This sets the same property on the
* underlying path helper.
* @see HttpRequestPathHelper#setUrlDecode(boolean)
*/
public void setUrlDecode(boolean urlDecode) {
this.pathHelper.setUrlDecode(urlDecode);
}
/**
* Set the {@link HttpRequestPathHelper} to use for resolution of lookup
* paths. Use this to override the default implementation with a custom
* subclass or to share common path helper settings across multiple
* HandlerMappings.
*/
public void setPathHelper(HttpRequestPathHelper pathHelper) {
this.pathHelper = pathHelper;
}
/**
* Return the {@link HttpRequestPathHelper} implementation to use for
* resolution of lookup paths.
*/
public HttpRequestPathHelper getPathHelper() {
return this.pathHelper;
}
/**
* Set the PathMatcher implementation to use for matching URL paths
* against registered URL patterns.

View File

@@ -37,7 +37,6 @@ import org.springframework.lang.Nullable;
import org.springframework.util.PathMatcher;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.support.HttpRequestPathHelper;
import org.springframework.web.util.pattern.ParsingPathMatcher;
/**
@@ -55,8 +54,6 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
protected final Log logger = LogFactory.getLog(getClass());
private HttpRequestPathHelper pathHelper = new HttpRequestPathHelper();
private PathMatcher pathMatcher = new ParsingPathMatcher();
private final Map<String, ResourceWebHandler> handlerMap = new LinkedHashMap<>();
@@ -64,22 +61,6 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
private boolean autodetect = true;
/**
* Configure a {@code HttpRequestPathHelper} to use in
* {@link #getForRequestUrl(ServerWebExchange, String)}
* in order to derive the lookup path for a target request URL path.
*/
public void setPathHelper(HttpRequestPathHelper pathHelper) {
this.pathHelper = pathHelper;
}
/**
* Return the configured {@code HttpRequestPathHelper}.
*/
public HttpRequestPathHelper getPathHelper() {
return this.pathHelper;
}
/**
* Configure a {@code PathMatcher} to use when comparing target lookup path
* against resource mappings.

View File

@@ -17,6 +17,7 @@
package org.springframework.web.reactive.result.method;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -27,6 +28,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
import org.springframework.http.HttpHeaders;
@@ -34,7 +36,9 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.InvalidMediaTypeException;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.result.condition.NameValueExpression;
@@ -123,7 +127,9 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
// Now decode URI variables
if (!uriVariables.isEmpty()) {
uriVariables = getPathHelper().decodePathVariables(exchange, uriVariables);
uriVariables = uriVariables.entrySet().stream().collect(Collectors.toMap(
Entry::getKey, e -> StringUtils.uriDecode(e.getValue(), StandardCharsets.UTF_8)
));
}
exchange.getAttributes().put(BEST_MATCHING_PATTERN_ATTRIBUTE, bestPattern);
@@ -156,11 +162,41 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
semicolonContent = uriVarValue.substring(semicolonIndex + 1);
uriVariables.put(uriVar.getKey(), uriVarValue.substring(0, semicolonIndex));
}
result.put(uriVar.getKey(), getPathHelper().parseMatrixVariables(exchange, semicolonContent));
result.put(uriVar.getKey(), parseMatrixVariables(exchange, semicolonContent));
}
return result;
}
private static MultiValueMap<String, String> parseMatrixVariables(ServerWebExchange exchange,
String semicolonContent) {
MultiValueMap<String, String> vars = new LinkedMultiValueMap<>();
if (!StringUtils.hasText(semicolonContent)) {
return vars;
}
StringTokenizer pairs = new StringTokenizer(semicolonContent, ";");
while (pairs.hasMoreTokens()) {
String pair = pairs.nextToken();
int index = pair.indexOf('=');
if (index != -1) {
String name = pair.substring(0, index);
String rawValue = pair.substring(index + 1);
for (String value : StringUtils.commaDelimitedListToStringArray(rawValue)) {
vars.add(name, value);
}
}
else {
vars.add(pair, "");
}
}
MultiValueMap<String, String> decoded = new LinkedMultiValueMap<>(vars.size());
vars.forEach((key, values) -> values.forEach(value -> {
String decodedValue = StringUtils.uriDecode(value, StandardCharsets.UTF_8);
decoded.add(key, decodedValue);
}));
return decoded;
}
/**
* Iterate all RequestMappingInfos once again, look if any match by URL at
* least and raise exceptions accordingly.

View File

@@ -63,7 +63,11 @@ import org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewRes
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClass;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;
@@ -250,7 +254,6 @@ public class WebFluxConfigurationSupportTests {
assertEquals(Ordered.LOWEST_PRECEDENCE - 1, handlerMapping.getOrder());
assertNotNull(handlerMapping.getPathHelper());
assertNotNull(handlerMapping.getPathMatcher());
SimpleUrlHandlerMapping urlHandlerMapping = (SimpleUrlHandlerMapping) handlerMapping;

View File

@@ -4,7 +4,6 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mapping" class="org.springframework.web.reactive.handler.SimpleUrlHandlerMapping">
<property name="urlDecode" value="true" />
<property name="mappings">
<value>
welcome.html=mainController