public class ServletUriComponentsBuilder extends UriComponentsBuilder
Note: This class uses values from "Forwarded"
(RFC 7239),
"X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
if present, in order to reflect the client-originated protocol and address.
Consider using the ForwardedHeaderFilter
in order to choose from a
central place whether to extract and use, or to discard such headers.
See the Spring Framework reference for more on this filter.
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
originalPath |
Modifier | Constructor and Description |
---|---|
protected |
ServletUriComponentsBuilder()
Default constructor.
|
protected |
ServletUriComponentsBuilder(ServletUriComponentsBuilder other)
Create a deep copy of the given ServletUriComponentsBuilder.
|
Modifier and Type | Method and Description |
---|---|
ServletUriComponentsBuilder |
cloneBuilder()
Clone this
UriComponentsBuilder . |
static ServletUriComponentsBuilder |
fromContextPath(HttpServletRequest request)
Prepare a builder from the host, port, scheme, and context path of the
given HttpServletRequest.
|
static ServletUriComponentsBuilder |
fromCurrentContextPath()
Same as
fromContextPath(HttpServletRequest) except the
request is obtained through RequestContextHolder . |
static ServletUriComponentsBuilder |
fromCurrentRequest()
Same as
fromRequest(HttpServletRequest) except the
request is obtained through RequestContextHolder . |
static ServletUriComponentsBuilder |
fromCurrentRequestUri()
Same as
fromRequestUri(HttpServletRequest) except the
request is obtained through RequestContextHolder . |
static ServletUriComponentsBuilder |
fromCurrentServletMapping()
Same as
fromServletMapping(HttpServletRequest) except the
request is obtained through RequestContextHolder . |
static ServletUriComponentsBuilder |
fromRequest(HttpServletRequest request)
Prepare a builder by copying the scheme, host, port, path, and
query string of an HttpServletRequest.
|
static ServletUriComponentsBuilder |
fromRequestUri(HttpServletRequest request)
Prepare a builder from the host, port, scheme, and path (but not the query)
of the HttpServletRequest.
|
static ServletUriComponentsBuilder |
fromServletMapping(HttpServletRequest request)
Prepare a builder from the host, port, scheme, context path, and
servlet mapping of the given HttpServletRequest.
|
protected static HttpServletRequest |
getCurrentRequest()
Obtain current request through
RequestContextHolder . |
private static java.lang.String |
getForwardedPrefix(HttpServletRequest request) |
private static java.lang.String |
getRequestUriWithForwardedPrefix(HttpServletRequest request) |
private static ServletUriComponentsBuilder |
initFromRequest(HttpServletRequest request)
Initialize a builder with a scheme, host,and port (but not path and query).
|
private void |
initPath(java.lang.String path) |
java.lang.String |
removePathExtension()
Remove any path extension from the
requestURI . |
build, build, build, build, buildAndExpand, buildAndExpand, clone, encode, encode, fragment, fromHttpRequest, fromHttpUrl, fromOriginHeader, fromPath, fromUri, fromUriString, host, newInstance, path, pathSegment, port, port, query, queryParam, queryParams, replacePath, replaceQuery, replaceQueryParam, replaceQueryParams, scheme, schemeSpecificPart, toUriString, uri, uriComponents, uriVariables, userInfo
@Nullable private java.lang.String originalPath
protected ServletUriComponentsBuilder()
protected ServletUriComponentsBuilder(ServletUriComponentsBuilder other)
other
- the other builder to copy frompublic static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request)
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value
of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFiller
.
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request)
If the servlet is mapped by name, e.g. "/main/*"
, the path
will end with "/main". If the servlet is mapped otherwise, e.g.
"/"
or "*.do"
, the result will be the same as
if calling fromContextPath(HttpServletRequest)
.
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value
of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFiller
.
public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request)
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value
of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFiller
.
public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request)
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value
of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFiller
.
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request)
@Nullable private static java.lang.String getForwardedPrefix(HttpServletRequest request)
private static java.lang.String getRequestUriWithForwardedPrefix(HttpServletRequest request)
public static ServletUriComponentsBuilder fromCurrentContextPath()
fromContextPath(HttpServletRequest)
except the
request is obtained through RequestContextHolder
.
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value
of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFiller
.
public static ServletUriComponentsBuilder fromCurrentServletMapping()
fromServletMapping(HttpServletRequest)
except the
request is obtained through RequestContextHolder
.
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value
of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFiller
.
public static ServletUriComponentsBuilder fromCurrentRequestUri()
fromRequestUri(HttpServletRequest)
except the
request is obtained through RequestContextHolder
.
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value
of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFiller
.
public static ServletUriComponentsBuilder fromCurrentRequest()
fromRequest(HttpServletRequest)
except the
request is obtained through RequestContextHolder
.
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value
of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFiller
.
protected static HttpServletRequest getCurrentRequest()
RequestContextHolder
.private void initPath(java.lang.String path)
@Nullable public java.lang.String removePathExtension()
requestURI
. This method must be invoked before any calls to UriComponentsBuilder.path(String)
or UriComponentsBuilder.pathSegment(String...)
.
GET http://foo.com/rest/books/6.json ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request); String ext = builder.removePathExtension(); String uri = builder.path("/pages/1.{ext}").buildAndExpand(ext).toUriString(); assertEquals("http://foo.com/rest/books/6/pages/1.json", result);
null
public ServletUriComponentsBuilder cloneBuilder()
UriComponentsBuilder
UriComponentsBuilder
.cloneBuilder
in class UriComponentsBuilder
UriComponentsBuilder
object