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

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#wrap() . 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 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 2
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 3
Source File: UserDestinationMessageHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void handleMessageWithoutActiveSession() {
	this.handler.setBroadcastDestination("/topic/unresolved");
	given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
	this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));

	ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
	Mockito.verify(this.brokerChannel).send(captor.capture());

	Message message = captor.getValue();
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	assertEquals("/topic/unresolved", accessor.getDestination());
	assertEquals("/user/joe/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION));

	// Should ignore our own broadcast to brokerChannel

	this.handler.handleMessage(message);
	Mockito.verifyNoMoreInteractions(this.brokerChannel);
}
 
Example 4
Source File: SubscriptionMethodReturnValueHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testMessageSentToChannel() throws Exception {
	given(this.messageChannel.send(any(Message.class))).willReturn(true);

	String sessionId = "sess1";
	String subscriptionId = "subs1";
	String destination = "/dest";
	Message<?> inputMessage = createInputMessage(sessionId, subscriptionId, destination, null);

	this.handler.handleReturnValue(PAYLOAD, this.subscribeEventReturnType, inputMessage);

	verify(this.messageChannel).send(this.messageCaptor.capture());
	assertNotNull(this.messageCaptor.getValue());

	Message<?> message = this.messageCaptor.getValue();
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message);

	assertNull("SimpMessageHeaderAccessor should have disabled id", headerAccessor.getId());
	assertNull("SimpMessageHeaderAccessor should have disabled timestamp", headerAccessor.getTimestamp());
	assertEquals(sessionId, headerAccessor.getSessionId());
	assertEquals(subscriptionId, headerAccessor.getSubscriptionId());
	assertEquals(destination, headerAccessor.getDestination());
	assertEquals(MIME_TYPE, headerAccessor.getContentType());
	assertEquals(this.subscribeEventReturnType, headerAccessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
 
Example 5
Source File: SimpleBrokerMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void connect() {

	this.messageHandler.start();

	String id = "sess1";
	Message<String> connectMessage = createConnectMessage(id, new TestPrincipal("joe"), null);
	this.messageHandler.setTaskScheduler(this.taskScheduler);
	this.messageHandler.handleMessage(connectMessage);

	verify(this.clientOutboundChannel, times(1)).send(this.messageCaptor.capture());
	Message<?> connectAckMessage = this.messageCaptor.getValue();

	SimpMessageHeaderAccessor connectAckHeaders = SimpMessageHeaderAccessor.wrap(connectAckMessage);
	assertEquals(connectMessage, connectAckHeaders.getHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER));
	assertEquals(id, connectAckHeaders.getSessionId());
	assertEquals("joe", connectAckHeaders.getUser().getName());
	assertArrayEquals(new long[] {10000, 10000},
			SimpMessageHeaderAccessor.getHeartbeat(connectAckHeaders.getMessageHeaders()));
}
 
Example 6
Source File: UserDestinationMessageHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void handleMessageWithoutActiveSession() {
	this.handler.setBroadcastDestination("/topic/unresolved");
	given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
	this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));

	ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
	Mockito.verify(this.brokerChannel).send(captor.capture());

	Message message = captor.getValue();
	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
	assertEquals("/topic/unresolved", accessor.getDestination());
	assertEquals("/user/joe/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION));

	// Should ignore our own broadcast to brokerChannel

	this.handler.handleMessage(message);
	Mockito.verifyNoMoreInteractions(this.brokerChannel);
}
 
