Polishing

This commit is contained in:
Juergen Hoeller
2020-10-13 01:00:50 +02:00
parent f355b174ad
commit a29929db5b
8 changed files with 92 additions and 67 deletions

View File

@@ -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.
@@ -78,6 +78,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
}
/**
* Create a new {@link AbstractNamedValueMethodArgumentResolver} instance.
* @param beanFactory a bean factory to use for resolving ${...} placeholder
* and #{...} SpEL expressions in default values, or {@code null} if default
* values are not expected to contain expressions
@@ -129,7 +130,6 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
catch (TypeMismatchException ex) {
throw new MethodArgumentTypeMismatchException(arg, ex.getRequiredType(),
namedValueInfo.name, parameter, ex.getCause());
}
}
@@ -168,8 +168,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);
@@ -182,12 +182,12 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
*/
@Nullable
private Object resolveStringValue(String value) {
if (this.configurableBeanFactory == null) {
if (this.configurableBeanFactory == null || this.expressionContext == null) {
return value;
}
String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
if (exprResolver == null || this.expressionContext == null) {
if (exprResolver == null) {
return value;
}
return exprResolver.evaluate(placeholdersResolved, this.expressionContext);

View File

@@ -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.
@@ -48,13 +48,13 @@ import org.springframework.web.method.HandlerMethod;
*/
public class InvocableHandlerMethod extends HandlerMethod {
@Nullable
private WebDataBinderFactory dataBinderFactory;
private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
private HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
@Nullable
private WebDataBinderFactory dataBinderFactory;
/**
* Create an instance from a {@code HandlerMethod}.
@@ -84,20 +84,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;
}
/**
@@ -109,6 +100,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.
@@ -156,9 +155,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;
}

View File

@@ -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;
*
* <p>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;
}
}

View File

@@ -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.
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -45,59 +46,65 @@ 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;
HttpHeaders headers = this.multipartRequest.getMultipartHeaders(this.partName);
if (headers == null) {
throw new MissingServletRequestPartException(partName);
HttpHeaders multipartHeaders = this.multipartRequest.getMultipartHeaders(requestPartName);
if (multipartHeaders == null) {
throw new MissingServletRequestPartException(requestPartName);
}
this.headers = headers;
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();
Part part = this.multipartRequest.getPart(this.requestPartName);
if (part != null) {
return part.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(determineCharset()));
}
// 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(determineCharset()));
}
throw new IllegalStateException("No body available for request part '" + this.requestPartName + "'");
}
private Charset determineCharset() {