SPR-9075 Add fromRequestUri() methods to ServletUriComponentsBuilder
This commit is contained in:
parent
f61410705c
commit
95683f5137
|
|
@ -23,13 +23,11 @@ import org.springframework.util.StringUtils;
|
|||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
* A builder for {@link UriComponents} that offers static factory methods to
|
||||
* extract information from an {@code HttpServletRequest}.
|
||||
* A UriComponentsBuilder that extracts information from an HttpServletRequest.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.1
|
||||
|
|
@ -50,24 +48,25 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return a builder initialized with the host, port, scheme, and the
|
||||
* context path of the given request.
|
||||
* Prepare a builder from the host, port, scheme, and context path of
|
||||
* an HttpServletRequest.
|
||||
*/
|
||||
public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request) {
|
||||
ServletUriComponentsBuilder builder = fromRequest(request);
|
||||
builder.replacePath(new UrlPathHelper().getContextPath(request));
|
||||
builder.replacePath(request.getContextPath());
|
||||
builder.replaceQuery(null);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a builder initialized with the host, port, scheme, context path,
|
||||
* and the servlet mapping of the given request.
|
||||
* Prepare a builder from the host, port, scheme, context path, and
|
||||
* servlet mapping of an HttpServletRequest. The results may vary depending
|
||||
* on the type of servlet mapping used.
|
||||
*
|
||||
* <p>For example if the servlet is mapped by name, i.e. {@code "/main/*"},
|
||||
* then the resulting path will be {@code /appContext/main}. If the servlet
|
||||
* path is not mapped by name, i.e. {@code "/"} or {@code "*.html"}, then
|
||||
* the resulting path will contain the context path only.
|
||||
* <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path
|
||||
* will end with "/main". If the servlet is mapped otherwise, e.g.
|
||||
* {@code "/"} or {@code "*.do"}, the result will be the same as
|
||||
* if calling {@link #fromContextPath(HttpServletRequest)}.
|
||||
*/
|
||||
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) {
|
||||
ServletUriComponentsBuilder builder = fromContextPath(request);
|
||||
|
|
@ -78,8 +77,19 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return a builder initialized with all available information in the given
|
||||
* request including scheme, host, port, path, and query string.
|
||||
* Prepare a builder from the host, port, scheme, and path of
|
||||
* an HttpSevletRequest.
|
||||
*/
|
||||
public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request) {
|
||||
ServletUriComponentsBuilder builder = fromRequest(request);
|
||||
builder.replacePath(request.getRequestURI());
|
||||
builder.replaceQuery(null);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a builder by copying the scheme, host, port, path, and
|
||||
* query string of an HttpServletRequest.
|
||||
*/
|
||||
public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request) {
|
||||
String scheme = request.getScheme();
|
||||
|
|
@ -91,30 +101,38 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
|||
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
|
||||
builder.port(port);
|
||||
}
|
||||
builder.path(new UrlPathHelper().getRequestUri(request));
|
||||
builder.path(request.getRequestURI());
|
||||
builder.query(request.getQueryString());
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent to {@link #fromContextPath(HttpServletRequest)} except the
|
||||
* request is obtained via {@link RequestContextHolder}.
|
||||
* Same as {@link #fromContextPath(HttpServletRequest)} except the
|
||||
* request is obtained through {@link RequestContextHolder}.
|
||||
*/
|
||||
public static ServletUriComponentsBuilder fromCurrentContextPath() {
|
||||
return fromContextPath(getCurrentRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent to {@link #fromServletMapping(HttpServletRequest)} except the
|
||||
* request is obtained via {@link RequestContextHolder}.
|
||||
* Same as {@link #fromServletMapping(HttpServletRequest)} except the
|
||||
* request is obtained through {@link RequestContextHolder}.
|
||||
*/
|
||||
public static ServletUriComponentsBuilder fromCurrentServletMapping() {
|
||||
return fromServletMapping(getCurrentRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent to {@link #fromRequest(HttpServletRequest)} except the
|
||||
* request is obtained via {@link RequestContextHolder}.
|
||||
* Same as {@link #fromRequestUri(HttpServletRequest)} except the
|
||||
* request is obtained through {@link RequestContextHolder}.
|
||||
*/
|
||||
public static ServletUriComponentsBuilder fromCurrentRequestUri() {
|
||||
return fromRequestUri(getCurrentRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #fromRequest(HttpServletRequest)} except the
|
||||
* request is obtained through {@link RequestContextHolder}.
|
||||
*/
|
||||
public static ServletUriComponentsBuilder fromCurrentRequest() {
|
||||
return fromRequest(getCurrentRequest());
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -44,7 +44,6 @@ public class ServletUriComponentsBuilderTests {
|
|||
public void fromRequest() {
|
||||
request.setRequestURI("/mvc-showcase/data/param");
|
||||
request.setQueryString("foo=123");
|
||||
|
||||
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
|
||||
|
||||
assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result);
|
||||
|
|
@ -52,11 +51,10 @@ public class ServletUriComponentsBuilderTests {
|
|||
|
||||
@Test
|
||||
public void fromRequestEncodedPath() {
|
||||
request.setRequestURI("/mvc-showcase/data/foo%20bar;jsessionid=123");
|
||||
|
||||
request.setRequestURI("/mvc-showcase/data/foo%20bar");
|
||||
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
|
||||
|
||||
assertEquals("http://localhost/mvc-showcase/data/foo bar", result);
|
||||
assertEquals("http://localhost/mvc-showcase/data/foo%20bar", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -71,17 +69,24 @@ public class ServletUriComponentsBuilderTests {
|
|||
public void fromRequestAtypicalHttpsPort() {
|
||||
request.setScheme("https");
|
||||
request.setServerPort(9043);
|
||||
|
||||
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
|
||||
|
||||
assertEquals("https://localhost:9043", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromRequestUri() {
|
||||
request.setRequestURI("/mvc-showcase/data/param");
|
||||
request.setQueryString("foo=123");
|
||||
String result = ServletUriComponentsBuilder.fromRequestUri(request).build().toUriString();
|
||||
|
||||
assertEquals("http://localhost/mvc-showcase/data/param", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromContextPath() {
|
||||
request.setRequestURI("/mvc-showcase/data/param");
|
||||
request.setQueryString("foo=123");
|
||||
|
||||
String result = ServletUriComponentsBuilder.fromContextPath(request).build().toUriString();
|
||||
|
||||
assertEquals("http://localhost/mvc-showcase", result);
|
||||
|
|
@ -92,7 +97,6 @@ public class ServletUriComponentsBuilderTests {
|
|||
request.setRequestURI("/mvc-showcase/app/simple");
|
||||
request.setServletPath("/app");
|
||||
request.setQueryString("foo=123");
|
||||
|
||||
String result = ServletUriComponentsBuilder.fromServletMapping(request).build().toUriString();
|
||||
|
||||
assertEquals("http://localhost/mvc-showcase/app", result);
|
||||
|
|
@ -103,7 +107,6 @@ public class ServletUriComponentsBuilderTests {
|
|||
request.setRequestURI("/mvc-showcase/data/param");
|
||||
request.setQueryString("foo=123");
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request));
|
||||
|
||||
try {
|
||||
String result = ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue