From bcda243f637391d9d7c243b847853a3624baba49 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 29 Mar 2018 17:34:28 +0200 Subject: [PATCH] Polishing --- .../aop/config/AopNamespaceUtils.java | 6 +- .../support/CronSequenceGenerator.java | 2 +- .../core/convert/Property.java | 6 +- .../org/springframework/util/StringUtils.java | 8 +-- .../spel/support/ReflectionHelper.java | 2 +- .../web/util/UriComponentsBuilder.java | 63 ++++++++++--------- .../web/util/UriComponentsBuilderTests.java | 10 +-- .../ViewControllerBeanDefinitionParser.java | 2 +- .../resource/PathResourceResolver.java | 11 ++-- .../ServletUriComponentsBuilderTests.java | 17 ++--- 10 files changed, 61 insertions(+), 66 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceUtils.java b/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceUtils.java index 8298f9e099..bf02fb4045 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/AopNamespaceUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2017 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. @@ -81,11 +81,11 @@ public abstract class AopNamespaceUtils { private static void useClassProxyingIfNecessary(BeanDefinitionRegistry registry, Element sourceElement) { if (sourceElement != null) { - boolean proxyTargetClass = Boolean.valueOf(sourceElement.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE)); + boolean proxyTargetClass = Boolean.parseBoolean(sourceElement.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE)); if (proxyTargetClass) { AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry); } - boolean exposeProxy = Boolean.valueOf(sourceElement.getAttribute(EXPOSE_PROXY_ATTRIBUTE)); + boolean exposeProxy = Boolean.parseBoolean(sourceElement.getAttribute(EXPOSE_PROXY_ATTRIBUTE)); if (exposeProxy) { AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry); } diff --git a/spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java b/spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java index 64cdf5aa6b..ff592c415c 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java +++ b/spring-context/src/main/java/org/springframework/scheduling/support/CronSequenceGenerator.java @@ -350,7 +350,7 @@ public class CronSequenceGenerator { if (!split[0].contains("-")) { range[1] = max - 1; } - int delta = Integer.valueOf(split[1]); + int delta = Integer.parseInt(split[1]); if (delta <= 0) { throw new IllegalArgumentException("Incrementer delta must be 1 or higher: '" + field + "' in expression \"" + this.expression + "\""); 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 9c6db43c76..c7c1bddcc6 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 @@ -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. @@ -36,8 +36,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 b2ed59a890..8dcef81e92 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -694,11 +694,11 @@ 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 accepts spaces as separators, as an alternative to underscores + * @param localeString the locale {@code String}: following {@code Locale's} + * {@code toString()} format ("en", "en_UK", etc), also accepting spaces as + * separators (as an alternative to underscores) * @return a corresponding {@code Locale} instance, or {@code null} if none * @throws IllegalArgumentException in case of an invalid locale specification */ diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java index f40a1883e8..cde4bd4e26 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java @@ -368,7 +368,7 @@ public class ReflectionHelper { } - static enum ArgumentsMatchKind { + enum ArgumentsMatchKind { /** An exact match is where the parameter types exactly match what the method/constructor is expecting */ EXACT, 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 5e63576023..388aee0d3f 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-2016 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. @@ -240,8 +240,8 @@ public class UriComponentsBuilder implements Cloneable { * be parsed unambiguously. Such values should be substituted for URI * variables to enable correct parsing: *

-	 * String uriString = "/hotels/42?filter={value}";
-	 * UriComponentsBuilder.fromUriString(uriString).buildAndExpand("hot&cold");
+	 * String urlString = "https://example.com/hotels/42?filter={value}";
+	 * UriComponentsBuilder.fromHttpUrl(urlString).buildAndExpand("hot&cold");
 	 * 
* @param httpUrl the source URI * @return the URI components of the URI @@ -373,10 +373,10 @@ public class UriComponentsBuilder implements Cloneable { } - // URI components methods + // Instance methods /** - * Initialize all components of this URI builder with the components of the given URI. + * Initialize components of this builder from components of the given URI. * @param uri the URI * @return this UriComponentsBuilder */ @@ -412,6 +412,21 @@ public class UriComponentsBuilder implements Cloneable { return this; } + /** + * Set or append individual URI components of this builder from the values + * of the given {@link UriComponents} instance. + *

For the semantics of each component (i.e. set vs append) check the + * builder methods on this class. For example {@link #host(String)} sets + * while {@link #path(String)} appends. + * @param uriComponents the UriComponents to copy from + * @return this UriComponentsBuilder + */ + public UriComponentsBuilder uriComponents(UriComponents uriComponents) { + Assert.notNull(uriComponents, "UriComponents must not be null"); + uriComponents.copyToUriComponentsBuilder(this); + return this; + } + /** * Set the URI scheme. The given scheme may contain URI template variables, * and may also be {@code null} to clear the scheme of this builder. @@ -423,17 +438,6 @@ public class UriComponentsBuilder implements Cloneable { return this; } - /** - * Set all components of this URI builder from the given {@link UriComponents}. - * @param uriComponents the UriComponents instance - * @return this UriComponentsBuilder - */ - public UriComponentsBuilder uriComponents(UriComponents uriComponents) { - Assert.notNull(uriComponents, "UriComponents must not be null"); - uriComponents.copyToUriComponentsBuilder(this); - return this; - } - /** * Set the URI scheme-specific-part. When invoked, this method overwrites * {@linkplain #userInfo(String) user-info}, {@linkplain #host(String) host}, @@ -509,17 +513,6 @@ public class UriComponentsBuilder implements Cloneable { return this; } - /** - * Set the path of this builder overriding all existing path and path segment values. - * @param path the URI path; a {@code null} value results in an empty path. - * @return this UriComponentsBuilder - */ - public UriComponentsBuilder replacePath(String path) { - this.pathBuilder = new CompositePathComponentBuilder(path); - resetSchemeSpecificPart(); - return this; - } - /** * Append path segments to the existing path. Each path segment may contain * URI template variables and should not contain any slashes. @@ -533,6 +526,17 @@ public class UriComponentsBuilder implements Cloneable { return this; } + /** + * Set the path of this builder overriding all existing path and path segment values. + * @param path the URI path (a {@code null} value results in an empty path) + * @return this UriComponentsBuilder + */ + public UriComponentsBuilder replacePath(String path) { + this.pathBuilder = new CompositePathComponentBuilder(path); + resetSchemeSpecificPart(); + return this; + } + /** * Append the given query to the existing query of this builder. * The given query may contain URI template variables. @@ -582,7 +586,7 @@ public class UriComponentsBuilder implements Cloneable { * Append the given query parameter to the existing query parameters. The * given name or any of the values may contain URI template variables. If no * values are given, the resulting URI will contain the query parameter name - * only (i.e. {@code ?foo} instead of {@code ?foo=bar}. + * only (i.e. {@code ?foo} instead of {@code ?foo=bar}). * @param name the query parameter name * @param values the query parameter values * @return this UriComponentsBuilder @@ -704,7 +708,7 @@ public class UriComponentsBuilder implements 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); } @@ -740,7 +744,6 @@ public class UriComponentsBuilder implements Cloneable { /** * Public declaration of Object's {@code clone()} method. * Delegates to {@link #cloneBuilder()}. - * @see Object#clone() */ @Override public Object clone() { 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 c8bfa07ccd..85fdff1205 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 @@ -185,7 +185,7 @@ public class UriComponentsBuilderTests { } @Test // SPR-9832 - public void fromUriStringQueryParamWithReservedCharInValue() throws URISyntaxException { + public void fromUriStringQueryParamWithReservedCharInValue() { String uri = "http://www.google.com/ig/calculator?q=1USD=?EUR"; UriComponents result = UriComponentsBuilder.fromUriString(uri).build(); @@ -494,7 +494,7 @@ public class UriComponentsBuilderTests { } @Test - public void path() throws URISyntaxException { + public void path() { UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar"); UriComponents result = builder.build(); @@ -503,7 +503,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(); @@ -593,7 +593,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(); @@ -605,7 +605,7 @@ public class UriComponentsBuilderTests { } @Test - public void emptyQueryParam() throws URISyntaxException { + public void emptyQueryParam() { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.queryParam("baz").build(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java index e514caf735..6bfcb11df6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java @@ -75,7 +75,7 @@ class ViewControllerBeanDefinitionParser implements BeanDefinitionParser { HttpStatus statusCode = null; if (element.hasAttribute("status-code")) { - int statusValue = Integer.valueOf(element.getAttribute("status-code")); + int statusValue = Integer.parseInt(element.getAttribute("status-code")); statusCode = HttpStatus.valueOf(statusValue); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java index 63baaf1baa..c0a1ffb277 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java @@ -183,10 +183,11 @@ public class PathResourceResolver extends AbstractResourceResolver { return resource; } else if (logger.isTraceEnabled()) { - logger.trace("Resource path=\"" + resourcePath + "\" was successfully resolved " + - "but resource=\"" + resource.getURL() + "\" is neither under the " + - "current location=\"" + location.getURL() + "\" nor under any of the " + - "allowed locations=" + Arrays.asList(getAllowedLocations())); + Resource[] allowedLocations = getAllowedLocations(); + logger.trace("Resource path \"" + resourcePath + "\" was successfully resolved " + + "but resource \"" + resource.getURL() + "\" is neither under the " + + "current location \"" + location.getURL() + "\" nor under any of the " + + "allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]")); } } return null; @@ -287,7 +288,7 @@ public class PathResourceResolver extends AbstractResourceResolver { String decodedPath = URLDecoder.decode(resourcePath, "UTF-8"); if (decodedPath.contains("../") || decodedPath.contains("..\\")) { if (logger.isTraceEnabled()) { - logger.trace("Ignoring invalid resource path with escape sequences [" + resourcePath + "]"); + logger.trace("Resolved resource path contains encoded \"../\" or \"..\\\": " + resourcePath); } return true; } 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 91e402cc51..23f45e603b 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()); }