Java Code Examples for org.springframework.messaging.support.MessageHeaderAccessor#setHeader()

The following examples show how to use org.springframework.messaging.support.MessageHeaderAccessor#setHeader() . 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: MessageMappingMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected Mono<Void> handleMatch(CompositeMessageCondition mapping, HandlerMethod handlerMethod, Message<?> message) {
	Set<String> patterns = mapping.getCondition(DestinationPatternsMessageCondition.class).getPatterns();
	if (!CollectionUtils.isEmpty(patterns)) {
		String pattern = patterns.iterator().next();
		String destination = getDestination(message);
		Assert.state(destination != null, "Missing destination header");
		Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, destination);
		if (!CollectionUtils.isEmpty(vars)) {
			MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
			Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required");
			mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
		}
	}
	return super.handleMatch(mapping, handlerMethod, message);
}
 
Example 2
Source File: SimpAnnotationMethodMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void handleMatch(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod,
		String lookupDestination, Message<?> message) {

	Set<String> patterns = mapping.getDestinationConditions().getPatterns();
	if (!CollectionUtils.isEmpty(patterns)) {
		String pattern = patterns.iterator().next();
		Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, lookupDestination);
		if (!CollectionUtils.isEmpty(vars)) {
			MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
			Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required");
			mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
		}
	}

	try {
		SimpAttributesContextHolder.setAttributesFromMessage(message);
		super.handleMatch(mapping, handlerMethod, lookupDestination, message);
	}
	finally {
		SimpAttributesContextHolder.resetAttributes();
	}
}
 
Example 3
Source File: NegotiatingMessageConverterWrapper.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Override
public Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint) {
	MimeType accepted = headers.get(ACCEPT, MimeType.class);
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.copyHeaders(headers);
	accessor.removeHeader(ACCEPT);
	// Fall back to (concrete) 'contentType' header if 'accept' is not present.
	// MimeType.includes() below should then amount to equality.
	if (accepted == null) {
		accepted = headers.get(MessageHeaders.CONTENT_TYPE, MimeType.class);
	}

	if (accepted != null) {
		for (MimeType supportedConcreteType : delegate.getSupportedMimeTypes()) {
			if (accepted.includes(supportedConcreteType)) {
				// Note the use of setHeader() which will set the value even if already present.
				accessor.setHeader(MessageHeaders.CONTENT_TYPE, supportedConcreteType);
				Message<?> result = delegate.toMessage(payload, accessor.toMessageHeaders(), conversionHint);
				if (result != null) {
					return result;
				}
			}
		}
	}
	return null;
}
 
Example 4
Source File: SimpAnnotationMethodMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected void handleMatch(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod,
		String lookupDestination, Message<?> message) {

	Set<String> patterns = mapping.getDestinationConditions().getPatterns();
	if (!CollectionUtils.isEmpty(patterns)) {
		String pattern = patterns.iterator().next();
		Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, lookupDestination);
		if (!CollectionUtils.isEmpty(vars)) {
			MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
			Assert.state(mha != null && mha.isMutable(), "Mutable MessageHeaderAccessor required");
			mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
		}
	}

	try {
		SimpAttributesContextHolder.setAttributesFromMessage(message);
		super.handleMatch(mapping, handlerMethod, lookupDestination, message);
	}
	finally {
		SimpAttributesContextHolder.resetAttributes();
	}
}
 
Example 5
Source File: AbstractMethodMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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()) +
				", lookupDestination='" + lookupDestination + "'");
	}

	handleMessageInternal(message, lookupDestination);
	headerAccessor.setImmutable();
}
 
Example 6
Source File: SimpAnnotationMethodMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected void handleMatch(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod,
		String lookupDestination, Message<?> message) {

	Set<String> patterns = mapping.getDestinationConditions().getPatterns();
	if (!CollectionUtils.isEmpty(patterns)) {
		String pattern = patterns.iterator().next();
		Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, lookupDestination);
		if (!CollectionUtils.isEmpty(vars)) {
			MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
			Assert.state(mha != null && mha.isMutable());
			mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
		}
	}

	try {
		SimpAttributesContextHolder.setAttributesFromMessage(message);
		super.handleMatch(mapping, handlerMethod, lookupDestination, message);
	}
	finally {
		SimpAttributesContextHolder.resetAttributes();
	}
}
 
Example 7
Source File: MessageHeaderPropagationTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipWrongValueTypeForRemoval() {
	MessageHeaderAccessor carrier = carrier();
	carrier.setHeader(NativeMessageHeaderAccessor.NATIVE_HEADERS,
			"{spanTraceId=[123], spanId=[456], spanSampled=[0]}");
	MessageHeaderPropagation.removeAnyTraceHeaders(carrier,
			Collections.singletonList("b3"));
}
 
Example 8
Source File: GenericMessagingTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void convertAndSendWithSimpMessageHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("key", "value");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	this.template.convertAndSend("channel", "data", headers);
	List<Message<byte[]>> messages = this.messageChannel.getMessages();
	Message<byte[]> message = messages.get(0);

	assertSame(headers, message.getHeaders());
	assertFalse(accessor.isMutable());
}
 
