From f8b729aa5fd6b6d571e7b6413b93cf73d5c9f9c0 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 17 Sep 2014 01:51:23 +0200 Subject: [PATCH] Polishing --- .../AutowiredAnnotationBeanPostProcessor.java | 4 +- ...wiredAnnotationBeanPostProcessorTests.java | 2 +- .../support/GenericConversionService.java | 5 +- .../org/springframework/util/SocketUtils.java | 2 - .../GenericConversionServiceTests.java | 29 ++++++++- .../NamedParameterJdbcTemplate.java | 2 +- .../messaging/handler/annotation/Header.java | 5 +- .../HeaderMethodArgumentResolverTests.java | 2 - .../client/AbstractClientHttpRequest.java | 8 +-- .../SimpleClientHttpRequestFactory.java | 33 +++++------ .../http/client/SimpleClientHttpResponse.java | 4 +- ...ractCookieValueMethodArgumentResolver.java | 12 ++-- ...tractNamedValueMethodArgumentResolver.java | 2 +- ...ExpressionValueMethodArgumentResolver.java | 8 ++- ...equestHeaderMapMethodArgumentResolver.java | 7 ++- .../RequestHeaderMethodArgumentResolver.java | 16 ++--- .../RequestParamMethodArgumentResolver.java | 23 ++++---- .../web/util/OpaqueUriComponents.java | 10 ++-- .../web/util/UriComponents.java | 4 +- .../web/util/UriComponentsBuilder.java | 59 +++++++++---------- .../springframework/web/util/UriTemplate.java | 24 ++++---- .../MatrixVariableMethodArgumentResolver.java | 9 ++- .../PathVariableMethodArgumentResolver.java | 13 ++-- ...vletCookieValueMethodArgumentResolver.java | 13 ++-- 24 files changed, 157 insertions(+), 139 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 7f67f5f1d0..b676e33449 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -241,7 +241,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean if (requiredConstructor != null) { throw new BeanCreationException(beanName, "Invalid autowire-marked constructor: " + candidate + - ". Found another constructor with 'required' Autowired annotation: " + + ". Found constructor with 'required' Autowired annotation already: " + requiredConstructor); } if (candidate.getParameterTypes().length == 0) { @@ -253,7 +253,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean if (!candidates.isEmpty()) { throw new BeanCreationException(beanName, "Invalid autowire-marked constructors: " + candidates + - ". Found another constructor with 'required' Autowired annotation: " + + ". Found constructor with 'required' Autowired annotation: " + candidate); } requiredConstructor = candidate; diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java index 3307bc944e..a5e9413c4a 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java @@ -2610,7 +2610,7 @@ public class AutowiredAnnotationBeanPostProcessorTests { } - public static class GenericInterface1Impl implements GenericInterface1{ + public static class GenericInterface1Impl implements GenericInterface1 { @Autowired private GenericInterface2 gi2; diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index d96cee5d81..613729d39d 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -200,7 +200,7 @@ public class GenericConversionService implements ConfigurableConversionService { } - // subclassing hooks + // Protected template methods /** * Template method to convert a null source. @@ -548,7 +548,7 @@ public class GenericConversionService implements ConfigurableConversionService { Class candidate = hierarchy.get(i); candidate = (array ? candidate.getComponentType() : ClassUtils.resolvePrimitiveIfNecessary(candidate)); Class superclass = candidate.getSuperclass(); - if (candidate.getSuperclass() != null && superclass != Object.class) { + if (superclass != null && superclass != Object.class) { addToClassHierarchy(i + 1, candidate.getSuperclass(), array, hierarchy, visited); } for (Class implementedInterface : candidate.getInterfaces()) { @@ -563,6 +563,7 @@ public class GenericConversionService implements ConfigurableConversionService { private void addToClassHierarchy(int index, Class type, boolean asArray, List> hierarchy, Set> visited) { + if (asArray) { type = Array.newInstance(type, 0).getClass(); } diff --git a/spring-core/src/main/java/org/springframework/util/SocketUtils.java b/spring-core/src/main/java/org/springframework/util/SocketUtils.java index 665ab99e9f..bd451250c8 100644 --- a/spring-core/src/main/java/org/springframework/util/SocketUtils.java +++ b/spring-core/src/main/java/org/springframework/util/SocketUtils.java @@ -194,7 +194,6 @@ public class SocketUtils { private static enum SocketType { TCP { - @Override protected boolean isPortAvailable(int port) { try { @@ -209,7 +208,6 @@ public class SocketUtils { }, UDP { - @Override protected boolean isPortAvailable(int port) { try { diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index a9f22acff2..9a4095d4d7 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -57,11 +57,13 @@ import static org.junit.Assert.*; * @author Keith Donald * @author Juergen Hoeller * @author Phillip Webb + * @author David Haraburda */ public class GenericConversionServiceTests { private GenericConversionService conversionService = new GenericConversionService(); + @Test public void canConvert() { assertFalse(conversionService.canConvert(String.class, Integer.class)); @@ -749,6 +751,13 @@ public class GenericConversionServiceTests { assertEquals("A", result); } + @Test + public void testSubclassOfEnumToString() throws Exception { + conversionService.addConverter(new EnumToStringConverter(conversionService)); + String result = conversionService.convert(EnumWithSubclass.FIRST, String.class); + assertEquals("FIRST", result); + } + @Test public void testEnumWithInterfaceToStringConversion() { // SPR-9692 @@ -850,6 +859,7 @@ public class GenericConversionServiceTests { @ExampleAnnotation public String annotatedString; + @Retention(RetentionPolicy.RUNTIME) public static @interface ExampleAnnotation { } @@ -892,8 +902,7 @@ public class GenericConversionServiceTests { } @Override - public Object convert(Object source, TypeDescriptor sourceType, - TypeDescriptor targetType) { + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { return null; } @@ -936,6 +945,7 @@ public class GenericConversionServiceTests { String getCode(); } + public static enum MyEnum implements MyEnumInterface { A { @@ -947,6 +957,17 @@ public class GenericConversionServiceTests { } + public enum EnumWithSubclass { + + FIRST { + @Override + public String toString() { + return "1st"; + } + } + } + + public static class MyStringToRawCollectionConverter implements Converter { @Override @@ -955,6 +976,7 @@ public class GenericConversionServiceTests { } } + public static class MyStringToGenericCollectionConverter implements Converter> { @Override @@ -963,6 +985,7 @@ public class GenericConversionServiceTests { } } + private static class MyEnumInterfaceToStringConverter implements Converter { @Override @@ -971,6 +994,7 @@ public class GenericConversionServiceTests { } } + public static class MyStringToStringCollectionConverter implements Converter> { @Override @@ -979,6 +1003,7 @@ public class GenericConversionServiceTests { } } + public static class MyStringToIntegerCollectionConverter implements Converter> { @Override diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java index ff59b02051..0baf5cab98 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java @@ -357,7 +357,7 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations @Override public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) { - ParsedSql parsedSql = this.getParsedSql(sql); + ParsedSql parsedSql = getParsedSql(sql); return NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(parsedSql, batchArgs, getJdbcOperations()); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java index 0d4f1d2637..b0367339ab 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -40,8 +40,7 @@ public @interface Header { /** * Whether the header is required. - *

- * Default is {@code true}, leading to an exception if the header missing. Switch this + *

Default is {@code true}, leading to an exception if the header missing. Switch this * to {@code false} if you prefer a {@code null} in case of the header missing. */ boolean required() default true; diff --git a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java index ba0be7f848..2b5da30d73 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java @@ -88,7 +88,6 @@ public class HeaderMethodArgumentResolverTests { @Test public void resolveArgumentNativeHeader() throws Exception { - TestMessageHeaderAccessor headers = new TestMessageHeaderAccessor(); headers.setNativeHeader("param1", "foo"); Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); @@ -98,7 +97,6 @@ public class HeaderMethodArgumentResolverTests { @Test public void resolveArgumentNativeHeaderAmbiguity() throws Exception { - TestMessageHeaderAccessor headers = new TestMessageHeaderAccessor(); headers.setHeader("param1", "foo"); headers.setNativeHeader("param1", "native-foo"); diff --git a/spring-web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java index 0b2c359549..4603fc4536 100644 --- a/spring-web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/AbstractClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -23,7 +23,8 @@ import org.springframework.http.HttpHeaders; import org.springframework.util.Assert; /** - * Abstract base for {@link ClientHttpRequest} that makes sure that headers and body are not written multiple times. + * Abstract base for {@link ClientHttpRequest} that makes sure that headers + * and body are not written multiple times. * * @author Arjen Poutsma * @since 3.0 @@ -55,8 +56,7 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest { } /** - * Asserts that this request has not been {@linkplain #execute() executed} yet. - * + * Assert that this request has not been {@linkplain #execute() executed} yet. * @throws IllegalStateException if this request has been executed */ protected void assertNotExecuted() { diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java index ff67b6e7fa..6f14c1b220 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -28,7 +28,7 @@ import org.springframework.http.HttpMethod; import org.springframework.util.Assert; /** - * {@link ClientHttpRequestFactory} implementation that uses standard J2SE facilities. + * {@link ClientHttpRequestFactory} implementation that uses standard JDK facilities. * * @author Arjen Poutsma * @author Juergen Hoeller @@ -36,8 +36,7 @@ import org.springframework.util.Assert; * @see java.net.HttpURLConnection * @see HttpComponentsClientHttpRequestFactory */ -public class SimpleClientHttpRequestFactory - implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory { +public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory { private static final int DEFAULT_CHUNK_SIZE = 4096; @@ -111,14 +110,12 @@ public class SimpleClientHttpRequestFactory } /** - * Set if the underlying URLConnection can be set to 'output streaming' mode. When - * output streaming is enabled, authentication and redirection cannot be handled - * automatically. If output streaming is disabled the - * {@link HttpURLConnection#setFixedLengthStreamingMode(int) - * setFixedLengthStreamingMode} and - * {@link HttpURLConnection#setChunkedStreamingMode(int) setChunkedStreamingMode} - * methods of the underlying connection will never be called. - *

Default is {@code true}. + * Set if the underlying URLConnection can be set to 'output streaming' mode. + * Default is {@code true}. + *

When output streaming is enabled, authentication and redirection cannot be handled automatically. + * If output streaming is disabled, the {@link HttpURLConnection#setFixedLengthStreamingMode} and + * {@link HttpURLConnection#setChunkedStreamingMode} methods of the underlying connection will never + * be called. * @param outputStreaming if output streaming is enabled */ public void setOutputStreaming(boolean outputStreaming) { @@ -126,15 +123,15 @@ public class SimpleClientHttpRequestFactory } /** - * Sets the task executor for this request factory. Setting this property is required - * for {@linkplain #createAsyncRequest(URI, HttpMethod) creating asynchronous - * request}. + * Set the task executor for this request factory. Setting this property is required + * for {@linkplain #createAsyncRequest(URI, HttpMethod) creating asynchronous requests}. * @param taskExecutor the task executor */ public void setTaskExecutor(AsyncListenableTaskExecutor taskExecutor) { this.taskExecutor = taskExecutor; } + @Override public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException { HttpURLConnection connection = openConnection(uri.toURL(), this.proxy); @@ -153,10 +150,8 @@ public class SimpleClientHttpRequestFactory * is required before calling this method. */ @Override - public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) - throws IOException { - Assert.state(this.taskExecutor != null, "Asynchronous execution requires an " + - "AsyncTaskExecutor to be set"); + public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException { + Assert.state(this.taskExecutor != null, "Asynchronous execution requires an AsyncTaskExecutor to be set"); HttpURLConnection connection = openConnection(uri.toURL(), this.proxy); prepareConnection(connection, httpMethod.name()); if (this.bufferRequestBody) { diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java index 986a9202f6..cc7e627d0a 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -24,7 +24,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.util.StringUtils; /** - * {@link ClientHttpResponse} implementation that uses standard J2SE facilities. + * {@link ClientHttpResponse} implementation that uses standard JDK facilities. * Obtained via {@link SimpleBufferingClientHttpRequest#execute()} and * {@link SimpleStreamingClientHttpRequest#execute()}. * diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractCookieValueMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractCookieValueMethodArgumentResolver.java index f49909345a..be22df6889 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractCookieValueMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractCookieValueMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -48,6 +48,7 @@ public abstract class AbstractCookieValueMethodArgumentResolver extends Abstract super(beanFactory); } + @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(CookieValue.class); @@ -60,16 +61,17 @@ public abstract class AbstractCookieValueMethodArgumentResolver extends Abstract } @Override - protected void handleMissingValue(String cookieName, MethodParameter param) throws ServletRequestBindingException { - String paramType = param.getParameterType().getName(); - throw new ServletRequestBindingException( - "Missing cookie named '" + cookieName + "' for method parameter type [" + paramType + "]"); + protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException { + throw new ServletRequestBindingException("Missing cookie '" + name + + "' for method parameter of type " + parameter.getParameterType().getSimpleName()); } + private static class CookieValueNamedValueInfo extends NamedValueInfo { private CookieValueNamedValueInfo(CookieValue annotation) { super(annotation.value(), annotation.required(), annotation.defaultValue()); } } + } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java index 0675ecea78..2e6d0d28d4 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java @@ -96,7 +96,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle } arg = handleNullValue(namedValueInfo.name, arg, paramType); } - else if ("".equals(arg) && (namedValueInfo.defaultValue != null)) { + else if ("".equals(arg) && namedValueInfo.defaultValue != null) { arg = resolveDefaultValue(namedValueInfo.defaultValue); } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ExpressionValueMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ExpressionValueMethodArgumentResolver.java index 5d20848c0b..73393f720a 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ExpressionValueMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ExpressionValueMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -48,6 +48,7 @@ public class ExpressionValueMethodArgumentResolver extends AbstractNamedValueMet super(beanFactory); } + @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(Value.class); @@ -60,8 +61,7 @@ public class ExpressionValueMethodArgumentResolver extends AbstractNamedValueMet } @Override - protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) - throws Exception { + protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception { // No name to resolve return null; } @@ -71,10 +71,12 @@ public class ExpressionValueMethodArgumentResolver extends AbstractNamedValueMet throw new UnsupportedOperationException("@Value is never required: " + parameter.getMethod()); } + private static class ExpressionValueNamedValueInfo extends NamedValueInfo { private ExpressionValueNamedValueInfo(Value annotation) { super("@Value", false, annotation.value()); } } + } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolver.java index e335b9dbf5..4d0e4d6395 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -47,8 +47,8 @@ public class RequestHeaderMapMethodArgumentResolver implements HandlerMethodArgu @Override public boolean supportsParameter(MethodParameter parameter) { - return parameter.hasParameterAnnotation(RequestHeader.class) - && Map.class.isAssignableFrom(parameter.getParameterType()); + return parameter.hasParameterAnnotation(RequestHeader.class) && + Map.class.isAssignableFrom(parameter.getParameterType()); } @Override @@ -85,4 +85,5 @@ public class RequestHeaderMapMethodArgumentResolver implements HandlerMethodArgu return result; } } + } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolver.java index 70067715ac..5ac1cc535f 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -52,10 +52,11 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho super(beanFactory); } + @Override public boolean supportsParameter(MethodParameter parameter) { - return parameter.hasParameterAnnotation(RequestHeader.class) - && !Map.class.isAssignableFrom(parameter.getParameterType()); + return parameter.hasParameterAnnotation(RequestHeader.class) && + !Map.class.isAssignableFrom(parameter.getParameterType()); } @Override @@ -76,16 +77,17 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho } @Override - protected void handleMissingValue(String headerName, MethodParameter param) throws ServletRequestBindingException { - String paramType = param.getParameterType().getName(); - throw new ServletRequestBindingException( - "Missing header '" + headerName + "' for method parameter type [" + paramType + "]"); + protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException { + throw new ServletRequestBindingException("Missing request header '" + name + + "' for method parameter of type " + parameter.getParameterType().getSimpleName()); } + private static class RequestHeaderNamedValueInfo extends NamedValueInfo { private RequestHeaderNamedValueInfo(RequestHeader annotation) { super(annotation.value(), annotation.required(), annotation.defaultValue()); } } + } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java index 8e64fd8f70..f23939a54f 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java @@ -152,19 +152,16 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod @Override protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) { - RequestParam annotation = parameter.getParameterAnnotation(RequestParam.class); - return (annotation != null) ? - new RequestParamNamedValueInfo(annotation) : - new RequestParamNamedValueInfo(); + RequestParam ann = parameter.getParameterAnnotation(RequestParam.class); + return (ann != null ? new RequestParamNamedValueInfo(ann) : new RequestParamNamedValueInfo()); } @Override protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception { - Object arg; - HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class); MultipartHttpServletRequest multipartRequest = - WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class); + WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class); + Object arg; if (MultipartFile.class.equals(parameter.getParameterType())) { assertIsMultipartRequest(servletRequest); @@ -179,7 +176,8 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod else if (isMultipartFileArray(parameter)) { assertIsMultipartRequest(servletRequest); Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?"); - arg = multipartRequest.getFiles(name).toArray(new MultipartFile[0]); + List multipartFiles = multipartRequest.getFiles(name); + arg = multipartFiles.toArray(new MultipartFile[multipartFiles.size()]); } else if ("javax.servlet.http.Part".equals(parameter.getParameterType().getName())) { assertIsMultipartRequest(servletRequest); @@ -251,8 +249,8 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod } @Override - protected void handleMissingValue(String paramName, MethodParameter parameter) throws ServletException { - throw new MissingServletRequestParameterException(paramName, parameter.getParameterType().getSimpleName()); + protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { + throw new MissingServletRequestParameterException(name, parameter.getParameterType().getSimpleName()); } @Override @@ -265,8 +263,8 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod return; } - RequestParam annot = parameter.getParameterAnnotation(RequestParam.class); - String name = StringUtils.isEmpty(annot.value()) ? parameter.getParameterName() : annot.value(); + RequestParam ann = parameter.getParameterAnnotation(RequestParam.class); + String name = (ann == null || StringUtils.isEmpty(ann.value()) ? parameter.getParameterName() : ann.value()); if (value == null) { builder.queryParam(name); @@ -298,6 +296,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod } } + private static class RequestPartResolver { public static Object resolvePart(HttpServletRequest servletRequest) throws Exception { diff --git a/spring-web/src/main/java/org/springframework/web/util/OpaqueUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/OpaqueUriComponents.java index 706ad83ae3..438ec0b1b7 100644 --- a/spring-web/src/main/java/org/springframework/web/util/OpaqueUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/OpaqueUriComponents.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -95,10 +95,10 @@ final class OpaqueUriComponents extends UriComponents { @Override protected UriComponents expandInternal(UriTemplateVariables uriVariables) { - String expandedScheme = expandUriComponent(this.getScheme(), uriVariables); - String expandedSSp = expandUriComponent(this.ssp, uriVariables); - String expandedFragment = expandUriComponent(this.getFragment(), uriVariables); - return new OpaqueUriComponents(expandedScheme, expandedSSp, expandedFragment); + String expandedScheme = expandUriComponent(getScheme(), uriVariables); + String expandedSsp = expandUriComponent(getSchemeSpecificPart(), uriVariables); + String expandedFragment = expandUriComponent(getFragment(), uriVariables); + return new OpaqueUriComponents(expandedScheme, expandedSsp, expandedFragment); } @Override diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponents.java b/spring-web/src/main/java/org/springframework/web/util/UriComponents.java index e81e2ba786..fc747ae9f1 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponents.java @@ -252,7 +252,6 @@ public abstract class UriComponents implements Serializable { * Get the value for the given URI variable name. * If the value is {@code null}, an empty String is expanded. * If the value is {@link #SKIP_VALUE}, the URI variable is not expanded. - * * @param name the variable name * @return the variable value, possibly {@code null} or {@link #SKIP_VALUE} */ @@ -295,8 +294,7 @@ public abstract class UriComponents implements Serializable { @Override public Object getValue(String name) { if (!this.valueIterator.hasNext()) { - throw new IllegalArgumentException( - "Not enough variable values available to expand '" + name + "'"); + throw new IllegalArgumentException("Not enough variable values available to expand '" + name + "'"); } return this.valueIterator.next(); } 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 73c17a9f54..0b1e7f22c1 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 @@ -163,20 +163,20 @@ public class UriComponentsBuilder { */ public static UriComponentsBuilder fromUriString(String uri) { Assert.hasLength(uri, "'uri' must not be empty"); - Matcher m = URI_PATTERN.matcher(uri); - if (m.matches()) { + Matcher matcher = URI_PATTERN.matcher(uri); + if (matcher.matches()) { UriComponentsBuilder builder = new UriComponentsBuilder(); - String scheme = m.group(2); - String userInfo = m.group(5); - String host = m.group(6); - String port = m.group(8); - String path = m.group(9); - String query = m.group(11); - String fragment = m.group(13); + String scheme = matcher.group(2); + String userInfo = matcher.group(5); + String host = matcher.group(6); + String port = matcher.group(8); + String path = matcher.group(9); + String query = matcher.group(11); + String fragment = matcher.group(13); boolean opaque = false; if (StringUtils.hasLength(scheme)) { - String s = uri.substring(scheme.length()); - if (!s.startsWith(":/")) { + String rest = uri.substring(scheme.length()); + if (!rest.startsWith(":/")) { opaque = true; } } @@ -223,25 +223,23 @@ public class UriComponentsBuilder { */ public static UriComponentsBuilder fromHttpUrl(String httpUrl) { Assert.notNull(httpUrl, "'httpUrl' must not be null"); - Matcher m = HTTP_URL_PATTERN.matcher(httpUrl); - if (m.matches()) { + Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl); + if (matcher.matches()) { UriComponentsBuilder builder = new UriComponentsBuilder(); - - String scheme = m.group(1); - builder.scheme((scheme != null) ? scheme.toLowerCase() : scheme); - builder.userInfo(m.group(4)); - String host = m.group(5); + String scheme = matcher.group(1); + builder.scheme(scheme != null ? scheme.toLowerCase() : null); + builder.userInfo(matcher.group(4)); + String host = matcher.group(5); if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) { throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL"); } builder.host(host); - String port = m.group(7); + String port = matcher.group(7); if (StringUtils.hasLength(port)) { builder.port(Integer.parseInt(port)); } - builder.path(m.group(8)); - builder.query(m.group(10)); - + builder.path(matcher.group(8)); + builder.query(matcher.group(10)); return builder; } else { @@ -264,7 +262,7 @@ public class UriComponentsBuilder { * Build a {@code UriComponents} instance from the various components * contained in this builder. * @param encoded whether all the components set in this builder are - * encoded ({@code true}) or not ({@code false}). + * encoded ({@code true}) or not ({@code false}) * @return the URI components */ public UriComponents build(boolean encoded) { @@ -509,13 +507,12 @@ public class UriComponentsBuilder { */ public UriComponentsBuilder query(String query) { if (query != null) { - Matcher m = QUERY_PARAM_PATTERN.matcher(query); - while (m.find()) { - String name = m.group(1); - String eq = m.group(2); - String value = m.group(3); - queryParam(name, (value != null ? value : - (StringUtils.hasLength(eq) ? "" : null))); + Matcher matcher = QUERY_PARAM_PATTERN.matcher(query); + while (matcher.find()) { + String name = matcher.group(1); + String eq = matcher.group(2); + String value = matcher.group(3); + queryParam(name, (value != null ? value : (StringUtils.hasLength(eq) ? "" : null))); } } else { @@ -550,7 +547,7 @@ public class UriComponentsBuilder { Assert.notNull(name, "'name' must not be null"); if (!ObjectUtils.isEmpty(values)) { for (Object value : values) { - String valueAsString = value != null ? value.toString() : null; + String valueAsString = (value != null ? value.toString() : null); this.queryParams.add(name, valueAsString); } } diff --git a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java index b4f9461991..3d71c0cdef 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -86,17 +86,17 @@ public class UriTemplate implements Serializable { * UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); * Map<String, String> uriVariables = new HashMap<String, String>(); * uriVariables.put("booking", "42"); - * uriVariables.put("hotel", "1"); + * uriVariables.put("hotel", "Rest & Relax"); * System.out.println(template.expand(uriVariables)); * - * will print:

{@code http://example.com/hotels/1/bookings/42}
+ * will print:
{@code http://example.com/hotels/Rest%20%26%20Relax/bookings/42}
* @param uriVariables the map of URI variables * @return the expanded URI * @throws IllegalArgumentException if {@code uriVariables} is {@code null}; * or if it does not contain values for all the variable names */ public URI expand(Map uriVariables) { - UriComponents expandedComponents = uriComponents.expand(uriVariables); + UriComponents expandedComponents = this.uriComponents.expand(uriVariables); UriComponents encodedComponents = expandedComponents.encode(); return encodedComponents.toUri(); } @@ -107,16 +107,16 @@ public class UriTemplate implements Serializable { *

Example: *

      * UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
-     * System.out.println(template.expand("1", "42));
+     * System.out.println(template.expand("Rest & Relax", "42));
      * 
- * will print:
{@code http://example.com/hotels/1/bookings/42}
+ * will print:
{@code http://example.com/hotels/Rest%20%26%20Relax/bookings/42}
* @param uriVariableValues the array of URI variables * @return the expanded URI * @throws IllegalArgumentException if {@code uriVariables} is {@code null} * or if it does not contain sufficient variables */ public URI expand(Object... uriVariableValues) { - UriComponents expandedComponents = uriComponents.expand(uriVariableValues); + UriComponents expandedComponents = this.uriComponents.expand(uriVariableValues); UriComponents encodedComponents = expandedComponents.encode(); return encodedComponents.toUri(); } @@ -177,11 +177,11 @@ public class UriTemplate implements Serializable { private Parser(String uriTemplate) { Assert.hasText(uriTemplate, "'uriTemplate' must not be null"); - Matcher m = NAMES_PATTERN.matcher(uriTemplate); + Matcher matcher = NAMES_PATTERN.matcher(uriTemplate); int end = 0; - while (m.find()) { - this.patternBuilder.append(quote(uriTemplate, end, m.start())); - String match = m.group(1); + while (matcher.find()) { + this.patternBuilder.append(quote(uriTemplate, end, matcher.start())); + String match = matcher.group(1); int colonIdx = match.indexOf(':'); if (colonIdx == -1) { this.patternBuilder.append(DEFAULT_VARIABLE_PATTERN); @@ -199,7 +199,7 @@ public class UriTemplate implements Serializable { String variableName = match.substring(0, colonIdx); this.variableNames.add(variableName); } - end = m.end(); + end = matcher.end(); } this.patternBuilder.append(quote(uriTemplate, end, uriTemplate.length())); int lastIdx = this.patternBuilder.length() - 1; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MatrixVariableMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MatrixVariableMethodArgumentResolver.java index e4fd61ceac..8e563d22c2 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MatrixVariableMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MatrixVariableMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -114,10 +114,9 @@ public class MatrixVariableMethodArgumentResolver extends AbstractNamedValueMeth } @Override - protected void handleMissingValue(String name, MethodParameter param) throws ServletRequestBindingException { - String paramType = param.getParameterType().getName(); - throw new ServletRequestBindingException( - "Missing matrix variable '" + name + "' for method parameter type [" + paramType + "]"); + protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException { + throw new ServletRequestBindingException("Missing matrix variable '" + name + + "' for method parameter of type " + parameter.getParameterType().getSimpleName()); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java index f6ed15f3c4..13ad39dc84 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/PathVariableMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -101,10 +101,9 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod } @Override - protected void handleMissingValue(String name, MethodParameter param) throws ServletRequestBindingException { - String paramType = param.getParameterType().getName(); - throw new ServletRequestBindingException( - "Missing URI template variable '" + name + "' for method parameter type [" + paramType + "]"); + protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException { + throw new ServletRequestBindingException("Missing URI template variable '" + name + + "' for method parameter of type " + parameter.getParameterType().getSimpleName()); } @Override @@ -130,8 +129,8 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod return; } - PathVariable annot = parameter.getParameterAnnotation(PathVariable.class); - String name = StringUtils.isEmpty(annot.value()) ? parameter.getParameterName() : annot.value(); + PathVariable ann = parameter.getParameterAnnotation(PathVariable.class); + String name = (ann == null || StringUtils.isEmpty(ann.value()) ? parameter.getParameterName() : ann.value()); if (conversionService != null) { value = conversionService.convert(value, new TypeDescriptor(parameter), STRING_TYPE_DESCRIPTOR); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java index 48080aa16a..8c51d5d8d2 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletCookieValueMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -27,8 +27,8 @@ import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.WebUtils; /** - * An {@link org.springframework.web.method.annotation.AbstractCookieValueMethodArgumentResolver} that resolves cookie - * values from an {@link HttpServletRequest}. + * An {@link org.springframework.web.method.annotation.AbstractCookieValueMethodArgumentResolver} + * that resolves cookie values from an {@link HttpServletRequest}. * * @author Rossen Stoyanchev * @since 3.1 @@ -37,17 +37,19 @@ public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValu private UrlPathHelper urlPathHelper = new UrlPathHelper(); + public ServletCookieValueMethodArgumentResolver(ConfigurableBeanFactory beanFactory) { super(beanFactory); } + public void setUrlPathHelper(UrlPathHelper urlPathHelper) { this.urlPathHelper = urlPathHelper; } + @Override - protected Object resolveName(String cookieName, MethodParameter parameter, NativeWebRequest webRequest) - throws Exception { + protected Object resolveName(String cookieName, MethodParameter parameter, NativeWebRequest webRequest) throws Exception { HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class); Cookie cookieValue = WebUtils.getCookie(servletRequest, cookieName); if (Cookie.class.isAssignableFrom(parameter.getParameterType())) { @@ -60,4 +62,5 @@ public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValu return null; } } + }