|
15 | 15 | */
|
16 | 16 | package org.springframework.hateoas.mvc;
|
17 | 17 |
|
18 |
| -import static org.springframework.util.StringUtils.*; |
19 |
| - |
20 | 18 | import lombok.RequiredArgsConstructor;
|
21 | 19 | import lombok.experimental.Delegate;
|
22 | 20 |
|
| 21 | +import static org.springframework.util.StringUtils.hasText; |
| 22 | + |
23 | 23 | import java.lang.reflect.Method;
|
24 | 24 | import java.net.URI;
|
25 | 25 | import java.util.Map;
|
|
32 | 32 | import org.springframework.hateoas.core.DummyInvocationUtils;
|
33 | 33 | import org.springframework.hateoas.core.LinkBuilderSupport;
|
34 | 34 | import org.springframework.hateoas.core.MappingDiscoverer;
|
| 35 | +import org.springframework.http.server.ServletServerHttpRequest; |
35 | 36 | import org.springframework.util.Assert;
|
36 | 37 | import org.springframework.util.ConcurrentReferenceHashMap;
|
37 | 38 | import org.springframework.web.bind.annotation.RequestMapping;
|
|
52 | 53 | * @author Greg Turnquist
|
53 | 54 | * @author Kevin Conaway
|
54 | 55 | * @author Andrew Naydyonock
|
| 56 | + * @author Oliver Trosien |
55 | 57 | */
|
56 | 58 | public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
|
57 | 59 |
|
@@ -253,56 +255,29 @@ public String toString() {
|
253 | 255 | }
|
254 | 256 |
|
255 | 257 | /**
|
256 |
| - * Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with the host tweaked in case the |
257 |
| - * request contains an {@code X-Forwarded-Host} header and the scheme tweaked in case the request contains an |
258 |
| - * {@code X-Forwarded-Ssl} header |
| 258 | + * Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with |
| 259 | + * scheme tweaked in case the request contains an {@code X-Forwarded-Ssl} header, which is not (yet) |
| 260 | + * supported by the underlying {@link UriComponentsBuilder}. |
259 | 261 | *
|
260 | 262 | * @return
|
261 | 263 | */
|
262 | 264 | static UriComponentsBuilder getBuilder() {
|
263 |
| - |
264 |
| - HttpServletRequest request = getCurrentRequest(); |
265 |
| - ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request); |
266 |
| - |
267 |
| - ForwardedHeader forwarded = ForwardedHeader.of(request.getHeader(ForwardedHeader.NAME)); |
268 |
| - String proto = hasText(forwarded.getProto()) ? forwarded.getProto() : request.getHeader("X-Forwarded-Proto"); |
269 |
| - String forwardedSsl = request.getHeader("X-Forwarded-Ssl"); |
270 |
| - |
271 |
| - if (hasText(proto)) { |
272 |
| - builder.scheme(proto); |
273 |
| - } else if (hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) { |
274 |
| - builder.scheme("https"); |
275 |
| - } |
276 |
| - |
277 |
| - String host = forwarded.getHost(); |
278 |
| - host = hasText(host) ? host : request.getHeader("X-Forwarded-Host"); |
279 |
| - |
280 |
| - if (!hasText(host)) { |
281 |
| - return builder; |
282 |
| - } |
283 |
| - |
284 |
| - String[] hosts = commaDelimitedListToStringArray(host); |
285 |
| - String hostToUse = hosts[0]; |
286 |
| - |
287 |
| - if (hostToUse.contains(":")) { |
288 |
| - |
289 |
| - String[] hostAndPort = split(hostToUse, ":"); |
290 |
| - |
291 |
| - builder.host(hostAndPort[0]); |
292 |
| - builder.port(Integer.parseInt(hostAndPort[1])); |
293 |
| - |
294 |
| - } else { |
295 |
| - builder.host(hostToUse); |
296 |
| - builder.port(-1); // reset port if it was forwarded from default port |
297 |
| - } |
298 |
| - |
299 |
| - String port = request.getHeader("X-Forwarded-Port"); |
300 |
| - |
301 |
| - if (hasText(port)) { |
302 |
| - builder.port(Integer.parseInt(port)); |
303 |
| - } |
304 |
| - |
305 |
| - return builder; |
| 265 | + |
| 266 | + HttpServletRequest request = getCurrentRequest(); |
| 267 | + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request)); |
| 268 | + |
| 269 | + // special case handling for X-Forwarded-Ssl: |
| 270 | + // apply it, but only if X-Forwarded-Proto is unset. |
| 271 | + |
| 272 | + String forwardedSsl = request.getHeader("X-Forwarded-Ssl"); |
| 273 | + ForwardedHeader forwarded = ForwardedHeader.of(request.getHeader(ForwardedHeader.NAME)); |
| 274 | + String proto = hasText(forwarded.getProto()) ? forwarded.getProto() : request.getHeader("X-Forwarded-Proto"); |
| 275 | + |
| 276 | + if (!hasText(proto) && hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on")) { |
| 277 | + builder.scheme("https"); |
| 278 | + } |
| 279 | + |
| 280 | + return builder; |
306 | 281 | }
|
307 | 282 |
|
308 | 283 | /**
|
|
0 commit comments