Example 9
Source File: MessageSendingTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void convertAndSendPayloadAndMutableHeadersToDestination() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders messageHeaders = accessor.getMessageHeaders();

	this.template.setMessageConverter(new StringMessageConverter());
	this.template.convertAndSend("somewhere", "payload", messageHeaders);

	MessageHeaders actual = this.template.message.getHeaders();
	assertSame(messageHeaders, actual);
	assertEquals(new MimeType("text", "plain", Charset.forName("UTF-8")), actual.get(MessageHeaders.CONTENT_TYPE));
	assertEquals("bar", actual.get("foo"));
}
 
Example 10
Source File: SimpleMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void toMessageWithPayloadAndMutableHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	Message<?> message = this.converter.toMessage("payload", headers);

	assertEquals("payload", message.getPayload());
	assertSame(headers, message.getHeaders());
	assertEquals("bar", message.getHeaders().get("foo"));
}
 
Example 11
Source File: MessageHeaderPropagationTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipWrongValueTypeForGet() {
	MessageHeaderAccessor carrier = carrier();
	carrier.setHeader(NativeMessageHeaderAccessor.NATIVE_HEADERS,
			"{spanTraceId=[123], spanId=[456], spanSampled=[0]}");
	MessageHeaderPropagation.INSTANCE.get(carrier, "b3");
}
 
Example 12
Source File: GenericMessagingTemplateTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void convertAndSendWithSimpMessageHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("key", "value");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	this.template.convertAndSend("channel", "data", headers);
	List<Message<byte[]>> messages = this.messageChannel.getMessages();
	Message<byte[]> message = messages.get(0);

	assertSame(headers, message.getHeaders());
	assertFalse(accessor.isMutable());
}
 
Example 13
Source File: MessageHeaderPropagationTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipWrongValueTypeForPut() {
	MessageHeaderAccessor carrier = carrier();
	carrier.setHeader(NativeMessageHeaderAccessor.NATIVE_HEADERS,
			"{spanTraceId=[123], spanId=[456], spanSampled=[0]}");
	MessageHeaderPropagation.INSTANCE.put(carrier, "b3", "1234");
}
 
Example 14
Source File: SimpleMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void toMessageWithPayloadAndMutableHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	Message<?> message = this.converter.toMessage("payload", headers);

	assertEquals("payload", message.getPayload());
	assertSame(headers, message.getHeaders());
	assertEquals("bar", message.getHeaders().get("foo"));
}
 
Example 15
Source File: GenericMessagingTemplateTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void convertAndSendWithSimpMessageHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("key", "value");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	this.template.convertAndSend("channel", "data", headers);
	List<Message<byte[]>> messages = this.messageChannel.getMessages();
	Message<byte[]> message = messages.get(0);

	assertSame(headers, message.getHeaders());
	assertFalse(accessor.isMutable());
}
 
Example 16
Source File: MessageSendingTemplateTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void convertAndSendPayloadAndMutableHeadersToDestination() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders messageHeaders = accessor.getMessageHeaders();

	this.template.setMessageConverter(new StringMessageConverter());
	this.template.convertAndSend("somewhere", "payload", messageHeaders);

	MessageHeaders actual = this.template.message.getHeaders();
	assertSame(messageHeaders, actual);
	assertEquals(new MimeType("text", "plain", StandardCharsets.UTF_8), actual.get(MessageHeaders.CONTENT_TYPE));
	assertEquals("bar", actual.get("foo"));
}
 
Example 17
Source File: MessageMappingMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<?> message(String destination, String... content) {
	Flux<DataBuffer> payload = Flux.fromIterable(Arrays.asList(content)).map(parts -> toDataBuffer(parts));
	MessageHeaderAccessor headers = new MessageHeaderAccessor();
	headers.setLeaveMutable(true);
	headers.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, destination);
	return MessageBuilder.createMessage(payload, headers.getMessageHeaders());
}
 
Example 18
Source File: SimpleMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void toMessageWithPayloadAndMutableHeaders() {
	MessageHeaderAccessor accessor = new MessageHeaderAccessor();
	accessor.setHeader("foo", "bar");
	accessor.setLeaveMutable(true);
	MessageHeaders headers = accessor.getMessageHeaders();

	Message<?> message = this.converter.toMessage("payload", headers);

	assertEquals("payload", message.getPayload());
	assertSame(headers, message.getHeaders());
	assertEquals("bar", message.getHeaders().get("foo"));
}
 
Example 19
Source File: MessagingRSocket.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private MessageHeaders createHeaders(String destination, @Nullable MonoProcessor<?> replyMono) {
	MessageHeaderAccessor headers = new MessageHeaderAccessor();
	headers.setLeaveMutable(true);
	headers.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, destination);
	if (this.dataMimeType != null) {
		headers.setContentType(this.dataMimeType);
	}
	headers.setHeader(RSocketRequesterMethodArgumentResolver.RSOCKET_REQUESTER_HEADER, this.requester);
	if (replyMono != null) {
		headers.setHeader(RSocketPayloadReturnValueHandler.RESPONSE_HEADER, replyMono);
	}
	headers.setHeader(HandlerMethodReturnValueHandler.DATA_BUFFER_FACTORY_HEADER, this.bufferFactory);
	return headers.getMessageHeaders();
}
 
Example 20
Source File: MessageHeaderPropagationTest.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNullValue() {
	MessageHeaderAccessor carrier = carrier();
	carrier.setHeader("B3", "48485a3953bb6124-1234");
	carrier.setHeader("B3", "48485a3953bb61240000000-1234");
	String value = MessageHeaderPropagation.INSTANCE.get(carrier, "non existent key");
	assertThat(value).isNull();
}