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 408e018999..599fd92267 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 @@ -55,6 +55,7 @@ import org.springframework.integration.http.support.DefaultHttpHeaderMapper; import org.springframework.integration.mapping.HeaderMapper; import org.springframework.integration.support.MessageBuilder; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -76,6 +77,7 @@ import org.springframework.web.client.RestTemplate; * @author Oleg Zhurakousky * @author Gary Russell * @author Gunnar Hillert + * @author Artem Bilan * @since 2.0 */ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMessageHandler { @@ -294,9 +296,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe gConversionService.addConverter(new ClassToStringConverter()); gConversionService.addConverter(new ObjectToStringConverter()); - if (conversionService != null) { - this.evaluationContext.setTypeConverter(new StandardTypeConverter(gConversionService)); - } + this.evaluationContext.setTypeConverter(new StandardTypeConverter(gConversionService)); } private class ClassToStringConverter implements Converter, String> { @@ -350,18 +350,18 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe if (this.transferCookies) { this.doConvertSetCookie(headers); } + MessageBuilder replyBuilder = null; if (httpResponse.hasBody()) { Object responseBody = httpResponse.getBody(); - MessageBuilder replyBuilder = (responseBody instanceof Message) ? + replyBuilder = (responseBody instanceof Message) ? MessageBuilder.fromMessage((Message) responseBody) : MessageBuilder.withPayload(responseBody); - replyBuilder.setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, httpResponse.getStatusCode()); - return replyBuilder.copyHeaders(headers).build(); + } else { - return MessageBuilder.withPayload(httpResponse). - copyHeaders(headers).setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, httpResponse.getStatusCode()). - build(); + replyBuilder = MessageBuilder.withPayload(httpResponse); } + replyBuilder.setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, httpResponse.getStatusCode()); + return replyBuilder.copyHeaders(headers).build(); } return null; } @@ -546,7 +546,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe expectedResponseTypeName = this.expectedResponseTypeExpression.getValue(this.evaluationContext, requestMessage, String.class); } if (StringUtils.hasText(expectedResponseTypeName)){ - expectedResponseType = Class.forName(expectedResponseTypeName); + expectedResponseType = ClassUtils.forName(expectedResponseTypeName, ClassUtils.getDefaultClassLoader()); } return expectedResponseType; } diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/config/OutboundResponseTypeTests-context-fail.xml b/spring-integration-http/src/test/java/org/springframework/integration/http/config/OutboundResponseTypeTests-context-fail.xml index 925fdc56fd..81238c5019 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/config/OutboundResponseTypeTests-context-fail.xml +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/config/OutboundResponseTypeTests-context-fail.xml @@ -5,16 +5,16 @@ xmlns:int-http="http://www.springframework.org/schema/integration/http" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd - http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.2.xsd"> + http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd"> + - - - + + diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/config/OutboundResponseTypeTests-context.xml b/spring-integration-http/src/test/java/org/springframework/integration/http/config/OutboundResponseTypeTests-context.xml index dae66323b5..74c1768ed9 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/config/OutboundResponseTypeTests-context.xml +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/config/OutboundResponseTypeTests-context.xml @@ -5,18 +5,23 @@ xmlns:int-http="http://www.springframework.org/schema/integration/http" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd - http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.2.xsd"> + http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd"> - + - + + + + * see https://jira.springsource.org/browse/INT-2397 */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) public class OutboundResponseTypeTests { private static HttpServer server; + private static MyHandler httpHandler; + @Autowired + private QueueChannel replyChannel; + + @Autowired + private MessageChannel requestChannel; + + @Autowired + private MessageChannel resTypeSetChannel; + + @Autowired + private MessageChannel resPrimitiveStringPresentationChannel; + + @Autowired + private MessageChannel resTypeExpressionSetChannel; + @BeforeClass public static void createServer() throws Exception { httpHandler = new MyHandler(); @@ -56,89 +83,84 @@ public class OutboundResponseTypeTests { server.createContext("/testApps/outboundResponse", httpHandler); server.start(); } + @AfterClass public static void stopServer() throws Exception { server.stop(0); } @Test - public void testDefaultResponseType() throws Exception{ - - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "OutboundResponseTypeTests-context.xml", this.getClass()); - - MessageChannel channel = context.getBean("requestChannel", MessageChannel.class); - QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class); - - channel.send(new GenericMessage("Hello")); - Message message = replyChannel.receive(5000); + public void testDefaultResponseType() throws Exception { + this.requestChannel.send(new GenericMessage("Hello")); + Message message = this.replyChannel.receive(5000); assertNotNull(message); assertTrue(message.getPayload() instanceof ResponseEntity); } @Test - public void testWithResponseTypeSet() throws Exception{ - - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "OutboundResponseTypeTests-context.xml", this.getClass()); - - MessageChannel channel = context.getBean("resTypeSetChannel", MessageChannel.class); - QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class); - - channel.send(new GenericMessage("Hello")); - Message message = replyChannel.receive(5000); + public void testWithResponseTypeSet() throws Exception { + this.resTypeSetChannel.send(new GenericMessage("Hello")); + Message message = this.replyChannel.receive(5000); assertNotNull(message); assertTrue(message.getPayload() instanceof String); } @Test - public void testWithResponseTypeExpressionSet() throws Exception{ - - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "OutboundResponseTypeTests-context.xml", this.getClass()); - - MessageChannel channel = context.getBean("resTypeExpressionSetChannel", MessageChannel.class); - QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class); - - channel.send(new GenericMessage("java.lang.String")); - Message message = replyChannel.receive(5000); + public void testWithResponseTypeExpressionSet() throws Exception { + this.resTypeExpressionSetChannel.send(new GenericMessage("java.lang.String")); + Message message = this.replyChannel.receive(5000); assertNotNull(message); assertTrue(message.getPayload() instanceof String); } @Test - public void testWithResponseTypeExpressionSetAsClass() throws Exception{ - - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( - "OutboundResponseTypeTests-context.xml", this.getClass()); - - MessageChannel channel = context.getBean("resTypeExpressionSetChannel", MessageChannel.class); - QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class); - - channel.send(new GenericMessage>(String.class)); - Message message = replyChannel.receive(5000); + public void testWithResponseTypeExpressionSetAsClass() throws Exception { + this.resTypeExpressionSetChannel.send(new GenericMessage>(String.class)); + Message message = this.replyChannel.receive(5000); assertNotNull(message); assertTrue(message.getPayload() instanceof String); } - @Test(expected=BeanDefinitionParsingException.class) - public void testMutuallyExclusivityInMethodAndMethodExpression() throws Exception{ + @Test + public void testInt2706ResponseTypeExpressionAsPrimitive() throws Exception { + this.resTypeExpressionSetChannel.send(new GenericMessage("byte[]")); + Message message = this.replyChannel.receive(5000); + assertNotNull(message); + assertTrue(message.getPayload() instanceof byte[]); + } - new ClassPathXmlApplicationContext( - "OutboundResponseTypeTests-context-fail.xml", this.getClass()); + @Test + public void testInt2706ResponseTypePrimitiveArrayClassAsString() throws Exception { + this.resPrimitiveStringPresentationChannel.send(new GenericMessage("hello".getBytes())); + Message message = this.replyChannel.receive(5000); + assertNotNull(message); + assertTrue(message.getPayload() instanceof byte[]); + } + + @Test + public void testMutuallyExclusivityInMethodAndMethodExpression() throws Exception { + try { + new ClassPathXmlApplicationContext("OutboundResponseTypeTests-context-fail.xml", this.getClass()); + fail("Expected BeansException"); + } + catch (BeansException e) { + assertTrue(e instanceof BeanDefinitionParsingException); + assertTrue(e.getMessage().contains("The 'expected-response-type' and 'expected-response-type-expression' are mutually exclusive")); + } } static class MyHandler implements HttpHandler { + private String httpMethod = "POST"; - public void setHttpMethod(String httpMethod){ + public void setHttpMethod(String httpMethod) { this.httpMethod = httpMethod; } public void handle(HttpExchange t) throws IOException { String requestMethod = t.getRequestMethod(); String response = null; - if (requestMethod.equalsIgnoreCase(this.httpMethod)){ + if (requestMethod.equalsIgnoreCase(this.httpMethod)) { response = httpMethod; t.getResponseHeaders().add("Content-Type", MediaType.TEXT_PLAIN.toString()); //Required for Spring 3.0.x t.sendResponseHeaders(200, response.length());