Example 7
Source File: SubscriptionMethodReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testMessageSentToChannel() throws Exception {
	given(this.messageChannel.send(any(Message.class))).willReturn(true);

	String sessionId = "sess1";
	String subscriptionId = "subs1";
	String destination = "/dest";
	Message<?> inputMessage = createInputMessage(sessionId, subscriptionId, destination, null);

	this.handler.handleReturnValue(PAYLOAD, this.subscribeEventReturnType, inputMessage);

	verify(this.messageChannel).send(this.messageCaptor.capture());
	assertNotNull(this.messageCaptor.getValue());

	Message<?> message = this.messageCaptor.getValue();
	SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.wrap(message);

	assertNull("SimpMessageHeaderAccessor should have disabled id", headerAccessor.getId());
	assertNull("SimpMessageHeaderAccessor should have disabled timestamp", headerAccessor.getTimestamp());
	assertEquals(sessionId, headerAccessor.getSessionId());
	assertEquals(subscriptionId, headerAccessor.getSubscriptionId());
	assertEquals(destination, headerAccessor.getDestination());
	assertEquals(MIME_TYPE, headerAccessor.getContentType());
	assertEquals(this.subscribeEventReturnType, headerAccessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
}
 
Example 8
Source File: UserDestinationMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handleMessageFromBrokerWithActiveSession() {
	TestSimpUser simpUser = new TestSimpUser("joe");
	simpUser.addSessions(new TestSimpSession("123"));
	given(this.registry.getUser("joe")).willReturn(simpUser);

	this.handler.setBroadcastDestination("/topic/unresolved");
	given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);

	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE);
	accessor.setSessionId("system123");
	accessor.setDestination("/topic/unresolved");
	accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo");
	accessor.setNativeHeader("customHeader", "customHeaderValue");
	accessor.setLeaveMutable(true);
	byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
	this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));

	ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
	Mockito.verify(this.brokerChannel).send(captor.capture());
	assertNotNull(captor.getValue());
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(captor.getValue());
	assertEquals("/queue/foo-user123", headers.getDestination());
	assertEquals("/user/queue/foo", headers.getFirstNativeHeader(ORIGINAL_DESTINATION));
	assertEquals("customHeaderValue", headers.getFirstNativeHeader("customHeader"));
	assertArrayEquals(payload, (byte[]) captor.getValue().getPayload());
}
 
Example 9
Source File: SimpleBrokerMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void connect() {
	String id = "sess1";

	Message<String> connectMessage = startSession(id);
	Message<?> connectAckMessage = this.messageCaptor.getValue();

	SimpMessageHeaderAccessor connectAckHeaders = SimpMessageHeaderAccessor.wrap(connectAckMessage);
	assertEquals(connectMessage, connectAckHeaders.getHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER));
	assertEquals(id, connectAckHeaders.getSessionId());
	assertEquals("joe", connectAckHeaders.getUser().getName());
	assertArrayEquals(new long[] {10000, 10000},
			SimpMessageHeaderAccessor.getHeartbeat(connectAckHeaders.getMessageHeaders()));
}
 
Example 10
Source File: UserDestinationMessageHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handleMessage() {
	TestSimpUser simpUser = new TestSimpUser("joe");
	simpUser.addSessions(new TestSimpSession("123"));
	given(this.registry.getUser("joe")).willReturn(simpUser);
	given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
	this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));

	ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
	Mockito.verify(this.brokerChannel).send(captor.capture());

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(captor.getValue());
	assertEquals("/queue/foo-user123", accessor.getDestination());
	assertEquals("/user/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION));
}
 
Example 11
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 12
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 13
Source File: SimpleBrokerMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void logMessage(Message<?> message) {
	if (logger.isDebugEnabled()) {
		SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
		accessor = (accessor != null ? accessor : SimpMessageHeaderAccessor.wrap(message));
		logger.debug("Processing " + accessor.getShortLogMessage(message.getPayload()));
	}
}
 
Example 14
Source File: UserDestinationMessageHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handleMessage() {
	TestSimpUser simpUser = new TestSimpUser("joe");
	simpUser.addSessions(new TestSimpSession("123"));
	when(this.registry.getUser("joe")).thenReturn(simpUser);
	given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
	this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));

	ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
	Mockito.verify(this.brokerChannel).send(captor.capture());

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(captor.getValue());
	assertEquals("/queue/foo-user123", accessor.getDestination());
	assertEquals("/user/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION));
}
 
Example 15
Source File: SimpleBrokerMessageHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void logMessage(Message<?> message) {
	if (logger.isDebugEnabled()) {
		SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
		accessor = (accessor != null ? accessor : SimpMessageHeaderAccessor.wrap(message));
		logger.debug("Processing " + accessor.getShortLogMessage(message.getPayload()));
	}
}
 
