Example usage for org.springframework.messaging Message getHeaders

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

Introduction

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

Prototype

MessageHeaders getHeaders();

Source Link

Document

Return message headers for the message (never null but may be empty).

Usage

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void existingFileIgnored() throws Exception {
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build();
    QueueChannel output = new QueueChannel();
    File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg");
    FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile));
    handler.setCharset(DEFAULT_ENCODING);
    handler.setOutputChannel(output);//  w  w w  .  j a v  a  2  s.c o m
    handler.setFileExistsMode(FileExistsMode.IGNORE);
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    assertFileContentIs(result, "foo");
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void existingWritingFileIgnored() throws Exception {
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build();
    QueueChannel output = new QueueChannel();
    File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg.writing");
    FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile));
    handler.setCharset(DEFAULT_ENCODING);
    handler.setOutputChannel(output);/*from   w w  w. j  a v  a  2s .c o  m*/
    handler.setFileExistsMode(FileExistsMode.IGNORE);
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    File destFile = (File) result.getPayload();
    assertNotSame(destFile, sourceFile);
    assertThat(destFile.exists(), is(false));
    assertThat(outFile.exists(), is(true));
}

From source file:org.springframework.integration.file.FileWritingMessageHandlerTests.java

@Test
public void existingWritingFileNotIgnoredIfEmptySuffix() throws Exception {
    Message<?> message = MessageBuilder.withPayload(SAMPLE_CONTENT).build();
    QueueChannel output = new QueueChannel();
    File outFile = temp.newFile("/outputDirectory/" + message.getHeaders().getId().toString() + ".msg.writing");
    FileCopyUtils.copy("foo".getBytes(), new FileOutputStream(outFile));
    handler.setCharset(DEFAULT_ENCODING);
    handler.setOutputChannel(output);//  w w  w.j a va  2  s  .  com
    handler.setFileExistsMode(FileExistsMode.IGNORE);
    handler.setTemporaryFileSuffix("");
    handler.handleMessage(message);
    Message<?> result = output.receive(0);
    File destFile = (File) result.getPayload();
    assertNotSame(destFile, sourceFile);
    assertFileContentIsMatching(result);
    assertThat(outFile.exists(), is(true));
    assertFileContentIs(outFile, "foo");
}

From source file:org.springframework.integration.ftp.dsl.FtpTests.java

@Test
public void testFtpInboundStreamFlow() throws Exception {
    QueueChannel out = new QueueChannel();
    StandardIntegrationFlow flow = IntegrationFlows.from(
            Ftp.inboundStreamingAdapter(new FtpRemoteFileTemplate(sessionFactory()))
                    .remoteDirectory("ftpSource").regexFilter(".*\\.txt$"),
            e -> e.id("ftpInboundAdapter").poller(Pollers.fixedDelay(100))).channel(out).get();
    IntegrationFlowRegistration registration = this.flowContext.registration(flow).register();
    Message<?> message = out.receive(10_000);
    assertNotNull(message);/*www  .  jav  a  2  s  .c o  m*/
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" ftpSource1.txt", "ftpSource2.txt"));
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();

    message = out.receive(10_000);
    assertNotNull(message);
    assertThat(message.getPayload(), instanceOf(InputStream.class));
    assertThat(message.getHeaders().get(FileHeaders.REMOTE_FILE), isOneOf(" ftpSource1.txt", "ftpSource2.txt"));
    new IntegrationMessageHeaderAccessor(message).getCloseableResource().close();

    registration.destroy();
}

From source file:org.springframework.integration.ftp.inbound.FtpStreamingMessageSourceTests.java

@SuppressWarnings("unchecked")
@Test//from  w w  w  .j  a v a 2 s.c  o m
public void testAllContents() {
    Message<byte[]> received = (Message<byte[]>) this.data.receive(10000);
    assertNotNull(received);
    assertThat(new String(received.getPayload()), equalTo("source1"));
    String fileInfo = (String) received.getHeaders().get(FileHeaders.REMOTE_FILE_INFO);
    assertThat(fileInfo, containsString("remoteDirectory\":\"ftpSource"));
    assertThat(fileInfo, containsString("permissions\":\"-rw-------"));
    assertThat(fileInfo, containsString("size\":7"));
    assertThat(fileInfo, containsString("directory\":false"));
    assertThat(fileInfo, containsString("filename\":\" ftpSource1.txt"));
    assertThat(fileInfo, containsString("modified\":"));
    assertThat(fileInfo, containsString("link\":false"));
    received = (Message<byte[]>) this.data.receive(10000);
    assertNotNull(received);
    assertThat(new String(received.getPayload()), equalTo("source2"));
    fileInfo = (String) received.getHeaders().get(FileHeaders.REMOTE_FILE_INFO);
    assertThat(fileInfo, containsString("remoteDirectory\":\"ftpSource"));
    assertThat(fileInfo, containsString("permissions\":\"-rw-------"));
    assertThat(fileInfo, containsString("size\":7"));
    assertThat(fileInfo, containsString("directory\":false"));
    assertThat(fileInfo, containsString("filename\":\"ftpSource2.txt"));
    assertThat(fileInfo, containsString("modified\":"));
    assertThat(fileInfo, containsString("link\":false"));

    this.adapter.stop();
    this.source.setFileInfoJson(false);
    this.data.purge(null);
    this.adapter.start();
    received = (Message<byte[]>) this.data.receive(10000);
    assertNotNull(received);
    assertThat(received.getHeaders().get(FileHeaders.REMOTE_FILE_INFO), instanceOf(FtpFileInfo.class));
    this.adapter.stop();
}

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

@Test
public void testStream() {
    String dir = "ftpSource/";
    this.inboundGetStream.send(new GenericMessage<Object>(dir + " ftpSource1.txt"));
    Message<?> result = this.output.receive(1000);
    assertNotNull(result);/*from  w  w  w.j a v a2  s .  c  om*/
    assertEquals("source1", result.getPayload());
    assertEquals("ftpSource/", result.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals(" ftpSource1.txt", result.getHeaders().get(FileHeaders.REMOTE_FILE));

    Session<?> session = (Session<?>) result.getHeaders()
            .get(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE);
    // Returned to cache
    assertTrue(session.isOpen());
    // Raw reading is finished
    assertFalse(TestUtils.getPropertyValue(session, "targetSession.readingRaw", AtomicBoolean.class).get());

    // Check that we can use the same session from cache to read another remote InputStream
    this.inboundGetStream.send(new GenericMessage<Object>(dir + "ftpSource2.txt"));
    result = this.output.receive(1000);
    assertNotNull(result);
    assertEquals("source2", result.getPayload());
    assertEquals("ftpSource/", result.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("ftpSource2.txt", result.getHeaders().get(FileHeaders.REMOTE_FILE));
    assertSame(TestUtils.getPropertyValue(session, "targetSession"), TestUtils.getPropertyValue(
            result.getHeaders().get(IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE), "targetSession"));
}

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  w w. j  ava2 s.  co m*/
        } 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.handler.support.MessagingMethodInvokerHelper.java

private boolean contentTypeIsJson(Message<?> message) {
    Object contentType = message.getHeaders().get(MessageHeaders.CONTENT_TYPE);
    return contentType != null && contentType.toString().contains("json");
}

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

public static MessageHistory read(Message<?> message) {
    return message != null ? message.getHeaders().get(HEADER_NAME, MessageHistory.class) : null;
}

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  ww .  j  a  v a2 s  .  c om*/
        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;
}