Java Code Examples for org.springframework.messaging.support.MessageBuilder#setHeaderIfAbsent()

The following examples show how to use org.springframework.messaging.support.MessageBuilder#setHeaderIfAbsent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
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();
}
 
Example 2
Source File: AbstractMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
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();
}
 
Example 3
Source File: DefaultRocketMQListenerContainer.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private Message<?> doConvert(Object payload, MessageHeaders headers) {
    Message<?> message = this.messageConverter instanceof SmartMessageConverter ?
        ((SmartMessageConverter) this.messageConverter).toMessage(payload, headers, null) :
        this.messageConverter.toMessage(payload, headers);
    if (message == null) {
        String payloadType = payload.getClass().getName();
        Object contentType = headers != null ? headers.get(MessageHeaders.CONTENT_TYPE) : null;
        throw new MessageConversionException("Unable to convert payload with type='" + payloadType +
            "', contentType='" + contentType + "', converter=[" + this.messageConverter + "]");
    }
    MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);
    return builder.build();
}
 
Example 4
Source File: RocketMQTemplate.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
@Override
protected Message<?> doConvert(Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
    Message<?> message = super.doConvert(payload, headers, postProcessor);
    MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);
    return builder.build();
}
 
Example 5
Source File: RocketMQTemplate.java    From spring-boot-starter-rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
protected Message<?> doConvert(Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
    String content;
    if (payload instanceof String) {
        content = (String) payload;
    } else {
        // if payload not as string, use objectMapper change it.
        try {
            content = objectMapper.writeValueAsString(payload);
        } catch (JsonProcessingException e) {
            log.info("convert payload to String failed. payload:{}", payload);
            throw new RuntimeException("convert to payload to String failed.", e);
        }
    }

    MessageBuilder<?> builder = MessageBuilder.withPayload(content);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN);

    Message<?> message = builder.build();
    if (postProcessor != null) {
        message = postProcessor.postProcessMessage(message);
    }
    return message;
}
 
Example 6
Source File: AbstractMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}