/** * Constructor to create user from a local user. */ public TransferSimpUser(SimpUser user) { this.name = user.getName(); Set<SimpSession> sessions = user.getSessions(); this.sessions = new HashSet<>(sessions.size()); for (SimpSession session : sessions) { this.sessions.add(new TransferSimpSession(session)); } }
private Set<String> getSessionIdsByUser(String userName, @Nullable String sessionId) { Set<String> sessionIds; SimpUser user = this.userRegistry.getUser(userName); if (user != null) { if (sessionId != null && user.getSession(sessionId) != null) { sessionIds = Collections.singleton(sessionId); } else { Set<SimpSession> sessions = user.getSessions(); sessionIds = new HashSet<>(sessions.size()); for (SimpSession session : sessions) { sessionIds.add(session.getId()); } } } else { sessionIds = Collections.emptySet(); } return sessionIds; }
public Map<String, SimpSession> findSessions(String userName) { Map<String, SimpSession> map = new HashMap<>(4); SimpUser user = localRegistry.getUser(userName); if (user != null) { for (SimpSession session : user.getSessions()) { map.put(session.getId(), session); } } for (UserRegistrySnapshot registry : remoteRegistries.values()) { TransferSimpUser transferUser = registry.getUserMap().get(userName); if (transferUser != null) { transferUser.addSessions(map); } } return map; }
@Test // SPR-13800 public void getSessionsWhenUserIsConnectedToMultipleServers() throws Exception { // Add user to local registry TestSimpUser localUser = new TestSimpUser("joe"); TestSimpSession localSession = new TestSimpSession("sess123"); localUser.addSessions(localSession); when(this.localRegistry.getUser("joe")).thenReturn(localUser); // Prepare broadcast message from remote server TestSimpUser remoteUser = new TestSimpUser("joe"); TestSimpSession remoteSession = new TestSimpSession("sess456"); remoteUser.addSessions(remoteSession); SimpUserRegistry remoteRegistry = mock(SimpUserRegistry.class); when(remoteRegistry.getUsers()).thenReturn(Collections.singleton(remoteUser)); Object remoteRegistryDto = new MultiServerUserRegistry(remoteRegistry).getLocalRegistryDto(); Message<?> message = this.converter.toMessage(remoteRegistryDto, null); // Add remote registry this.registry.addRemoteRegistryDto(message, this.converter, 20000); assertEquals(1, this.registry.getUserCount()); SimpUser user = this.registry.getUsers().iterator().next(); assertTrue(user.hasSessions()); assertEquals(2, user.getSessions().size()); assertThat(user.getSessions(), containsInAnyOrder(localSession, remoteSession)); assertSame(localSession, user.getSession("sess123")); assertEquals(remoteSession, user.getSession("sess456")); user = this.registry.getUser("joe"); assertEquals(2, user.getSessions().size()); assertThat(user.getSessions(), containsInAnyOrder(localSession, remoteSession)); assertSame(localSession, user.getSession("sess123")); assertEquals(remoteSession, user.getSession("sess456")); }
@Test public void addOneSessionId() { TestPrincipal user = new TestPrincipal("joe"); Message<byte[]> message = createMessage(SimpMessageType.CONNECT_ACK, "123"); SessionConnectedEvent event = new SessionConnectedEvent(this, message, user); DefaultSimpUserRegistry registry = new DefaultSimpUserRegistry(); registry.onApplicationEvent(event); SimpUser simpUser = registry.getUser("joe"); assertNotNull(simpUser); assertEquals(1, registry.getUserCount()); assertEquals(1, simpUser.getSessions().size()); assertNotNull(simpUser.getSession("123")); }
@Test public void getUserFromRemoteRegistry() throws Exception { // Prepare broadcast message from remote server TestSimpUser testUser = new TestSimpUser("joe"); TestSimpSession testSession = new TestSimpSession("remote-sess"); testSession.addSubscriptions(new TestSimpSubscription("remote-sub", "/remote-dest")); testUser.addSessions(testSession); SimpUserRegistry testRegistry = mock(SimpUserRegistry.class); when(testRegistry.getUsers()).thenReturn(Collections.singleton(testUser)); Object registryDto = new MultiServerUserRegistry(testRegistry).getLocalRegistryDto(); Message<?> message = this.converter.toMessage(registryDto, null); // Add remote registry this.registry.addRemoteRegistryDto(message, this.converter, 20000); assertEquals(1, this.registry.getUserCount()); SimpUser user = this.registry.getUser("joe"); assertNotNull(user); assertTrue(user.hasSessions()); assertEquals(1, user.getSessions().size()); SimpSession session = user.getSession("remote-sess"); assertNotNull(session); assertEquals("remote-sess", session.getId()); assertSame(user, session.getUser()); assertEquals(1, session.getSubscriptions().size()); SimpSubscription subscription = session.getSubscriptions().iterator().next(); assertEquals("remote-sub", subscription.getId()); assertSame(session, subscription.getSession()); assertEquals("/remote-dest", subscription.getDestination()); }
@Test public void removeSessionIds() { DefaultSimpUserRegistry registry = new DefaultSimpUserRegistry(); TestPrincipal user = new TestPrincipal("joe"); Message<byte[]> message = createMessage(SimpMessageType.CONNECT_ACK, "123"); SessionConnectedEvent connectedEvent = new SessionConnectedEvent(this, message, user); registry.onApplicationEvent(connectedEvent); message = createMessage(SimpMessageType.CONNECT_ACK, "456"); connectedEvent = new SessionConnectedEvent(this, message, user); registry.onApplicationEvent(connectedEvent); message = createMessage(SimpMessageType.CONNECT_ACK, "789"); connectedEvent = new SessionConnectedEvent(this, message, user); registry.onApplicationEvent(connectedEvent); SimpUser simpUser = registry.getUser("joe"); assertNotNull(simpUser); assertEquals(3, simpUser.getSessions().size()); CloseStatus status = CloseStatus.GOING_AWAY; message = createMessage(SimpMessageType.DISCONNECT, "456"); SessionDisconnectEvent disconnectEvent = new SessionDisconnectEvent(this, message, "456", status, user); registry.onApplicationEvent(disconnectEvent); message = createMessage(SimpMessageType.DISCONNECT, "789"); disconnectEvent = new SessionDisconnectEvent(this, message, "789", status, user); registry.onApplicationEvent(disconnectEvent); assertEquals(1, simpUser.getSessions().size()); assertNotNull(simpUser.getSession("123")); }
@Test public void addMultipleSessionIds() { DefaultSimpUserRegistry registry = new DefaultSimpUserRegistry(); TestPrincipal user = new TestPrincipal("joe"); Message<byte[]> message = createMessage(SimpMessageType.CONNECT_ACK, "123"); SessionConnectedEvent event = new SessionConnectedEvent(this, message, user); registry.onApplicationEvent(event); message = createMessage(SimpMessageType.CONNECT_ACK, "456"); event = new SessionConnectedEvent(this, message, user); registry.onApplicationEvent(event); message = createMessage(SimpMessageType.CONNECT_ACK, "789"); event = new SessionConnectedEvent(this, message, user); registry.onApplicationEvent(event); SimpUser simpUser = registry.getUser("joe"); assertNotNull(simpUser); assertEquals(1, registry.getUserCount()); assertEquals(3, simpUser.getSessions().size()); assertNotNull(simpUser.getSession("123")); assertNotNull(simpUser.getSession("456")); assertNotNull(simpUser.getSession("789")); }
/** * Constructor to create user from a local user. */ public TransferSimpUser(SimpUser user) { this.name = user.getName(); Set<SimpSession> sessions = user.getSessions(); this.sessions = new HashSet<>(sessions.size()); for (SimpSession session : sessions) { this.sessions.add(new TransferSimpSession(session)); } }
public Map<String, SimpSession> findSessions(String userName) { Map<String, SimpSession> map = new HashMap<>(4); SimpUser user = localRegistry.getUser(userName); if (user != null) { for (SimpSession session : user.getSessions()) { map.put(session.getId(), session); } } for (UserRegistrySnapshot registry : remoteRegistries.values()) { TransferSimpUser transferUser = registry.getUserMap().get(userName); if (transferUser != null) { transferUser.addSessions(map); } } return map; }
private Set<String> getSessionIdsByUser(String userName, @Nullable String sessionId) { Set<String> sessionIds; SimpUser user = this.userRegistry.getUser(userName); if (user != null) { if (sessionId != null && user.getSession(sessionId) != null) { sessionIds = Collections.singleton(sessionId); } else { Set<SimpSession> sessions = user.getSessions(); sessionIds = new HashSet<>(sessions.size()); for (SimpSession session : sessions) { sessionIds.add(session.getId()); } } } else { sessionIds = Collections.emptySet(); } return sessionIds; }
/** * Constructor to create user from a local user. */ public TransferSimpUser(SimpUser user) { this.name = user.getName(); Set<SimpSession> sessions = user.getSessions(); this.sessions = new HashSet<>(sessions.size()); for (SimpSession session : sessions) { this.sessions.add(new TransferSimpSession(session)); } }
/** * Constructor to create user from a local user. */ public TransferSimpUser(SimpUser user) { this.name = user.getName(); Set<SimpSession> sessions = user.getSessions(); this.sessions = new HashSet<>(sessions.size()); for (SimpSession session : sessions) { this.sessions.add(new TransferSimpSession(session)); } }
public Map<String, SimpSession> findSessions(String userName) { Map<String, SimpSession> map = new HashMap<>(4); SimpUser user = localRegistry.getUser(userName); if (user != null) { for (SimpSession session : user.getSessions()) { map.put(session.getId(), session); } } for (UserRegistrySnapshot registry : remoteRegistries.values()) { TransferSimpUser transferUser = registry.getUserMap().get(userName); if (transferUser != null) { transferUser.addSessions(map); } } return map; }
public Map<String, SimpSession> findSessions(String userName) { Map<String, SimpSession> map = new HashMap<>(4); SimpUser user = localRegistry.getUser(userName); if (user != null) { for (SimpSession session : user.getSessions()) { map.put(session.getId(), session); } } for (UserRegistrySnapshot registry : remoteRegistries.values()) { TransferSimpUser transferUser = registry.getUserMap().get(userName); if (transferUser != null) { transferUser.addSessions(map); } } return map; }
private Set<String> getSessionIdsByUser(String userName, @Nullable String sessionId) { Set<String> sessionIds; SimpUser user = this.userRegistry.getUser(userName); if (user != null) { if (sessionId != null && user.getSession(sessionId) != null) { sessionIds = Collections.singleton(sessionId); } else { Set<SimpSession> sessions = user.getSessions(); sessionIds = new HashSet<>(sessions.size()); for (SimpSession session : sessions) { sessionIds.add(session.getId()); } } } else { sessionIds = Collections.emptySet(); } return sessionIds; }
private Set<String> getSessionIdsByUser(String userName, @Nullable String sessionId) { Set<String> sessionIds; SimpUser user = this.userRegistry.getUser(userName); if (user != null) { if (sessionId != null && user.getSession(sessionId) != null) { sessionIds = Collections.singleton(sessionId); } else { Set<SimpSession> sessions = user.getSessions(); sessionIds = new HashSet<>(sessions.size()); for (SimpSession session : sessions) { sessionIds.add(session.getId()); } } } else { sessionIds = Collections.emptySet(); } return sessionIds; }
/** * Send an {@link EventMessage} to every client that is currently subscribed to the * provided topicURI except the ones listed in the excludeUsers set. * * @param topicURI the name of the topic * @param event the payload of the {@link EventMessage} * @param excludeUsers a set of users that will be excluded. If null or empty no user * will be excluded. */ public void sendToAllExceptUsers(String topicURI, Object event, Set<String> excludeUsers) { Set<String> excludeSessionIds = null; if (excludeUsers != null && !excludeUsers.isEmpty()) { excludeSessionIds = new HashSet<>(excludeUsers.size()); for (String user : excludeUsers) { for (SimpSession session : this.simpUserRegistry.getUser(user) .getSessions()) { excludeSessionIds.add(session.getId()); } } } this.eventMessenger.sendToAllExcept(topicURI, event, excludeSessionIds); }
/** * Send an {@link EventMessage} to every client that is currently subscribed to the * given topicURI and are listed in the eligibleUsers set. If no user of the provided * set is subscribed to the topicURI nothing happens. * * @param topicURI the name of the topic * @param event the payload of the {@link EventMessage} * @param eligibleUsers only the users listed here will receive the EVENT message. If * null or empty nobody receives the message. */ public void sendToUsers(String topicURI, Object event, Set<String> eligibleUsers) { Set<String> eligibleSessionIds = null; if (eligibleUsers != null && !eligibleUsers.isEmpty()) { eligibleSessionIds = new HashSet<>(eligibleUsers.size()); for (String user : eligibleUsers) { for (SimpSession session : this.simpUserRegistry.getUser(user) .getSessions()) { eligibleSessionIds.add(session.getId()); } } } this.eventMessenger.sendTo(topicURI, event, eligibleSessionIds); }
/** * Send an EventMessage directly to each client listed in the users set parameter. A * user is ignored if there is no entry in the {@link SimpUserRegistry} for his * username. * <p> * In contrast to {@link #sendToUsers(String, Object, Set)} this method does not check * if the receivers are subscribed to the destination. The * {@link SimpleBrokerMessageHandler} is not involved in sending these messages. * * @param topicURI the name of the topic * @param event the payload of the {@link EventMessage} * @param users list of receivers for the EVENT message */ public void sendToUsersDirect(String topicURI, Object event, Set<String> users) { Set<String> webSocketSessionIds = null; if (users != null && !users.isEmpty()) { webSocketSessionIds = new HashSet<>(users.size()); for (String user : users) { for (SimpSession session : this.simpUserRegistry.getUser(user) .getSessions()) { webSocketSessionIds.add(session.getId()); } } } this.eventMessenger.sendToDirect(topicURI, event, webSocketSessionIds); }