Java Code Examples for org.springframework.messaging.simp.SimpMessageHeaderAccessor#setNativeHeader()

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#setNativeHeader() . 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: UserDestinationMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public void handleUnresolved(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	if (SimpMessageHeaderAccessor.getFirstNativeHeader(
			SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, headers) != null) {
		// Re-broadcast
		return;
	}
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	String destination = accessor.getDestination();
	accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, destination);
	accessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + destination + " -> " + getBroadcastDestination());
	}
	this.messagingTemplate.send(getBroadcastDestination(), message);
}
 
Example 2
Source File: DefaultSubscriptionRegistryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void registerSubscriptionWithSelector() throws Exception {
	String sessionId = "sess01";
	String subscriptionId = "subs01";
	String destination = "/foo";
	String selector = "headers.foo == 'bar'";

	this.registry.registerSubscription(subscribeMessage(sessionId, subscriptionId, destination, selector));

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setDestination(destination);
	accessor.setNativeHeader("foo", "bar");
	Message<?> message = MessageBuilder.createMessage("", accessor.getMessageHeaders());

	MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
	assertNotNull(actual);
	assertEquals(1, actual.size());
	assertEquals(Collections.singletonList(subscriptionId), actual.get(sessionId));

	actual = this.registry.findSubscriptions(createMessage(destination));
	assertNotNull(actual);
	assertEquals(0, actual.size());
}
 
Example 3
Source File: DefaultSubscriptionRegistryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void registerSubscriptionWithSelectorNotSupported() {
	String sessionId = "sess01";
	String subscriptionId = "subs01";
	String destination = "/foo";
	String selector = "headers.foo == 'bar'";

	this.registry.setSelectorHeaderName(null);
	this.registry.registerSubscription(subscribeMessage(sessionId, subscriptionId, destination, selector));

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setDestination(destination);
	accessor.setNativeHeader("foo", "bazz");
	Message<?> message = MessageBuilder.createMessage("", accessor.getMessageHeaders());

	MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
	assertNotNull(actual);
	assertEquals(1, actual.size());
	assertEquals(Collections.singletonList(subscriptionId), actual.get(sessionId));
}
 
Example 4
Source File: UserDestinationMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public void handleUnresolved(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	if (SimpMessageHeaderAccessor.getFirstNativeHeader(ORIGINAL_DESTINATION, headers) != null) {
		// Re-broadcast
		return;
	}
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	String destination = accessor.getDestination();
	accessor.setNativeHeader(ORIGINAL_DESTINATION, destination);
	accessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + destination + " -> " + getBroadcastDestination());
	}
	this.messagingTemplate.send(getBroadcastDestination(), message);
}
 
Example 5
Source File: UserDestinationMessageHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public Message<?> preHandle(Message<?> message) throws MessagingException {
	String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
	if (!getBroadcastDestination().equals(destination)) {
		return message;
	}
	SimpMessageHeaderAccessor accessor = getAccessor(message, SimpMessageHeaderAccessor.class);
	if (accessor.getSessionId() == null) {
		// Our own broadcast
		return null;
	}
	destination = accessor.getFirstNativeHeader(ORIGINAL_DESTINATION);
	if (logger.isTraceEnabled()) {
		logger.trace("Checking unresolved user destination: " + destination);
	}
	SimpMessageHeaderAccessor newAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	for (String name : accessor.toNativeHeaderMap().keySet()) {
		if (NO_COPY_LIST.contains(name)) {
			continue;
		}
		newAccessor.setNativeHeader(name, accessor.getFirstNativeHeader(name));
	}
	newAccessor.setDestination(destination);
	newAccessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); // ensure send doesn't block
	return MessageBuilder.createMessage(message.getPayload(), newAccessor.getMessageHeaders());
}
 
Example 6
Source File: UserDestinationMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
public void handleUnresolved(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	if (SimpMessageHeaderAccessor.getFirstNativeHeader(
			SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, headers) != null) {
		// Re-broadcast
		return;
	}
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	String destination = accessor.getDestination();
	accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, destination);
	accessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + destination + " -> " + getBroadcastDestination());
	}
	this.messagingTemplate.send(getBroadcastDestination(), message);
}
 
Example 7
Source File: DefaultSubscriptionRegistryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void registerSubscriptionWithSelectorNotSupported() {
	String sessionId = "sess01";
	String subscriptionId = "subs01";
	String destination = "/foo";
	String selector = "headers.foo == 'bar'";

	this.registry.setSelectorHeaderName(null);
	this.registry.registerSubscription(subscribeMessage(sessionId, subscriptionId, destination, selector));

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setDestination(destination);
	accessor.setNativeHeader("foo", "bazz");
	Message<?> message = MessageBuilder.createMessage("", accessor.getMessageHeaders());

	MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
	assertNotNull(actual);
	assertEquals(1, actual.size());
	assertEquals(Collections.singletonList(subscriptionId), actual.get(sessionId));
}
 
