Search in sources :

Example 1 with DestinationUserNameProvider

use of org.springframework.messaging.simp.user.DestinationUserNameProvider in project spring-framework by spring-projects.

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);
    String sessionId = accessor.getSessionId();
    if (event instanceof SessionSubscribeEvent) {
        LocalSimpSession session = this.sessions.get(sessionId);
        if (session != null) {
            String id = accessor.getSubscriptionId();
            String destination = accessor.getDestination();
            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();
            session.removeSubscription(subscriptionId);
        }
    }
}
Also used : Principal(java.security.Principal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) DestinationUserNameProvider(org.springframework.messaging.simp.user.DestinationUserNameProvider)

Aggregations

Principal (java.security.Principal)1 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)1 DestinationUserNameProvider (org.springframework.messaging.simp.user.DestinationUserNameProvider)1