INT-2397 http-method-expression

Add support for http-method-expression attribute to the HTTP Outbound Gateway/Adapter

INT-2397 polishing

INT-2397 polishing based on PR comments

INT-2397 more polishing based on PR comments

INT-2397 polishing

INT-2397 plishing PR comments

INT-2397 polishing
This commit is contained in:
Oleg Zhurakousky
2012-05-31 20:53:23 -04:00
committed by Gary Russell
parent a011f9dda7
commit b105872bb7
12 changed files with 307 additions and 47 deletions

View File

@@ -28,6 +28,7 @@ import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -98,7 +99,7 @@ public class HttpOutboundChannelAdapterParserTests {
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test1", uriExpression.getValue());
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
}
@@ -127,7 +128,7 @@ public class HttpOutboundChannelAdapterParserTests {
assertEquals(errorHandlerBean, templateAccessor.getPropertyValue("errorHandler"));
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test2/{foo}", uriExpression.getValue());
assertEquals(HttpMethod.GET, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals(HttpMethod.GET.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(false, handlerAccessor.getPropertyValue("extractPayload"));
Map<String, Expression> uriVariableExpressions =
@@ -172,7 +173,7 @@ public class HttpOutboundChannelAdapterParserTests {
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test1", uriExpression.getValue());
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
}
@@ -195,7 +196,7 @@ public class HttpOutboundChannelAdapterParserTests {
SpelExpression expression = (SpelExpression) handlerAccessor.getPropertyValue("uriExpression");
assertNotNull(expression);
assertEquals("'http://localhost/test1'", expression.getExpressionString());
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
}
@@ -218,7 +219,7 @@ public class HttpOutboundChannelAdapterParserTests {
SpelExpression expression = (SpelExpression) handlerAccessor.getPropertyValue("uriExpression");
assertNotNull(expression);
assertEquals("'http://localhost/test1'", expression.getExpressionString());
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
}

View File

@@ -26,6 +26,7 @@ import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -39,6 +40,7 @@ import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.ObjectUtils;
@@ -80,7 +82,7 @@ public class HttpOutboundGatewayParserTests {
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test1", uriExpression.getValue());
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
assertEquals(false, handlerAccessor.getPropertyValue("transferCookies"));
@@ -109,7 +111,7 @@ public class HttpOutboundGatewayParserTests {
assertEquals(String.class, handlerAccessor.getPropertyValue("expectedResponseType"));
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test2", uriExpression.getValue());
assertEquals(HttpMethod.PUT, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals(HttpMethod.PUT.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(false, handlerAccessor.getPropertyValue("extractPayload"));
Object requestFactoryBean = this.applicationContext.getBean("testRequestFactory");
@@ -151,7 +153,7 @@ public class HttpOutboundGatewayParserTests {
SpelExpression expression = (SpelExpression) handlerAccessor.getPropertyValue("uriExpression");
assertNotNull(expression);
assertEquals("'http://localhost/test1'", expression.getExpressionString());
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
assertEquals(false, handlerAccessor.getPropertyValue("transferCookies"));

View File

@@ -0,0 +1,125 @@
/*
* 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.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 HttpOutboundGatewayWithMethodExpression {
private HttpServer server;
private MyHandler httpHandler;
@Before
public void createServer() throws Exception {
httpHandler = new MyHandler();
server = HttpServer.create(new InetSocketAddress(51234), 0);
server.createContext("/testApps/httpMethod", httpHandler);
server.start();
}
@After
public void stopServer() throws Exception {
server.stop(0);
}
@Test
public void testDefaultMethod() throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"http-outbound-gateway-with-httpmethod-expression.xml", this.getClass());
MessageChannel channel = context.getBean("defaultChannel", MessageChannel.class);
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
httpHandler.setHttpMethod("POST");
channel.send(new GenericMessage<String>("Hello"));
Message<?> message = replyChannel.receive(5000);
assertNotNull(message);
assertEquals("POST", message.getPayload());
}
@Test
public void testExplicitlySetMethod() throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"http-outbound-gateway-with-httpmethod-expression.xml", this.getClass());
MessageChannel channel = context.getBean("requestChannel", MessageChannel.class);
QueueChannel replyChannel = context.getBean("replyChannel", QueueChannel.class);
httpHandler.setHttpMethod("GET");
channel.send(new GenericMessage<String>("GET"));
Message<?> message = replyChannel.receive(5000);
assertNotNull(message);
assertEquals("GET", message.getPayload());
}
@Test(expected=BeanDefinitionParsingException.class)
public void testMutuallyExclusivityInMethodAndMethodExpression() throws Exception{
new ClassPathXmlApplicationContext(
"http-outbound-gateway-with-httpmethod-expression-fail.xml", this.getClass());
}
class MyHandler implements HttpHandler {
private 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)){
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

@@ -0,0 +1,23 @@
<?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:51234/testApps/httpMethod"
request-channel="requestChannel"
reply-channel="replyChannel"
expected-response-type="java.lang.String"
http-method-expression="payload"
http-method="GET"/>
<int:channel id="replyChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -0,0 +1,26 @@
<?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:51234/testApps/httpMethod"
request-channel="requestChannel"
reply-channel="replyChannel"
expected-response-type="java.lang.String"
http-method-expression="payload"/>
<int-http:outbound-gateway url="http://localhost:51234/testApps/httpMethod"
request-channel="defaultChannel"
reply-channel="replyChannel"
expected-response-type="java.lang.String"/>
<int:channel id="replyChannel">
<int:queue/>
</int:channel>
</beans>

View File

@@ -30,7 +30,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.integration.Message;
import org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler;
import org.springframework.integration.message.GenericMessage;
/**
@@ -47,6 +46,7 @@ public class UriVariableExpressionTests {
SpelExpressionParser parser = new SpelExpressionParser();
handler.setUriVariableExpressions(Collections.singletonMap("foo", parser.parseExpression("payload")));
handler.setRequestFactory(new SimpleClientHttpRequestFactory() {
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
uriHolder.set(uri);
throw new RuntimeException("intentional");