Example 16
Source File: UserDestinationMessageHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handleMessageFromBrokerWithActiveSession() {
	TestSimpUser simpUser = new TestSimpUser("joe");
	simpUser.addSessions(new TestSimpSession("123"));
	when(this.registry.getUser("joe")).thenReturn(simpUser);

	this.handler.setBroadcastDestination("/topic/unresolved");
	given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);

	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE);
	accessor.setSessionId("system123");
	accessor.setDestination("/topic/unresolved");
	accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo");
	accessor.setNativeHeader("customHeader", "customHeaderValue");
	accessor.setLeaveMutable(true);
	byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
	this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));

	ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
	Mockito.verify(this.brokerChannel).send(captor.capture());
	assertNotNull(captor.getValue());
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(captor.getValue());
	assertEquals("/queue/foo-user123", headers.getDestination());
	assertEquals("/user/queue/foo", headers.getFirstNativeHeader(ORIGINAL_DESTINATION));
	assertEquals("customHeaderValue", headers.getFirstNativeHeader("customHeader"));
	assertArrayEquals(payload, (byte[]) captor.getValue().getPayload());
}
 
Example 17
Source File: SimpleBrokerMessageHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void connect() {
	String id = "sess1";

	Message<String> connectMessage = startSession(id);
	Message<?> connectAckMessage = this.messageCaptor.getValue();

	SimpMessageHeaderAccessor connectAckHeaders = SimpMessageHeaderAccessor.wrap(connectAckMessage);
	assertEquals(connectMessage, connectAckHeaders.getHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER));
	assertEquals(id, connectAckHeaders.getSessionId());
	assertEquals("joe", connectAckHeaders.getUser().getName());
	assertArrayEquals(new long[] {10000, 10000},
			SimpMessageHeaderAccessor.getHeartbeat(connectAckHeaders.getMessageHeaders()));
}
 
Example 18
Source File: SimpleBrokerMessageHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private boolean messageCaptured(String sessionId, String subscriptionId, String destination) {
	for (Message<?> message : this.messageCaptor.getAllValues()) {
		SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
		if (sessionId.equals(headers.getSessionId())) {
			if (subscriptionId.equals(headers.getSubscriptionId())) {
				if (destination.equals(headers.getDestination())) {
					return true;
				}
			}
		}
	}
	return false;
}
 
Example 19
Source File: UserDestinationMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void handleMessageFromBrokerWithActiveSession() {

	TestSimpUser simpUser = new TestSimpUser("joe");
	simpUser.addSessions(new TestSimpSession("123"));
	when(this.registry.getUser("joe")).thenReturn(simpUser);

	this.handler.setBroadcastDestination("/topic/unresolved");
	given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);

	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.MESSAGE);
	accessor.setSessionId("system123");
	accessor.setDestination("/topic/unresolved");
	accessor.setNativeHeader(ORIGINAL_DESTINATION, "/user/joe/queue/foo");
	accessor.setNativeHeader("customHeader", "customHeaderValue");
	accessor.setLeaveMutable(true);
	byte[] payload = "payload".getBytes(Charset.forName("UTF-8"));
	this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));

	ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
	Mockito.verify(this.brokerChannel).send(captor.capture());
	assertNotNull(captor.getValue());
	SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(captor.getValue());
	assertEquals("/queue/foo-user123", headers.getDestination());
	assertEquals("/user/queue/foo", headers.getFirstNativeHeader(ORIGINAL_DESTINATION));
	assertEquals("customHeaderValue", headers.getFirstNativeHeader("customHeader"));
	assertArrayEquals(payload, (byte[]) captor.getValue().getPayload());
}
 
Example 20
Source File: UserDestinationMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void handleMessage() {
	TestSimpUser simpUser = new TestSimpUser("joe");
	simpUser.addSessions(new TestSimpSession("123"));
	when(this.registry.getUser("joe")).thenReturn(simpUser);
	given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
	this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo"));

	ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
	Mockito.verify(this.brokerChannel).send(captor.capture());

	SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(captor.getValue());
	assertEquals("/queue/foo-user123", accessor.getDestination());
	assertEquals("/user/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION));
}