Skip to content

Commit 3ed24bc

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

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,9 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
4747
protected ServletUriComponentsBuilder() {
4848
}
4949

50+
51+
// Factory methods based on a HttpServletRequest
52+
5053
/**
5154
* Prepare a builder from the host, port, scheme, and context path of
5255
* an HttpServletRequest.
@@ -60,9 +63,7 @@ public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest req
6063

6164
/**
6265
* Prepare a builder from the host, port, scheme, context path, and
63-
* servlet mapping of an HttpServletRequest. The results may vary depending
64-
* on the type of servlet mapping used.
65-
*
66+
* servlet mapping of the given HttpServletRequest.
6667
* <p>If the servlet is mapped by name, e.g. {@code "/main/*"}, the path
6768
* will end with "/main". If the servlet is mapped otherwise, e.g.
6869
* {@code "/"} or {@code "*.do"}, the result will be the same as
@@ -114,14 +115,17 @@ public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request
114115
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
115116
builder.scheme(scheme);
116117
builder.host(host);
117-
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
118+
if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) {
118119
builder.port(port);
119120
}
120121
builder.path(request.getRequestURI());
121122
builder.query(request.getQueryString());
122123
return builder;
123124
}
124125

126+
127+
// Alternative methods relying on RequestContextHolder to find the request
128+
125129
/**
126130
* Same as {@link #fromContextPath(HttpServletRequest)} except the
127131
* request is obtained through {@link RequestContextHolder}.
@@ -155,7 +159,7 @@ public static ServletUriComponentsBuilder fromCurrentRequest() {
155159
}
156160

157161
/**
158-
* Obtain the request through {@link RequestContextHolder}.
162+
* Obtain current request through {@link RequestContextHolder}.
159163
*/
160164
protected static HttpServletRequest getCurrentRequest() {
161165
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

0 commit comments

Comments
 (0)