Search in sources :

Example 11 with MessageHeaderAccessor

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

the class AbstractMessageConverter method toMessage.

@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint) {
    if (!canConvertTo(payload, headers)) {
        return null;
    }
    payload = convertToInternal(payload, headers, conversionHint);
    if (payload == null) {
        return null;
    }
    MimeType mimeType = getDefaultContentType(payload);
    if (headers != null) {
        MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
        if (accessor != null && accessor.isMutable()) {
            accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
            return MessageBuilder.createMessage(payload, accessor.getMessageHeaders());
        }
    }
    MessageBuilder<?> builder = MessageBuilder.withPayload(payload);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
    return builder.build();
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MimeType(org.springframework.util.MimeType)

Example 12 with MessageHeaderAccessor

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

the class AbstractMethodMessageHandler method handleMessage.

@Override
public void handleMessage(Message<?> message) throws MessagingException {
    String destination = getDestination(message);
    if (destination == null) {
        return;
    }
    String lookupDestination = getLookupDestination(destination);
    if (lookupDestination == null) {
        return;
    }
    MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message);
    headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination);
    headerAccessor.setLeaveMutable(true);
    message = MessageBuilder.createMessage(message.getPayload(), headerAccessor.getMessageHeaders());
    if (logger.isDebugEnabled()) {
        logger.debug("Searching methods to handle " + headerAccessor.getShortLogMessage(message.getPayload()));
    }
    handleMessageInternal(message, lookupDestination);
    headerAccessor.setImmutable();
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor)

Aggregations

MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)12 Test (org.junit.Test)5 MessageHeaders (org.springframework.messaging.MessageHeaders)4 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)3 Message (org.springframework.messaging.Message)2 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 NativeMessageHeaderAccessor (org.springframework.messaging.support.NativeMessageHeaderAccessor)2 MimeType (org.springframework.util.MimeType)2 Method (java.lang.reflect.Method)1 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)1 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1