INT-2448 Add view-expression to Http Inbound

https://jira.springsource.org/browse/INT-2488

Previously the view name could be specified; with this
change, either the view name or a view-epression can be
provided, with the reply message being the root object
of the evaluation context.

INT-2448 Polishing

Remove deprecation from view-name.

Reuse evaluation context.

INT-2448 Polishing

PR Comments
This commit is contained in:
Gary Russell
2012-05-22 15:01:09 -04:00
committed by Oleg Zhurakousky
parent 72f5a26682
commit 733e0ead45
10 changed files with 301 additions and 100 deletions

View File

@@ -28,6 +28,12 @@
<inbound-gateway id="inboundController" request-channel="requests" reply-channel="responses" view-name="foo" error-code="oops"/>
<inbound-gateway id="inboundControllerViewExp"
request-channel="requests"
reply-channel="responses"
view-expression="'bar'"
error-code="oops"/>
<inbound-gateway id="withMappedHeaders" request-channel="requests"
mapped-response-headers="abc, xyz"
mapped-request-headers="foo,bar"/>

View File

@@ -37,6 +37,8 @@ import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.converter.Converter;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.http.HttpHeaders;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHeaders;
@@ -61,15 +63,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class HttpInboundGatewayParserTests {
@Autowired
@Qualifier("inboundGateway")
private HttpRequestHandlingMessagingGateway gateway;
@Autowired
@Qualifier("withMappedHeaders")
private HttpRequestHandlingMessagingGateway withMappedHeaders;
@Autowired
@Qualifier("withMappedHeadersAndConverter")
private HttpRequestHandlingMessagingGateway withMappedHeadersAndConverter;
@@ -77,6 +79,9 @@ public class HttpInboundGatewayParserTests {
@Autowired
private HttpRequestHandlingController inboundController;
@Autowired
private HttpRequestHandlingController inboundControllerViewExp;
@Autowired
private SubscribableChannel requests;
@@ -96,7 +101,7 @@ public class HttpInboundGatewayParserTests {
assertEquals(Long.valueOf(1234), TestUtils.getPropertyValue(messagingTemplate, "sendTimeout"));
assertEquals(Long.valueOf(4567), TestUtils.getPropertyValue(messagingTemplate, "receiveTimeout"));
}
@Test(timeout=1000)
public void checkFlow() throws Exception {
requests.subscribe(handlerExpecting(any(Message.class)));
@@ -115,13 +120,25 @@ public class HttpInboundGatewayParserTests {
DirectFieldAccessor accessor = new DirectFieldAccessor(inboundController);
String errorCode = (String) accessor.getPropertyValue("errorCode");
assertEquals("oops", errorCode);
LiteralExpression viewExpression = (LiteralExpression) accessor.getPropertyValue("viewExpression");
assertEquals("foo", viewExpression.getValue());
}
@Test
public void testControllerViewExp() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(inboundControllerViewExp);
String errorCode = (String) accessor.getPropertyValue("errorCode");
assertEquals("oops", errorCode);
SpelExpression viewExpression = (SpelExpression) accessor.getPropertyValue("viewExpression");
assertNotNull(viewExpression);
assertEquals("'bar'", viewExpression.getExpressionString());
}
@Test
public void requestWithHeaders() throws Exception {
DefaultHttpHeaderMapper headerMapper =
DefaultHttpHeaderMapper headerMapper =
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeaders, "headerMapper");
HttpHeaders headers = new HttpHeaders();
headers.set("foo", "foo");
headers.set("bar", "bar");
@@ -130,7 +147,7 @@ public class HttpInboundGatewayParserTests {
assertTrue(map.size() == 2);
assertEquals("foo", map.get("foo"));
assertEquals("bar", map.get("bar"));
Map<String, Object> mapOfHeaders = new HashMap<String, Object>();
mapOfHeaders.put("abc", "abc");
MessageHeaders mh = new MessageHeaders(mapOfHeaders);
@@ -140,12 +157,12 @@ public class HttpInboundGatewayParserTests {
List<String> abc = headers.get("X-abc");
assertEquals("abc", abc.get(0));
}
@Test
public void requestWithHeadersWithConversionService() throws Exception {
DefaultHttpHeaderMapper headerMapper =
DefaultHttpHeaderMapper headerMapper =
(DefaultHttpHeaderMapper) TestUtils.getPropertyValue(withMappedHeadersAndConverter, "headerMapper");
HttpHeaders headers = new HttpHeaders();
headers.set("foo", "foo");
headers.set("bar", "bar");
@@ -154,7 +171,7 @@ public class HttpInboundGatewayParserTests {
assertTrue(map.size() == 2);
assertEquals("foo", map.get("foo"));
assertEquals("bar", map.get("bar"));
Map<String, Object> mapOfHeaders = new HashMap<String, Object>();
mapOfHeaders.put("abc", "abc");
Person person = new Person();
@@ -169,7 +186,7 @@ public class HttpInboundGatewayParserTests {
List<String> personHeaders = headers.get("X-person");
assertEquals("Oleg", personHeaders.get(0));
}
public static class Person{
private String name;
@@ -181,13 +198,13 @@ public class HttpInboundGatewayParserTests {
this.name = name;
}
}
public static class PersonConverter implements Converter<Person, String>{
public String convert(Person source) {
return source.getName();
}
}
}

View File

@@ -19,21 +19,28 @@ package org.springframework.integration.http.inbound;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.Message;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
/**
* @author Mark Fisher
* @author Gary Russell
* @since 2.0
*/
public class HttpRequestHandlingControllerTests {
@@ -57,6 +64,26 @@ public class HttpRequestHandlingControllerTests {
assertEquals("hello", requestMessage.getPayload());
}
@Test
public void sendOnlyViewExpression() throws Exception {
QueueChannel requestChannel = new QueueChannel();
HttpRequestHandlingController controller = new HttpRequestHandlingController(false);
controller.setRequestChannel(requestChannel);
Expression viewExpression = new SpelExpressionParser().parseExpression("'baz'");
controller.setViewExpression(viewExpression);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContent("hello".getBytes());
request.setContentType("text/plain");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView modelAndView = controller.handleRequest(request, response);
assertEquals("baz", modelAndView.getViewName());
assertEquals(0, modelAndView.getModel().size());
Message<?> requestMessage = requestChannel.receive(0);
assertNotNull(requestMessage);
assertEquals("hello", requestMessage.getPayload());
}
@Test
public void requestReply() throws Exception {
DirectChannel requestChannel = new DirectChannel();
@@ -83,6 +110,63 @@ public class HttpRequestHandlingControllerTests {
assertEquals("HELLO", reply);
}
@Test
public void requestReplyViewExpressionString() throws Exception {
DirectChannel requestChannel = new DirectChannel();
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Message<String> handleRequestMessage(Message<?> requestMessage) {
return MessageBuilder.withPayload("foo")
.setHeader("bar", "baz").build();
}
};
requestChannel.subscribe(handler);
HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
controller.setRequestChannel(requestChannel);
Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']");
controller.setViewExpression(viewExpression);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContent("hello".getBytes());
request.setContentType("text/plain");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView modelAndView = controller.handleRequest(request, response);
assertEquals("baz", modelAndView.getViewName());
assertEquals(1, modelAndView.getModel().size());
Object reply = modelAndView.getModel().get("reply");
assertNotNull(reply);
assertEquals("foo", reply);
}
@Test
public void requestReplyViewExpressionView() throws Exception {
final View view = mock(View.class);
DirectChannel requestChannel = new DirectChannel();
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Message<String> handleRequestMessage(Message<?> requestMessage) {
return MessageBuilder.withPayload("foo")
.setHeader("bar", view).build();
}
};
requestChannel.subscribe(handler);
HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
controller.setRequestChannel(requestChannel);
Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']");
controller.setViewExpression(viewExpression);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContent("hello".getBytes());
request.setContentType("text/plain");
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView modelAndView = controller.handleRequest(request, response);
assertSame(view, modelAndView.getView());
assertEquals(1, modelAndView.getModel().size());
Object reply = modelAndView.getModel().get("reply");
assertNotNull(reply);
assertEquals("foo", reply);
}
@Test
public void requestReplyWithCustomReplyKey() throws Exception {
DirectChannel requestChannel = new DirectChannel();

View File

@@ -44,6 +44,7 @@ import org.springframework.util.SerializationUtils;
/**
* @author Mark Fisher
* @author Gary Russell
* @since 2.0
*/
public class HttpRequestHandlingMessagingGatewayTests {
@@ -89,6 +90,7 @@ public class HttpRequestHandlingMessagingGatewayTests {
public void stringExpectedWithReply() throws Exception {
DirectChannel requestChannel = new DirectChannel();
requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage.getPayload().toString().toUpperCase();
}
@@ -110,6 +112,7 @@ public class HttpRequestHandlingMessagingGatewayTests {
public void noAcceptHeaderOnRequest() throws Exception {
DirectChannel requestChannel = new DirectChannel();
requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
return requestMessage.getPayload().toString().toUpperCase();
}
@@ -149,7 +152,7 @@ public class HttpRequestHandlingMessagingGatewayTests {
@Test
public void multiValueParameterMap() throws Exception {
QueueChannel channel = new QueueChannel();
QueueChannel channel = new QueueChannel();
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false);
gateway.setRequestChannel(channel);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
@@ -175,7 +178,7 @@ public class HttpRequestHandlingMessagingGatewayTests {
@Test
public void serializableRequestBody() throws Exception {
QueueChannel channel = new QueueChannel();
QueueChannel channel = new QueueChannel();
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false);
gateway.setRequestPayloadType(TestBean.class);
gateway.setRequestChannel(channel);
@@ -200,7 +203,7 @@ public class HttpRequestHandlingMessagingGatewayTests {
private static class TestHttpMessageConverter extends AbstractHttpMessageConverter<Exception> {
public TestHttpMessageConverter() {
setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
}

View File

@@ -16,10 +16,12 @@
package org.springframework.integration.http.inbound;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.Test;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.Message;
@@ -30,21 +32,20 @@ import org.springframework.integration.core.MessageHandler;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.junit.Assert.assertEquals;
/**
* @author Oleg Zhurakousky
* @author Gary Russell
*/
public class HttpRequestHandlingMessagingGatewayWithPathMappingTests {
private static ExpressionParser PARSER = new SpelExpressionParser();
@Test
public void withoutExpression() throws Exception {
DirectChannel echoChannel = new DirectChannel();
echoChannel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) throws MessagingException {
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
replyChannel.send(message);
@@ -56,18 +57,19 @@ public class HttpRequestHandlingMessagingGatewayWithPathMappingTests {
request.setParameter("foo", "bar");
request.setContent("hello".getBytes());
request.setRequestURI("/fname/bill/lname/clinton");
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setPath("/fname/{f}/lname/{l}");
gateway.setRequestChannel(echoChannel);
MockHttpServletResponse response = new MockHttpServletResponse();
Object result = gateway.doHandleRequest(request, response);
assertEquals("hello", result);
assertTrue(result instanceof Message);
assertEquals("hello", ((Message<?>) result).getPayload());
}
@Test
public void withPayloadExpressionPointingToPathVariable() throws Exception {
DirectChannel echoChannel = new DirectChannel();
@@ -92,16 +94,17 @@ public class HttpRequestHandlingMessagingGatewayWithPathMappingTests {
gateway.setPayloadExpression(PARSER.parseExpression("#pathVariables.f"));
Object result = gateway.doHandleRequest(request, response);
assertEquals("bill", result);
assertTrue(result instanceof Message);
assertEquals("bill", ((Message<?>)result).getPayload());
}
@SuppressWarnings("unchecked")
@Test
public void withoutPayloadExpressionPointingToUriVariables() throws Exception {
DirectChannel echoChannel = new DirectChannel();
echoChannel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) throws MessagingException {
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
replyChannel.send(message);
@@ -109,20 +112,21 @@ public class HttpRequestHandlingMessagingGatewayWithPathMappingTests {
});
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setMethod("POST");
request.setContentType("text/plain");
request.setParameter("foo", "bar");
request.setContent("hello".getBytes());
request.setRequestURI("/fname/bill/lname/clinton");
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
gateway.setPath("/fname/{f}/lname/{l}");
gateway.setRequestChannel(echoChannel);
gateway.setPayloadExpression(PARSER.parseExpression("#pathVariables"));
Object result = gateway.doHandleRequest(request, response);
assertEquals("bill", ((Map<String, Object>)result).get("f"));
assertTrue(result instanceof Message);
assertEquals("bill", ((Map<String, Object>) ((Message<?>)result).getPayload()).get("f"));
}
}