001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.messaging.simp.user; 018 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.HashSet; 022import java.util.Map; 023import java.util.Set; 024 025import org.springframework.util.CollectionUtils; 026 027/** 028 * An adapter that allows a {@code UserSessionRegistry}, which is deprecated in 029 * favor of {@code SimpUserRegistry}, to be used as a {@code SimpUserRegistry}. 030 * Due to the more limited information available, methods such as 031 * {@link #getUsers()} and {@link #findSubscriptions} are not supported. 032 * 033 * <p>As of 4.2, this adapter is used only in applications that explicitly 034 * register a custom {@code UserSessionRegistry} bean by overriding 035 * {@link org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration#userSessionRegistry()}. 036 * 037 * @author Rossen Stoyanchev 038 * @since 4.2 039 */ 040@SuppressWarnings("deprecation") 041public class UserSessionRegistryAdapter implements SimpUserRegistry { 042 043 private final UserSessionRegistry userSessionRegistry; 044 045 046 public UserSessionRegistryAdapter(UserSessionRegistry registry) { 047 this.userSessionRegistry = registry; 048 } 049 050 051 @Override 052 public SimpUser getUser(String userName) { 053 Set<String> sessionIds = this.userSessionRegistry.getSessionIds(userName); 054 return (!CollectionUtils.isEmpty(sessionIds) ? new SimpUserAdapter(userName, sessionIds) : null); 055 } 056 057 @Override 058 public Set<SimpUser> getUsers() { 059 throw new UnsupportedOperationException("UserSessionRegistry does not expose a listing of users"); 060 } 061 062 @Override 063 public int getUserCount() { 064 throw new UnsupportedOperationException("UserSessionRegistry does not expose a user count"); 065 } 066 067 @Override 068 public Set<SimpSubscription> findSubscriptions(SimpSubscriptionMatcher matcher) { 069 throw new UnsupportedOperationException("UserSessionRegistry does not support operations across users"); 070 } 071 072 073 /** 074 * Expose the only information available from a UserSessionRegistry 075 * (name and session id's) as a {@code SimpUser}. 076 */ 077 private static class SimpUserAdapter implements SimpUser { 078 079 private final String name; 080 081 private final Map<String, SimpSession> sessions; 082 083 public SimpUserAdapter(String name, Set<String> sessionIds) { 084 this.name = name; 085 this.sessions = new HashMap<String, SimpSession>(sessionIds.size()); 086 for (String sessionId : sessionIds) { 087 this.sessions.put(sessionId, new SimpSessionAdapter(sessionId)); 088 } 089 } 090 091 @Override 092 public String getName() { 093 return this.name; 094 } 095 096 @Override 097 public boolean hasSessions() { 098 return !this.sessions.isEmpty(); 099 } 100 101 @Override 102 public SimpSession getSession(String sessionId) { 103 return this.sessions.get(sessionId); 104 } 105 106 @Override 107 public Set<SimpSession> getSessions() { 108 return new HashSet<SimpSession>(this.sessions.values()); 109 } 110 } 111 112 113 /** 114 * Expose the only information available from a UserSessionRegistry 115 * (session ids but no subscriptions) as a {@code SimpSession}. 116 */ 117 private static class SimpSessionAdapter implements SimpSession { 118 119 private final String id; 120 121 public SimpSessionAdapter(String id) { 122 this.id = id; 123 } 124 125 @Override 126 public String getId() { 127 return this.id; 128 } 129 130 @Override 131 public SimpUser getUser() { 132 return null; 133 } 134 135 @Override 136 public Set<SimpSubscription> getSubscriptions() { 137 return Collections.<SimpSubscription>emptySet(); 138 } 139 } 140 141}