Example 8
Source File: MessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void toMessageWithMutableMessageHeaders() {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	accessor.setHeader("foo", "bar");
	accessor.setNativeHeader("fooNative", "barNative");
	accessor.setLeaveMutable(true);

	MessageHeaders headers = accessor.getMessageHeaders();
	Message<?> message = this.converter.toMessage("ABC", headers);

	assertSame(headers, message.getHeaders());
	assertNull(message.getHeaders().getId());
	assertNull(message.getHeaders().getTimestamp());
	assertEquals(MimeTypeUtils.TEXT_PLAIN, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
 
Example 9
Source File: DefaultSubscriptionRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Message<?> subscribeMessage(String sessionId, String subscriptionId, String dest, String selector) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId(subscriptionId);
	if (dest != null) {
		accessor.setDestination(dest);
	}
	if (selector != null) {
		accessor.setNativeHeader("selector", selector);
	}
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
 
Example 10
Source File: MessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void toMessageWithMutableMessageHeaders() {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	accessor.setHeader("foo", "bar");
	accessor.setNativeHeader("fooNative", "barNative");
	accessor.setLeaveMutable(true);

	MessageHeaders headers = accessor.getMessageHeaders();
	Message<?> message = this.converter.toMessage("ABC", headers);

	assertSame(headers, message.getHeaders());
	assertNull(message.getHeaders().getId());
	assertNull(message.getHeaders().getTimestamp());
	assertEquals(MimeTypeUtils.TEXT_PLAIN, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
 
Example 11
Source File: UserDestinationMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	if (this.broadcastHandler != null) {
		message = this.broadcastHandler.preHandle(message);
		if (message == null) {
			return;
		}
	}
	UserDestinationResult result = this.destinationResolver.resolveDestination(message);
	if (result == null) {
		return;
	}
	if (result.getTargetDestinations().isEmpty()) {
		if (logger.isTraceEnabled()) {
			logger.trace("No active sessions for user destination: " + result.getSourceDestination());
		}
		if (this.broadcastHandler != null) {
			this.broadcastHandler.handleUnresolved(message);
		}
		return;
	}
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	initHeaders(accessor);
	accessor.setNativeHeader(ORIGINAL_DESTINATION, result.getSubscribeDestination());
	accessor.setLeaveMutable(true);
	message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + result.getSourceDestination() + " -> " + result.getTargetDestinations());
	}
	for (String target : result.getTargetDestinations()) {
		this.messagingTemplate.send(target, message);
	}
}
 
Example 12
Source File: DefaultSubscriptionRegistryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Message<?> subscribeMessage(String sessionId, String subscriptionId, String dest, String selector) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId(subscriptionId);
	if (dest != null) {
		accessor.setDestination(dest);
	}
	if (selector != null) {
		accessor.setNativeHeader("selector", selector);
	}
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
 
Example 13
Source File: DefaultSubscriptionRegistryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void registerSubscriptionWithSelector() {
	String sessionId = "sess01";
	String subscriptionId = "subs01";
	String destination = "/foo";
	String selector = "headers.foo == 'bar'";

	this.registry.registerSubscription(subscribeMessage(sessionId, subscriptionId, destination, selector));

	// First, try with selector header

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setDestination(destination);
	accessor.setNativeHeader("foo", "bar");
	Message<?> message = MessageBuilder.createMessage("", accessor.getMessageHeaders());

	MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
	assertNotNull(actual);
	assertEquals(1, actual.size());
	assertEquals(Collections.singletonList(subscriptionId), actual.get(sessionId));

	// Then without

	actual = this.registry.findSubscriptions(createMessage(destination));
	assertNotNull(actual);
	assertEquals(0, actual.size());
}
 
Example 14
Source File: UserDestinationMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	Message<?> messageToUse = message;
	if (this.broadcastHandler != null) {
		messageToUse = this.broadcastHandler.preHandle(message);
		if (messageToUse == null) {
			return;
		}
	}

	UserDestinationResult result = this.destinationResolver.resolveDestination(messageToUse);
	if (result == null) {
		return;
	}

	if (result.getTargetDestinations().isEmpty()) {
		if (logger.isTraceEnabled()) {
			logger.trace("No active sessions for user destination: " + result.getSourceDestination());
		}
		if (this.broadcastHandler != null) {
			this.broadcastHandler.handleUnresolved(messageToUse);
		}
		return;
	}

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(messageToUse);
	initHeaders(accessor);
	accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination());
	accessor.setLeaveMutable(true);

	messageToUse = MessageBuilder.createMessage(messageToUse.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + result.getSourceDestination() + " -> " + result.getTargetDestinations());
	}
	for (String target : result.getTargetDestinations()) {
		this.messagingTemplate.send(target, messageToUse);
	}
}
 
