Skip to content

Commit 45322a7

Browse files
committed
Polishing
1 parent 678ea4e commit 45322a7

File tree

5 files changed

+61
-52
lines changed

5 files changed

+61
-52
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -146,8 +146,9 @@ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValu
146146
if (info.name.isEmpty()) {
147147
name = parameter.getParameterName();
148148
if (name == null) {
149-
throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() +
150-
"] not available, and parameter name information not found in class file either.");
149+
throw new IllegalArgumentException(
150+
"Name for argument of type [" + parameter.getNestedParameterType().getName() +
151+
"] not specified, and parameter name information not found in class file either.");
151152
}
152153
}
153154
String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue);

spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -126,7 +126,6 @@ else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
126126
catch (TypeMismatchException ex) {
127127
throw new MethodArgumentTypeMismatchException(arg, ex.getRequiredType(),
128128
namedValueInfo.name, parameter, ex.getCause());
129-
130129
}
131130
}
132131

@@ -165,8 +164,8 @@ private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValu
165164
name = parameter.getParameterName();
166165
if (name == null) {
167166
throw new IllegalArgumentException(
168-
"Name for argument type [" + parameter.getNestedParameterType().getName() +
169-
"] not available, and parameter name information not found in class file either.");
167+
"Name for argument of type [" + parameter.getNestedParameterType().getName() +
168+
"] not specified, and parameter name information not found in class file either.");
170169
}
171170
}
172171
String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue);

spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,12 +47,12 @@
4747
*/
4848
public class InvocableHandlerMethod extends HandlerMethod {
4949

50-
private WebDataBinderFactory dataBinderFactory;
51-
52-
private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
50+
private HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
5351

5452
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
5553

54+
private WebDataBinderFactory dataBinderFactory;
55+
5656

5757
/**
5858
* Create an instance from a {@code HandlerMethod}.
@@ -82,20 +82,11 @@ public InvocableHandlerMethod(Object bean, String methodName, Class<?>... parame
8282
}
8383

8484

85-
/**
86-
* Set the {@link WebDataBinderFactory} to be passed to argument resolvers allowing them to create
87-
* a {@link WebDataBinder} for data binding and type conversion purposes.
88-
* @param dataBinderFactory the data binder factory.
89-
*/
90-
public void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) {
91-
this.dataBinderFactory = dataBinderFactory;
92-
}
93-
9485
/**
9586
* Set {@link HandlerMethodArgumentResolver}s to use to use for resolving method argument values.
9687
*/
9788
public void setHandlerMethodArgumentResolvers(HandlerMethodArgumentResolverComposite argumentResolvers) {
98-
this.argumentResolvers = argumentResolvers;
89+
this.resolvers = argumentResolvers;
9990
}
10091

10192
/**
@@ -107,6 +98,14 @@ public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDisc
10798
this.parameterNameDiscoverer = parameterNameDiscoverer;
10899
}
109100

101+
/**
102+
* Set the {@link WebDataBinderFactory} to be passed to argument resolvers allowing them
103+
* to create a {@link WebDataBinder} for data binding and type conversion purposes.
104+
*/
105+
public void setDataBinderFactory(WebDataBinderFactory dataBinderFactory) {
106+
this.dataBinderFactory = dataBinderFactory;
107+
}
108+
110109

111110
/**
112111
* Invoke the method after resolving its argument values in the context of the given request.
@@ -153,9 +152,9 @@ private Object[] getMethodArgumentValues(NativeWebRequest request, ModelAndViewC
153152
if (args[i] != null) {
154153
continue;
155154
}
156-
if (this.argumentResolvers.supportsParameter(parameter)) {
155+
if (this.resolvers.supportsParameter(parameter)) {
157156
try {
158-
args[i] = this.argumentResolvers.resolveArgument(
157+
args[i] = this.resolvers.resolveArgument(
159158
parameter, mavContainer, request, this.dataBinderFactory);
160159
continue;
161160
}

spring-web/src/main/java/org/springframework/web/multipart/support/MissingServletRequestPartException.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@
2626
*
2727
* <p>This may be because the request is not a multipart/form-data request,
2828
* because the part is not present in the request, or because the web
29-
* application is not configured correctly for processing multipart requests,
29+
* application is not configured correctly for processing multipart requests,
3030
* e.g. no {@link MultipartResolver}.
3131
*
3232
* @author Rossen Stoyanchev
@@ -35,17 +35,24 @@
3535
@SuppressWarnings("serial")
3636
public class MissingServletRequestPartException extends ServletException {
3737

38-
private final String partName;
38+
private final String requestPartName;
3939

4040

41-
public MissingServletRequestPartException(String partName) {
42-
super("Required request part '" + partName + "' is not present");
43-
this.partName = partName;
41+
/**
42+
* Constructor for MissingServletRequestPartException.
43+
* @param requestPartName the name of the missing part of the multipart request
44+
*/
45+
public MissingServletRequestPartException(String requestPartName) {
46+
super("Required request part '" + requestPartName + "' is not present");
47+
this.requestPartName = requestPartName;
4448
}
4549

