Example usage for org.springframework.messaging.support MessageHeaderAccessor setHeaderIfAbsent

List of usage examples for org.springframework.messaging.support MessageHeaderAccessor setHeaderIfAbsent

Introduction

In this page you can find the example usage for org.springframework.messaging.support MessageHeaderAccessor setHeaderIfAbsent.

Prototype

public void setHeaderIfAbsent(String name, Object value) 

Source Link

Document

Set the value for the given header name only if the header name is not already associated with a value.

Usage

From source file:org.springframework.messaging.converter.AbstractMessageConverter.java

@Override
@Nullable/* w  w  w  .  j a v  a2s .com*/
public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers,
        @Nullable Object conversionHint) {
    if (!canConvertTo(payload, headers)) {
        return null;
    }

    Object payloadToUse = convertToInternal(payload, headers, conversionHint);
    if (payloadToUse == null) {
        return null;
    }

    MimeType mimeType = getDefaultContentType(payloadToUse);
    if (headers != null) {
        MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers,
                MessageHeaderAccessor.class);
        if (accessor != null && accessor.isMutable()) {
            if (mimeType != null) {
                accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
            }
            return MessageBuilder.createMessage(payloadToUse, accessor.getMessageHeaders());
        }
    }

    MessageBuilder<?> builder = MessageBuilder.withPayload(payloadToUse);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    if (mimeType != null) {
        builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
    }
    return builder.build();
}