Commit bdab24a6b0d9952fe7c61c0e421cddff97ee139b
- Diff rendering mode:
- inline
- side by side
spring-integration-http/src/main/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandler.java
(25 / 24)
|   | |||
| 18 | 18 | ||
| 19 | 19 | import java.net.URI; | |
| 20 | 20 | import java.nio.charset.Charset; | |
| 21 | import java.util.ArrayList; | ||
| 21 | 22 | import java.util.Arrays; | |
| 22 | 23 | import java.util.Collection; | |
| 24 | import java.util.Collections; | ||
| 23 | 25 | import java.util.HashMap; | |
| 24 | 26 | import java.util.List; | |
| 25 | 27 | import java.util.Map; | |
| … | … | ||
| 260 | 260 | HttpHeaders httpHeaders = new HttpHeaders(); | |
| 261 | 261 | this.headerMapper.fromHeaders(requestMessage.getHeaders(), httpHeaders); | |
| 262 | 262 | Object payload = requestMessage.getPayload(); | |
| 263 | |||
| 264 | MediaType contentType = (payload instanceof String) ? this.resolveContentType((String) payload, this.charset) | ||
| 265 | : this.resolveContentType(payload); | ||
| 266 | if (contentType.equals(MediaType.APPLICATION_FORM_URLENCODED) || contentType.equals(MediaType.MULTIPART_FORM_DATA)){ | ||
| 267 | if (!(payload instanceof MultiValueMap)){ | ||
| 263 | if (httpHeaders.getContentType() == null) { | ||
| 264 | MediaType contentType = (payload instanceof String) ? this.resolveContentType((String) payload, this.charset) | ||
| 265 | : this.resolveContentType(payload); | ||
| 266 | httpHeaders.setContentType(contentType); | ||
| 267 | } | ||
| 268 | if (MediaType.APPLICATION_FORM_URLENCODED.equals(httpHeaders.getContentType()) || | ||
| 269 | MediaType.MULTIPART_FORM_DATA.equals(httpHeaders.getContentType())) { | ||
| 270 | if (!(payload instanceof MultiValueMap)) { | ||
| 268 | 271 | payload = this.convertToMultiValueMap((Map) payload); | |
| 269 | 272 | } | |
| 270 | 273 | } | |
| 271 | httpHeaders.setContentType(contentType); | ||
| 272 | 274 | if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) { | |
| 273 | 275 | return new HttpEntity<Object>(payload, httpHeaders); | |
| 274 | 276 | } | |
| … | … | ||
| 292 | 292 | else if (content instanceof Source) { | |
| 293 | 293 | contentType = MediaType.TEXT_XML; | |
| 294 | 294 | } | |
| 295 | else if (content instanceof Map){ | ||
| 296 | /* | ||
| 297 | * We need to check separately for MULTIPART as well as URLENCODED simply because | ||
| 298 | * MultiValueMap<Object, Object> is actually valid content for serialization | ||
| 299 | */ | ||
| 300 | if (this.isFormData((Map<Object, ?>) content)){ | ||
| 301 | if (this.isMultipart((Map<String, ?>)content)){ | ||
| 295 | else if (content instanceof Map) { | ||
| 296 | // We need to check separately for MULTIPART as well as URLENCODED simply because | ||
| 297 | // MultiValueMap<Object, Object> is actually valid content for serialization | ||
| 298 | if (this.isFormData((Map<Object, ?>) content)) { | ||
| 299 | if (this.isMultipart((Map<String, ?>)content)) { | ||
| 302 | 300 | contentType = MediaType.MULTIPART_FORM_DATA; | |
| 303 | 301 | } | |
| 304 | 302 | else { | |
| … | … | ||
| 313 | 313 | private MediaType resolveContentType(String content, String charset) { | |
| 314 | 314 | return new MediaType("text", "plain", Charset.forName(charset)); | |
| 315 | 315 | } | |
| 316 | |||
| 317 | |||
| 316 | |||
| 318 | 317 | @SuppressWarnings("unchecked") | |
| 319 | private MultiValueMap<Object, Object> convertToMultiValueMap(Map<Object, Object> simpleContentMap){ | ||
| 320 | |||
| 318 | private MultiValueMap<Object, Object> convertToMultiValueMap(Map<Object, Object> simpleMap) { | ||
| 321 | 319 | LinkedMultiValueMap<Object, Object> multipartValueMap = new LinkedMultiValueMap<Object, Object>(); | |
| 322 | for (Object key : simpleContentMap.keySet()) { | ||
| 323 | Object value = simpleContentMap.get(key); | ||
| 324 | if (value instanceof Object[]){ | ||
| 320 | for (Object key : simpleMap.keySet()) { | ||
| 321 | Object value = simpleMap.get(key); | ||
| 322 | if (value instanceof Object[]) { | ||
| 325 | 323 | Object[] valueArray = (Object[]) value; | |
| 326 | 324 | value = Arrays.asList(valueArray); | |
| 327 | } | ||
| 328 | if (value instanceof Collection){ | ||
| 329 | multipartValueMap.put(key, (List<Object>) value); | ||
| 330 | } | ||
| 325 | } | ||
| 326 | if (value instanceof Collection) { | ||
| 327 | multipartValueMap.put(key, new ArrayList<Object>((Collection<?>) value)); | ||
| 328 | } | ||
| 331 | 329 | else { | |
| 332 | 330 | multipartValueMap.add(key, value); | |
| 333 | 331 | } | |
| 334 | 332 | } | |
| 335 | 333 | return multipartValueMap; | |
| 336 | 334 | } | |
| 335 | |||
| 337 | 336 | /** | |
| 338 | 337 | * If all keys are Strings, and some values are not Strings we'll consider | |
| 339 | 338 | * the Map to be multipart/form-data |
Comments
Add your comment
Please log in to comment



Add a new comment:
Login or create an account to post a comment