4650

51+
/**
52+
* Return the name of the offending part of the multipart request.
53+
*/
4754
public String getRequestPartName() {
48-
return this.partName;
55+
return this.requestPartName;
4956
}
5057

5158
}

spring-web/src/main/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequest.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,59 +45,62 @@ public class RequestPartServletServerHttpRequest extends ServletServerHttpReques
4545

4646
private final MultipartHttpServletRequest multipartRequest;
4747

48-
private final String partName;
48+
private final String requestPartName;
4949

50-
private final HttpHeaders headers;
50+
private final HttpHeaders multipartHeaders;
5151

5252

5353
/**
5454
* Create a new {@code RequestPartServletServerHttpRequest} instance.
5555
* @param request the current servlet request
56-
* @param partName the name of the part to adapt to the {@link ServerHttpRequest} contract
56+
* @param requestPartName the name of the part to adapt to the {@link ServerHttpRequest} contract
5757
* @throws MissingServletRequestPartException if the request part cannot be found
5858
* @throws MultipartException if MultipartHttpServletRequest cannot be initialized
5959
*/
60-
public RequestPartServletServerHttpRequest(HttpServletRequest request, String partName)
60+
public RequestPartServletServerHttpRequest(HttpServletRequest request, String requestPartName)
6161
throws MissingServletRequestPartException {
6262

6363
super(request);
6464

6565
this.multipartRequest = MultipartResolutionDelegate.asMultipartHttpServletRequest(request);
66-
this.partName = partName;
66+
this.requestPartName = requestPartName;
6767

68-
this.headers = this.multipartRequest.getMultipartHeaders(this.partName);
69-
if (this.headers == null) {
70-
throw new MissingServletRequestPartException(partName);
68+
HttpHeaders multipartHeaders = this.multipartRequest.getMultipartHeaders(requestPartName);
69+
if (multipartHeaders == null) {
70+
throw new MissingServletRequestPartException(requestPartName);
7171
}
72+
this.multipartHeaders = multipartHeaders;
7273
}
7374

7475

7576
@Override
7677
public HttpHeaders getHeaders() {
77-
return this.headers;
78+
return this.multipartHeaders;
7879
}
7980

80-
8181
@Override
8282
public InputStream getBody() throws IOException {
83+
// Prefer Servlet Part resolution to cover file as well as parameter streams
8384
if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) {
8485
try {
85-
return this.multipartRequest.getPart(this.partName).getInputStream();
86+
return this.multipartRequest.getPart(this.requestPartName).getInputStream();
8687
}
8788
catch (Exception ex) {
88-
throw new MultipartException("Could not parse multipart servlet request", ex);
89+
throw new MultipartException("Failed to retrieve request part '" + this.requestPartName + "'", ex);
8990
}
9091
}
91-
else {
92-
MultipartFile file = this.multipartRequest.getFile(this.partName);
93-
if (file != null) {
94-
return file.getInputStream();
95-
}
96-
else {
97-
String paramValue = this.multipartRequest.getParameter(this.partName);
98-
return new ByteArrayInputStream(paramValue.getBytes(determineEncoding()));
99-
}
92+
93+
// Spring-style distinction between MultipartFile and String parameters
94+
MultipartFile file = this.multipartRequest.getFile(this.requestPartName);
95+
if (file != null) {
96+
return file.getInputStream();
97+
}
98+
String paramValue = this.multipartRequest.getParameter(this.requestPartName);
99+
if (paramValue != null) {
100+
return new ByteArrayInputStream(paramValue.getBytes(determineEncoding()));
100101
}
102+
103+
throw new IllegalStateException("No body available for request part '" + this.requestPartName + "'");
101104
}
102105

103106
private String determineEncoding() {

0 commit comments

Comments
 (0)