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.sftp.outbound.SftpServerOutboundTests.java

@Test
public void testInt3088MPutRecursive() {
    String dir = "sftpSource/";
    this.inboundMGetRecursive.send(new GenericMessage<Object>(dir + "*"));
    while (output.receive(0) != null) {
        // drain//w ww . j  a  v  a 2  s  .  co  m
    }
    this.inboundMPutRecursive.send(new GenericMessage<File>(getSourceLocalDirectory()));
    @SuppressWarnings("unchecked")
    Message<List<String>> out = (Message<List<String>>) this.output.receive(1000);
    assertNotNull(out);
    assertEquals(3, out.getPayload().size());
    assertThat(out.getPayload().get(0), not(equalTo(out.getPayload().get(1))));
    assertThat(out.getPayload().get(0), anyOf(equalTo("sftpTarget/localSource1.txt"),
            equalTo("sftpTarget/localSource2.txt"), equalTo("sftpTarget/subLocalSource/subLocalSource1.txt")));
    assertThat(out.getPayload().get(1), anyOf(equalTo("sftpTarget/localSource1.txt"),
            equalTo("sftpTarget/localSource2.txt"), equalTo("sftpTarget/subLocalSource/subLocalSource1.txt")));
    assertThat(out.getPayload().get(2), anyOf(equalTo("sftpTarget/localSource1.txt"),
            equalTo("sftpTarget/localSource2.txt"), equalTo("sftpTarget/subLocalSource/subLocalSource1.txt")));
}

From source file:org.springframework.integration.sftp.outbound.SftpServerOutboundTests.java

@Test
public void testInt3088MPutRecursiveFiltered() {
    String dir = "sftpSource/";
    this.inboundMGetRecursive.send(new GenericMessage<Object>(dir + "*"));
    while (output.receive(0) != null) {
        // drain//w  ww.ja v a2 s. c o  m
    }
    this.inboundMPutRecursiveFiltered.send(new GenericMessage<File>(getSourceLocalDirectory()));
    @SuppressWarnings("unchecked")
    Message<List<String>> out = (Message<List<String>>) this.output.receive(1000);
    assertNotNull(out);
    assertEquals(2, out.getPayload().size());
    assertThat(out.getPayload().get(0), not(equalTo(out.getPayload().get(1))));
    assertThat(out.getPayload().get(0), anyOf(equalTo("sftpTarget/localSource1.txt"),
            equalTo("sftpTarget/localSource2.txt"), equalTo("sftpTarget/subLocalSource/subLocalSource1.txt")));
    assertThat(out.getPayload().get(1), anyOf(equalTo("sftpTarget/localSource1.txt"),
            equalTo("sftpTarget/localSource2.txt"), equalTo("sftpTarget/subLocalSource/subLocalSource1.txt")));
}

From source file:org.springframework.integration.sftp.outbound.SftpServerOutboundTests.java

@Test
public void testStream() {
    Session<?> session = spy(this.sessionFactory.getSession());
    session.close();/*from  www. j  a  va 2  s.com*/

    String dir = "sftpSource/";
    this.inboundGetStream.send(new GenericMessage<Object>(dir + " sftpSource1.txt"));
    Message<?> result = this.output.receive(1000);
    assertNotNull(result);
    assertEquals("source1", result.getPayload());
    assertEquals("sftpSource/", result.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals(" sftpSource1.txt", result.getHeaders().get(FileHeaders.REMOTE_FILE));
    verify(session).close();
}

From source file:org.springframework.integration.splunk.support.SplunkExecutor.java

/**
 * Executes the outbound Splunk Operation.
 *///from   w  w  w . j a  v a 2s  .c om
public Object write(final Message<?> message) {
    try {
        SplunkEvent payload = (SplunkEvent) message.getPayload();
        writer.write(payload);
    } catch (Exception e) {
        String errorMsg = "error in writing data into Splunk";
        logger.warn(errorMsg, e);
        throw new MessageHandlingException(message, errorMsg, e);
    }
    return null;
}

From source file:org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper.java

@SuppressWarnings("unchecked")
@Override/*from  w  w w  .  ja v  a  2  s .c o  m*/
public byte[] fromMessage(Message<?> message) throws Exception {
    Map<String, Object> headersToEncode = this.allHeaders ? message.getHeaders()
            : pruneHeaders(message.getHeaders());

    if (this.rawBytes && message.getPayload() instanceof byte[]) {
        return fromBytesPayload((byte[]) message.getPayload(), headersToEncode);
    } else {
        Message<?> messageToEncode = message;

        if (!this.allHeaders) {
            if (!headersToEncode.containsKey(MessageHeaders.ID)) {
                headersToEncode.put(MessageHeaders.ID, MessageHeaders.ID_VALUE_NONE);
            }
            if (!headersToEncode.containsKey(MessageHeaders.TIMESTAMP)) {
                headersToEncode.put(MessageHeaders.TIMESTAMP, -1L);
            }

            messageToEncode = new MutableMessage<>(message.getPayload(), headersToEncode);
        }

        return this.objectMapper.writeValueAsBytes(messageToEncode);
    }
}

From source file:org.springframework.integration.support.MessageBuilder.java

/**
 * Private constructor to be invoked from the static factory methods only.
 *//*  www.ja  v  a 2  s .c  o  m*/
private MessageBuilder(T payload, Message<T> originalMessage) {
    Assert.notNull(payload, "payload must not be null");
    this.payload = payload;
    this.originalMessage = originalMessage;
    this.headerAccessor = new IntegrationMessageHeaderAccessor(originalMessage);
    if (originalMessage != null) {
        this.modified = (!this.payload.equals(originalMessage.getPayload()));
    }
}

From source file:org.springframework.integration.support.MessageBuilder.java

/**
 * Create a builder for a new {@link Message} instance pre-populated with all of the headers copied from the
 * provided message. The payload of the provided Message will also be used as the payload for the new message.
 *
 * @param message the Message from which the payload and all headers will be copied
 * @param <T> The type of the payload.
 * @return A MessageBuilder./*w w w . ja v  a  2  s .  com*/
 */
public static <T> MessageBuilder<T> fromMessage(Message<T> message) {
    Assert.notNull(message, "message must not be null");
    return new MessageBuilder<T>(message.getPayload(), message);
}

From source file:org.springframework.integration.syslog.inbound.SyslogReceivingChannelAdapterTests.java

@Test
public void testAsMapFalse() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(
            SyslogReceivingChannelAdapterFactoryBean.Protocol.udp);
    int port = SocketUtils.findAvailableUdpSocket(1514);
    factory.setPort(port);// ww  w  . ja v a2  s . c  o  m
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.afterPropertiesSet();
    factory.start();
    UdpSyslogReceivingChannelAdapter adapter = (UdpSyslogReceivingChannelAdapter) factory.getObject();
    DefaultMessageConverter defaultMessageConverter = new DefaultMessageConverter();
    defaultMessageConverter.setAsMap(false);
    adapter.setConverter(defaultMessageConverter);
    Thread.sleep(1000);
    byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes("UTF-8");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", port));
    DatagramSocket socket = new DatagramSocket();
    socket.send(packet);
    socket.close();
    Message<?> message = outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("WEBERN", message.getHeaders().get("syslog_HOST"));
    assertEquals("<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE",
            new String((byte[]) message.getPayload(), "UTF-8"));
    adapter.stop();
}

