protected List<UserSessionModel> getUserSessions(RealmModel realm, Predicate<Map.Entry<String, SessionEntityWrapper<UserSessionEntity>>> predicate, boolean offline) { Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); cache = CacheDecorators.skipCacheLoaders(cache); Stream<Map.Entry<String, SessionEntityWrapper<UserSessionEntity>>> cacheStream = cache.entrySet().stream(); List<UserSessionModel> resultSessions = new LinkedList<>(); Iterator<UserSessionEntity> itr = cacheStream.filter(predicate) .map(Mappers.userSessionEntity()) .iterator(); while (itr.hasNext()) { UserSessionEntity userSessionEntity = itr.next(); resultSessions.add(wrap(realm, userSessionEntity, offline)); } return resultSessions; }
protected List<UserSessionModel> getUserSessionModels(RealmModel realm, int firstResult, int maxResults, boolean offline, UserSessionPredicate predicate) { Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); cache = CacheDecorators.skipCacheLoaders(cache); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> clientSessionCache = getClientSessionCache(offline); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> clientSessionCacheDecorated = CacheDecorators.skipCacheLoaders(clientSessionCache); Stream<UserSessionEntity> stream = cache.entrySet().stream() .filter(predicate) .map(Mappers.userSessionEntity()) .sorted(Comparators.userSessionLastSessionRefresh()); if (firstResult > 0) { stream = stream.skip(firstResult); } if (maxResults > 0) { stream = stream.limit(maxResults); } final List<UserSessionModel> sessions = new LinkedList<>(); Iterator<UserSessionEntity> itr = stream.iterator(); while (itr.hasNext()) { UserSessionEntity userSessionEntity = itr.next(); sessions.add(wrap(realm, userSessionEntity, offline)); } return sessions; }
@Override public List<UserSessionModel> getOfflineUserSessions(RealmModel realm, UserModel user) { List<UserSessionModel> userSessions = new LinkedList<>(); Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = CacheDecorators.skipCacheLoaders(offlineSessionCache); Iterator<UserSessionEntity> itr = cache.entrySet().stream() .filter(UserSessionPredicate.create(realm.getId()).user(user.getId())) .map(Mappers.userSessionEntity()) .iterator(); while (itr.hasNext()) { UserSessionEntity userSessionEntity = itr.next(); UserSessionModel userSession = wrap(realm, userSessionEntity, true); userSessions.add(userSession); } return userSessions; }
protected void removeUserSessions(RealmModel realm, UserModel user, boolean offline) { Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); cache = CacheDecorators.skipCacheLoaders(cache); Iterator<UserSessionEntity> itr = cache.entrySet().stream().filter(UserSessionPredicate.create(realm.getId()).user(user.getId())).map(Mappers.userSessionEntity()).iterator(); while (itr.hasNext()) { UserSessionEntity userSessionEntity = itr.next(); removeUserSession(userSessionEntity, offline); } }
private void removeLocalUserSessions(String realmId, boolean offline) { FuturesHelper futures = new FuturesHelper(); Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline); Cache<String, SessionEntityWrapper<UserSessionEntity>> localCache = CacheDecorators.localCache(cache); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> clientSessionCache = getClientSessionCache(offline); Cache<UUID, SessionEntityWrapper<AuthenticatedClientSessionEntity>> localClientSessionCache = CacheDecorators.localCache(clientSessionCache); Cache<String, SessionEntityWrapper<UserSessionEntity>> localCacheStoreIgnore = CacheDecorators.skipCacheLoaders(localCache); final AtomicInteger userSessionsSize = new AtomicInteger(); localCacheStoreIgnore .entrySet() .stream() .filter(SessionPredicate.create(realmId)) .map(Mappers.userSessionEntity()) .forEach(new Consumer<UserSessionEntity>() { @Override public void accept(UserSessionEntity userSessionEntity) { userSessionsSize.incrementAndGet(); // Remove session from remoteCache too. Use removeAsync for better perf Future future = localCache.removeAsync(userSessionEntity.getId()); futures.addTask(future); userSessionEntity.getAuthenticatedClientSessions().forEach((clientUUID, clientSessionId) -> { Future f = localClientSessionCache.removeAsync(clientSessionId); futures.addTask(f); }); } }); futures.waitForAllToFinish(); log.debugf("Removed %d sessions in realm %s. Offline: %b", (Object) userSessionsSize.get(), realmId, offline); }
.stream() .filter(predicate) .map(Mappers.userSessionEntity()) .forEach(new Consumer<UserSessionEntity>() {
.stream() .filter(UserSessionPredicate.create(realm.getId()).expired(expired, expiredRefresh, expiredRememberMe, expiredRefreshRememberMe)) .map(Mappers.userSessionEntity()) .forEach(new Consumer<UserSessionEntity>() {