use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework by spring-projects.
the class UserDestinationMessageHandlerTests method handleMessageWithoutActiveSession.
@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);
}
use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework by spring-projects.
the class UserDestinationMessageHandlerTests method handleMessageFromBrokerWithActiveSession.
@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());
}
use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework by spring-projects.
the class UserDestinationMessageHandlerTests method handleMessage.
@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));
}
use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework by spring-projects.
the class UserDestinationMessageHandler method handleMessage.
@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(SimpMessageHeaderAccessor.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);
}
}
use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework by spring-projects.
the class SimpleBrokerMessageHandlerTests method createMessage.
private Message<String> createMessage(String destination, String payload) {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
headers.setDestination(destination);
return MessageBuilder.createMessage(payload, headers.getMessageHeaders());
}
Aggregations