polishing
This commit is contained in:
parent
de84703c02
commit
ad78b5cdb5
|
|
@ -37,10 +37,10 @@ import org.springframework.util.MultiValueMap;
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @since 3.0.2
|
||||||
* @see org.springframework.web.client.RestTemplate
|
* @see org.springframework.web.client.RestTemplate
|
||||||
* @see #getBody()
|
* @see #getBody()
|
||||||
* @see #getHeaders()
|
* @see #getHeaders()
|
||||||
* @since 3.0.2
|
|
||||||
*/
|
*/
|
||||||
public class HttpEntity<T> {
|
public class HttpEntity<T> {
|
||||||
|
|
||||||
|
|
@ -49,10 +49,12 @@ public class HttpEntity<T> {
|
||||||
*/
|
*/
|
||||||
public static final HttpEntity EMPTY = new HttpEntity();
|
public static final HttpEntity EMPTY = new HttpEntity();
|
||||||
|
|
||||||
|
|
||||||
private final HttpHeaders headers;
|
private final HttpHeaders headers;
|
||||||
|
|
||||||
private final T body;
|
private final T body;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new, empty {@code HttpEntity}.
|
* Create a new, empty {@code HttpEntity}.
|
||||||
*/
|
*/
|
||||||
|
|
@ -62,7 +64,6 @@ public class HttpEntity<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code HttpEntity} with the given body and no headers.
|
* Create a new {@code HttpEntity} with the given body and no headers.
|
||||||
*
|
|
||||||
* @param body the entity body
|
* @param body the entity body
|
||||||
*/
|
*/
|
||||||
public HttpEntity(T body) {
|
public HttpEntity(T body) {
|
||||||
|
|
@ -71,7 +72,6 @@ public class HttpEntity<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code HttpEntity} with the given headers and no body.
|
* Create a new {@code HttpEntity} with the given headers and no body.
|
||||||
*
|
|
||||||
* @param headers the entity headers
|
* @param headers the entity headers
|
||||||
*/
|
*/
|
||||||
public HttpEntity(Map<String, String> headers) {
|
public HttpEntity(Map<String, String> headers) {
|
||||||
|
|
@ -80,7 +80,6 @@ public class HttpEntity<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code HttpEntity} with the given headers and no body.
|
* Create a new {@code HttpEntity} with the given headers and no body.
|
||||||
*
|
|
||||||
* @param headers the entity headers
|
* @param headers the entity headers
|
||||||
*/
|
*/
|
||||||
public HttpEntity(MultiValueMap<String, String> headers) {
|
public HttpEntity(MultiValueMap<String, String> headers) {
|
||||||
|
|
@ -89,7 +88,6 @@ public class HttpEntity<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code HttpEntity} with the given body and {@code Content-Type} header value.
|
* 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}
|
* @param contentType the value of the {@code Content-Type header}
|
||||||
*/
|
*/
|
||||||
|
|
@ -99,7 +97,6 @@ public class HttpEntity<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code HttpEntity} with the given body and headers.
|
* 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
|
* @param headers the entity headers
|
||||||
*/
|
*/
|
||||||
|
|
@ -109,7 +106,6 @@ public class HttpEntity<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code HttpEntity} with the given body and headers.
|
* 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
|
* @param headers the entity headers
|
||||||
*/
|
*/
|
||||||
|
|
@ -122,6 +118,29 @@ public class HttpEntity<T> {
|
||||||
this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
|
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<String, String> toMultiValueMap(Map<String, String> map) {
|
private static MultiValueMap<String, String> toMultiValueMap(Map<String, String> map) {
|
||||||
if (map == null) {
|
if (map == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -144,25 +163,4 @@ public class HttpEntity<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -185,7 +185,44 @@ public class HandlerMethodInvoker {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
public final void updateModelAttributes(Object handler, Map<String, Object> 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<String, Object> model = (mavModel != null ? mavModel : implicitModel);
|
||||||
|
try {
|
||||||
|
for (String attrName : new HashSet<String>(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,
|
private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,
|
||||||
NativeWebRequest webRequest, ExtendedModelMap implicitModel) throws Exception {
|
NativeWebRequest webRequest, ExtendedModelMap implicitModel) throws Exception {
|
||||||
|
|
||||||
|
|
@ -274,7 +311,7 @@ public class HandlerMethodInvoker {
|
||||||
args[i] = this.sessionStatus;
|
args[i] = this.sessionStatus;
|
||||||
}
|
}
|
||||||
else if (HttpEntity.class.isAssignableFrom(paramType)) {
|
else if (HttpEntity.class.isAssignableFrom(paramType)) {
|
||||||
args[i] = resolveHttpEntityRequest(methodParam, webRequest, handler);
|
args[i] = resolveHttpEntityRequest(methodParam, webRequest);
|
||||||
}
|
}
|
||||||
else if (Errors.class.isAssignableFrom(paramType)) {
|
else if (Errors.class.isAssignableFrom(paramType)) {
|
||||||
throw new IllegalStateException("Errors/BindingResult argument declared " +
|
throw new IllegalStateException("Errors/BindingResult argument declared " +
|
||||||
|
|
@ -546,20 +583,22 @@ public class HandlerMethodInvoker {
|
||||||
*/
|
*/
|
||||||
protected Object resolveRequestBody(MethodParameter methodParam, NativeWebRequest webRequest, Object handler)
|
protected Object resolveRequestBody(MethodParameter methodParam, NativeWebRequest webRequest, Object handler)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
|
||||||
return readWithMessageConverters(methodParam, createHttpInputMessage(webRequest), methodParam.getParameterType());
|
return readWithMessageConverters(methodParam, createHttpInputMessage(webRequest), methodParam.getParameterType());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
private HttpEntity resolveHttpEntityRequest(MethodParameter methodParam, NativeWebRequest webRequest)
|
||||||
private HttpEntity resolveHttpEntityRequest(MethodParameter methodParam, NativeWebRequest webRequest, Object handler)
|
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
|
||||||
HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
|
HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
|
||||||
Class<?> paramType = getHttpEntityType(methodParam);
|
Class<?> paramType = getHttpEntityType(methodParam);
|
||||||
Object body = readWithMessageConverters(methodParam, inputMessage, paramType);
|
Object body = readWithMessageConverters(methodParam, inputMessage, paramType);
|
||||||
return new HttpEntity(body, inputMessage.getHeaders());
|
return new HttpEntity<Object>(body, inputMessage.getHeaders());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object readWithMessageConverters(MethodParameter methodParam, HttpInputMessage inputMessage, Class paramType)
|
private Object readWithMessageConverters(MethodParameter methodParam, HttpInputMessage inputMessage, Class paramType)
|
||||||
throws Exception{
|
throws Exception {
|
||||||
|
|
||||||
MediaType contentType = inputMessage.getHeaders().getContentType();
|
MediaType contentType = inputMessage.getHeaders().getContentType();
|
||||||
if (contentType == null) {
|
if (contentType == null) {
|
||||||
StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType()));
|
StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType()));
|
||||||
|
|
@ -717,43 +756,6 @@ public class HandlerMethodInvoker {
|
||||||
return binder;
|
return binder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public final void updateModelAttributes(Object handler, Map<String, Object> 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<String, Object> model = (mavModel != null ? mavModel : implicitModel);
|
|
||||||
try {
|
|
||||||
for (String attrName : new HashSet<String>(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
|
* Determine whether the given value qualifies as a "binding candidate", i.e. might potentially be subject to
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue