Search in sources :

Example 41 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class MessageHeaderAccessorTests method existingHeadersModification.

@Test
public void existingHeadersModification() throws InterruptedException {
    Map<String, Object> map = new HashMap<>();
    map.put("foo", "bar");
    map.put("bar", "baz");
    GenericMessage<String> message = new GenericMessage<>("payload", map);
    Thread.sleep(50);
    MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
    accessor.setHeader("foo", "BAR");
    MessageHeaders actual = accessor.getMessageHeaders();
    assertEquals(3, actual.size());
    assertNotEquals(message.getHeaders().getId(), actual.getId());
    assertEquals("BAR", actual.get("foo"));
    assertEquals("baz", actual.get("bar"));
}
Also used : HashMap(java.util.HashMap) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 42 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class MessageHeaderAccessorTests method leaveMutableDefaultBehavior.

@Test
public void leaveMutableDefaultBehavior() {
    MessageHeaderAccessor accessor = new MessageHeaderAccessor();
    accessor.setHeader("foo", "bar");
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<?> message = MessageBuilder.createMessage("payload", headers);
    this.thrown.expect(IllegalStateException.class);
    this.thrown.expectMessage("Already immutable");
    accessor.setLeaveMutable(true);
    this.thrown.expect(IllegalStateException.class);
    this.thrown.expectMessage("Already immutable");
    accessor.setHeader("foo", "baz");
    assertEquals("bar", headers.get("foo"));
    assertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class));
}
Also used : MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 43 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class MessageHeaderAccessorTests method copyHeadersIfAbsent.

@Test
public void copyHeadersIfAbsent() {
    Map<String, Object> map1 = new HashMap<>();
    map1.put("foo", "bar");
    GenericMessage<String> message = new GenericMessage<>("payload", map1);
    MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
    Map<String, Object> map2 = new HashMap<>();
    map2.put("foo", "BAR");
    map2.put("bar", "baz");
    accessor.copyHeadersIfAbsent(map2);
    MessageHeaders actual = accessor.getMessageHeaders();
    assertEquals(3, actual.size());
    assertEquals("bar", actual.get("foo"));
    assertEquals("baz", actual.get("bar"));
}
Also used : HashMap(java.util.HashMap) MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 44 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class StompHeaderAccessorTests method messageIdAndTimestampDefaultBehavior.

@Test
public void messageIdAndTimestampDefaultBehavior() {
    StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.SEND);
    MessageHeaders headers = headerAccessor.getMessageHeaders();
    assertNull(headers.getId());
    assertNull(headers.getTimestamp());
}
Also used : MessageHeaders(org.springframework.messaging.MessageHeaders) Test(org.junit.Test)

Example 45 with MessageHeaders

use of org.springframework.messaging.MessageHeaders in project spring-framework by spring-projects.

the class SimpleJmsHeaderMapper method toHeaders.

@Override
public MessageHeaders toHeaders(javax.jms.Message jmsMessage) {
    Map<String, Object> headers = new HashMap<>();
    try {
        try {
            String correlationId = jmsMessage.getJMSCorrelationID();
            if (correlationId != null) {
                headers.put(JmsHeaders.CORRELATION_ID, correlationId);
            }
        } catch (Exception ex) {
            logger.info("Failed to read JMSCorrelationID property - skipping", ex);
        }
        try {
            Destination destination = jmsMessage.getJMSDestination();
            if (destination != null) {
                headers.put(JmsHeaders.DESTINATION, destination);
            }
        } catch (Exception ex) {
            logger.info("Failed to read JMSDestination property - skipping", ex);
        }
        try {
            int deliveryMode = jmsMessage.getJMSDeliveryMode();
            headers.put(JmsHeaders.DELIVERY_MODE, deliveryMode);
        } catch (Exception ex) {
            logger.info("Failed to read JMSDeliveryMode property - skipping", ex);
        }
        try {
            long expiration = jmsMessage.getJMSExpiration();
            headers.put(JmsHeaders.EXPIRATION, expiration);
        } catch (Exception ex) {
            logger.info("Failed to read JMSExpiration property - skipping", ex);
        }
        try {
            String messageId = jmsMessage.getJMSMessageID();
            if (messageId != null) {
                headers.put(JmsHeaders.MESSAGE_ID, messageId);
            }
        } catch (Exception ex) {
            logger.info("Failed to read JMSMessageID property - skipping", ex);
        }
        try {
            headers.put(JmsHeaders.PRIORITY, jmsMessage.getJMSPriority());
        } catch (Exception ex) {
            logger.info("Failed to read JMSPriority property - skipping", ex);
        }
        try {
            Destination replyTo = jmsMessage.getJMSReplyTo();
            if (replyTo != null) {
                headers.put(JmsHeaders.REPLY_TO, replyTo);
            }
        } catch (Exception ex) {
            logger.info("Failed to read JMSReplyTo property - skipping", ex);
        }
        try {
            headers.put(JmsHeaders.REDELIVERED, jmsMessage.getJMSRedelivered());
        } catch (Exception ex) {
            logger.info("Failed to read JMSRedelivered property - skipping", ex);
        }
        try {
            String type = jmsMessage.getJMSType();
            if (type != null) {
                headers.put(JmsHeaders.TYPE, type);
            }
        } catch (Exception ex) {
            logger.info("Failed to read JMSType property - skipping", ex);
        }
        try {
            headers.put(JmsHeaders.TIMESTAMP, jmsMessage.getJMSTimestamp());
        } catch (Exception ex) {
            logger.info("Failed to read JMSTimestamp property - skipping", ex);
        }
        Enumeration<?> jmsPropertyNames = jmsMessage.getPropertyNames();
        if (jmsPropertyNames != null) {
            while (jmsPropertyNames.hasMoreElements()) {
                String propertyName = jmsPropertyNames.nextElement().toString();
                try {
                    String headerName = this.toHeaderName(propertyName);
                    headers.put(headerName, jmsMessage.getObjectProperty(propertyName));
                } catch (Exception ex) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Error occurred while mapping JMS property '" + propertyName + "' to Message header", ex);
                    }
                }
            }
        }
    } catch (JMSException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error occurred while mapping from JMS properties to MessageHeaders", ex);
        }
    }
    return new MessageHeaders(headers);
}
Also used : Destination(javax.jms.Destination) HashMap(java.util.HashMap) JMSException(javax.jms.JMSException) MessageHeaders(org.springframework.messaging.MessageHeaders) JMSException(javax.jms.JMSException)

Aggregations

MessageHeaders (org.springframework.messaging.MessageHeaders)54 Test (org.junit.Test)39 HashMap (java.util.HashMap)15 Message (org.springframework.messaging.Message)9 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)6 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)5 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 MimeType (org.springframework.util.MimeType)3 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 MessageConverter (org.springframework.messaging.converter.MessageConverter)2 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)2 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 Method (java.lang.reflect.Method)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Destination (javax.jms.Destination)1 JMSException (javax.jms.JMSException)1