Search in sources :

Example 56 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class UserDestinationMessageHandlerTests method handleMessage.

@Test
@SuppressWarnings("rawtypes")
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());
    assertThat(accessor.getDestination()).isEqualTo("/queue/foo-user123");
    assertThat(accessor.getFirstNativeHeader(ORIGINAL_DESTINATION)).isEqualTo("/user/queue/foo");
}
Also used : Message(org.springframework.messaging.Message) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 57 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class UserDestinationMessageHandlerTests method handleMessageFromBrokerWithActiveSession.

@Test
@SuppressWarnings("rawtypes")
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());
    assertThat(captor.getValue()).isNotNull();
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(captor.getValue());
    assertThat(headers.getDestination()).isEqualTo("/queue/foo-user123");
    assertThat(headers.getFirstNativeHeader(ORIGINAL_DESTINATION)).isEqualTo("/user/queue/foo");
    assertThat(headers.getFirstNativeHeader("customHeader")).isEqualTo("customHeaderValue");
    assertThat((byte[]) captor.getValue().getPayload()).isEqualTo(payload);
}
Also used : Message(org.springframework.messaging.Message) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 58 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class DefaultSimpUserRegistry method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    AbstractSubProtocolEvent subProtocolEvent = (AbstractSubProtocolEvent) event;
    Message<?> message = subProtocolEvent.getMessage();
    SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
    Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
    String sessionId = accessor.getSessionId();
    Assert.state(sessionId != null, "No session id");
    if (event instanceof SessionSubscribeEvent) {
        LocalSimpSession session = this.sessions.get(sessionId);
        if (session != null) {
            String id = accessor.getSubscriptionId();
            String destination = accessor.getDestination();
            if (id != null && destination != null) {
                session.addSubscription(id, destination);
            }
        }
    } else if (event instanceof SessionConnectedEvent) {
        Principal user = subProtocolEvent.getUser();
        if (user == null) {
            return;
        }
        String name = user.getName();
        if (user instanceof DestinationUserNameProvider) {
            name = ((DestinationUserNameProvider) user).getDestinationUserName();
        }
        synchronized (this.sessionLock) {
            LocalSimpUser simpUser = this.users.get(name);
            if (simpUser == null) {
                simpUser = new LocalSimpUser(name);
                this.users.put(name, simpUser);
            }
            LocalSimpSession session = new LocalSimpSession(sessionId, simpUser);
            simpUser.addSession(session);
            this.sessions.put(sessionId, session);
        }
    } else if (event instanceof SessionDisconnectEvent) {
        synchronized (this.sessionLock) {
            LocalSimpSession session = this.sessions.remove(sessionId);
            if (session != null) {
                LocalSimpUser user = session.getUser();
                user.removeSession(sessionId);
                if (!user.hasSessions()) {
                    this.users.remove(user.getName());
                }
            }
        }
    } else if (event instanceof SessionUnsubscribeEvent) {
        LocalSimpSession session = this.sessions.get(sessionId);
        if (session != null) {
            String subscriptionId = accessor.getSubscriptionId();
            if (subscriptionId != null) {
                session.removeSubscription(subscriptionId);
            }
        }
    }
}
Also used : Principal(java.security.Principal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) DestinationUserNameProvider(org.springframework.messaging.simp.user.DestinationUserNameProvider)

Example 59 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class WebSocketAnnotationMethodMessageHandlerTests method globalException.

@Test
public void globalException() throws Exception {
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
    headers.setSessionId("session1");
    headers.setSessionAttributes(new ConcurrentHashMap<>());
    headers.setDestination("/exception");
    Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
    this.messageHandler.handleMessage(message);
    TestControllerAdvice controllerAdvice = this.applicationContext.getBean(TestControllerAdvice.class);
    assertThat(controllerAdvice.isExceptionHandled()).isTrue();
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 60 with SimpMessageHeaderAccessor

use of org.springframework.messaging.simp.SimpMessageHeaderAccessor in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class DefaultSimpUserRegistryTests method createMessage.

private Message<byte[]> createMessage(SimpMessageType type, String sessionId, String subscriptionId, String destination) {
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(type);
    accessor.setSessionId(sessionId);
    if (destination != null) {
        accessor.setDestination(destination);
    }
    if (subscriptionId != null) {
        accessor.setSubscriptionId(subscriptionId);
    }
    return MessageBuilder.createMessage(new byte[0], accessor.getMessageHeaders());
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Aggregations

SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)198 Test (org.junit.jupiter.api.Test)103 Message (org.springframework.messaging.Message)50 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)19 TextMessage (org.springframework.web.socket.TextMessage)16 MessageHeaders (org.springframework.messaging.MessageHeaders)15 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