private void afterDeserialization(SessionLookup sessionLookup) { this.sessionLookup = sessionLookup; for (TransferSimpSession session : this.sessions) { session.setUser(this); session.afterDeserialization(); } }
/** * Constructor to create DTO from a local user subscription. */ public TransferSimpSubscription(SimpSubscription subscription) { this.id = subscription.getId(); this.session = new TransferSimpSession(); this.destination = subscription.getDestination(); }
private void afterDeserialization() { for (TransferSimpSubscription subscription : this.subscriptions) { subscription.setSession(this); } }
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; }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof SimpSubscription)) { return false; } SimpSubscription otherSubscription = (SimpSubscription) other; return (getId().equals(otherSubscription.getId()) && ObjectUtils.nullSafeEquals(getSession(), otherSubscription.getSession())); }
/** * Constructor to create DTO from the local user session. */ public TransferSimpSession(SimpSession session) { this.id = session.getId(); this.user = new TransferSimpUser(); Set<SimpSubscription> subscriptions = session.getSubscriptions(); this.subscriptions = new HashSet<>(subscriptions.size()); for (SimpSubscription subscription : subscriptions) { this.subscriptions.add(new TransferSimpSubscription(subscription)); } }
/** * 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)); } }
@Test // SPR-11325 public void handleSubscribeOneUserMultipleSessions() { TestSimpUser simpUser = new TestSimpUser("joe"); simpUser.addSessions(new TestSimpSession("123"), new TestSimpSession("456")); when(this.registry.getUser("joe")).thenReturn(simpUser); TestPrincipal user = new TestPrincipal("joe"); Message<?> message = createMessage(SimpMessageType.SUBSCRIBE, user, "456", "/user/queue/foo"); UserDestinationResult actual = this.resolver.resolveDestination(message); assertEquals(1, actual.getTargetDestinations().size()); assertEquals("/queue/foo-user456", actual.getTargetDestinations().iterator().next()); }
/** * Constructor to create DTO from a local user registry. */ public UserRegistrySnapshot(String id, SimpUserRegistry registry) { this.id = id; Set<SimpUser> users = registry.getUsers(); this.users = new HashMap<>(users.size()); for (SimpUser user : users) { this.users.put(user.getName(), new TransferSimpUser(user)); } }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof SimpSubscription)) { return false; } SimpSubscription otherSubscription = (SimpSubscription) other; return (ObjectUtils.nullSafeEquals(getSession(), otherSubscription.getSession()) && this.id.equals(otherSubscription.getId())); }
@Before public void setup() { TestSimpUser simpUser = new TestSimpUser("joe"); simpUser.addSessions(new TestSimpSession("123")); this.registry = mock(SimpUserRegistry.class); when(this.registry.getUser("joe")).thenReturn(simpUser); this.resolver = new DefaultUserDestinationResolver(this.registry); }
@Override @Nullable public SimpUser getUser(String userName) { // Prefer remote registries due to cross-server SessionLookup for (UserRegistrySnapshot registry : this.remoteRegistries.values()) { SimpUser user = registry.getUserMap().get(userName); if (user != null) { return user; } } return this.localRegistry.getUser(userName); }
@Override public int getUserCount() { int userCount = 0; for (UserRegistrySnapshot registry : this.remoteRegistries.values()) { userCount += registry.getUserMap().size(); } userCount += this.localRegistry.getUserCount(); return userCount; }
@Override public void handleMessage(Message<?> message) throws MessagingException { MessageConverter converter = this.brokerTemplate.getMessageConverter(); this.userRegistry.addRemoteRegistryDto(message, converter, getRegistryExpirationPeriod()); }
@Override public boolean hasSessions() { if (this.sessionLookup != null) { return !this.sessionLookup.findSessions(getName()).isEmpty(); } return !this.sessions.isEmpty(); }
/** * Default constructor for JSON deserialization. */ @SuppressWarnings("unused") public TransferSimpSubscription() { this.id = ""; this.session = new TransferSimpSession(); this.destination = ""; }
Object getLocalRegistryDto() { return new UserRegistrySnapshot(this.id, this.localRegistry); }
@Test public void handleMessageEncodedUserName() { String userName = "http://joe.openid.example.org/"; TestSimpUser simpUser = new TestSimpUser(userName); simpUser.addSessions(new TestSimpSession("openid123")); when(this.registry.getUser(userName)).thenReturn(simpUser); String destination = "/user/" + StringUtils.replace(userName, "/", "%2F") + "/queue/foo"; Message<?> message = createMessage(SimpMessageType.MESSAGE, new TestPrincipal("joe"), null, destination); UserDestinationResult actual = this.resolver.resolveDestination(message); assertEquals(1, actual.getTargetDestinations().size()); assertEquals("/queue/foo-useropenid123", actual.getTargetDestinations().iterator().next()); }
@Override public Set<SimpSession> getSessions() { if (this.sessionLookup != null) { Map<String, SimpSession> sessions = this.sessionLookup.findSessions(getName()); return new HashSet<>(sessions.values()); } return new HashSet<>(this.sessions); }