Search in sources :

Example 21 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class ContentEnricherTests method simpleProperty.

@Test
public void simpleProperty() {
    QueueChannel replyChannel = new QueueChannel();
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new Source("John", "Doe");
        }
    });
    ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    SpelExpressionParser parser = new SpelExpressionParser();
    Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
    propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
    enricher.setPropertyExpressions(propertyExpressions);
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    Target target = new Target("replace me");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    enricher.handleMessage(requestMessage);
    Message<?> reply = replyChannel.receive(0);
    assertEquals("Doe, John", ((Target) reply.getPayload()).getName());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Example 22 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class ContentEnricherTests method clonePayload.

@Test
public void clonePayload() {
    QueueChannel replyChannel = new QueueChannel();
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new Source("John", "Doe");
        }
    });
    ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    enricher.setShouldClonePayload(true);
    SpelExpressionParser parser = new SpelExpressionParser();
    Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
    propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
    enricher.setPropertyExpressions(propertyExpressions);
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    Target target = new Target("replace me");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    enricher.handleMessage(requestMessage);
    Message<?> reply = replyChannel.receive(0);
    Target result = (Target) reply.getPayload();
    assertEquals("Doe, John", result.getName());
    assertNotSame(target, result);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Example 23 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class HttpRequestHandlingControllerTests method requestReplyWithCustomReplyKey.

@Test
public void requestReplyWithCustomReplyKey() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return requestMessage.getPayload().toString().toUpperCase();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.setReplyKey("myReply");
    controller.afterPropertiesSet();
    controller.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("howdy".getBytes());
    // request.setContentType("text/plain"); //Works in Spring 3.1.2.RELEASE but NOT in 3.0.7.RELEASE
    // Instead do:
    request.addHeader("Content-Type", "text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("foo", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    assertNull(modelAndView.getModel().get("reply"));
    Object reply = modelAndView.getModel().get("myReply");
    assertEquals("HOWDY", reply);
}
Also used : Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) ModelAndView(org.springframework.web.servlet.ModelAndView) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 24 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class HttpRequestHandlingControllerTests method requestReplyViewExpressionView.

@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.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']");
    controller.setViewExpression(viewExpression);
    controller.afterPropertiesSet();
    controller.start();
    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);
}
Also used : Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 25 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class HttpRequestHandlingControllerTests method requestReply.

@Test
public void requestReply() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return requestMessage.getPayload().toString().toUpperCase();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.afterPropertiesSet();
    controller.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    // request.setContentType("text/plain"); //Works in Spring 3.1.2.RELEASE but NOT in 3.0.7.RELEASE
    // Instead do:
    request.addHeader("Content-Type", "text/plain");
    // For Spring 3.0.7.RELEASE the Content must be set,
    request.setContent("hello".getBytes());
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("foo", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertEquals("HELLO", reply);
}
Also used : Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) ModelAndView(org.springframework.web.servlet.ModelAndView) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)52 Test (org.junit.Test)46 BeanFactory (org.springframework.beans.factory.BeanFactory)37 QueueChannel (org.springframework.integration.channel.QueueChannel)29 Message (org.springframework.messaging.Message)28 DirectChannel (org.springframework.integration.channel.DirectChannel)23 GenericMessage (org.springframework.messaging.support.GenericMessage)20 Matchers.containsString (org.hamcrest.Matchers.containsString)19 ErrorMessage (org.springframework.messaging.support.ErrorMessage)18 ArrayList (java.util.ArrayList)14 AdviceMessage (org.springframework.integration.message.AdviceMessage)14 Advice (org.aopalliance.aop.Advice)13 HashMap (java.util.HashMap)12 MessageHandlingException (org.springframework.messaging.MessageHandlingException)12 Expression (org.springframework.expression.Expression)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 MessageHandlingExpressionEvaluatingAdviceException (org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException)9 MessagingException (org.springframework.messaging.MessagingException)9