@Bean public UserDestinationMessageHandler userDestinationMessageHandler() { UserDestinationMessageHandler handler = new UserDestinationMessageHandler(clientInboundChannel(), brokerChannel(), userDestinationResolver()); String destination = getBrokerRegistry().getUserDestinationBroadcast(); if (destination != null) { handler.setBroadcastDestination(destination); } return handler; }
private void initHeaders(SimpMessageHeaderAccessor headerAccessor) { if (getHeaderInitializer() != null) { getHeaderInitializer().initHeaders(headerAccessor); } }
initHeaders(accessor); accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination()); accessor.setLeaveMutable(true);
@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); }
@Override public final void stop(Runnable callback) { synchronized (this.lifecycleMonitor) { stop(); callback.run(); } }
@Before public void setup() { this.registry = mock(SimpUserRegistry.class); this.brokerChannel = mock(SubscribableChannel.class); UserDestinationResolver resolver = new DefaultUserDestinationResolver(this.registry); this.handler = new UserDestinationMessageHandler(new StubMessageChannel(), this.brokerChannel, resolver); }
@Test public void userBroadcastsDisabledWithSimpleBroker() { ApplicationContext context = loadConfig(SimpleBrokerConfig.class); SimpUserRegistry registry = context.getBean(SimpUserRegistry.class); assertNotNull(registry); assertNotEquals(MultiServerUserRegistry.class, registry.getClass()); UserDestinationMessageHandler handler = context.getBean(UserDestinationMessageHandler.class); assertNull(handler.getBroadcastDestination()); Object nullBean = context.getBean("userRegistryMessageHandler"); assertTrue(nullBean.equals(null)); }
@Test public void ignoreMessage() { // no destination this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", null)); Mockito.verifyZeroInteractions(this.brokerChannel); // not a user destination this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/queue/foo")); Mockito.verifyZeroInteractions(this.brokerChannel); // subscribe + not a user destination this.handler.handleMessage(createWith(SimpMessageType.SUBSCRIBE, "joe", "123", "/queue/foo")); Mockito.verifyZeroInteractions(this.brokerChannel); // no match on message type this.handler.handleMessage(createWith(SimpMessageType.CONNECT, "joe", "123", "user/joe/queue/foo")); Mockito.verifyZeroInteractions(this.brokerChannel); }
@Test public void handleMessageFromBrokerWithoutActiveSession() { 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.setLeaveMutable(true); byte[] payload = "payload".getBytes(StandardCharsets.UTF_8); this.handler.handleMessage(MessageBuilder.createMessage(payload, accessor.getMessageHeaders())); // No re-broadcast verifyNoMoreInteractions(this.brokerChannel); }
@Override public final void stop(Runnable callback) { synchronized (this.lifecycleMonitor) { stop(); callback.run(); } }
@Test public void userBroadcasts() { ApplicationContext context = loadConfig(BrokerRelayConfig.class); SimpUserRegistry userRegistry = context.getBean(SimpUserRegistry.class); assertEquals(MultiServerUserRegistry.class, userRegistry.getClass()); UserDestinationMessageHandler handler1 = context.getBean(UserDestinationMessageHandler.class); assertEquals("/topic/unresolved-user-destination", handler1.getBroadcastDestination()); UserRegistryMessageHandler handler2 = context.getBean(UserRegistryMessageHandler.class); assertEquals("/topic/simp-user-registry", handler2.getBroadcastDestination()); StompBrokerRelayMessageHandler relay = context.getBean(StompBrokerRelayMessageHandler.class); assertNotNull(relay.getSystemSubscriptions()); assertEquals(2, relay.getSystemSubscriptions().size()); assertSame(handler1, relay.getSystemSubscriptions().get("/topic/unresolved-user-destination")); assertSame(handler2, relay.getSystemSubscriptions().get("/topic/simp-user-registry")); }
@Test public void handleUnsubscribe() { given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); this.handler.handleMessage(createWith(SimpMessageType.UNSUBSCRIBE, "joe", "123", "/user/queue/foo")); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); Mockito.verify(this.brokerChannel).send(captor.capture()); Message message = captor.getValue(); assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(message.getHeaders())); }
@Bean public UserDestinationMessageHandler userDestinationMessageHandler() { UserDestinationMessageHandler handler = new UserDestinationMessageHandler(clientInboundChannel(), brokerChannel(), userDestinationResolver()); String destination = getBrokerRegistry().getUserDestinationBroadcast(); if (destination != null) { handler.setBroadcastDestination(destination); } return handler; }
@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()); }
@Override public final void stop(Runnable callback) { synchronized (this.lifecycleMonitor) { stop(); callback.run(); } }
private void initHeaders(SimpMessageHeaderAccessor headerAccessor) { if (getHeaderInitializer() != null) { getHeaderInitializer().initHeaders(headerAccessor); } }
initHeaders(accessor); accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination()); accessor.setLeaveMutable(true);
assertEquals(destination, userDestHandler.getBroadcastDestination()); assertNotNull(messageBroker.getSystemSubscriptions()); assertSame(userDestHandler, messageBroker.getSystemSubscriptions().get(destination));
@Test public void handleSubscribe() { given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); this.handler.handleMessage(createWith(SimpMessageType.SUBSCRIBE, "joe", SESSION_ID, "/user/queue/foo")); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); Mockito.verify(this.brokerChannel).send(captor.capture()); Message message = captor.getValue(); assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(message.getHeaders())); }
@Bean public UserDestinationMessageHandler userDestinationMessageHandler() { UserDestinationMessageHandler handler = new UserDestinationMessageHandler(clientInboundChannel(), brokerChannel(), userDestinationResolver()); String destination = getBrokerRegistry().getUserDestinationBroadcast(); if (destination != null) { handler.setBroadcastDestination(destination); } return handler; }