diff --git a/spring-core/src/main/java/org/springframework/core/convert/Property.java b/spring-core/src/main/java/org/springframework/core/convert/Property.java index cd8fdff39b6..398a66bb633 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/Property.java +++ b/spring-core/src/main/java/org/springframework/core/convert/Property.java @@ -37,8 +37,8 @@ import org.springframework.util.StringUtils; * is not available in a number of environments (e.g. Android, Java ME), so this is * desirable for portability of Spring's core conversion facility. * - *

Used to build a TypeDescriptor from a property location. - * The built TypeDescriptor can then be used to convert from/to the property type. + *

Used to build a {@link TypeDescriptor} from a property location. The built + * {@code TypeDescriptor} can then be used to convert from/to the property type. * * @author Keith Donald * @author Phillip Webb diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 1bc5cc91229..2df8e9c645c 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -748,8 +748,8 @@ public abstract class StringUtils { } /** - * Parse the given {@code localeString} value into a {@link Locale}. - *

This is the inverse operation of {@link Locale#toString Locale's toString}. + * Parse the given {@code String} value into a {@link Locale}, accepting + * the {@link Locale#toString} format as well as BCP 47 language tags. * @param localeValue the locale value: following either {@code Locale's} * {@code toString()} format ("en", "en_UK", etc), also accepting spaces as * separators (as an alternative to underscores), or BCP 47 (e.g. "en-UK") @@ -770,7 +770,7 @@ public abstract class StringUtils { } /** - * Parse the given {@code localeString} value into a {@link Locale}. + * Parse the given {@code String} representation into a {@link Locale}. *

This is the inverse operation of {@link Locale#toString Locale's toString}. * @param localeString the locale {@code String}: following {@code Locale's} * {@code toString()} format ("en", "en_UK", etc), also accepting spaces as diff --git a/spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java b/spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java index 1f66c194872..073409a0d67 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java +++ b/spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java @@ -60,12 +60,13 @@ public abstract class CorsUtils { if (origin == null) { return true; } - UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpRequest(request); - UriComponents actualUrl = urlBuilder.build(); + + UriComponents actualUrl = UriComponentsBuilder.fromHttpRequest(request).build(); String actualHost = actualUrl.getHost(); int actualPort = getPort(actualUrl.getScheme(), actualUrl.getPort()); Assert.notNull(actualHost, "Actual request host must not be null"); Assert.isTrue(actualPort != -1, "Actual request port must not be undefined"); + UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build(); return (actualHost.equals(originUrl.getHost()) && actualPort == getPort(originUrl.getScheme(), originUrl.getPort())); diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index 1990d1e0e93..7489f7e06cc 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -760,7 +760,7 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable { } } - if ((this.scheme != null) && ((this.scheme.equals("http") && "80".equals(this.port)) || + if (this.scheme != null && ((this.scheme.equals("http") && "80".equals(this.port)) || (this.scheme.equals("https") && "443".equals(this.port)))) { port(null); } diff --git a/spring-web/src/main/java/org/springframework/web/util/WebUtils.java b/spring-web/src/main/java/org/springframework/web/util/WebUtils.java index 9a9ffd30eac..3a390245fa1 100644 --- a/spring-web/src/main/java/org/springframework/web/util/WebUtils.java +++ b/spring-web/src/main/java/org/springframework/web/util/WebUtils.java @@ -711,6 +711,7 @@ public abstract class WebUtils { if (origin == null) { return true; } + String scheme; String host; int port; @@ -720,12 +721,9 @@ public abstract class WebUtils { scheme = servletRequest.getScheme(); host = servletRequest.getServerName(); port = servletRequest.getServerPort(); - - if(containsForwardedHeaders(servletRequest)) { + if (containsForwardedHeaders(servletRequest)) { UriComponents actualUrl = new UriComponentsBuilder() - .scheme(scheme) - .host(host) - .port(port) + .scheme(scheme).host(host).port(port) .adaptFromForwardedHeaders(headers) .build(); scheme = actualUrl.getScheme(); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index cac46eefdfc..41a3f7e96a7 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -502,7 +502,7 @@ public class UriComponentsBuilderTests { } @Test - public void path() throws URISyntaxException { + public void path() { UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar"); UriComponents result = builder.build(); @@ -511,7 +511,7 @@ public class UriComponentsBuilderTests { } @Test - public void pathSegments() throws URISyntaxException { + public void pathSegments() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.pathSegment("foo").pathSegment("bar").build(); @@ -565,7 +565,7 @@ public class UriComponentsBuilderTests { } @Test // SPR-12398 - public void pathWithDuplicateSlashes() throws URISyntaxException { + public void pathWithDuplicateSlashes() { UriComponents uriComponents = UriComponentsBuilder.fromPath("/foo/////////bar").build(); assertEquals("/foo/bar", uriComponents.getPath()); } @@ -601,7 +601,7 @@ public class UriComponentsBuilderTests { } @Test - public void queryParams() throws URISyntaxException { + public void queryParams() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.queryParam("baz", "qux", 42).build(); @@ -613,7 +613,7 @@ public class UriComponentsBuilderTests { } @Test - public void emptyQueryParam() throws URISyntaxException { + public void emptyQueryParam() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.queryParam("baz").build(); @@ -736,7 +736,7 @@ public class UriComponentsBuilderTests { } @Test - public void testClone() throws URISyntaxException { + public void testClone() { UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance(); builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1").fragment("f1"); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java index 91e402cc510..23f45e603b8 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,19 +16,15 @@ package org.springframework.web.servlet.support; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - import org.junit.Before; import org.junit.Test; -import org.springframework.http.HttpRequest; -import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; + +import static org.junit.Assert.*; /** * Unit tests for @@ -94,10 +90,7 @@ public class ServletUriComponentsBuilderTests { request.addHeader("X-Forwarded-Proto", "https"); request.addHeader("X-Forwarded-Host", "84.198.58.199"); request.addHeader("X-Forwarded-Port", "443"); - - HttpRequest httpRequest = new ServletServerHttpRequest(request); - UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); - + UriComponents result = ServletUriComponentsBuilder.fromRequest(request).build(); assertEquals("https://84.198.58.199/mvc-showcase", result.toString()); } @@ -114,7 +107,6 @@ public class ServletUriComponentsBuilderTests { this.request.setRequestURI("/bar"); this.request.addHeader("X-Forwarded-Prefix", "/foo"); UriComponents result = ServletUriComponentsBuilder.fromRequest(this.request).build(); - assertEquals("http://localhost/foo/bar", result.toUriString()); } @@ -123,7 +115,6 @@ public class ServletUriComponentsBuilderTests { this.request.setRequestURI("/bar"); this.request.addHeader("X-Forwarded-Prefix", "/foo/"); UriComponents result = ServletUriComponentsBuilder.fromRequest(this.request).build(); - assertEquals("http://localhost/foo/bar", result.toUriString()); }