Expose RequestPath in ServerHttpRequest

The new structured getPath() method replaces the existing
getContextPath() + getPathWithinApplication().

Issue: SPR-15648
This commit is contained in:
Rossen Stoyanchev
2017-06-11 21:57:54 -04:00
parent 2d17411ec4
commit 38a12ed4ba
26 changed files with 127 additions and 115 deletions

View File

@@ -100,7 +100,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
@Override
public Mono<Object> getHandlerInternal(ServerWebExchange exchange) {
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
Object handler;
try {
handler = lookupHandler(lookupPath, exchange);

View File

@@ -166,7 +166,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
private int getLookupPathIndex(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
String requestPath = request.getURI().getPath();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
return requestPath.indexOf(lookupPath);
}

View File

@@ -186,7 +186,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
return this;
}
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
List<String> matches = getMatchingPatterns(lookupPath);
return matches.isEmpty() ? null :
@@ -243,7 +243,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
*/
@Override
public int compareTo(PatternsRequestCondition other, ServerWebExchange exchange) {
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(lookupPath);
Iterator<String> iterator = this.patterns.iterator();
Iterator<String> iteratorOther = other.patterns.iterator();

View File

@@ -257,7 +257,7 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
*/
@Override
public Mono<HandlerMethod> getHandlerInternal(ServerWebExchange exchange) {
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
if (logger.isDebugEnabled()) {
logger.debug("Looking up handler method for path " + lookupPath);
}

View File

@@ -202,9 +202,11 @@ public class RedirectView extends AbstractUrlBasedView {
String url = getUrl();
Assert.state(url != null, "'url' not set");
ServerHttpRequest request = exchange.getRequest();
StringBuilder targetUrl = new StringBuilder();
if (isContextRelative() && url.startsWith("/")) {
targetUrl.append(exchange.getRequest().getContextPath());
targetUrl.append(request.getPath().contextPath().value());
}
targetUrl.append(url);
@@ -214,7 +216,7 @@ public class RedirectView extends AbstractUrlBasedView {
}
if (isPropagateQuery()) {
targetUrl = appendCurrentRequestQuery(targetUrl.toString(), exchange.getRequest());
targetUrl = appendCurrentRequestQuery(targetUrl.toString(), request);
}
String result = targetUrl.toString();

View File

@@ -28,6 +28,7 @@ import org.springframework.context.NoSuchMessageException;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
@@ -180,10 +181,10 @@ public class RequestContext {
/**
* Return the context path of the current web application. This is
* useful for building links to other resources within the application.
* <p>Delegates to {@link ServerHttpRequest#getContextPath()}.
* <p>Delegates to {@link ServerHttpRequest#getPath()}.
*/
public String getContextPath() {
return this.exchange.getRequest().getContextPath();
return this.exchange.getRequest().getPath().contextPath().value();
}
/**
@@ -193,7 +194,7 @@ public class RequestContext {
* absolute path also URL-encoded accordingly
*/
public String getContextUrl(String relativeUrl) {
String url = getContextPath() + relativeUrl;
String url = StringUtils.applyRelativePath(getContextPath() + "/", relativeUrl);
return getExchange().getResponse().encodeUrl(url);
}
@@ -208,7 +209,7 @@ public class RequestContext {
* absolute path also URL-encoded accordingly
*/
public String getContextUrl(String relativeUrl, Map<String, ?> params) {
String url = getContextPath() + relativeUrl;
String url = StringUtils.applyRelativePath(getContextPath() + "/", relativeUrl);
UriTemplate template = new UriTemplate(url);
url = template.expand(params).toASCIIString();
return getExchange().getResponse().encodeUrl(url);

View File

@@ -258,7 +258,7 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport
* Use the request path the leading and trailing slash stripped.
*/
private String getDefaultViewName(ServerWebExchange exchange) {
String path = exchange.getRequest().getPathWithinApplication();
String path = exchange.getRequest().getPath().pathWithinApplication().value();
if (path.startsWith("/")) {
path = path.substring(1);
}

View File

@@ -213,7 +213,7 @@ public class RequestMappingInfoHandlerMappingTests {
@SuppressWarnings("unchecked")
public void handleMatchUriTemplateVariables() throws Exception {
ServerWebExchange exchange = get("/1/2").toExchange();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
RequestMappingInfo key = paths("/{path1}/{path2}").build();
this.handlerMapping.handleMatch(key, lookupPath, exchange);
@@ -232,7 +232,7 @@ public class RequestMappingInfoHandlerMappingTests {
URI url = URI.create("/group/a%2Fb");
ServerWebExchange exchange = method(HttpMethod.GET, url).toExchange();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, lookupPath, exchange);
String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
@@ -248,7 +248,7 @@ public class RequestMappingInfoHandlerMappingTests {
public void handleMatchBestMatchingPatternAttribute() throws Exception {
RequestMappingInfo key = paths("/{path1}/2", "/**").build();
ServerWebExchange exchange = get("/1/2").toExchange();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, lookupPath, exchange);
assertEquals("/{path1}/2", exchange.getAttributes().get(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
@@ -258,7 +258,7 @@ public class RequestMappingInfoHandlerMappingTests {
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() throws Exception {
RequestMappingInfo key = paths().build();
ServerWebExchange exchange = get("/1/2").toExchange();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(key, lookupPath, exchange);
assertEquals("/1/2", exchange.getAttributes().get(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
@@ -367,7 +367,7 @@ public class RequestMappingInfoHandlerMappingTests {
private void handleMatch(ServerWebExchange exchange, String pattern) {
RequestMappingInfo info = paths(pattern).build();
String lookupPath = exchange.getRequest().getPathWithinApplication();
String lookupPath = exchange.getRequest().getPath().pathWithinApplication().value();
this.handlerMapping.handleMatch(info, lookupPath, exchange);
}

View File

@@ -117,7 +117,7 @@ public class ContextPathIntegrationTests {
@GetMapping("/test")
public String handle(ServerHttpRequest request) {
return "Tested in " + request.getContextPath();
return "Tested in " + request.getPath().contextPath().value();
}
}

View File

@@ -47,7 +47,7 @@ public class RedirectViewTests {
@Before
public void setup() {
this.exchange = MockServerHttpRequest.get("/").contextPath("/context").toExchange();
this.exchange = MockServerHttpRequest.get("/context/path").contextPath("/context").toExchange();
}

View File

@@ -34,7 +34,8 @@ import static org.junit.Assert.assertEquals;
*/
public class RequestContextTests {
private final MockServerWebExchange exchange = MockServerHttpRequest.get("/").contextPath("foo/").toExchange();
private final MockServerWebExchange exchange = MockServerHttpRequest.get("/foo/path")
.contextPath("/foo").toExchange();
private GenericApplicationContext applicationContext;
@@ -50,7 +51,7 @@ public class RequestContextTests {
@Test
public void testGetContextUrl() throws Exception {
RequestContext context = new RequestContext(this.exchange, this.model, this.applicationContext);
assertEquals("foo/bar", context.getContextUrl("bar"));
assertEquals("/foo/bar", context.getContextUrl("bar"));
}
@Test
@@ -59,7 +60,7 @@ public class RequestContextTests {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("spam", "bucket");
assertEquals("foo/bar?spam=bucket", context.getContextUrl("{foo}?spam={spam}", map));
assertEquals("/foo/bar?spam=bucket", context.getContextUrl("{foo}?spam={spam}", map));
}
@Test
@@ -68,7 +69,7 @@ public class RequestContextTests {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar baz");
map.put("spam", "&bucket=");
assertEquals("foo/bar%20baz?spam=%26bucket%3D", context.getContextUrl("{foo}?spam={spam}", map));
assertEquals("/foo/bar%20baz?spam=%26bucket%3D", context.getContextUrl("{foo}?spam={spam}", map));
}
}