Search in sources :

Example 26 with AbstractReplyProducingMessageHandler

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

the class HttpRequestHandlingControllerTests method requestReplyWithFullMessageInModel.

@Test
public void requestReplyWithFullMessageInModel() 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.setExtractReplyPayload(false);
    controller.afterPropertiesSet();
    controller.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("abc".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());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertTrue(reply instanceof Message<?>);
    assertEquals("ABC", ((Message<?>) reply).getPayload());
}
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 27 with AbstractReplyProducingMessageHandler

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

the class HttpRequestHandlingMessagingGatewayTests method INT2680DuplicateContentTypeHeader.

@Test
public void INT2680DuplicateContentTypeHeader() throws Exception {
    final DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return MessageBuilder.withPayload("Cartman".getBytes()).setHeader("Content-type", "text/plain").build();
        }
    });
    final List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
    supportedMediaTypes.add(MediaType.TEXT_HTML);
    final ByteArrayHttpMessageConverter messageConverter = new ByteArrayHttpMessageConverter();
    messageConverter.setSupportedMediaTypes(supportedMediaTypes);
    final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(messageConverter);
    final HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setMessageConverters(messageConverters);
    gateway.setRequestChannel(requestChannel);
    gateway.start();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    final ContentTypeCheckingMockHttpServletResponse response = new ContentTypeCheckingMockHttpServletResponse();
    gateway.handleRequest(request, response);
    assertEquals("Cartman", response.getContentAsString());
    /* Before fixing INT2680, 2 content type headers were being written. */
    final List<String> contentTypes = response.getContentTypeList();
    assertEquals("Exptecting only 1 content type being set.", Integer.valueOf(1), Integer.valueOf(contentTypes.size()));
    assertEquals("text/plain", contentTypes.get(0));
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) SerializingHttpMessageConverter(org.springframework.integration.http.converter.SerializingHttpMessageConverter) AbstractHttpMessageConverter(org.springframework.http.converter.AbstractHttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 28 with AbstractReplyProducingMessageHandler

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

the class HttpRequestHandlingMessagingGatewayTests method timeoutErrorFlow.

@Test
public void timeoutErrorFlow() throws Exception {
    QueueChannel requestChannel = new QueueChannel();
    HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setRequestChannel(requestChannel);
    gateway.setReplyTimeout(0);
    DirectChannel errorChannel = new DirectChannel();
    errorChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new GenericMessage<String>("foo", Collections.<String, Object>singletonMap(HttpHeaders.STATUS_CODE, HttpStatus.GATEWAY_TIMEOUT));
        }
    });
    gateway.setErrorChannel(errorChannel);
    gateway.afterPropertiesSet();
    gateway.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");
    MockHttpServletResponse response = new MockHttpServletResponse();
    gateway.handleRequest(request, response);
    Message<?> message = requestChannel.receive(0);
    assertNotNull(message);
    assertEquals(504, response.getStatus());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 29 with AbstractReplyProducingMessageHandler

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

the class ApplicationEventListeningMessageProducerTests method anyApplicationEventCausesExceptionWithErrorHandling.

@Test(expected = MessageHandlingException.class)
public void anyApplicationEventCausesExceptionWithErrorHandling() {
    DirectChannel channel = new DirectChannel();
    channel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            throw new RuntimeException("Failed");
        }
    });
    ApplicationEventListeningMessageProducer adapter = new ApplicationEventListeningMessageProducer();
    adapter.setOutputChannel(channel);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);
    adapter.start();
    adapter.onApplicationEvent(new TestApplicationEvent1());
    Message<?> message = errorChannel.receive(10000);
    assertNotNull(message);
    assertEquals("Failed", ((Exception) message.getPayload()).getCause().getMessage());
    adapter.setErrorChannel(null);
    adapter.onApplicationEvent(new TestApplicationEvent1());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 30 with AbstractReplyProducingMessageHandler

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

the class ImapMailReceiverTests method testIdleChannelAdapterException.

@Test
public void testIdleChannelAdapterException() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("ImapIdleChannelAdapterParserTests-context.xml", ImapIdleChannelAdapterParserTests.class);
    ImapIdleChannelAdapter adapter = context.getBean("simpleAdapter", ImapIdleChannelAdapter.class);
    // ImapMailReceiver receiver = (ImapMailReceiver) TestUtils.getPropertyValue(adapter, "mailReceiver");
    DirectChannel channel = new DirectChannel();
    channel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(org.springframework.messaging.Message<?> requestMessage) {
            throw new RuntimeException("Failed");
        }
    });
    adapter.setOutputChannel(channel);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);
    AbstractMailReceiver receiver = new ImapMailReceiver();
    receiver = spy(receiver);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(IMAPFolder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);
    willAnswer(invocation -> true).given(folder).isOpen();
    willAnswer(invocation -> null).given(receiver).openFolder();
    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
    adapterAccessor.setPropertyValue("mailReceiver", receiver);
    MimeMessage mailMessage = mock(MimeMessage.class);
    Flags flags = mock(Flags.class);
    given(mailMessage.getFlags()).willReturn(flags);
    final Message[] messages = new Message[] { mailMessage };
    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();
    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    adapter.start();
    org.springframework.messaging.Message<?> replMessage = errorChannel.receive(10000);
    assertNotNull(replMessage);
    assertEquals("Failed", ((Exception) replMessage.getPayload()).getCause().getMessage());
    adapter.stop();
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) Flags(javax.mail.Flags) Folder(javax.mail.Folder) IMAPFolder(com.sun.mail.imap.IMAPFolder) MessagingException(javax.mail.MessagingException) FolderClosedException(javax.mail.FolderClosedException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException) Field(java.lang.reflect.Field) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MimeMessage(javax.mail.internet.MimeMessage) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) 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