Example usage for org.springframework.messaging Message getPayload

List of usage examples for org.springframework.messaging Message getPayload

Introduction

In this page you can find the example usage for org.springframework.messaging Message getPayload.

Prototype

T getPayload();

Source Link

Document

Return the message payload.

Usage

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
@SuppressWarnings("unchecked")
public void testLsForNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();//from  w  w w.j  a v a 2 s  . c  o  m

    this.inboundLs.send(new GenericMessage<String>("foo"));
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertThat(receive.getPayload(), instanceOf(List.class));
    List<String> files = (List<String>) receive.getPayload();
    assertEquals(2, files.size());
    assertThat(files, containsInAnyOrder(" ftpSource1.txt", "ftpSource2.txt"));

    FTPFile[] ftpFiles = ftpSessionFactory.getSession().list(null);
    for (FTPFile ftpFile : ftpFiles) {
        if (!ftpFile.isDirectory()) {
            assertTrue(files.contains(ftpFile.getName()));
        }
    }
}

From source file:org.springframework.integration.ftp.outbound.FtpServerOutboundTests.java

@Test
public void testInboundChannelAdapterWithNullDir() throws IOException {
    Session<FTPFile> session = ftpSessionFactory.getSession();
    ((FTPClient) session.getClientInstance()).changeWorkingDirectory("ftpSource");
    session.close();/*from w  w w  .  ja  v a  2  s .co m*/
    this.ftpInbound.start();

    Message<?> message = this.output.receive(10000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(File.class));
    assertEquals(" ftpSource1.txt", ((File) message.getPayload()).getName());

    message = this.output.receive(10000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(File.class));
    assertEquals("ftpSource2.txt", ((File) message.getPayload()).getName());

    assertNull(this.output.receive(10));

    this.ftpInbound.stop();
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testGoodResult() {
    this.whichTest = 0;
    this.handler.handleMessage(new GenericMessage<String>("foo"));
    assertNull(this.output.receive(0));
    this.latch.countDown();
    Message<?> received = this.output.receive(10000);
    assertNotNull(received);//from   w  ww . j  a v a  2 s.c o  m
    assertEquals("reply", received.getPayload());
    assertNull(this.failedCallbackException);
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testGoodResultWithReplyChannelHeader() {
    this.whichTest = 0;
    this.handler.setOutputChannel(null);
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("foo").setReplyChannel(replyChannel).build();
    this.handler.handleMessage(message);
    assertNull(replyChannel.receive(0));
    this.latch.countDown();
    Message<?> received = replyChannel.receive(10000);
    assertNotNull(received);//w ww  .  j av  a2s.c om
    assertEquals("reply", received.getPayload());
    assertNull(this.failedCallbackException);
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testGoodResultWithNoReplyChannelHeaderNoOutput() throws Exception {
    this.whichTest = 0;
    this.handler.setOutputChannel(null);
    QueueChannel errorChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    this.handler.handleMessage(message);
    assertNull(this.output.receive(0));
    this.latch.countDown();
    Message<?> errorMessage = errorChannel.receive(1000);
    assertNotNull(errorMessage);//  w  ww  .  ja  va2  s  .  c  o m
    assertThat(errorMessage.getPayload(), instanceOf(DestinationResolutionException.class));
    assertEquals("no output-channel or replyChannel header available",
            ((Throwable) errorMessage.getPayload()).getMessage());
    assertNull(((MessagingException) errorMessage.getPayload()).getFailedMessage());
    assertNotNull(this.failedCallbackException);
    assertThat(this.failedCallbackException.getMessage(), containsString("or replyChannel header"));
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testRuntimeException() {
    QueueChannel errorChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    this.handler.handleMessage(message);
    assertNull(this.output.receive(0));
    this.whichTest = 1;
    this.latch.countDown();
    Message<?> received = errorChannel.receive(10000);
    assertNotNull(received);/* w  w  w  . j a va2 s . c  om*/
    assertThat(received.getPayload(), instanceOf(MessageHandlingException.class));
    assertEquals("foo", ((Throwable) received.getPayload()).getCause().getMessage());
    assertSame(message, ((MessagingException) received.getPayload()).getFailedMessage());
    assertNull(this.failedCallbackException);
}

From source file:org.springframework.integration.handler.AsyncHandlerTests.java

@Test
public void testMessagingException() {
    QueueChannel errorChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build();
    this.handler.handleMessage(message);
    assertNull(this.output.receive(0));
    this.whichTest = 2;
    this.latch.countDown();
    Message<?> received = errorChannel.receive(10000);
    assertNotNull(received);//from   w w w.j a va 2  s .  c  om
    assertThat(received.getPayload(), instanceOf(MessagingException.class));
    assertSame(message, ((MessagingException) received.getPayload()).getFailedMessage());
    assertNull(this.failedCallbackException);
}

From source file:org.springframework.integration.handler.LambdaMessageProcessor.java

private Object[] buildArgs(Message<?> message) {
    Object[] args = new Object[this.parameterTypes.length];
    for (int i = 0; i < this.parameterTypes.length; i++) {
        Class<?> parameterType = this.parameterTypes[i];
        if (Message.class.isAssignableFrom(parameterType)) {
            args[i] = message;/*from  w ww  . ja  va  2s.  c om*/
        } else if (Map.class.isAssignableFrom(parameterType)) {
            if (message.getPayload() instanceof Map && this.parameterTypes.length == 1) {
                args[i] = message.getPayload();
            } else {
                args[i] = message.getHeaders();
            }
        } else {
            if (this.payloadType != null
                    && !ClassUtils.isAssignable(this.payloadType, message.getPayload().getClass())) {
                if (Message.class.isAssignableFrom(this.payloadType)) {
                    args[i] = message;
                } else {
                    args[i] = this.messageConverter.fromMessage(message, this.payloadType);
                }
            } else {
                args[i] = message.getPayload();
            }
        }
    }
    return args;
}

From source file:org.springframework.integration.history.MessageHistory.java

@SuppressWarnings("unchecked")
public static <T> Message<T> write(Message<T> message, NamedComponent component,
        MessageBuilderFactory messageBuilderFactory) {
    Assert.notNull(message, "Message must not be null");
    Assert.notNull(component, "Component must not be null");
    Properties metadata = extractMetadata(component);
    if (!metadata.isEmpty()) {
        MessageHistory previousHistory = message.getHeaders().get(HEADER_NAME, MessageHistory.class);
        List<Properties> components = (previousHistory != null) ? new ArrayList<Properties>(previousHistory)
                : new ArrayList<Properties>();
        components.add(metadata);//from w  w  w  .  j  a v  a2  s  .co m
        MessageHistory history = new MessageHistory(components);

        if (message instanceof MutableMessage) {
            message.getHeaders().put(HEADER_NAME, history);
        } else if (message instanceof ErrorMessage) {
            IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
            headerAccessor.setHeader(HEADER_NAME, history);
            Throwable payload = ((ErrorMessage) message).getPayload();
            ErrorMessage errorMessage = new ErrorMessage(payload, headerAccessor.toMessageHeaders());
            message = (Message<T>) errorMessage;
        } else if (message instanceof AdviceMessage) {
            IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
            headerAccessor.setHeader(HEADER_NAME, history);
            message = new AdviceMessage<T>(message.getPayload(), headerAccessor.toMessageHeaders(),
                    ((AdviceMessage<?>) message).getInputMessage());
        } else {
            if (!(message instanceof GenericMessage)
                    && (messageBuilderFactory instanceof DefaultMessageBuilderFactory
                            || messageBuilderFactory instanceof MutableMessageBuilderFactory)) {
                if (logger.isWarnEnabled()) {
                    logger.warn("MessageHistory rebuilds the message and produces the result of the ["
                            + messageBuilderFactory + "], not an instance of the provided type ["
                            + message.getClass() + "]. Consider to supply a custom MessageBuilderFactory "
                            + "to retain custom messages during MessageHistory tracking.");
                }
            }
            message = messageBuilderFactory.fromMessage(message).setHeader(HEADER_NAME, history).build();
        }
    }
    return message;
}

From source file:org.springframework.integration.http.inbound.HttpRequestHandlingControllerTests.java

@Test
public void sendOnly() throws Exception {
    QueueChannel requestChannel = new QueueChannel();
    HttpRequestHandlingController controller = new HttpRequestHandlingController(false);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.afterPropertiesSet();//  w w w  .  j a  v a 2 s  . c  o  m
    controller.start();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("hello".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(0, modelAndView.getModel().size());
    Message<?> requestMessage = requestChannel.receive(0);
    assertNotNull(requestMessage);
    assertEquals("hello", requestMessage.getPayload());
}