Search in sources :

Example 26 with SimpMessageHeaderAccessor

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);
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 27 with SimpMessageHeaderAccessor

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());
}
Also used : Message(org.springframework.messaging.Message) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 28 with SimpMessageHeaderAccessor

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));
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 29 with SimpMessageHeaderAccessor

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);
    }
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 30 with SimpMessageHeaderAccessor

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());
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)54 Test (org.junit.Test)34 Message (org.springframework.messaging.Message)14 MessageHeaders (org.springframework.messaging.MessageHeaders)5 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)5 TextMessage (org.springframework.web.socket.TextMessage)5 MessageChannel (org.springframework.messaging.MessageChannel)3 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)3 TestPrincipal (org.springframework.messaging.simp.TestPrincipal)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 Principal (java.security.Principal)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 MissingCsrfTokenException (org.springframework.security.web.csrf.MissingCsrfTokenException)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 MessageSendingOperations (org.springframework.messaging.core.MessageSendingOperations)1