Introduce LookupPath in WebFlux request routing
This commit adds the `LookupPath` class that contains the full request path relative to the web context; the application can get from it various information, including the file extension and path parameters (if any). Since that operation is done multiple times for each request, this object is stored as an attribute at the `ServerWebExchange` level. Issue: SPR-15397
This commit is contained in:
@@ -24,8 +24,8 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.support.HttpRequestPathHelper;
|
||||
import org.springframework.web.util.pattern.ParsingPathMatcher;
|
||||
import org.springframework.web.server.support.LookupPath;
|
||||
|
||||
/**
|
||||
* Provide a per reactive request {@link CorsConfiguration} instance based on a
|
||||
@@ -43,8 +43,6 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource
|
||||
|
||||
private PathMatcher pathMatcher = new ParsingPathMatcher();
|
||||
|
||||
private HttpRequestPathHelper pathHelper = new HttpRequestPathHelper();
|
||||
|
||||
|
||||
/**
|
||||
* Set the PathMatcher implementation to use for matching URL paths
|
||||
@@ -56,26 +54,6 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource
|
||||
this.pathMatcher = pathMatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if context path and request URI should be URL-decoded. Both are returned
|
||||
* <i>undecoded</i> by the Servlet API, in contrast to the servlet path.
|
||||
* <p>Uses either the request encoding or the default encoding according
|
||||
* to the Servlet spec (ISO-8859-1).
|
||||
* @see HttpRequestPathHelper#setUrlDecode
|
||||
*/
|
||||
public void setUrlDecode(boolean urlDecode) {
|
||||
this.pathHelper.setUrlDecode(urlDecode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the UrlPathHelper to use for resolution of lookup paths.
|
||||
* <p>Use this to override the default UrlPathHelper with a custom subclass.
|
||||
*/
|
||||
public void setHttpRequestPathHelper(HttpRequestPathHelper pathHelper) {
|
||||
Assert.notNull(pathHelper, "HttpRequestPathHelper must not be null");
|
||||
this.pathHelper = pathHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set CORS configuration based on URL patterns.
|
||||
*/
|
||||
@@ -102,7 +80,7 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource
|
||||
|
||||
@Override
|
||||
public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) {
|
||||
String lookupPath = this.pathHelper.getLookupPathForRequest(exchange);
|
||||
String lookupPath = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get().getPath();
|
||||
for (Map.Entry<String, CorsConfiguration> entry : this.corsConfigurations.entrySet()) {
|
||||
if (this.pathMatcher.match(entry.getKey(), lookupPath)) {
|
||||
return entry.getValue();
|
||||
|
||||
@@ -57,9 +57,14 @@ public class HttpRequestPathHelper {
|
||||
}
|
||||
|
||||
|
||||
public String getLookupPathForRequest(ServerWebExchange exchange) {
|
||||
public LookupPath getLookupPathForRequest(ServerWebExchange exchange) {
|
||||
String path = getPathWithinApplication(exchange.getRequest());
|
||||
return (shouldUrlDecode() ? decode(exchange, path) : path);
|
||||
path = (shouldUrlDecode() ? decode(exchange, path) : path);
|
||||
int begin = path.lastIndexOf('/') + 1;
|
||||
int end = path.length();
|
||||
int paramIndex = path.indexOf(';', begin);
|
||||
int extIndex = path.lastIndexOf('.', paramIndex != -1 ? paramIndex : end);
|
||||
return new LookupPath(path, extIndex, paramIndex);
|
||||
}
|
||||
|
||||
private String getPathWithinApplication(ServerHttpRequest request) {
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2002-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.server.support;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
/**
|
||||
* Lookup path information of an incoming HTTP request.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
* @see HttpRequestPathHelper
|
||||
*/
|
||||
public final class LookupPath {
|
||||
|
||||
public static final String LOOKUP_PATH_ATTRIBUTE = LookupPath.class.getName();
|
||||
|
||||
private final String path;
|
||||
|
||||
private final int fileExtensionIndex;
|
||||
|
||||
private final int pathParametersIndex;
|
||||
|
||||
public LookupPath(String path, int fileExtensionIndex, int pathParametersIndex) {
|
||||
this.path = path;
|
||||
this.fileExtensionIndex = fileExtensionIndex;
|
||||
this.pathParametersIndex = pathParametersIndex;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
if (this.pathParametersIndex != -1) {
|
||||
// TODO: variant without the path parameter information?
|
||||
//return this.path.substring(0, this.pathParametersIndex);
|
||||
return this.path;
|
||||
}
|
||||
else {
|
||||
return this.path;
|
||||
}
|
||||
}
|
||||
|
||||
public String getPathWithoutExtension() {
|
||||
if (this.fileExtensionIndex != -1) {
|
||||
return this.path.substring(0, this.fileExtensionIndex);
|
||||
}
|
||||
else {
|
||||
return this.path;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getFileExtension() {
|
||||
if (this.fileExtensionIndex == -1) {
|
||||
return null;
|
||||
}
|
||||
else if (this.pathParametersIndex == -1) {
|
||||
return this.path.substring(this.fileExtensionIndex);
|
||||
}
|
||||
else {
|
||||
return this.path.substring(this.fileExtensionIndex, this.pathParametersIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getPathParameters() {
|
||||
return this.pathParametersIndex == -1 ?
|
||||
null : this.path.substring(this.pathParametersIndex + 1);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user