From bdab24a6b0d9952fe7c61c0e421cddff97ee139b Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 2 Dec 2010 19:50:12 -0500 Subject: [PATCH] INT-1657, INT-1658 outbound http adapters now use an explicitly mapped 'Content-Type' header and only fallback to determining the header value from payload type. Also fixed a casting issue in the MultiValueMap conversion code. --- .../HttpRequestExecutingMessageHandler.java | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/spring-integration-http/src/main/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandler.java b/spring-integration-http/src/main/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandler.java index 4ca9f3cbf4..4e733d0e83 100755 --- a/spring-integration-http/src/main/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandler.java +++ b/spring-integration-http/src/main/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandler.java @@ -18,8 +18,10 @@ package org.springframework.integration.http.outbound; import java.net.URI; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -258,15 +260,17 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe HttpHeaders httpHeaders = new HttpHeaders(); this.headerMapper.fromHeaders(requestMessage.getHeaders(), httpHeaders); Object payload = requestMessage.getPayload(); - - MediaType contentType = (payload instanceof String) ? this.resolveContentType((String) payload, this.charset) - : this.resolveContentType(payload); - if (contentType.equals(MediaType.APPLICATION_FORM_URLENCODED) || contentType.equals(MediaType.MULTIPART_FORM_DATA)){ - if (!(payload instanceof MultiValueMap)){ + if (httpHeaders.getContentType() == null) { + MediaType contentType = (payload instanceof String) ? this.resolveContentType((String) payload, this.charset) + : this.resolveContentType(payload); + httpHeaders.setContentType(contentType); + } + if (MediaType.APPLICATION_FORM_URLENCODED.equals(httpHeaders.getContentType()) || + MediaType.MULTIPART_FORM_DATA.equals(httpHeaders.getContentType())) { + if (!(payload instanceof MultiValueMap)) { payload = this.convertToMultiValueMap((Map) payload); } } - httpHeaders.setContentType(contentType); if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) { return new HttpEntity(payload, httpHeaders); } @@ -288,13 +292,11 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe else if (content instanceof Source) { contentType = MediaType.TEXT_XML; } - else if (content instanceof Map){ - /* - * We need to check separately for MULTIPART as well as URLENCODED simply because - * MultiValueMap is actually valid content for serialization - */ - if (this.isFormData((Map) content)){ - if (this.isMultipart((Map)content)){ + else if (content instanceof Map) { + // We need to check separately for MULTIPART as well as URLENCODED simply because + // MultiValueMap is actually valid content for serialization + if (this.isFormData((Map) content)) { + if (this.isMultipart((Map)content)) { contentType = MediaType.MULTIPART_FORM_DATA; } else { @@ -311,27 +313,26 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe private MediaType resolveContentType(String content, String charset) { return new MediaType("text", "plain", Charset.forName(charset)); } - - + @SuppressWarnings("unchecked") - private MultiValueMap convertToMultiValueMap(Map simpleContentMap){ - + private MultiValueMap convertToMultiValueMap(Map simpleMap) { LinkedMultiValueMap multipartValueMap = new LinkedMultiValueMap(); - for (Object key : simpleContentMap.keySet()) { - Object value = simpleContentMap.get(key); - if (value instanceof Object[]){ + for (Object key : simpleMap.keySet()) { + Object value = simpleMap.get(key); + if (value instanceof Object[]) { Object[] valueArray = (Object[]) value; value = Arrays.asList(valueArray); - } - if (value instanceof Collection){ - multipartValueMap.put(key, (List) value); - } + } + if (value instanceof Collection) { + multipartValueMap.put(key, new ArrayList((Collection) value)); + } else { multipartValueMap.add(key, value); } } return multipartValueMap; } + /** * If all keys are Strings, and some values are not Strings we'll consider * the Map to be multipart/form-data