Polish use of LookupPath
This commit is contained in:
@@ -80,7 +80,9 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource
|
||||
|
||||
@Override
|
||||
public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) {
|
||||
String lookupPath = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get().getPath();
|
||||
String lookupPath = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
|
||||
.map(LookupPath::getPath)
|
||||
.orElseThrow(() -> new IllegalStateException("No LookupPath attribute."));
|
||||
for (Map.Entry<String, CorsConfiguration> entry : this.corsConfigurations.entrySet()) {
|
||||
if (this.pathMatcher.match(entry.getKey(), lookupPath)) {
|
||||
return entry.getValue();
|
||||
|
||||
@@ -29,18 +29,21 @@ public final class LookupPath {
|
||||
|
||||
public static final String LOOKUP_PATH_ATTRIBUTE = LookupPath.class.getName();
|
||||
|
||||
|
||||
private final String path;
|
||||
|
||||
private final int fileExtStartIndex;
|
||||
|
||||
private final int fileExtEndIndex;
|
||||
|
||||
|
||||
public LookupPath(String path, int fileExtStartIndex, int fileExtEndIndex) {
|
||||
this.path = path;
|
||||
this.fileExtStartIndex = fileExtStartIndex;
|
||||
this.fileExtEndIndex = fileExtEndIndex;
|
||||
}
|
||||
|
||||
|
||||
public String getPath() {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
@Test
|
||||
public void empty() {
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
assertNull(this.configSource.getCorsConfiguration(exchange));
|
||||
}
|
||||
|
||||
@@ -51,11 +51,11 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
this.configSource.registerCorsConfiguration("/bar/**", config);
|
||||
|
||||
ServerWebExchange exchange = MockServerHttpRequest.get("/foo/test.html").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
assertNull(this.configSource.getCorsConfiguration(exchange));
|
||||
|
||||
exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
|
||||
setLookupPathAttribute(exchange);
|
||||
initLookupPath(exchange);
|
||||
assertEquals(config, this.configSource.getCorsConfiguration(exchange));
|
||||
}
|
||||
|
||||
@@ -64,10 +64,9 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration());
|
||||
}
|
||||
|
||||
public void setLookupPathAttribute(ServerWebExchange exchange) {
|
||||
HttpRequestPathHelper helper = new HttpRequestPathHelper();
|
||||
private void initLookupPath(ServerWebExchange exchange) {
|
||||
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE,
|
||||
helper.getLookupPathForRequest(exchange));
|
||||
new HttpRequestPathHelper().getLookupPathForRequest(exchange));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user