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