Skip to content

Commit 2a240b0

Browse files
committed
ServletUriComponentsBuilder.java avoids NPE on scheme check
Issue: SPR-12723 (cherry picked from commit 61cc3b5)
1 parent 3b8d878 commit 2a240b0

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ protected ServletUriComponentsBuilder(ServletUriComponentsBuilder other) {
6363
this.originalPath = other.originalPath;
6464
}
6565

66+
67+
// Factory methods based on a HttpServletRequest
68+
6669
/**
6770
* Prepare a builder from the host, port, scheme, and context path of the
6871
* given HttpServletRequest.
@@ -76,7 +79,6 @@ public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest req
7679
/**
7780
* Prepare a builder from the host, port, scheme, context path, and
7881
* servlet mapping of the given HttpServletRequest.
79-
*
8082
* <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path
8183
* will end with "/main". If the servlet is mapped otherwise, e.g.
8284
* {@code "/"} or {@code "*.do"}, the result will be the same as
@@ -124,17 +126,12 @@ private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest re
124126
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
125127
builder.scheme(scheme);
126128
builder.host(host);
127-
if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
129+
if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
128130
builder.port(port);
129131
}
130132
return builder;
131133
}
132134

133-
private void initPath(String path) {
134-
this.originalPath = path;
135-
replacePath(path);
136-
}
137-
138135
private static String prependForwardedPrefix(HttpServletRequest request, String path) {
139136
String prefix = request.getHeader("X-Forwarded-Prefix");
140137
if (StringUtils.hasText(prefix)) {
@@ -178,9 +175,8 @@ public static ServletUriComponentsBuilder fromCurrentRequest() {
178175
return fromRequest(getCurrentRequest());
179176
}
180177

181-
182178
/**
183-
* Get the request through {@link RequestContextHolder}.
179+
* Obtain current request through {@link RequestContextHolder}.
184180
*/
185181
protected static HttpServletRequest getCurrentRequest() {
186182
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
@@ -191,12 +187,17 @@ protected static HttpServletRequest getCurrentRequest() {
191187
return servletRequest;
192188
}
193189

190+
191+
private void initPath(String path) {
192+
this.originalPath = path;
193+
replacePath(path);
194+
}
195+
194196
/**
195197
* Remove any path extension from the {@link HttpServletRequest#getRequestURI()
196198
* requestURI}. This method must be invoked before any calls to {@link #path(String)}
197199
* or {@link #pathSegment(String...)}.
198200
* <pre>
199-
*
200201
* GET http://foo.com/rest/books/6.json
201202
*
202203
* ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request);

0 commit comments

Comments
 (0)