diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java index 70f1f2e8e4a..982d153cbc3 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -146,8 +146,9 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle if (info.name.isEmpty()) { name = parameter.getParameterName(); if (name == null) { - throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() + - "] not available, and parameter name information not found in class file either."); + throw new IllegalArgumentException( + "Name for argument of type [" + parameter.getNestedParameterType().getName() + + "] not specified, and parameter name information not found in class file either."); } } String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.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 a0ca3a218c0..2251f2f43f5 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -126,7 +126,6 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle catch (TypeMismatchException ex) { throw new MethodArgumentTypeMismatchException(arg, ex.getRequiredType(), namedValueInfo.name, parameter, ex.getCause()); - } } @@ -165,8 +164,8 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle name = parameter.getParameterName(); if (name == null) { throw new IllegalArgumentException( - "Name for argument type [" + parameter.getNestedParameterType().getName() + - "] not available, and parameter name information not found in class file either."); + "Name for argument of type [" + parameter.getNestedParameterType().getName() + + "] not specified, and parameter name information not found in class file either."); } } String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue); diff --git a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java index 7b46cbae38f..6e713240928 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java +++ b/spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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,12 +47,12 @@ import org.springframework.web.method.HandlerMethod; */ public class InvocableHandlerMethod extends HandlerMethod { - private WebDataBinderFactory dataBinderFactory; - - private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite(); + private HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite(); private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer(); + private WebDataBinderFactory dataBinderFactory; + /** * Create an instance from a {@code HandlerMethod}. @@ -82,20 +82,11 @@ public class InvocableHandlerMethod extends HandlerMethod { } - /** - * Set the {@link WebDataBinderFactory} to be passed to argument resolvers allowing them to create - * a {@link WebDataBinder} for data binding and type conversion purposes. - * @param dataBinderFactory the data binder factory. - */ - public void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) { - this.dataBinderFactory = dataBinderFactory; - } - /** * Set {@link HandlerMethodArgumentResolver}s to use to use for resolving method argument values. */ public void setHandlerMethodArgumentResolvers(HandlerMethodArgumentResolverComposite argumentResolvers) { - this.argumentResolvers = argumentResolvers; + this.resolvers = argumentResolvers; } /** @@ -107,6 +98,14 @@ public class InvocableHandlerMethod extends HandlerMethod { this.parameterNameDiscoverer = parameterNameDiscoverer; } + /** + * Set the {@link WebDataBinderFactory} to be passed to argument resolvers allowing them + * to create a {@link WebDataBinder} for data binding and type conversion purposes. + */ + public void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) { + this.dataBinderFactory = dataBinderFactory; + } + /** * Invoke the method after resolving its argument values in the context of the given request. @@ -153,9 +152,9 @@ public class InvocableHandlerMethod extends HandlerMethod { if (args[i] != null) { continue; } - if (this.argumentResolvers.supportsParameter(parameter)) { + if (this.resolvers.supportsParameter(parameter)) { try { - args[i] = this.argumentResolvers.resolveArgument( + args[i] = this.resolvers.resolveArgument( parameter, mavContainer, request, this.dataBinderFactory); continue; } diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java b/spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java index 62e1f1dee68..414405d8038 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 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. @@ -26,7 +26,7 @@ import org.springframework.web.multipart.MultipartResolver; * *
This may be because the request is not a multipart/form-data request, * because the part is not present in the request, or because the web - * application is not configured correctly for processing multipart requests, + * application is not configured correctly for processing multipart requests, * e.g. no {@link MultipartResolver}. * * @author Rossen Stoyanchev @@ -35,17 +35,24 @@ import org.springframework.web.multipart.MultipartResolver; @SuppressWarnings("serial") public class MissingServletRequestPartException extends ServletException { - private final String partName; + private final String requestPartName; - public MissingServletRequestPartException(String partName) { - super("Required request part '" + partName + "' is not present"); - this.partName = partName; + /** + * Constructor for MissingServletRequestPartException. + * @param requestPartName the name of the missing part of the multipart request + */ + public MissingServletRequestPartException(String requestPartName) { + super("Required request part '" + requestPartName + "' is not present"); + this.requestPartName = requestPartName; } + /** + * Return the name of the offending part of the multipart request. + */ public String getRequestPartName() { - return this.partName; + return this.requestPartName; } } diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java index 351a58d9ccb..35234bf86c9 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 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. @@ -45,59 +45,62 @@ public class RequestPartServletServerHttpRequest extends ServletServerHttpReques private final MultipartHttpServletRequest multipartRequest; - private final String partName; + private final String requestPartName; - private final HttpHeaders headers; + private final HttpHeaders multipartHeaders; /** * Create a new {@code RequestPartServletServerHttpRequest} instance. * @param request the current servlet request - * @param partName the name of the part to adapt to the {@link ServerHttpRequest} contract + * @param requestPartName the name of the part to adapt to the {@link ServerHttpRequest} contract * @throws MissingServletRequestPartException if the request part cannot be found * @throws MultipartException if MultipartHttpServletRequest cannot be initialized */ - public RequestPartServletServerHttpRequest(HttpServletRequest request, String partName) + public RequestPartServletServerHttpRequest(HttpServletRequest request, String requestPartName) throws MissingServletRequestPartException { super(request); this.multipartRequest = MultipartResolutionDelegate.asMultipartHttpServletRequest(request); - this.partName = partName; + this.requestPartName = requestPartName; - this.headers = this.multipartRequest.getMultipartHeaders(this.partName); - if (this.headers == null) { - throw new MissingServletRequestPartException(partName); + HttpHeaders multipartHeaders = this.multipartRequest.getMultipartHeaders(requestPartName); + if (multipartHeaders == null) { + throw new MissingServletRequestPartException(requestPartName); } + this.multipartHeaders = multipartHeaders; } @Override public HttpHeaders getHeaders() { - return this.headers; + return this.multipartHeaders; } - @Override public InputStream getBody() throws IOException { + // Prefer Servlet Part resolution to cover file as well as parameter streams if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { try { - return this.multipartRequest.getPart(this.partName).getInputStream(); + return this.multipartRequest.getPart(this.requestPartName).getInputStream(); } catch (Exception ex) { - throw new MultipartException("Could not parse multipart servlet request", ex); + throw new MultipartException("Failed to retrieve request part '" + this.requestPartName + "'", ex); } } - else { - MultipartFile file = this.multipartRequest.getFile(this.partName); - if (file != null) { - return file.getInputStream(); - } - else { - String paramValue = this.multipartRequest.getParameter(this.partName); - return new ByteArrayInputStream(paramValue.getBytes(determineEncoding())); - } + + // Spring-style distinction between MultipartFile and String parameters + MultipartFile file = this.multipartRequest.getFile(this.requestPartName); + if (file != null) { + return file.getInputStream(); + } + String paramValue = this.multipartRequest.getParameter(this.requestPartName); + if (paramValue != null) { + return new ByteArrayInputStream(paramValue.getBytes(determineEncoding())); } + + throw new IllegalStateException("No body available for request part '" + this.requestPartName + "'"); } private String determineEncoding() {