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.web.socket.messaging.StompSubProtocolHandler.java

@Override
@Nullable
public String resolveSessionId(Message<?> message) {
    return SimpMessageHeaderAccessor.getSessionId(message.getHeaders());
}

From source file:org.springframework.xd.dirt.integration.bus.MessageBusSupport.java

protected final Message<?> serializePayloadIfNecessary(Message<?> message, MimeType to) {
    Object originalPayload = message.getPayload();
    Object originalContentType = message.getHeaders().get(MessageHeaders.CONTENT_TYPE);
    Object contentType = originalContentType;
    if (to.equals(ALL)) {
        return message;
    } else if (to.equals(APPLICATION_OCTET_STREAM)) {
        //Pass content type as String since some transport adapters will exclude CONTENT_TYPE Header otherwise
        contentType = JavaClassMimeTypeConversion.mimeTypeFromObject(originalPayload).toString();
        Object payload = serializePayloadIfNecessary(originalPayload);
        MessageBuilder<Object> messageBuilder = MessageBuilder.withPayload(payload)
                .copyHeaders(message.getHeaders()).setHeader(MessageHeaders.CONTENT_TYPE, contentType);
        if (originalContentType != null) {
            messageBuilder.setHeader(XdHeaders.XD_ORIGINAL_CONTENT_TYPE, originalContentType);
        }/*from w ww  . j  av a2s. co  m*/
        return messageBuilder.build();
    } else {
        throw new IllegalArgumentException("'to' can only be 'ALL' or 'APPLICATION_OCTET_STREAM'");
    }
}

From source file:org.springframework.xd.dirt.integration.bus.MessageBusSupport.java

protected final Message<?> deserializePayloadIfNecessary(Message<?> message) {
    Message<?> messageToSend = message;
    Object originalPayload = message.getPayload();
    MimeType contentType = contentTypeResolver.resolve(message.getHeaders());
    Object payload = deserializePayload(originalPayload, contentType);
    if (payload != null) {
        MessageBuilder<Object> transformed = MessageBuilder.withPayload(payload)
                .copyHeaders(message.getHeaders());
        Object originalContentType = message.getHeaders().get(XdHeaders.XD_ORIGINAL_CONTENT_TYPE);
        transformed.setHeader(MessageHeaders.CONTENT_TYPE, originalContentType);
        transformed.setHeader(XdHeaders.XD_ORIGINAL_CONTENT_TYPE, null);
        messageToSend = transformed.build();
    }/*w  w w  .java  2 s  .  c o  m*/
    return messageToSend;
}