Example 15
Source File: UserDestinationMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
public Message<?> preHandle(Message<?> message) throws MessagingException {
	String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
	if (!getBroadcastDestination().equals(destination)) {
		return message;
	}
	SimpMessageHeaderAccessor accessor =
			SimpMessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
	Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
	if (accessor.getSessionId() == null) {
		// Our own broadcast
		return null;
	}
	destination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
	if (logger.isTraceEnabled()) {
		logger.trace("Checking unresolved user destination: " + destination);
	}
	SimpMessageHeaderAccessor newAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	for (String name : accessor.toNativeHeaderMap().keySet()) {
		if (NO_COPY_LIST.contains(name)) {
			continue;
		}
		newAccessor.setNativeHeader(name, accessor.getFirstNativeHeader(name));
	}
	if (destination != null) {
		newAccessor.setDestination(destination);
	}
	newAccessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); // ensure send doesn't block
	return MessageBuilder.createMessage(message.getPayload(), newAccessor.getMessageHeaders());
}
 
Example 16
Source File: UserDestinationMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handleMessage(Message<?> message) throws MessagingException {
	Message<?> messageToUse = message;
	if (this.broadcastHandler != null) {
		messageToUse = this.broadcastHandler.preHandle(message);
		if (messageToUse == null) {
			return;
		}
	}

	UserDestinationResult result = this.destinationResolver.resolveDestination(messageToUse);
	if (result == null) {
		return;
	}

	if (result.getTargetDestinations().isEmpty()) {
		if (logger.isTraceEnabled()) {
			logger.trace("No active sessions for user destination: " + result.getSourceDestination());
		}
		if (this.broadcastHandler != null) {
			this.broadcastHandler.handleUnresolved(messageToUse);
		}
		return;
	}

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(messageToUse);
	initHeaders(accessor);
	accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination());
	accessor.setLeaveMutable(true);

	messageToUse = MessageBuilder.createMessage(messageToUse.getPayload(), accessor.getMessageHeaders());
	if (logger.isTraceEnabled()) {
		logger.trace("Translated " + result.getSourceDestination() + " -> " + result.getTargetDestinations());
	}
	for (String target : result.getTargetDestinations()) {
		this.messagingTemplate.send(target, messageToUse);
	}
}
 
Example 17
Source File: DefaultSubscriptionRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Message<?> subscribeMessage(String sessionId, String subscriptionId, String dest, String selector) {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
	accessor.setSessionId(sessionId);
	accessor.setSubscriptionId(subscriptionId);
	if (dest != null) {
		accessor.setDestination(dest);
	}
	if (selector != null) {
		accessor.setNativeHeader("selector", selector);
	}
	return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
 
Example 18
Source File: DefaultSubscriptionRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void registerSubscriptionWithSelector() {
	String sessionId = "sess01";
	String subscriptionId = "subs01";
	String destination = "/foo";
	String selector = "headers.foo == 'bar'";

	this.registry.registerSubscription(subscribeMessage(sessionId, subscriptionId, destination, selector));

	// First, try with selector header

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
	accessor.setDestination(destination);
	accessor.setNativeHeader("foo", "bar");
	Message<?> message = MessageBuilder.createMessage("", accessor.getMessageHeaders());

	MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
	assertNotNull(actual);
	assertEquals(1, actual.size());
	assertEquals(Collections.singletonList(subscriptionId), actual.get(sessionId));

	// Then without

	actual = this.registry.findSubscriptions(createMessage(destination));
	assertNotNull(actual);
	assertEquals(0, actual.size());
}
 
Example 19
Source File: MessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void toMessageWithMutableMessageHeaders() {
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	accessor.setHeader("foo", "bar");
	accessor.setNativeHeader("fooNative", "barNative");
	accessor.setLeaveMutable(true);

	MessageHeaders headers = accessor.getMessageHeaders();
	Message<?> message = this.converter.toMessage("ABC", headers);

	assertSame(headers, message.getHeaders());
	assertNull(message.getHeaders().getId());
	assertNull(message.getHeaders().getTimestamp());
	assertEquals(MimeTypeUtils.TEXT_PLAIN, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
 
Example 20
Source File: UserDestinationMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
public Message<?> preHandle(Message<?> message) throws MessagingException {
	String destination = SimpMessageHeaderAccessor.getDestination(message.getHeaders());
	if (!getBroadcastDestination().equals(destination)) {
		return message;
	}
	SimpMessageHeaderAccessor accessor =
			SimpMessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
	Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
	if (accessor.getSessionId() == null) {
		// Our own broadcast
		return null;
	}
	destination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
	if (logger.isTraceEnabled()) {
		logger.trace("Checking unresolved user destination: " + destination);
	}
	SimpMessageHeaderAccessor newAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
	for (String name : accessor.toNativeHeaderMap().keySet()) {
		if (NO_COPY_LIST.contains(name)) {
			continue;
		}
		newAccessor.setNativeHeader(name, accessor.getFirstNativeHeader(name));
	}
	if (destination != null) {
		newAccessor.setDestination(destination);
	}
	newAccessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); // ensure send doesn't block
	return MessageBuilder.createMessage(message.getPayload(), newAccessor.getMessageHeaders());
}