Skip to content

Commit ad78b5c

Browse files
committed
polishing
1 parent de84703 commit ad78b5c

File tree

2 files changed

+75
-75
lines changed

2 files changed

+75
-75
lines changed

org.springframework.web/src/main/java/org/springframework/http/HttpEntity.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
* </pre>
3838
*
3939
* @author Arjen Poutsma
40+
* @since 3.0.2
4041
* @see org.springframework.web.client.RestTemplate
4142
* @see #getBody()
4243
* @see #getHeaders()
43-
* @since 3.0.2
4444
*/
4545
public class HttpEntity<T> {
4646

@@ -49,10 +49,12 @@ public class HttpEntity<T> {
4949
*/
5050
public static final HttpEntity EMPTY = new HttpEntity();
5151

52+
5253
private final HttpHeaders headers;
5354

5455
private final T body;
5556

57+
5658
/**
5759
* Create a new, empty {@code HttpEntity}.
5860
*/
@@ -62,7 +64,6 @@ private HttpEntity() {
6264

6365
/**
6466
* Create a new {@code HttpEntity} with the given body and no headers.
65-
*
6667
* @param body the entity body
6768
*/
6869
public HttpEntity(T body) {
@@ -71,7 +72,6 @@ public HttpEntity(T body) {
7172

7273
/**
7374
* Create a new {@code HttpEntity} with the given headers and no body.
74-
*
7575
* @param headers the entity headers
7676
*/
7777
public HttpEntity(Map<String, String> headers) {
@@ -80,7 +80,6 @@ public HttpEntity(Map<String, String> headers) {
8080

8181
/**
8282
* Create a new {@code HttpEntity} with the given headers and no body.
83-
*
8483
* @param headers the entity headers
8584
*/
8685
public HttpEntity(MultiValueMap<String, String> headers) {
@@ -89,8 +88,7 @@ public HttpEntity(MultiValueMap<String, String> headers) {
8988

9089
/**
9190
* Create a new {@code HttpEntity} with the given body and {@code Content-Type} header value.
92-
*
93-
* @param body the entity body
91+
* @param body the entity body
9492
* @param contentType the value of the {@code Content-Type header}
9593
*/
9694
public HttpEntity(T body, MediaType contentType) {
@@ -99,8 +97,7 @@ public HttpEntity(T body, MediaType contentType) {
9997

10098
/**
10199
* Create a new {@code HttpEntity} with the given body and headers.
102-
*
103-
* @param body the entity body
100+
* @param body the entity body
104101
* @param headers the entity headers
105102
*/
106103
public HttpEntity(T body, Map<String, String> headers) {
@@ -109,8 +106,7 @@ public HttpEntity(T body, Map<String, String> headers) {
109106

110107
/**
111108
* Create a new {@code HttpEntity} with the given body and headers.
112-
*
113-
* @param body the entity body
109+
* @param body the entity body
114110
* @param headers the entity headers
115111
*/
116112
public HttpEntity(T body, MultiValueMap<String, String> headers) {
@@ -122,6 +118,29 @@ public HttpEntity(T body, MultiValueMap<String, String> headers) {
122118
this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
123119
}
124120

121+
122+
/**
123+
* Returns the headers of this entity.
124+
*/
125+
public HttpHeaders getHeaders() {
126+
return this.headers;
127+
}
128+
129+
/**
130+
* Returns the body of this entity.
131+
*/
132+
public T getBody() {
133+
return this.body;
134+
}
135+
136+
/**
137+
* Indicates whether this entity has a body.
138+
*/
139+
public boolean hasBody() {
140+
return (this.body != null);
141+
}
142+
143+
125144
private static MultiValueMap<String, String> toMultiValueMap(Map<String, String> map) {
126145
if (map == null) {
127146
return null;
@@ -144,25 +163,4 @@ private static MultiValueMap<String, String> toMultiValueMap(MediaType contentTy
144163
}
145164
}
146165

147-
/**
148-
* Returns the headers of this entity.
149-
*/
150-
public HttpHeaders getHeaders() {
151-
return headers;
152-
}
153-
154-
/**
155-
* Returns the body of this entity.
156-
*/
157-
public T getBody() {
158-
return body;
159-
}
160-
161-
/**
162-
* Indicates whether this entity has a body.
163-
*/
164-
public boolean hasBody() {
165-
return body != null;
166-
}
167-
168166
}

org.springframework.web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,44 @@ public final Object invokeHandlerMethod(Method handlerMethod, Object handler,
185185
}
186186
}
187187

188-
@SuppressWarnings("unchecked")
188+
public final void updateModelAttributes(Object handler, Map<String, Object> mavModel,
189+
ExtendedModelMap implicitModel, NativeWebRequest webRequest) throws Exception {
190+
191+
if (this.methodResolver.hasSessionAttributes() && this.sessionStatus.isComplete()) {
192+
for (String attrName : this.methodResolver.getActualSessionAttributeNames()) {
193+
this.sessionAttributeStore.cleanupAttribute(webRequest, attrName);
194+
}
195+
}
196+
197+
// Expose model attributes as session attributes, if required.
198+
// Expose BindingResults for all attributes, making custom editors available.
199+
Map<String, Object> model = (mavModel != null ? mavModel : implicitModel);
200+
try {
201+
for (String attrName : new HashSet<String>(model.keySet())) {
202+
Object attrValue = model.get(attrName);
203+
boolean isSessionAttr =
204+
this.methodResolver.isSessionAttribute(attrName, (attrValue != null ? attrValue.getClass() : null));
205+
if (isSessionAttr && !this.sessionStatus.isComplete()) {
206+
this.sessionAttributeStore.storeAttribute(webRequest, attrName, attrValue);
207+
}
208+
if (!attrName.startsWith(BindingResult.MODEL_KEY_PREFIX) &&
209+
(isSessionAttr || isBindingCandidate(attrValue))) {
210+
String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attrName;
211+
if (mavModel != null && !model.containsKey(bindingResultKey)) {
212+
WebDataBinder binder = createBinder(webRequest, attrValue, attrName);
213+
initBinder(handler, attrName, binder, webRequest);
214+
mavModel.put(bindingResultKey, binder.getBindingResult());
215+
}
216+
}
217+
}
218+
}
219+
catch (InvocationTargetException ex) {
220+
// User-defined @InitBinder method threw an exception...
221+
ReflectionUtils.rethrowException(ex.getTargetException());
222+
}
223+
}
224+
225+
189226
private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,
190227
NativeWebRequest webRequest, ExtendedModelMap implicitModel) throws Exception {
191228

@@ -274,7 +311,7 @@ else if (SessionStatus.class.isAssignableFrom(paramType)) {
274311
args[i] = this.sessionStatus;
275312
}
276313
else if (HttpEntity.class.isAssignableFrom(paramType)) {
277-
args[i] = resolveHttpEntityRequest(methodParam, webRequest, handler);
314+
args[i] = resolveHttpEntityRequest(methodParam, webRequest);
278315
}
279316
else if (Errors.class.isAssignableFrom(paramType)) {
280317
throw new IllegalStateException("Errors/BindingResult argument declared " +
@@ -546,20 +583,22 @@ private Map resolveRequestHeaderMap(Class<? extends Map> mapType, NativeWebReque
546583
*/
547584
protected Object resolveRequestBody(MethodParameter methodParam, NativeWebRequest webRequest, Object handler)
548585
throws Exception {
586+
549587
return readWithMessageConverters(methodParam, createHttpInputMessage(webRequest), methodParam.getParameterType());
550588
}
551589

552-
@SuppressWarnings("unchecked")
553-
private HttpEntity resolveHttpEntityRequest(MethodParameter methodParam, NativeWebRequest webRequest, Object handler)
554-
throws Exception {
590+
private HttpEntity resolveHttpEntityRequest(MethodParameter methodParam, NativeWebRequest webRequest)
591+
throws Exception {
592+
555593
HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
556594
Class<?> paramType = getHttpEntityType(methodParam);
557595
Object body = readWithMessageConverters(methodParam, inputMessage, paramType);
558-
return new HttpEntity(body, inputMessage.getHeaders());
596+
return new HttpEntity<Object>(body, inputMessage.getHeaders());
559597
}
560598

561599
private Object readWithMessageConverters(MethodParameter methodParam, HttpInputMessage inputMessage, Class paramType)
562-
throws Exception{
600+
throws Exception {
601+
563602
MediaType contentType = inputMessage.getHeaders().getContentType();
564603
if (contentType == null) {
565604
StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType()));
@@ -717,43 +756,6 @@ else if (this.methodResolver.isSessionAttribute(name, paramType)) {
717756
return binder;
718757
}
719758

720-
@SuppressWarnings("unchecked")
721-
public final void updateModelAttributes(Object handler, Map<String, Object> mavModel,
722-
ExtendedModelMap implicitModel, NativeWebRequest webRequest) throws Exception {
723-
724-
if (this.methodResolver.hasSessionAttributes() && this.sessionStatus.isComplete()) {
725-
for (String attrName : this.methodResolver.getActualSessionAttributeNames()) {
726-
this.sessionAttributeStore.cleanupAttribute(webRequest, attrName);
727-
}
728-
}
729-
730-
// Expose model attributes as session attributes, if required.
731-
// Expose BindingResults for all attributes, making custom editors available.
732-
Map<String, Object> model = (mavModel != null ? mavModel : implicitModel);
733-
try {
734-
for (String attrName : new HashSet<String>(model.keySet())) {
735-
Object attrValue = model.get(attrName);
736-
boolean isSessionAttr =
737-
this.methodResolver.isSessionAttribute(attrName, (attrValue != null ? attrValue.getClass() : null));
738-
if (isSessionAttr && !this.sessionStatus.isComplete()) {
739-
this.sessionAttributeStore.storeAttribute(webRequest, attrName, attrValue);
740-
}
741-
if (!attrName.startsWith(BindingResult.MODEL_KEY_PREFIX) &&
742-
(isSessionAttr || isBindingCandidate(attrValue))) {
743-
String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attrName;
744-
if (mavModel != null && !model.containsKey(bindingResultKey)) {
745-
WebDataBinder binder = createBinder(webRequest, attrValue, attrName);
746-
initBinder(handler, attrName, binder, webRequest);
747-
mavModel.put(bindingResultKey, binder.getBindingResult());
748-
}
749-
}
750-
}
751-
}
752-
catch (InvocationTargetException ex) {
753-
// User-defined @InitBinder method threw an exception...
754-
ReflectionUtils.rethrowException(ex.getTargetException());
755-
}
756-
}
757759

758760
/**
759761
* Determine whether the given value qualifies as a "binding candidate", i.e. might potentially be subject to

0 commit comments

Comments
 (0)