提交 8dbe7539 编写于 作者: J Juergen Hoeller

Polishing

上级 23fa37b0
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
......@@ -309,6 +309,7 @@ public class MethodParameter {
/**
* Return the generic type of the method/constructor parameter.
* @return the parameter type (never {@code null})
* @since 3.0
*/
public Type getGenericParameterType() {
if (this.genericParameterType == null) {
......@@ -324,6 +325,12 @@ public class MethodParameter {
return this.genericParameterType;
}
/**
* Return the nested type of the method/constructor parameter.
* @return the parameter type (never {@code null})
* @see #getNestingLevel()
* @since 3.1
*/
public Class<?> getNestedParameterType() {
if (this.nestingLevel > 1) {
Type type = getGenericParameterType();
......
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
......@@ -1092,9 +1092,8 @@ public final class ResolvableType implements Serializable {
public static ResolvableType forMethodParameter(MethodParameter methodParameter, Type targetType) {
Assert.notNull(methodParameter, "MethodParameter must not be null");
ResolvableType owner = forType(methodParameter.getContainingClass()).as(methodParameter.getDeclaringClass());
return forType(targetType, new MethodParameterTypeProvider(methodParameter),
owner.asVariableResolver()).getNested(methodParameter.getNestingLevel(),
methodParameter.typeIndexesPerLevel);
return forType(targetType, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()).
getNested(methodParameter.getNestingLevel(), methodParameter.typeIndexesPerLevel);
}
/**
......
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
......@@ -32,10 +32,10 @@ import org.springframework.util.Assert;
import org.springframework.util.MimeType;
/**
* Abstract base class for {@link MessageConverter} implementations including support for
* common properties and a partial implementation of the conversion methods mainly to
* check if the converter supports the conversion based on the payload class and MIME
* type.
* Abstract base class for {@link MessageConverter} implementations including support
* for common properties and a partial implementation of the conversion methods,
* mainly to check if the converter supports the conversion based on the payload class
* and MIME type.
*
* @author Rossen Stoyanchev
* @since 4.0
......@@ -68,7 +68,7 @@ public abstract class AbstractMessageConverter implements MessageConverter {
* @param supportedMimeTypes the supported MIME types
*/
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes) {
Assert.notNull(supportedMimeTypes, "SupportedMimeTypes must not be null");
Assert.notNull(supportedMimeTypes, "supportedMimeTypes must not be null");
this.supportedMimeTypes = new ArrayList<MimeType>(supportedMimeTypes);
}
......@@ -83,13 +83,11 @@ public abstract class AbstractMessageConverter implements MessageConverter {
/**
* Configure the {@link ContentTypeResolver} to use to resolve the content
* type of an input message.
* <p>
* Note that if no resolver is configured, then
* <p>Note that if no resolver is configured, then
* {@link #setStrictContentTypeMatch(boolean) strictContentTypeMatch} should
* be left as {@code false} (the default) or otherwise this converter will
* ignore all messages.
* <p>
* By default, a {@code DefaultContentTypeResolver} instance is used.
* <p>By default, a {@code DefaultContentTypeResolver} instance is used.
*/
public void setContentTypeResolver(ContentTypeResolver resolver) {
this.contentTypeResolver = resolver;
......@@ -106,20 +104,17 @@ public abstract class AbstractMessageConverter implements MessageConverter {
* Whether this converter should convert messages for which no content type
* could be resolved through the configured
* {@link org.springframework.messaging.converter.ContentTypeResolver}.
* A converter can configured to be strict only when a
* {@link #setContentTypeResolver(ContentTypeResolver) contentTypeResolver}
* is configured and the list of {@link #getSupportedMimeTypes() supportedMimeTypes}
* is not be empty.
*
* then requires the content type of a message to be resolved
*
* When set to true, #supportsMimeType(MessageHeaders) will return false if the
* contentTypeResolver is not defined or if no content-type header is present.
* <p>A converter can configured to be strict only when a
* {@link #setContentTypeResolver contentTypeResolver} is configured and the
* list of {@link #getSupportedMimeTypes() supportedMimeTypes} is not be empty.
* <p>When this flag is set to {@code true}, {@link #supportsMimeType(MessageHeaders)}
* will return {@code false} if the {@link #setContentTypeResolver contentTypeResolver}
* is not defined or if no content-type header is present.
*/
public void setStrictContentTypeMatch(boolean strictContentTypeMatch) {
if (strictContentTypeMatch) {
Assert.notEmpty(getSupportedMimeTypes(), "Strict match requires non-empty list of supported mime types.");
Assert.notNull(getContentTypeResolver(), "Strict match requires ContentTypeResolver.");
Assert.notEmpty(getSupportedMimeTypes(), "Strict match requires non-empty list of supported mime types");
Assert.notNull(getContentTypeResolver(), "Strict match requires ContentTypeResolver");
}
this.strictContentTypeMatch = strictContentTypeMatch;
}
......@@ -166,14 +161,6 @@ public abstract class AbstractMessageConverter implements MessageConverter {
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null);
}
/**
* Whether the given class is supported by this converter.
* @param clazz the class to test for support
* @return {@code true} if supported; {@code false} otherwise
*/
protected abstract boolean supports(Class<?> clazz);
@Override
public final Object fromMessage(Message<?> message, Class<?> targetClass) {
if (!canConvertFrom(message, targetClass)) {
......@@ -186,14 +173,8 @@ public abstract class AbstractMessageConverter implements MessageConverter {
return (supports(targetClass) && supportsMimeType(message.getHeaders()));
}
/**
* Convert the message payload from serialized form to an Object.
*/
public abstract Object convertFromInternal(Message<?> message, Class<?> targetClass);
@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers) {
if (!canConvertTo(payload, headers)) {
return null;
}
......@@ -218,15 +199,10 @@ public abstract class AbstractMessageConverter implements MessageConverter {
}
protected boolean canConvertTo(Object payload, MessageHeaders headers) {
Class<?> clazz = (payload != null) ? payload.getClass() : null;
Class<?> clazz = (payload != null ? payload.getClass() : null);
return (supports(clazz) && supportsMimeType(headers));
}
/**
* Convert the payload object to serialized form.
*/
public abstract Object convertToInternal(Object payload, MessageHeaders headers);
protected boolean supportsMimeType(MessageHeaders headers) {
if (getSupportedMimeTypes().isEmpty()) {
return true;
......@@ -249,7 +225,26 @@ public abstract class AbstractMessageConverter implements MessageConverter {
}
protected MimeType getMimeType(MessageHeaders headers) {
return (this.contentTypeResolver != null) ? this.contentTypeResolver.resolve(headers) : null;
return (this.contentTypeResolver != null ? this.contentTypeResolver.resolve(headers) : null);
}
/**
* Whether the given class is supported by this converter.
* @param clazz the class to test for support
* @return {@code true} if supported; {@code false} otherwise
*/
protected abstract boolean supports(Class<?> clazz);
/**
* Convert the message payload from serialized form to an Object.
*/
public abstract Object convertFromInternal(Message<?> message, Class<?> targetClass);
/**
* Convert the payload object to serialized form.
*/
public abstract Object convertToInternal(Object payload, MessageHeaders headers);
}
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
......@@ -29,7 +29,6 @@ import org.springframework.http.MediaType;
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @since 3.2
*
* @see org.springframework.core.ParameterizedTypeReference
*/
public interface GenericHttpMessageConverter<T> extends HttpMessageConverter<T> {
......@@ -38,7 +37,7 @@ public interface GenericHttpMessageConverter<T> extends HttpMessageConverter<T>
* Indicates whether the given type can be read by this converter.
* @param type the type to test for readability
* @param contextClass a context class for the target type, for example a class
* in which the target type appears in a method signature, can be {@code null}
* in which the target type appears in a method signature (can be {@code null})
* @param mediaType the media type to read, can be {@code null} if not specified.
* Typically the value of a {@code Content-Type} header.
* @return {@code true} if readable; {@code false} otherwise
......@@ -51,7 +50,7 @@ public interface GenericHttpMessageConverter<T> extends HttpMessageConverter<T>
* been passed to the {@link #canRead canRead} method of this interface,
* which must have returned {@code true}.
* @param contextClass a context class for the target type, for example a class
* in which the target type appears in a method signature, can be {@code null}
* in which the target type appears in a method signature (can be {@code null})
* @param inputMessage the HTTP input message to read from
* @return the converted object
* @throws IOException in case of I/O errors
......
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
......@@ -596,7 +596,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
logger.debug(method.name() + " request for \"" + url + "\" resulted in " +
response.getRawStatusCode() + " (" + response.getStatusText() + ")");
}
catch (IOException e) {
catch (IOException ex) {
// ignore
}
}
......@@ -608,7 +608,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
logger.warn(method.name() + " request for \"" + url + "\" resulted in " +
response.getRawStatusCode() + " (" + response.getStatusText() + "); invoking error handler");
}
catch (IOException e) {
catch (IOException ex) {
// ignore
}
}
......@@ -668,12 +668,11 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
if (responseType != null) {
if (this.responseType != null) {
Class<?> responseClass = null;
if (responseType instanceof Class) {
responseClass = (Class<?>) responseType;
if (this.responseType instanceof Class) {
responseClass = (Class<?>) this.responseType;
}
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
for (HttpMessageConverter<?> converter : getMessageConverters()) {
if (responseClass != null) {
......@@ -682,13 +681,11 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
}
}
else if (converter instanceof GenericHttpMessageConverter) {
GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter;
if (genericConverter.canRead(responseType, null, null)) {
if (genericConverter.canRead(this.responseType, null, null)) {
allSupportedMediaTypes.addAll(getSupportedMediaTypes(converter));
}
}
}
if (!allSupportedMediaTypes.isEmpty()) {
MediaType.sortBySpecificity(allSupportedMediaTypes);
......@@ -744,9 +741,9 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
@SuppressWarnings("unchecked")
public void doWithRequest(ClientHttpRequest httpRequest) throws IOException {
super.doWithRequest(httpRequest);
if (!requestEntity.hasBody()) {
if (!this.requestEntity.hasBody()) {
HttpHeaders httpHeaders = httpRequest.getHeaders();
HttpHeaders requestHeaders = requestEntity.getHeaders();
HttpHeaders requestHeaders = this.requestEntity.getHeaders();
if (!requestHeaders.isEmpty()) {
httpHeaders.putAll(requestHeaders);
}
......@@ -755,9 +752,9 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
}
}
else {
Object requestBody = requestEntity.getBody();
Object requestBody = this.requestEntity.getBody();
Class<?> requestType = requestBody.getClass();
HttpHeaders requestHeaders = requestEntity.getHeaders();
HttpHeaders requestHeaders = this.requestEntity.getHeaders();
MediaType requestContentType = requestHeaders.getContentType();
for (HttpMessageConverter<?> messageConverter : getMessageConverters()) {
if (messageConverter.canWrite(requestType, requestContentType)) {
......
......@@ -268,9 +268,9 @@ public class UriComponentsBuilder implements Cloneable {
* Create a new {@code UriComponents} object from the URI associated with
* the given HttpRequest while also overlaying with values from the headers
* "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if present.
*
* @param request the source request
* @return the URI components of the UR
* @return the URI components of the URI
* @since 4.1.5
*/
public static UriComponentsBuilder fromHttpRequest(HttpRequest request) {
URI uri = request.getURI();
......
......@@ -18,8 +18,8 @@ package org.springframework.web.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
......@@ -33,7 +33,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.HttpRequest;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
......@@ -742,9 +742,9 @@ public abstract class WebUtils {
* like this {@code "q1=a;q1=b;q2=a,b,c"}. The resulting map would contain
* keys {@code "q1"} and {@code "q2"} with values {@code ["a","b"]} and
* {@code ["a","b","c"]} respectively.
*
* @param matrixVariables the unparsed matrix variables string
* @return a map with matrix variable names and values, never {@code null}
* @since 3.2
*/
public static MultiValueMap<String, String> parseMatrixVariables(String matrixVariables) {
MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>();
......@@ -773,12 +773,11 @@ public abstract class WebUtils {
* Check the given request origin against a list of allowed origins.
* A list containing "*" means that all origins are allowed.
* An empty list means only same origin is allowed.
*
* @return true if the request origin is valid, false otherwise
* @since 4.1.5
* @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
*/
public static boolean isValidOrigin(ServerHttpRequest request, List<String> allowedOrigins) {
public static boolean isValidOrigin(HttpRequest request, Collection<String> allowedOrigins) {
Assert.notNull(request, "Request must not be null");
Assert.notNull(allowedOrigins, "Allowed origins must not be null");
......@@ -791,7 +790,7 @@ public abstract class WebUtils {
UriComponents requestComponents = UriComponentsBuilder.fromHttpRequest(request).build();
int originPort = getPort(originComponents);
int requestPort = getPort(requestComponents);
return originComponents.getHost().equals(requestComponents.getHost()) && (originPort == requestPort);
return (originComponents.getHost().equals(requestComponents.getHost()) && originPort == requestPort);
}
else {
return allowedOrigins.contains(origin);
......
......@@ -104,7 +104,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
* from the given HttpInputMessage.
* @param <T> the expected type of the argument value to be created
* @param inputMessage the HTTP input message representing the current request
* @param methodParam the method argument
* @param methodParam the method parameter descriptor
* @param targetType the type of object to create, not necessarily the same as
* the method parameter type (e.g. for {@code HttpEntity<String>} method
* parameter the target type is String)
......@@ -113,8 +113,8 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
* @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
*/
@SuppressWarnings("unchecked")
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage,
MethodParameter methodParam, Type targetType) throws IOException, HttpMediaTypeNotSupportedException {
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter methodParam,
Type targetType) throws IOException, HttpMediaTypeNotSupportedException {
MediaType contentType;
try {
......@@ -128,6 +128,8 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
}
Class<?> contextClass = methodParam.getContainingClass();
Class<T> targetClass = (Class<T>)
ResolvableType.forMethodParameter(methodParam, targetType).resolve(Object.class);
for (HttpMessageConverter<?> converter : this.messageConverters) {
if (converter instanceof GenericHttpMessageConverter) {
......@@ -140,8 +142,6 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
return genericConverter.read(targetType, contextClass, inputMessage);
}
}
Class<T> targetClass = (Class<T>)
ResolvableType.forMethodParameter(methodParam, targetType).resolve(Object.class);
if (converter.canRead(targetClass, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading [" + targetClass.getName() + "] as \"" +
......
......@@ -80,12 +80,13 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
super(messageConverters);
}
/**
* Supports the following:
* <ul>
* <li>Annotated with {@code @RequestPart}
* <li>Of type {@link MultipartFile} unless annotated with {@code @RequestParam}.
* <li>Of type {@code javax.servlet.http.Part} unless annotated with {@code @RequestParam}.
* <li>Annotated with {@code @RequestPart}
* <li>Of type {@link MultipartFile} unless annotated with {@code @RequestParam}.
* <li>Of type {@code javax.servlet.http.Part} unless annotated with {@code @RequestParam}.
* </ul>
*/
@Override
......@@ -117,7 +118,7 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
assertIsMultipartRequest(servletRequest);
MultipartHttpServletRequest multipartRequest =
WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
String partName = getPartName(parameter);
Object arg;
......@@ -180,41 +181,44 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
}
}
private String getPartName(MethodParameter parameter) {
RequestPart annot = parameter.getParameterAnnotation(RequestPart.class);
private String getPartName(MethodParameter param) {
RequestPart annot = param.getParameterAnnotation(RequestPart.class);
String partName = (annot != null ? annot.value() : "");
if (partName.length() == 0) {
partName = parameter.getParameterName();
Assert.notNull(partName, "Request part name for argument type [" + parameter.getParameterType().getName() +
"] not specified, and parameter name information not found in class file either.");
partName = param.getParameterName();
if (partName == null) {
throw new IllegalArgumentException("Request part name for argument type [" +
param.getNestedParameterType().getName() +
"] not specified, and parameter name information not found in class file either.");
}
}
return partName;
}
private boolean isMultipartFileCollection(MethodParameter parameter) {
Class<?> collectionType = getCollectionParameterType(parameter);
private boolean isMultipartFileCollection(MethodParameter param) {
Class<?> collectionType = getCollectionParameterType(param);
return (collectionType != null && collectionType.equals(MultipartFile.class));
}
private boolean isMultipartFileArray(MethodParameter parameter) {
Class<?> paramType = parameter.getParameterType().getComponentType();
private boolean isMultipartFileArray(MethodParameter param) {
Class<?> paramType = param.getNestedParameterType().getComponentType();
return (paramType != null && MultipartFile.class.equals(paramType));
}
private boolean isPartCollection(MethodParameter parameter) {
Class<?> collectionType = getCollectionParameterType(parameter);
private boolean isPartCollection(MethodParameter param) {
Class<?> collectionType = getCollectionParameterType(param);
return (collectionType != null && "javax.servlet.http.Part".equals(collectionType.getName()));
}
private boolean isPartArray(MethodParameter parameter) {
Class<?> paramType = parameter.getParameterType().getComponentType();
private boolean isPartArray(MethodParameter param) {
Class<?> paramType = param.getNestedParameterType().getComponentType();
return (paramType != null && "javax.servlet.http.Part".equals(paramType.getName()));
}
private Class<?> getCollectionParameterType(MethodParameter parameter) {
Class<?> paramType = parameter.getParameterType();
private Class<?> getCollectionParameterType(MethodParameter param) {
Class<?> paramType = param.getNestedParameterType();
if (Collection.class.equals(paramType) || List.class.isAssignableFrom(paramType)){
Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(parameter);
Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(param);
if (valueType != null) {
return valueType;
}
......@@ -228,13 +232,13 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
* Spring's {@link org.springframework.validation.annotation.Validated},
* and custom annotations whose name starts with "Valid".
* @param binder the DataBinder to be used
* @param parameter the method parameter
* @param param the method parameter
* @throws MethodArgumentNotValidException in case of a binding error which
* is meant to be fatal (i.e. without a declared {@link Errors} parameter)
* @see #isBindingErrorFatal
*/
protected void validate(WebDataBinder binder, MethodParameter parameter) throws MethodArgumentNotValidException {
Annotation[] annotations = parameter.getParameterAnnotations();
protected void validate(WebDataBinder binder, MethodParameter param) throws MethodArgumentNotValidException {
Annotation[] annotations = param.getParameterAnnotations();
for (Annotation ann : annotations) {
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
......@@ -243,8 +247,8 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
binder.validate(validationHints);
BindingResult bindingResult = binder.getBindingResult();
if (bindingResult.hasErrors()) {
if (isBindingErrorFatal(parameter)) {
throw new MethodArgumentNotValidException(parameter, bindingResult);
if (isBindingErrorFatal(param)) {
throw new MethodArgumentNotValidException(param, bindingResult);
}
}
}
......
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
......@@ -93,7 +93,7 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
}
/**
* @throws MethodArgumentNotValidException if validation fails
* Throws MethodArgumentNotValidException if validation fails.
* @throws HttpMessageNotReadableException if {@link RequestBody#required()}
* is {@code true} and there is no body content or if there is no suitable
* converter to read the content with.
......@@ -143,8 +143,8 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
}
@Override
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest,
MethodParameter methodParam, Type paramType) throws IOException, HttpMediaTypeNotSupportedException {
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter methodParam,
Type paramType) throws IOException, HttpMediaTypeNotSupportedException {
final HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
HttpInputMessage inputMessage = new ServletServerHttpRequest(servletRequest);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册