From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@SuppressWarnings("unchecked")
@Test/*from   w  ww.j ava  2s .  c o  m*/
public void testBatchingAndCompression() throws Exception {
    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    MessageBus bus = getMessageBus();
    Properties properties = new Properties();
    properties.put("deliveryMode", "NON_PERSISTENT");
    properties.put("batchingEnabled", "true");
    properties.put("batchSize", "2");
    properties.put("batchBufferLimit", "100000");
    properties.put("batchTimeout", "30000");
    properties.put("compress", "true");

    DirectChannel output = new DirectChannel();
    output.setBeanName("batchingProducer");
    bus.bindProducer("batching.0", output, properties);

    while (template.receive("xdbus.batching.0") != null) {
    }

    Log logger = spy(TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor.logger", Log.class));
    new DirectFieldAccessor(TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor"))
            .setPropertyValue("logger", logger);
    when(logger.isTraceEnabled()).thenReturn(true);

    assertEquals(Deflater.BEST_SPEED,
            TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor.level"));

    output.send(new GenericMessage<>("foo".getBytes()));
    output.send(new GenericMessage<>("bar".getBytes()));

    Object out = spyOn("batching.0").receive(false);
    assertThat(out, instanceOf(byte[].class));
    assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String((byte[]) out));

    ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
    verify(logger).trace(captor.capture());
    assertThat(captor.getValue().toString(), containsString("Compressed 14 to "));

    QueueChannel input = new QueueChannel();
    input.setBeanName("batchingConsumer");
    bus.bindConsumer("batching.0", input, null);

    output.send(new GenericMessage<>("foo".getBytes()));
    output.send(new GenericMessage<>("bar".getBytes()));

    Message<byte[]> in = (Message<byte[]>) input.receive(10000);
    assertNotNull(in);
    assertEquals("foo", new String(in.getPayload()));
    in = (Message<byte[]>) input.receive(10000);
    assertNotNull(in);
    assertEquals("bar", new String(in.getPayload()));
    assertNull(in.getHeaders().get(AmqpHeaders.DELIVERY_MODE));

    bus.unbindProducers("batching.0");
    bus.unbindConsumers("batching.0");
}

From source file:org.springframework.xd.dirt.stream.FileSourceModuleTests.java

@Test
public void testFileContents() throws IOException {
    deployStream("filecontents", "file --mode=contents --dir=" + sourceDirName + " --fixedDelay=0 | sink");
    MessageTest test = new MessageTest() {

        @Override//from   w ww  .j a  v  a  2  s .com
        public void test(Message<?> message) throws MessagingException {
            byte[] bytes = (byte[]) message.getPayload();
            assertEquals("foo", new String(bytes));
            assertEquals("foo.txt", message.getHeaders().get(FileHeaders.FILENAME, String.class));
            assertEquals(MimeType.valueOf("application/octet-stream"),
                    contentTypeResolver.resolve(message.getHeaders()));
        }
    };
    StreamTestSupport.getSinkInputChannel("filecontents").subscribe(test);
    dropFile("foo.txt");
    test.waitForCompletion(1000);
    undeployStream("filecontents");
    assertTrue(test.getMessageHandled());
}

From source file:org.springframework.xd.dirt.stream.FileSourceModuleTests.java

@Test
public void testFileContentsUpperCase() throws IOException {
    deployStream("filecontentsuppercase",
            "file --mode=CONTENTS --dir=" + sourceDirName + " --fixedDelay=0 | sink");
    MessageTest test = new MessageTest() {

        @Override//from w  w  w  . j a  va2  s  .  c  om
        public void test(Message<?> message) throws MessagingException {
            byte[] bytes = (byte[]) message.getPayload();
            assertEquals("foo", new String(bytes));
            assertEquals("foo.txt", message.getHeaders().get(FileHeaders.FILENAME, String.class));
            assertEquals(MimeType.valueOf("application/octet-stream"),
                    contentTypeResolver.resolve(message.getHeaders()));
        }
    };
    StreamTestSupport.getSinkInputChannel("filecontentsuppercase").subscribe(test);
    dropFile("foo.txt");
    test.waitForCompletion(3000);
    undeployStream("filecontentsuppercase");
    assertTrue(test.getMessageHandled());
}

From source file:org.springframework.xd.dirt.stream.FileSourceModuleTests.java

@Test
public void testFileContentsUsingDefaultMode() throws IOException {
    deployStream("filecontentsdefault", "file --dir=" + sourceDirName + " --fixedDelay=0 | sink");
    MessageTest test = new MessageTest() {

        @Override/*  w ww  .  j  a va 2s.  c  o  m*/
        public void test(Message<?> message) throws MessagingException {
            byte[] bytes = (byte[]) message.getPayload();
            assertEquals("foo", new String(bytes));
            assertEquals("foo.txt", message.getHeaders().get(FileHeaders.FILENAME, String.class));
            assertEquals(MimeType.valueOf("application/octet-stream"),
                    contentTypeResolver.resolve(message.getHeaders()));
        }
    };
    StreamTestSupport.getSinkInputChannel("filecontentsdefault").subscribe(test);
    dropFile("foo.txt");
    test.waitForCompletion(1000);
    undeployStream("filecontentsdefault");
    assertTrue(test.getMessageHandled());
}

From source file:org.springframework.xd.dirt.stream.FileSourceModuleTests.java

@Test
public void testFileContentsAsString() throws IOException {
    deployStream("filestring",
            "file --dir=" + sourceDirName + " --fixedDelay=0 --outputType='text/plain;charset=UTF-8' | sink");
    MessageTest test = new MessageTest() {

        @Override//from   w ww. j ava2s .  co  m
        public void test(Message<?> message) throws MessagingException {
            assertEquals("foo", message.getPayload());
            assertEquals("foo2.txt", message.getHeaders().get(FileHeaders.FILENAME, String.class));
            assertEquals(MimeTypeUtils.APPLICATION_OCTET_STREAM,
                    contentTypeResolver.resolve(message.getHeaders()));
        }
    };
    StreamTestSupport.getSinkInputChannel("filestring").subscribe(test);
    dropFile("foo2.txt");
    test.waitForCompletion(6000);
    StreamTestSupport.getDeployedModule("filestring", 0).stop();
    assertTrue(test.getMessageHandled());
}

From source file:org.springframework.xd.dirt.stream.FileSourceModuleTests.java

@Test
public void testLinesMode() throws IOException {
    deployStream("textLine", "file --mode=lines --dir=" + sourceDirName + " --fixedDelay=0 | sink");
    MessageTest test = new MessageTest() {

        private AtomicInteger counter = new AtomicInteger(0);

        @Override// ww w . j a v a 2  s  . c o  m
        public void test(Message<?> message) throws MessagingException {
            assertTrue("Extected a String", message.getPayload() instanceof String);
            assertEquals("foo", message.getPayload());
            assertEquals("foo1.txt", message.getHeaders().get(FileHeaders.FILENAME, String.class));
            counter.incrementAndGet();
        }

        @Override
        public boolean getMessageHandled() {
            if (counter.get() == 10) {
                super.messageHandled = true;
            }
            return super.messageHandled;
        }

    };
    StreamTestSupport.getSinkInputChannel("textLine").subscribe(test);
    dropFile("foo1.txt", 10);
    test.waitForCompletion(3000);
    undeployStream("textLine");
    assertTrue(test.getMessageHandled());

}

From source file:org.springframework.xd.dirt.stream.FileSourceModuleTests.java

@Test
public void testLinesModeWithMarkersTrue() throws IOException {
    deployStream("textLineWithEmitMarkersTrue",
            "file --mode=lines --withMarkers=true --dir=" + sourceDirName + " --fixedDelay=0 | sink");
    MessageTest test = new MessageTest() {

        private AtomicInteger counter = new AtomicInteger(0);

        private FileMarker startFileMarker = null;

        private FileMarker endFileMarker = null;

        @Override//www. jav a 2  s.  c o m
        public void test(Message<?> message) throws MessagingException {
            if (message.getPayload() instanceof FileMarker) {
                final FileMarker fileMarker = (FileMarker) message.getPayload();
                if (FileMarker.Mark.START.equals(fileMarker.getMark())) {
                    startFileMarker = fileMarker;
                } else if (FileMarker.Mark.END.equals(fileMarker.getMark())) {
                    endFileMarker = fileMarker;
                }
            } else {
                assertTrue("Extected a String", message.getPayload() instanceof String);
                assertEquals("foo", message.getPayload());
                assertEquals("foo1.txt", message.getHeaders().get(FileHeaders.FILENAME, String.class));
                counter.incrementAndGet();
            }
        }

        @Override
        public boolean getMessageHandled() {
            if (counter.get() == 10 && startFileMarker != null && endFileMarker != null) {
                super.messageHandled = true;
            }
            return super.messageHandled;
        }

    };
    StreamTestSupport.getSinkInputChannel("textLineWithEmitMarkersTrue").subscribe(test);
    dropFile("foo1.txt", 10);
    test.waitForCompletion(3000);
    undeployStream("textLineWithEmitMarkersTrue");
    assertTrue(test.getMessageHandled());

}