Add getContextPath to ServerHttpRequest
Issue: SPR-14726
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user