Add getContextPath to ServerHttpRequest

Issue: SPR-14726
This commit is contained in:
Rossen Stoyanchev
2016-09-16 15:21:05 -04:00
parent 3772398700
commit 0bace1b0ae
3 changed files with 33 additions and 1 deletions

View File

@@ -25,10 +25,26 @@ import org.springframework.util.MultiValueMap;
* Represents a reactive server-side HTTP request
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @since 5.0
*/
public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage {
// TODO: https://jira.spring.io/browse/SPR-14726
/**
* Returns the portion of the URL path that represents the context path for
* the current {@link HttpHandler}. The context path is always at the
* beginning of the request path. It starts with "/" but but does not end
* with "/". This method may return an empty string if no context path is
* configured.
* @return the context path (not decoded) or an empty string
*/
default String getContextPath() {
return "";
}
/**
* Return a read-only map with parsed and decoded query parameter values.
*/

View File

@@ -81,6 +81,11 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest {
return HttpMethod.valueOf(getServletRequest().getMethod());
}
@Override
public String getContextPath() {
return getServletRequest().getContextPath();
}
@Override
protected URI initUri() throws URISyntaxException {
StringBuffer url = this.request.getRequestURL();

View File

@@ -19,8 +19,10 @@ import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
/**
@@ -54,10 +56,19 @@ public class HttpRequestPathHelper {
public String getLookupPathForRequest(ServerWebExchange exchange) {
String path = exchange.getRequest().getURI().getRawPath();
String path = getPathWithinApplication(exchange.getRequest());
return (this.shouldUrlDecode() ? decode(exchange, path) : path);
}
private String getPathWithinApplication(ServerHttpRequest request) {
String contextPath = request.getContextPath();
String path = request.getURI().getRawPath();
if (!StringUtils.hasText(contextPath)) {
return path;
}
return (path.length() > contextPath.length() ? path.substring(contextPath.length()) : "");
}
private String decode(ServerWebExchange exchange, String path) {
// TODO: look up request encoding?
try {