Search in sources :

Example 86 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class SimpleBrokerMessageHandlerTests method createConnectMessage.

private Message<String> createConnectMessage(String sessionId, Principal user, long[] heartbeat) {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
    accessor.setSessionId(sessionId);
    accessor.setUser(user);
    accessor.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, heartbeat);
    return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 87 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class SimpleBrokerMessageHandlerTests method connect.

@Test
public void connect() {
    String id = "sess1";
    Message<String> connectMessage = startSession(id);
    Message<?> connectAckMessage = this.messageCaptor.getValue();
    SimpMessageHeaderAccessor connectAckHeaders = SimpMessageHeaderAccessor.wrap(connectAckMessage);
    assertThat(connectAckHeaders.getHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER)).isEqualTo(connectMessage);
    assertThat(connectAckHeaders.getSessionId()).isEqualTo(id);
    assertThat(connectAckHeaders.getUser().getName()).isEqualTo("joe");
    assertThat(SimpMessageHeaderAccessor.getHeartbeat(connectAckHeaders.getMessageHeaders())).isEqualTo(new long[] { 10000, 10000 });
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 88 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class MessageBrokerConfigurationTests method testDotSeparator.

private void testDotSeparator(ApplicationContext context, boolean expectLeadingSlash) {
    MessageChannel inChannel = context.getBean("clientInboundChannel", MessageChannel.class);
    TestChannel outChannel = context.getBean("clientOutboundChannel", TestChannel.class);
    MessageChannel brokerChannel = context.getBean("brokerChannel", MessageChannel.class);
    inChannel.send(createConnectMessage("sess1", new long[] { 0, 0 }));
    // 1. Subscribe to user destination
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
    headers.setSessionId("sess1");
    headers.setSubscriptionId("subs1");
    headers.setDestination("/user/queue.q1");
    Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
    inChannel.send(message);
    // 2. Send message to user via inboundChannel
    headers = StompHeaderAccessor.create(StompCommand.SEND);
    headers.setSessionId("sess1");
    headers.setDestination("/user/sess1/queue.q1");
    message = MessageBuilder.createMessage("123".getBytes(), headers.getMessageHeaders());
    inChannel.send(message);
    assertThat(outChannel.messages.size()).isEqualTo(2);
    Message<?> outputMessage = outChannel.messages.remove(1);
    headers = StompHeaderAccessor.wrap(outputMessage);
    assertThat(headers.getMessageType()).isEqualTo(SimpMessageType.MESSAGE);
    Object expecteds1 = expectLeadingSlash ? "/queue.q1-usersess1" : "queue.q1-usersess1";
    assertThat(headers.getDestination()).isEqualTo(expecteds1);
    assertThat(new String((byte[]) outputMessage.getPayload())).isEqualTo("123");
    outChannel.messages.clear();
    // 3. Send message via broker channel
    SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel);
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
    accessor.setSessionId("sess1");
    template.convertAndSendToUser("sess1", "queue.q1", "456".getBytes(), accessor.getMessageHeaders());
    assertThat(outChannel.messages.size()).isEqualTo(1);
    outputMessage = outChannel.messages.remove(0);
    headers = StompHeaderAccessor.wrap(outputMessage);
    assertThat(headers.getMessageType()).isEqualTo(SimpMessageType.MESSAGE);
    Object expecteds = expectLeadingSlash ? "/queue.q1-usersess1" : "queue.q1-usersess1";
    assertThat(headers.getDestination()).isEqualTo(expecteds);
    assertThat(new String((byte[]) outputMessage.getPayload())).isEqualTo("456");
}
Also used : SimpMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate) MessageChannel(org.springframework.messaging.MessageChannel) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 89 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class StompBrokerRelayMessageHandlerTests method destinationExcluded.

@Test
void destinationExcluded() {
    this.brokerRelay.start();
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    headers.setSessionId("sess1");
    headers.setDestination("/user/daisy/foo");
    this.brokerRelay.handleMessage(MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders()));
    assertThat(this.tcpClient.getSentMessages().size()).isEqualTo(1);
    StompHeaderAccessor headers1 = this.tcpClient.getSentHeaders(0);
    assertThat(headers1.getCommand()).isEqualTo(StompCommand.CONNECT);
    assertThat(headers1.getSessionId()).isEqualTo(StompBrokerRelayMessageHandler.SYSTEM_SESSION_ID);
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 90 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-debug by Joker-5.

the class UserDestinationMessageHandlerTests method handleMessageWithoutActiveSession.

@Test
@SuppressWarnings("rawtypes")
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);
    assertThat(accessor.getDestination()).isEqualTo("/topic/unresolved");
    assertThat(accessor.getFirstNativeHeader(ORIGINAL_DESTINATION)).isEqualTo("/user/joe/queue/foo");
    // 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.jupiter.api.Test)

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)199 Test (org.junit.jupiter.api.Test)103 Message (org.springframework.messaging.Message)50 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)19 MessageHeaders (org.springframework.messaging.MessageHeaders)16 TextMessage (org.springframework.web.socket.TextMessage)16 Principal (java.security.Principal)13 MessageChannel (org.springframework.messaging.MessageChannel)11 GraphQLMessage (org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 LinkedHashMap (java.util.LinkedHashMap)7 HashMap (java.util.HashMap)6 TestPrincipal (org.springframework.core.testfixture.security.TestPrincipal)6 List (java.util.List)5 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 SimpMessagingTemplate (org.springframework.messaging.simp.SimpMessagingTemplate)4 RoomMessage (com.github.chipolaris.bootforum.messaging.RoomMessage)3 CountDownLatch (java.util.concurrent.CountDownLatch)3