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

View File

@ -175,12 +175,12 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport im
}
protected LookupPath getLookupPath(ServerWebExchange exchange) {
Optional<LookupPath> attribute = exchange.getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE);
return attribute.orElseGet(() -> {
LookupPath lookupPath = createLookupPath(exchange);
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, lookupPath);
return lookupPath;
});
return exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
.orElseGet(() -> {
LookupPath lookupPath = createLookupPath(exchange);
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, lookupPath);
return lookupPath;
});
}
protected LookupPath createLookupPath(ServerWebExchange exchange) {

View File

@ -185,7 +185,9 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
private int getLookupPathIndex(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
String requestPath = request.getURI().getPath();
LookupPath lookupPath = getPathHelper().getLookupPathForRequest(exchange);
LookupPath lookupPath = exchange
.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
.orElseGet(() -> getPathHelper().getLookupPathForRequest(exchange));
return requestPath.indexOf(lookupPath.getPath());
}

View File

@ -29,8 +29,8 @@ import java.util.Set;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.server.support.LookupPath;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.support.LookupPath;
import org.springframework.web.util.pattern.ParsingPathMatcher;
/**
@ -187,8 +187,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
return this;
}
LookupPath lookupPath = exchange
.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get();
LookupPath lookupPath = getLookupPath(exchange);
List<String> matches = getMatchingPatterns(lookupPath);
return matches.isEmpty() ? null :
@ -196,6 +195,11 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
this.useTrailingSlashMatch, this.fileExtensions);
}
private LookupPath getLookupPath(ServerWebExchange exchange) {
return exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
.orElseThrow(() -> new IllegalStateException("No LookupPath attribute."));
}
/**
* Find the patterns matching the given lookup path. Invoking this method should
* yield results equivalent to those of calling
@ -259,8 +263,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
*/
@Override
public int compareTo(PatternsRequestCondition other, ServerWebExchange exchange) {
LookupPath lookupPath = exchange
.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get();
LookupPath lookupPath = getLookupPath(exchange);
Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(lookupPath.getPath());
Iterator<String> iterator = this.patterns.iterator();
Iterator<String> iteratorOther = other.patterns.iterator();

View File

@ -32,13 +32,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.result.condition.RequestCondition;
import org.springframework.web.reactive.result.method.RequestMappingInfo;
import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.server.support.LookupPath;
import org.springframework.web.server.ServerWebExchange;
/**
* An extension of {@link RequestMappingInfoHandlerMapping} that creates
@ -169,11 +167,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
@Override
protected LookupPath createLookupPath(ServerWebExchange exchange) {
return getPathHelper().getLookupPathForRequest(exchange);
}
/**
* Uses method and type-level @{@link RequestMapping} annotations to create
* the RequestMappingInfo.

View File

@ -257,7 +257,9 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport
* Use the request path the leading and trailing slash stripped.
*/
private String getDefaultViewName(ServerWebExchange exchange) {
String path = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get().getPath();
String path = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
.map(LookupPath::getPath)
.orElseThrow(() -> new IllegalStateException("No LookupPath attribute."));
if (path.startsWith("/")) {
path = path.substring(1);
}

View File

@ -22,9 +22,9 @@ import java.util.Set;
import org.junit.Test;
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.support.HttpRequestPathHelper;
import org.springframework.web.server.support.LookupPath;
import org.springframework.web.server.ServerWebExchange;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -82,7 +82,7 @@ public class PatternsRequestConditionTests {
@Test
public void matchDirectPath() throws Exception {
PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo"));
PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo"));
assertNotNull(match);
}
@ -90,7 +90,7 @@ public class PatternsRequestConditionTests {
@Test
public void matchPattern() throws Exception {
PatternsRequestCondition condition = new PatternsRequestCondition("/foo/*");
PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo/bar"));
PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo/bar"));
assertNotNull(match);
}
@ -98,7 +98,7 @@ public class PatternsRequestConditionTests {
@Test
public void matchSortPatterns() throws Exception {
PatternsRequestCondition condition = new PatternsRequestCondition("/*/*", "/foo/bar", "/foo/*");
PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo/bar"));
PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo/bar"));
PatternsRequestCondition expected = new PatternsRequestCondition("/foo/bar", "/foo/*", "/*/*");
assertEquals(expected, match);
@ -106,7 +106,7 @@ public class PatternsRequestConditionTests {
@Test
public void matchSuffixPattern() throws Exception {
ServerWebExchange exchange = createExchange("/foo.html");
ServerWebExchange exchange = initExchange("/foo.html");
PatternsRequestCondition condition = new PatternsRequestCondition("/{foo}");
PatternsRequestCondition match = condition.getMatchingCondition(exchange);
@ -129,13 +129,13 @@ public class PatternsRequestConditionTests {
Set<String> extensions = Collections.singleton("json");
PatternsRequestCondition condition = new PatternsRequestCondition(patterns, null, true, false, extensions);
MockServerWebExchange exchange = createExchange("/jobs/my.job");
MockServerWebExchange exchange = initExchange("/jobs/my.job");
PatternsRequestCondition match = condition.getMatchingCondition(exchange);
assertNotNull(match);
assertEquals("/jobs/{jobName}", match.getPatterns().iterator().next());
exchange = createExchange("/jobs/my.job.json");
exchange = initExchange("/jobs/my.job.json");
match = condition.getMatchingCondition(exchange);
assertNotNull(match);
@ -152,7 +152,7 @@ public class PatternsRequestConditionTests {
PatternsRequestCondition combined = condition1.combine(condition2);
MockServerWebExchange exchange = createExchange("/prefix/suffix.json");
MockServerWebExchange exchange = initExchange("/prefix/suffix.json");
PatternsRequestCondition match = combined.getMatchingCondition(exchange);
assertNotNull(match);
@ -160,7 +160,7 @@ public class PatternsRequestConditionTests {
@Test
public void matchTrailingSlash() throws Exception {
MockServerWebExchange exchange = createExchange("/foo/");
MockServerWebExchange exchange = initExchange("/foo/");
PatternsRequestCondition condition = new PatternsRequestCondition("/foo");
PatternsRequestCondition match = condition.getMatchingCondition(exchange);
@ -175,7 +175,7 @@ public class PatternsRequestConditionTests {
assertEquals("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)",
"/foo/", match.getPatterns().iterator().next());
exchange = createExchange("/foo/");
exchange = initExchange("/foo/");
condition = new PatternsRequestCondition(new String[] {"/foo"}, null, false, false, null);
match = condition.getMatchingCondition(exchange);
@ -185,7 +185,7 @@ public class PatternsRequestConditionTests {
@Test
public void matchPatternContainsExtension() throws Exception {
PatternsRequestCondition condition = new PatternsRequestCondition("/foo.jpg");
PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo.html"));
PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo.html"));
assertNull(match);
}
@ -195,7 +195,7 @@ public class PatternsRequestConditionTests {
PatternsRequestCondition c1 = new PatternsRequestCondition("/foo*");
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo*");
assertEquals(0, c1.compareTo(c2, createExchange("/foo")));
assertEquals(0, c1.compareTo(c2, initExchange("/foo")));
}
@Test
@ -203,12 +203,12 @@ public class PatternsRequestConditionTests {
PatternsRequestCondition c1 = new PatternsRequestCondition("/fo*");
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo");
assertEquals(1, c1.compareTo(c2, createExchange("/foo")));
assertEquals(1, c1.compareTo(c2, initExchange("/foo")));
}
@Test
public void compareNumberOfMatchingPatterns() throws Exception {
ServerWebExchange exchange = createExchange("/foo.html");
ServerWebExchange exchange = initExchange("/foo.html");
PatternsRequestCondition c1 = new PatternsRequestCondition("/foo", "*.jpeg");
PatternsRequestCondition c2 = new PatternsRequestCondition("/foo", "*.html");
@ -220,10 +220,10 @@ public class PatternsRequestConditionTests {
assertEquals(1, match1.compareTo(match2, exchange));
}
private MockServerWebExchange createExchange(String path) {
private MockServerWebExchange initExchange(String path) {
MockServerWebExchange exchange = get(path).toExchange();
HttpRequestPathHelper helper = new HttpRequestPathHelper();
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange));
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE,
new HttpRequestPathHelper().getLookupPathForRequest(exchange));
return exchange;
}

View File

@ -67,7 +67,7 @@ public class RequestMappingInfoTests {
@Test
public void matchPatternsCondition() {
MockServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange();
setLookupPathAttribute(exchange);
initLookupPath(exchange);
RequestMappingInfo info = paths("/foo*", "/bar").build();
RequestMappingInfo expected = paths("/foo*").build();
@ -83,7 +83,7 @@ public class RequestMappingInfoTests {
@Test
public void matchParamsCondition() {
ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange();
setLookupPathAttribute(exchange);
initLookupPath(exchange);
RequestMappingInfo info = paths("/foo").params("foo=bar").build();
RequestMappingInfo match = info.getMatchingCondition(exchange);
@ -99,7 +99,7 @@ public class RequestMappingInfoTests {
@Test
public void matchHeadersCondition() {
ServerWebExchange exchange = MockServerHttpRequest.get("/foo").header("foo", "bar").toExchange();
setLookupPathAttribute(exchange);
initLookupPath(exchange);
RequestMappingInfo info = paths("/foo").headers("foo=bar").build();
RequestMappingInfo match = info.getMatchingCondition(exchange);
@ -115,7 +115,7 @@ public class RequestMappingInfoTests {
@Test
public void matchConsumesCondition() {
ServerWebExchange exchange = MockServerHttpRequest.post("/foo").contentType(MediaType.TEXT_PLAIN).toExchange();
setLookupPathAttribute(exchange);
initLookupPath(exchange);
RequestMappingInfo info = paths("/foo").consumes("text/plain").build();
RequestMappingInfo match = info.getMatchingCondition(exchange);
@ -131,7 +131,7 @@ public class RequestMappingInfoTests {
@Test
public void matchProducesCondition() {
ServerWebExchange exchange = MockServerHttpRequest.get("/foo").accept(MediaType.TEXT_PLAIN).toExchange();
setLookupPathAttribute(exchange);
initLookupPath(exchange);
RequestMappingInfo info = paths("/foo").produces("text/plain").build();
RequestMappingInfo match = info.getMatchingCondition(exchange);
@ -147,7 +147,7 @@ public class RequestMappingInfoTests {
@Test
public void matchCustomCondition() {
ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange();
setLookupPathAttribute(exchange);
initLookupPath(exchange);
RequestMappingInfo info = paths("/foo").params("foo=bar").build();
RequestMappingInfo match = info.getMatchingCondition(exchange);
@ -169,7 +169,7 @@ public class RequestMappingInfoTests {
RequestMappingInfo oneMethodOneParam = paths().methods(RequestMethod.GET).params("foo").build();
ServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange();
setLookupPathAttribute(exchange);
initLookupPath(exchange);
Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, exchange);
List<RequestMappingInfo> list = asList(none, oneMethod, oneMethodOneParam);
@ -279,9 +279,9 @@ public class RequestMappingInfoTests {
assertNull("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD", match);
}
public void setLookupPathAttribute(ServerWebExchange exchange) {
HttpRequestPathHelper helper = new HttpRequestPathHelper();
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange));
private void initLookupPath(ServerWebExchange exchange) {
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE,
new HttpRequestPathHelper().getLookupPathForRequest(exchange));
}
}

View File

@ -219,17 +219,17 @@ public class ViewResolutionResultHandlerTests {
ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account"));
MockServerWebExchange exchange = get("/account").toExchange();
addLookupPathAttribute(exchange);
initLookupPath(exchange);
handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
assertResponseBody(exchange, "account: {id=123}");
exchange = get("/account/").toExchange();
addLookupPathAttribute(exchange);
initLookupPath(exchange);
handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
assertResponseBody(exchange, "account: {id=123}");
exchange = get("/account.123").toExchange();
addLookupPathAttribute(exchange);
initLookupPath(exchange);
handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
assertResponseBody(exchange, "account: {id=123}");
}
@ -256,7 +256,7 @@ public class ViewResolutionResultHandlerTests {
HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext);
MockServerWebExchange exchange = get("/account").accept(APPLICATION_JSON).toExchange();
addLookupPathAttribute(exchange);
initLookupPath(exchange);
TestView defaultView = new TestView("jsonView", APPLICATION_JSON);
@ -307,9 +307,9 @@ public class ViewResolutionResultHandlerTests {
assertEquals("/", response.getHeaders().getLocation().toString());
}
private void addLookupPathAttribute(ServerWebExchange exchange) {
HttpRequestPathHelper helper = new HttpRequestPathHelper();
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange));
private void initLookupPath(ServerWebExchange exchange) {
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE,
new HttpRequestPathHelper().getLookupPathForRequest(exchange));
}
@ -333,7 +333,7 @@ public class ViewResolutionResultHandlerTests {
model.addAttribute("id", "123");
HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
MockServerWebExchange exchange = get(path).toExchange();
addLookupPathAttribute(exchange);
initLookupPath(exchange);
resultHandler(resolvers).handleResult(exchange, result).block(Duration.ofSeconds(5));
assertResponseBody(exchange, responseBody);
return exchange;