ServletUriComponentsBuilder.java avoids NPE on scheme check
Issue: SPR-12723
This commit is contained in:
parent
1273c90a64
commit
61cc3b5bff
|
@ -63,6 +63,9 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
||||||
this.originalPath = other.originalPath;
|
this.originalPath = other.originalPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Factory methods based on a HttpServletRequest
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a builder from the host, port, scheme, and context path of the
|
* Prepare a builder from the host, port, scheme, and context path of the
|
||||||
* given HttpServletRequest.
|
* given HttpServletRequest.
|
||||||
|
@ -76,13 +79,12 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
||||||
/**
|
/**
|
||||||
* Prepare a builder from the host, port, scheme, context path, and
|
* Prepare a builder from the host, port, scheme, context path, and
|
||||||
* servlet mapping of the given HttpServletRequest.
|
* servlet mapping of the given HttpServletRequest.
|
||||||
*
|
|
||||||
* <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path
|
* <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.
|
* will end with "/main". If the servlet is mapped otherwise, e.g.
|
||||||
* {@code "/"} or {@code "*.do"}, the result will be the same as
|
* {@code "/"} or {@code "*.do"}, the result will be the same as
|
||||||
* if calling {@link #fromContextPath(HttpServletRequest)}.
|
* if calling {@link #fromContextPath(HttpServletRequest)}.
|
||||||
*/
|
*/
|
||||||
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) {
|
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) {
|
||||||
ServletUriComponentsBuilder builder = fromContextPath(request);
|
ServletUriComponentsBuilder builder = fromContextPath(request);
|
||||||
if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) {
|
if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) {
|
||||||
builder.path(request.getServletPath());
|
builder.path(request.getServletPath());
|
||||||
|
@ -124,17 +126,12 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
||||||
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
|
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
|
||||||
builder.scheme(scheme);
|
builder.scheme(scheme);
|
||||||
builder.host(host);
|
builder.host(host);
|
||||||
if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
|
if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
|
||||||
builder.port(port);
|
builder.port(port);
|
||||||
}
|
}
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initPath(String path) {
|
|
||||||
this.originalPath = path;
|
|
||||||
replacePath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String prependForwardedPrefix(HttpServletRequest request, String path) {
|
private static String prependForwardedPrefix(HttpServletRequest request, String path) {
|
||||||
String prefix = request.getHeader("X-Forwarded-Prefix");
|
String prefix = request.getHeader("X-Forwarded-Prefix");
|
||||||
if (StringUtils.hasText(prefix)) {
|
if (StringUtils.hasText(prefix)) {
|
||||||
|
@ -178,9 +175,8 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
||||||
return fromRequest(getCurrentRequest());
|
return fromRequest(getCurrentRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the request through {@link RequestContextHolder}.
|
* Obtain current request through {@link RequestContextHolder}.
|
||||||
*/
|
*/
|
||||||
protected static HttpServletRequest getCurrentRequest() {
|
protected static HttpServletRequest getCurrentRequest() {
|
||||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||||
|
@ -191,12 +187,17 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
||||||
return servletRequest;
|
return servletRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void initPath(String path) {
|
||||||
|
this.originalPath = path;
|
||||||
|
replacePath(path);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove any path extension from the {@link HttpServletRequest#getRequestURI()
|
* Remove any path extension from the {@link HttpServletRequest#getRequestURI()
|
||||||
* requestURI}. This method must be invoked before any calls to {@link #path(String)}
|
* requestURI}. This method must be invoked before any calls to {@link #path(String)}
|
||||||
* or {@link #pathSegment(String...)}.
|
* or {@link #pathSegment(String...)}.
|
||||||
* <pre>
|
* <pre>
|
||||||
*
|
|
||||||
* GET http://foo.com/rest/books/6.json
|
* GET http://foo.com/rest/books/6.json
|
||||||
*
|
*
|
||||||
* ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request);
|
* ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request);
|
||||||
|
|
Loading…
Reference in New Issue