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");
}
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);
}
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);
}
}
}
}
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();
}
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());
}
Aggregations