Browse Source

Add getContextPath to ServerHttpRequest

Issue: SPR-14726
pull/1171/merge
Rossen Stoyanchev 9 years ago
parent
commit
0bace1b0ae
  1. 16
      spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java
  2. 5
      spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java
  3. 13
      spring-web/src/main/java/org/springframework/web/util/HttpRequestPathHelper.java

16
spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java

@ -25,10 +25,26 @@ import org.springframework.util.MultiValueMap; @@ -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.
*/

5
spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java

@ -81,6 +81,11 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest { @@ -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();

13
spring-web/src/main/java/org/springframework/web/util/HttpRequestPathHelper.java

@ -19,8 +19,10 @@ import java.io.UnsupportedEncodingException; @@ -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 { @@ -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 {

Loading…
Cancel
Save