INT-1760, INT-1759 Http outbound gateway response type

Change the default expected response type to ResponseEntity

Add support for expected-response-type-expression attribute
to accept SpEL to determine expected response type.

INT-1760, INT-1759 add documentation.

INT-1760, INT-1759 polished javadocs and schema doc

INT-1760, INT-1759 more polishing

INT-1760, INT-1759 polished documentation based on PR

INT-1760 Polishing

Fix Chain test.
This commit is contained in:
Oleg Zhurakousky
2012-06-06 12:33:34 -04:00
committed by Gary Russell
parent e6520821d1
commit 6bbe8057ab
12 changed files with 320 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -118,7 +118,7 @@ public class HttpOutboundChannelAdapterParserTests {
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate"));
ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory)
templateAccessor.getPropertyValue("requestFactory");
assertEquals(Boolean.class, handlerAccessor.getPropertyValue("expectedResponseType"));
assertEquals(Boolean.class.getName(), TestUtils.getPropertyValue(handler, "expectedResponseTypeExpression", Expression.class).getValue());
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
Object converterListBean = this.applicationContext.getBean("converterList");
assertEquals(converterListBean, templateAccessor.getPropertyValue("messageConverters"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -108,7 +108,8 @@ public class HttpOutboundGatewayParserTests {
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
Object converterListBean = this.applicationContext.getBean("converterList");
assertEquals(converterListBean, templateAccessor.getPropertyValue("messageConverters"));
assertEquals(String.class, handlerAccessor.getPropertyValue("expectedResponseType"));
assertEquals(String.class.getName(), TestUtils.getPropertyValue(handler, "expectedResponseTypeExpression", Expression.class).getValue());
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test2", uriExpression.getValue());
assertEquals(HttpMethod.PUT.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
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">
<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>
</beans>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
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">
<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="resTypeExpressionSetChannel"
reply-channel="replyChannel"
expected-response-type-expression="payload"/>
<int:channel id="replyChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,154 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.http.config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.ResponseEntity;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
/**
* @author Oleg Zhurakousky
*
* see https://jira.springsource.org/browse/INT-2397
*/
public class OutboundResponseTypeTests {
private static HttpServer server;
private static MyHandler httpHandler;
@BeforeClass
public static void createServer() throws Exception {
httpHandler = new MyHandler();
server = HttpServer.create(new InetSocketAddress(51235), 0);
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);
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);
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);
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);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
}
@Test(expected=BeanDefinitionParsingException.class)
public void testMutuallyExclusivityInMethodAndMethodExpression() throws Exception{
new ClassPathXmlApplicationContext(
"OutboundResponseTypeTests-context-fail.xml", this.getClass());
}
static class MyHandler implements HttpHandler {
private String httpMethod = "POST";
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)){
response = httpMethod;
t.sendResponseHeaders(200, response.length());
}
else {
response = "Request is NOT valid";
t.sendResponseHeaders(404, 0);
}
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}

View File

@@ -22,9 +22,6 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
@@ -34,10 +31,6 @@ import java.util.concurrent.atomic.AtomicReference;
import javax.xml.transform.Source;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
@@ -671,7 +664,7 @@ public class HttpRequestExecutingMessageHandlerTests {
PollableChannel output = ctx.getBean("replyChannel", PollableChannel.class);
Message<?> receive = output.receive();
assertEquals(HttpStatus.OK, receive.getPayload());
assertEquals(HttpStatus.OK, ((ResponseEntity<?>)receive.getPayload()).getStatusCode());
}