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 c4867fb685..cf33dbb94f 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 @@ -113,6 +113,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe public HttpRequestExecutingMessageHandler(String uri) { this(uri, null); } + /** * Create a handler that will send requests to the provided URI using a provided RestTemplate * @param uri @@ -268,96 +269,47 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe } } - @SuppressWarnings({ "unchecked", "rawtypes" }) private HttpEntity generateHttpRequest(Message message) throws Exception { Assert.notNull(message, "message must not be null"); - HttpEntity httpEntity = null; - - if (this.extractPayload){ - - Object payload = message.getPayload(); - if (payload instanceof HttpEntity) { - httpEntity = (HttpEntity) payload; - } - else { - HttpHeaders httpHeaders = new HttpHeaders(); - this.headerMapper.fromHeaders(message.getHeaders(), httpHeaders); - - if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) { - - 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); - } - } - httpEntity = new HttpEntity(payload, httpHeaders); - } - else { - httpEntity = new HttpEntity(httpHeaders); - } - } - } - else { - HttpHeaders httpHeaders = new HttpHeaders(); - this.headerMapper.fromHeaders(message.getHeaders(), httpHeaders); - httpHeaders.setContentType(new MediaType("application", "x-java-serialized-object")); - if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) { - httpEntity = new HttpEntity(message, httpHeaders); - } - else { - httpEntity = new HttpEntity(httpHeaders); - } - } - return httpEntity; + return (this.extractPayload) ? this.createHttpEntityFromPayload(message) + : this.createHttpEntityFromMessage(message); + } + + private HttpEntity createHttpEntityFromPayload(Message message) { + Object payload = message.getPayload(); + if (payload instanceof HttpEntity) { + // payload is already an HttpEntity, just return it as-is + return (HttpEntity) payload; + } + HttpHeaders httpHeaders = new HttpHeaders(); + this.headerMapper.fromHeaders(message.getHeaders(), httpHeaders); + if (!shouldIncludeRequestBody()) { + return new HttpEntity(httpHeaders); + } + // otherwise, we are creating a request with a body and need to deal with the content-type header as well + 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); + } + } + return new HttpEntity(payload, httpHeaders); + } + + private HttpEntity createHttpEntityFromMessage(Message message) { + HttpHeaders httpHeaders = new HttpHeaders(); + this.headerMapper.fromHeaders(message.getHeaders(), httpHeaders); + if (shouldIncludeRequestBody()) { + httpHeaders.setContentType(new MediaType("application", "x-java-serialized-object")); + return new HttpEntity(message, httpHeaders); + } + return new HttpEntity(httpHeaders); } - - - -// @SuppressWarnings({ "unchecked", "rawtypes"}) -// private HttpEntity createHttpEntityWithPayloadAsBody(Message requestMessage) { -// if (requestMessage.getPayload() instanceof HttpEntity) { -// return (HttpEntity) requestMessage.getPayload(); -// } -// HttpHeaders httpHeaders = new HttpHeaders(); -// this.headerMapper.fromHeaders(requestMessage.getHeaders(), httpHeaders); -// Object payload = requestMessage.getPayload(); -// -// if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) { //INT-1951 -// 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); -// } -// } -// if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) { -// return new HttpEntity(payload, httpHeaders); -// } -// return new HttpEntity(httpHeaders); -// } -// -// private HttpEntity createHttpEntityWithMessageAsBody(Message requestMessage) { -// HttpHeaders httpHeaders = new HttpHeaders(); -// -// if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) { //INT-1951 -// httpHeaders.setContentType(new MediaType("application", "x-java-serialized-object")); -// } -// -// return new HttpEntity(requestMessage, httpHeaders); -// } @SuppressWarnings("unchecked") private MediaType resolveContentType(Object content) { @@ -386,11 +338,15 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe return contentType; } + private boolean shouldIncludeRequestBody() { + return !HttpMethod.GET.equals(this.httpMethod); + } + private MediaType resolveContentType(String content, String charset) { return new MediaType("text", "plain", Charset.forName(charset)); } - private MultiValueMap convertToMultiValueMap(Map simpleMap) { + private MultiValueMap convertToMultiValueMap(Map simpleMap) { LinkedMultiValueMap multipartValueMap = new LinkedMultiValueMap(); for (Object key : simpleMap.keySet()) { Object value = simpleMap.get(key); diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandlerTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandlerTests.java index d606d4df5e..66c9be27b8 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandlerTests.java +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/outbound/HttpRequestExecutingMessageHandlerTests.java @@ -531,7 +531,7 @@ public class HttpRequestExecutingMessageHandlerTests { } @Test - public void contentTypeIsNotSet() throws Exception { + public void contentTypeIsNotSetForGetRequest() throws Exception { //GET HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration"); MockRestTemplate template = new MockRestTemplate(); @@ -550,6 +550,11 @@ public class HttpRequestExecutingMessageHandlerTests { HttpEntity request = template.lastRequestEntity.get(); assertNull(request.getHeaders().getContentType()); + /* TODO: reconsider the inclusion of content-type for various HttpMethods (only ignoring for GET as of 2.0.5) + * uncomment code below accordingly (see INT-1951) + */ + + /* //HEAD handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration"); template = new MockRestTemplate(); @@ -603,6 +608,7 @@ public class HttpRequestExecutingMessageHandlerTests { assertEquals("intentional", exception.getCause().getMessage()); request = template.lastRequestEntity.get(); assertNull(request.getHeaders().getContentType()); + */ } public static class City{