diff --git a/org.springframework.web/src/main/java/org/springframework/http/HttpEntity.java b/org.springframework.web/src/main/java/org/springframework/http/HttpEntity.java index 2baf302a74a..683c2b38711 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/HttpEntity.java +++ b/org.springframework.web/src/main/java/org/springframework/http/HttpEntity.java @@ -37,10 +37,10 @@ import org.springframework.util.MultiValueMap; * * * @author Arjen Poutsma + * @since 3.0.2 * @see org.springframework.web.client.RestTemplate * @see #getBody() * @see #getHeaders() - * @since 3.0.2 */ public class HttpEntity { @@ -49,10 +49,12 @@ public class HttpEntity { */ public static final HttpEntity EMPTY = new HttpEntity(); + private final HttpHeaders headers; private final T body; + /** * Create a new, empty {@code HttpEntity}. */ @@ -62,7 +64,6 @@ public class HttpEntity { /** * Create a new {@code HttpEntity} with the given body and no headers. - * * @param body the entity body */ public HttpEntity(T body) { @@ -71,7 +72,6 @@ public class HttpEntity { /** * Create a new {@code HttpEntity} with the given headers and no body. - * * @param headers the entity headers */ public HttpEntity(Map headers) { @@ -80,7 +80,6 @@ public class HttpEntity { /** * Create a new {@code HttpEntity} with the given headers and no body. - * * @param headers the entity headers */ public HttpEntity(MultiValueMap headers) { @@ -89,8 +88,7 @@ public class HttpEntity { /** * Create a new {@code HttpEntity} with the given body and {@code Content-Type} header value. - * - * @param body the entity body + * @param body the entity body * @param contentType the value of the {@code Content-Type header} */ public HttpEntity(T body, MediaType contentType) { @@ -99,8 +97,7 @@ public class HttpEntity { /** * Create a new {@code HttpEntity} with the given body and headers. - * - * @param body the entity body + * @param body the entity body * @param headers the entity headers */ public HttpEntity(T body, Map headers) { @@ -109,8 +106,7 @@ public class HttpEntity { /** * Create a new {@code HttpEntity} with the given body and headers. - * - * @param body the entity body + * @param body the entity body * @param headers the entity headers */ public HttpEntity(T body, MultiValueMap headers) { @@ -122,6 +118,29 @@ public class HttpEntity { this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders); } + + /** + * Returns the headers of this entity. + */ + public HttpHeaders getHeaders() { + return this.headers; + } + + /** + * Returns the body of this entity. + */ + public T getBody() { + return this.body; + } + + /** + * Indicates whether this entity has a body. + */ + public boolean hasBody() { + return (this.body != null); + } + + private static MultiValueMap toMultiValueMap(Map map) { if (map == null) { return null; @@ -144,25 +163,4 @@ public class HttpEntity { } } - /** - * Returns the headers of this entity. - */ - public HttpHeaders getHeaders() { - return headers; - } - - /** - * Returns the body of this entity. - */ - public T getBody() { - return body; - } - - /** - * Indicates whether this entity has a body. - */ - public boolean hasBody() { - return body != null; - } - } diff --git a/org.springframework.web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java b/org.springframework.web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java index ab26a79a6b8..dcb3a0df964 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java +++ b/org.springframework.web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java @@ -185,7 +185,44 @@ public class HandlerMethodInvoker { } } - @SuppressWarnings("unchecked") + public final void updateModelAttributes(Object handler, Map mavModel, + ExtendedModelMap implicitModel, NativeWebRequest webRequest) throws Exception { + + if (this.methodResolver.hasSessionAttributes() && this.sessionStatus.isComplete()) { + for (String attrName : this.methodResolver.getActualSessionAttributeNames()) { + this.sessionAttributeStore.cleanupAttribute(webRequest, attrName); + } + } + + // Expose model attributes as session attributes, if required. + // Expose BindingResults for all attributes, making custom editors available. + Map model = (mavModel != null ? mavModel : implicitModel); + try { + for (String attrName : new HashSet(model.keySet())) { + Object attrValue = model.get(attrName); + boolean isSessionAttr = + this.methodResolver.isSessionAttribute(attrName, (attrValue != null ? attrValue.getClass() : null)); + if (isSessionAttr && !this.sessionStatus.isComplete()) { + this.sessionAttributeStore.storeAttribute(webRequest, attrName, attrValue); + } + if (!attrName.startsWith(BindingResult.MODEL_KEY_PREFIX) && + (isSessionAttr || isBindingCandidate(attrValue))) { + String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attrName; + if (mavModel != null && !model.containsKey(bindingResultKey)) { + WebDataBinder binder = createBinder(webRequest, attrValue, attrName); + initBinder(handler, attrName, binder, webRequest); + mavModel.put(bindingResultKey, binder.getBindingResult()); + } + } + } + } + catch (InvocationTargetException ex) { + // User-defined @InitBinder method threw an exception... + ReflectionUtils.rethrowException(ex.getTargetException()); + } + } + + private Object[] resolveHandlerArguments(Method handlerMethod, Object handler, NativeWebRequest webRequest, ExtendedModelMap implicitModel) throws Exception { @@ -274,7 +311,7 @@ public class HandlerMethodInvoker { args[i] = this.sessionStatus; } else if (HttpEntity.class.isAssignableFrom(paramType)) { - args[i] = resolveHttpEntityRequest(methodParam, webRequest, handler); + args[i] = resolveHttpEntityRequest(methodParam, webRequest); } else if (Errors.class.isAssignableFrom(paramType)) { throw new IllegalStateException("Errors/BindingResult argument declared " + @@ -546,20 +583,22 @@ public class HandlerMethodInvoker { */ protected Object resolveRequestBody(MethodParameter methodParam, NativeWebRequest webRequest, Object handler) throws Exception { + return readWithMessageConverters(methodParam, createHttpInputMessage(webRequest), methodParam.getParameterType()); } - @SuppressWarnings("unchecked") - private HttpEntity resolveHttpEntityRequest(MethodParameter methodParam, NativeWebRequest webRequest, Object handler) - throws Exception { + private HttpEntity resolveHttpEntityRequest(MethodParameter methodParam, NativeWebRequest webRequest) + throws Exception { + HttpInputMessage inputMessage = createHttpInputMessage(webRequest); Class paramType = getHttpEntityType(methodParam); Object body = readWithMessageConverters(methodParam, inputMessage, paramType); - return new HttpEntity(body, inputMessage.getHeaders()); + return new HttpEntity(body, inputMessage.getHeaders()); } private Object readWithMessageConverters(MethodParameter methodParam, HttpInputMessage inputMessage, Class paramType) - throws Exception{ + throws Exception { + MediaType contentType = inputMessage.getHeaders().getContentType(); if (contentType == null) { StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType())); @@ -717,43 +756,6 @@ public class HandlerMethodInvoker { return binder; } - @SuppressWarnings("unchecked") - public final void updateModelAttributes(Object handler, Map mavModel, - ExtendedModelMap implicitModel, NativeWebRequest webRequest) throws Exception { - - if (this.methodResolver.hasSessionAttributes() && this.sessionStatus.isComplete()) { - for (String attrName : this.methodResolver.getActualSessionAttributeNames()) { - this.sessionAttributeStore.cleanupAttribute(webRequest, attrName); - } - } - - // Expose model attributes as session attributes, if required. - // Expose BindingResults for all attributes, making custom editors available. - Map model = (mavModel != null ? mavModel : implicitModel); - try { - for (String attrName : new HashSet(model.keySet())) { - Object attrValue = model.get(attrName); - boolean isSessionAttr = - this.methodResolver.isSessionAttribute(attrName, (attrValue != null ? attrValue.getClass() : null)); - if (isSessionAttr && !this.sessionStatus.isComplete()) { - this.sessionAttributeStore.storeAttribute(webRequest, attrName, attrValue); - } - if (!attrName.startsWith(BindingResult.MODEL_KEY_PREFIX) && - (isSessionAttr || isBindingCandidate(attrValue))) { - String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attrName; - if (mavModel != null && !model.containsKey(bindingResultKey)) { - WebDataBinder binder = createBinder(webRequest, attrValue, attrName); - initBinder(handler, attrName, binder, webRequest); - mavModel.put(bindingResultKey, binder.getBindingResult()); - } - } - } - } - catch (InvocationTargetException ex) { - // User-defined @InitBinder method threw an exception... - ReflectionUtils.rethrowException(ex.getTargetException()); - } - } /** * Determine whether the given value qualifies as a "binding candidate", i.e. might potentially be subject to