Add static factory/accessor methods to LookupPath
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.util.pattern.ParsingPathMatcher;
|
||||
import org.springframework.web.server.support.LookupPath;
|
||||
import org.springframework.web.util.pattern.ParsingPathMatcher;
|
||||
|
||||
/**
|
||||
* Provide a per reactive request {@link CorsConfiguration} instance based on a
|
||||
@@ -80,9 +80,7 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource
|
||||
|
||||
@Override
|
||||
public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) {
|
||||
String lookupPath = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
|
||||
.map(LookupPath::getPath)
|
||||
.orElseThrow(() -> new IllegalStateException("No LookupPath attribute."));
|
||||
String lookupPath = LookupPath.getCurrent(exchange).getPath();
|
||||
for (Map.Entry<String, CorsConfiguration> entry : this.corsConfigurations.entrySet()) {
|
||||
if (this.pathMatcher.match(entry.getKey(), lookupPath)) {
|
||||
return entry.getValue();
|
||||
|
||||
@@ -17,17 +17,23 @@
|
||||
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
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
* @see HttpRequestPathHelper
|
||||
*/
|
||||
public final class LookupPath {
|
||||
|
||||
public static final String LOOKUP_PATH_ATTRIBUTE = LookupPath.class.getName();
|
||||
/**
|
||||
* Name of request attribute under which the LookupPath is stored via
|
||||
* {@link #getOrCreate} and accessed via {@link #getCurrent}.
|
||||
*/
|
||||
public static final String LOOKUP_PATH_ATTRIBUTE_NAME = LookupPath.class.getName();
|
||||
|
||||
|
||||
private final String path;
|
||||
@@ -70,4 +76,34 @@ public final class LookupPath {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the LookupPath for the current request from the request attribute
|
||||
* {@link #LOOKUP_PATH_ATTRIBUTE_NAME} or otherwise create and stored it
|
||||
* under that attribute for subsequent use.
|
||||
* @param exchange the current exchange
|
||||
* @param pathHelper the pathHelper to create the LookupPath with
|
||||
* @return the LookupPath for the current request
|
||||
*/
|
||||
public static LookupPath getOrCreate(ServerWebExchange exchange, HttpRequestPathHelper pathHelper) {
|
||||
return exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE_NAME)
|
||||
.orElseGet(() -> {
|
||||
LookupPath lookupPath = pathHelper.getLookupPathForRequest(exchange);
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE_NAME, lookupPath);
|
||||
return lookupPath;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the LookupPath for the current request from the request attribute
|
||||
* {@link #LOOKUP_PATH_ATTRIBUTE_NAME} or raise an {@link IllegalStateException}
|
||||
* if not found.
|
||||
* @param exchange the current exchange
|
||||
* @return the LookupPath, never {@code null}
|
||||
*/
|
||||
public static LookupPath getCurrent(ServerWebExchange exchange) {
|
||||
return exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE_NAME)
|
||||
.orElseThrow(() -> new IllegalStateException("No LookupPath attribute."));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user