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

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

Introduction

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

Prototype

public MessageBuilder<T> setHeaderIfAbsent(String headerName, Object headerValue) 

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:io.pivotal.poc.dispatcher.MessageDispatcher.java

private String sendMessage(String topic, Object body, HttpHeaders requestHeaders) {
    MessageChannel channel = resolver.resolveDestination(topic + ".input");
    MessageBuilder<?> builder = MessageBuilder.withPayload(body);
    builder.setHeader(MessageHeaders.CONTENT_TYPE, requestHeaders.getContentType());
    for (Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) {
        String headerName = entry.getKey();
        if (requestHeadersToMap.contains(headerName)) {
            builder.setHeaderIfAbsent(headerName,
                    StringUtils.collectionToCommaDelimitedString(entry.getValue()));
        }//  ww  w  . ja  v  a 2  s.co m
    }
    Message<?> message = builder.build();
    channel.send(message);
    return message.getHeaders().getId().toString();
}

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

@Override
@Nullable// w w w.  j a  v  a  2  s.  co m
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();
}

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

@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers) {
    if (!canConvertTo(payload, headers)) {
        return null;
    }//from www  .  ja va2s  .c o  m
    payload = convertToInternal(payload, headers);
    MessageBuilder<?> builder = MessageBuilder.withPayload(payload);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    MimeType mimeType = getDefaultContentType(payload);
    if (mimeType != null) {
        builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
    }
    return builder.build();
}