Merge pull request #580 from artembilan/INT-2706

* INT-2706:
  INT-2706: fix 'expected-response-type' primitives
This commit is contained in:
Oleg Zhurakousky
2012-08-10 08:04:40 -04:00
4 changed files with 91 additions and 64 deletions

View File

@@ -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<Class<?>, 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;
}

View File

@@ -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">
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resTypeSetChannel"
reply-channel="replyChannel"
expected-response-type="java.lang.String"
expected-response-type-expression="payload"/>
<int:channel id="replyChannel">
<int:queue/>
</int:channel>

View File

@@ -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">
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="requestChannel"
reply-channel="replyChannel"/>
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resTypeSetChannel"
reply-channel="replyChannel"
expected-response-type="java.lang.String"/>
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resPrimitiveStringPresentationChannel"
reply-channel="replyChannel"
expected-response-type="[B"/>
<int-http:outbound-gateway url="http://localhost:51235/testApps/outboundResponse"
request-channel="resTypeExpressionSetChannel"
reply-channel="replyChannel"

View File

@@ -17,6 +17,7 @@ package org.springframework.integration.http.config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.OutputStream;
@@ -26,6 +27,10 @@ import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.MediaType;
@@ -34,6 +39,7 @@ import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
@@ -41,14 +47,35 @@ import com.sun.net.httpserver.HttpServer;
/**
* @author Oleg Zhurakousky
* @author Artem Bilan
* @since 2.2
*
* see https://jira.springsource.org/browse/INT-2397
* <p/>
* 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<String>("Hello"));
Message<?> message = replyChannel.receive(5000);
public void testDefaultResponseType() throws Exception {
this.requestChannel.send(new GenericMessage<String>("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<String>("Hello"));
Message<?> message = replyChannel.receive(5000);
public void testWithResponseTypeSet() throws Exception {
this.resTypeSetChannel.send(new GenericMessage<String>("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<String>("java.lang.String"));
Message<?> message = replyChannel.receive(5000);
public void testWithResponseTypeExpressionSet() throws Exception {
this.resTypeExpressionSetChannel.send(new GenericMessage<String>("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<Class<?>>(String.class));
Message<?> message = replyChannel.receive(5000);
public void testWithResponseTypeExpressionSetAsClass() throws Exception {
this.resTypeExpressionSetChannel.send(new GenericMessage<Class<?>>(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<String>("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<byte[]>("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());