Polish use of LookupPath

This commit is contained in:
Rossen Stoyanchev
2017-06-02 14:46:34 -04:00
parent 90df7dd279
commit a7020e419a
11 changed files with 67 additions and 63 deletions

View File

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

View File

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

View File

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