From source file:org.springframework.integration.syslog.inbound.SyslogReceivingChannelAdapterTests.java

@Test
public void testTcpRFC5424() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(
            SyslogReceivingChannelAdapterFactoryBean.Protocol.tcp);
    int port = SocketUtils.findAvailableServerSocket(1514);
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    final CountDownLatch latch = new CountDownLatch(2);
    doAnswer(invocation -> {/*from  ww w .  ja v a 2  s .  co m*/
        latch.countDown();
        return null;
    }).when(publisher).publishEvent(any(ApplicationEvent.class));
    factory.setBeanFactory(mock(BeanFactory.class));
    AbstractServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(port);
    connectionFactory.setDeserializer(new RFC6587SyslogDeserializer());
    connectionFactory.setApplicationEventPublisher(publisher);
    factory.setConnectionFactory(connectionFactory);
    factory.setConverter(new RFC5424MessageConverter());
    factory.afterPropertiesSet();
    factory.start();
    TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject();
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    doReturn(true).when(logger).isDebugEnabled();
    final CountDownLatch sawLog = new CountDownLatch(1);
    doAnswer(invocation -> {
        if (((String) invocation.getArgument(0)).contains("Error on syslog socket")) {
            sawLog.countDown();
        }
        invocation.callRealMethod();
        return null;
    }).when(logger).debug(anyString());
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    Thread.sleep(1000);
    byte[] buf = ("253 <14>1 2014-06-20T09:14:07+00:00 loggregator d0602076-b14a-4c55-852a-981e7afeed38 DEA - "
            + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"]"
            + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"] Removing instance")
                    .getBytes("UTF-8");
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write(buf);
    socket.close();
    assertTrue(sawLog.await(10, TimeUnit.SECONDS));
    @SuppressWarnings("unchecked")
    Message<Map<String, ?>> message = (Message<Map<String, ?>>) outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("loggregator", message.getPayload().get("syslog_HOST"));
    adapter.stop();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}

From source file:org.springframework.integration.syslog.inbound.SyslogReceivingChannelAdapterTests.java

@Test
public void testUdpRFC5424() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(
            SyslogReceivingChannelAdapterFactoryBean.Protocol.udp);
    int port = SocketUtils.findAvailableUdpSocket(1514);
    factory.setPort(port);//from   w  w  w.  java 2s .  co m
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.setConverter(new RFC5424MessageConverter());
    factory.afterPropertiesSet();
    factory.start();
    UdpSyslogReceivingChannelAdapter adapter = (UdpSyslogReceivingChannelAdapter) factory.getObject();
    Thread.sleep(1000);
    byte[] buf = ("<14>1 2014-06-20T09:14:07+00:00 loggregator d0602076-b14a-4c55-852a-981e7afeed38 DEA - "
            + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"]"
            + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"] Removing instance")
                    .getBytes("UTF-8");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", port));
    DatagramSocket socket = new DatagramSocket();
    socket.send(packet);
    socket.close();
    @SuppressWarnings("unchecked")
    Message<Map<String, ?>> message = (Message<Map<String, ?>>) outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("loggregator", message.getPayload().get("syslog_HOST"));
    adapter.stop();
}