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

The following examples show how to use org.springframework.messaging.support.MessageBuilder#withPayload() . 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: SimpMessageTypeMessageConditionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<byte[]> message(SimpMessageType messageType) {
	MessageBuilder<byte[]> builder = MessageBuilder.withPayload(new byte[0]);
	if (messageType != null) {
		builder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, messageType);
	}
	return builder.build();
}
 
Example 3
Source File: MessagingMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object fromMessage(javax.jms.Message message) throws JMSException, MessageConversionException {
	Map<String, Object> mappedHeaders = extractHeaders(message);
	Object convertedObject = extractPayload(message);
	MessageBuilder<Object> builder = (convertedObject instanceof org.springframework.messaging.Message ?
			MessageBuilder.fromMessage((org.springframework.messaging.Message<Object>) convertedObject) :
			MessageBuilder.withPayload(convertedObject));
	return builder.copyHeadersIfAbsent(mappedHeaders).build();
}
 
Example 4
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 5
Source File: SimpMessageTypeMessageConditionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Message<byte[]> message(SimpMessageType messageType) {
	MessageBuilder<byte[]> builder = MessageBuilder.withPayload(new byte[0]);
	if (messageType != null) {
		builder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, messageType);
	}
	return builder.build();
}
 
Example 6
Source File: MessagingMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object fromMessage(javax.jms.Message message) throws JMSException, MessageConversionException {
	Map<String, Object> mappedHeaders = extractHeaders(message);
	Object convertedObject = extractPayload(message);
	MessageBuilder<Object> builder = (convertedObject instanceof org.springframework.messaging.Message ?
			MessageBuilder.fromMessage((org.springframework.messaging.Message<Object>) convertedObject) :
			MessageBuilder.withPayload(convertedObject));
	return builder.copyHeadersIfAbsent(mappedHeaders).build();
}
 
Example 7
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 8
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();
}
 
Example 9
Source File: SimpMessageTypeMessageConditionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<byte[]> message(SimpMessageType messageType) {
	MessageBuilder<byte[]> builder = MessageBuilder.withPayload(new byte[0]);
	if (messageType != null) {
		builder.setHeader(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER, messageType);
	}
	return builder.build();
}
 
Example 10
Source File: MessagingMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object fromMessage(javax.jms.Message message) throws JMSException, MessageConversionException {
	if (message == null) {
		return null;
	}
	Map<String, Object> mappedHeaders = this.headerMapper.toHeaders(message);
	Object convertedObject = extractPayload(message);
	MessageBuilder<Object> builder = (convertedObject instanceof org.springframework.messaging.Message) ?
			MessageBuilder.fromMessage((org.springframework.messaging.Message<Object>) convertedObject) :
			MessageBuilder.withPayload(convertedObject);
	return builder.copyHeadersIfAbsent(mappedHeaders).build();
}
 
Example 11
Source File: SimpleJmsHeaderMapperTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private MessageBuilder<String> initBuilder() {
	return MessageBuilder.withPayload("test");
}
 
Example 12
Source File: SimpleJmsHeaderMapperTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private MessageBuilder<String> initBuilder() {
	return MessageBuilder.withPayload("test");
}
 
Example 13
Source File: SimpleJmsHeaderMapperTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private MessageBuilder<String> initBuilder() {
	return MessageBuilder.withPayload("test");
}