diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java index a5b8ff204d..72b64e104f 100755 --- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java +++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java @@ -65,6 +65,7 @@ import org.keycloak.models.sessions.infinispan.util.SessionTimeouts; import java.io.Serializable; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -331,9 +332,13 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { private UserSessionEntity getUserSessionEntity(RealmModel realm, String id, boolean offline) { InfinispanChangelogBasedTransaction tx = getTransaction(offline); SessionEntityWrapper entityWrapper = tx.get(id); - if (entityWrapper==null) return null; + if (entityWrapper == null) { + return null; + } UserSessionEntity entity = entityWrapper.getEntity(); - if (!entity.getRealmId().equals(realm.getId())) return null; + if (!entity.getRealmId().equals(realm.getId())) { + return null; + } return entity; } @@ -472,23 +477,10 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { return persister.loadUserSessionsStream(realm, client, true, firstResult, maxResults); } - final String clientUuid = client.getId(); - UserSessionPredicate predicate = UserSessionPredicate.create(realm.getId()).client(clientUuid); + UserSessionPredicate predicate = UserSessionPredicate.create(realm.getId()).client(client.getId()); - return getUserSessionModels(realm, firstResult, maxResults, offline, predicate); - } - - protected Stream getUserSessionModels(RealmModel realm, Integer firstResult, Integer maxResults, boolean offline, UserSessionPredicate predicate) { - Cache> cache = getCache(offline); - cache = CacheDecorators.skipCacheLoaders(cache); - - // return a stream that 'wraps' the infinispan cache stream so that the cache stream's elements are read one by one - // and then filtered/mapped locally to avoid serialization issues when trying to manipulate the cache stream directly. - Stream stream = StreamSupport.stream(cache.entrySet().stream().filter(predicate).spliterator(), false) - .map(Mappers.userSessionEntity()) - .sorted(Comparators.userSessionLastSessionRefresh()); - - return paginatedStream(stream, firstResult, maxResults).map(entity -> this.wrap(realm, entity, offline)); + return paginatedStream(getUserSessionsStream(realm, predicate, offline) + .sorted(Comparator.comparing(UserSessionModel::getLastSessionRefresh)), firstResult, maxResults); } @Override @@ -571,7 +563,7 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { return cache.entrySet().stream() .filter(UserSessionPredicate.create(realm.getId())) .map(Mappers.authClientSessionSetMapper()) - .flatMap((Serializable & Function, Stream>)Mappers::toStream) + .flatMap((Serializable & Function, Stream>) Mappers::toStream) .collect( CacheCollectors.serializableCollector( () -> Collectors.groupingBy(Function.identity(), Collectors.counting()) @@ -579,7 +571,7 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { ); } - protected long getUserSessionsCount(RealmModel realm, ClientModel client, boolean offline) { + protected long getUserSessionsCount(RealmModel realm, ClientModel client, boolean offline) { if (offline && loadOfflineSessionsFromDatabase) { // fetch the actual offline user session count from the database @@ -587,14 +579,7 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { return persister.getUserSessionsCount(realm, client, true); } - Cache> cache = getCache(offline); - cache = CacheDecorators.skipCacheLoaders(cache); - - final String clientUuid = client.getId(); - - return cache.entrySet().stream() - .filter(UserSessionPredicate.create(realm.getId()).client(clientUuid)) - .count(); + return getUserSessionsStream(realm, UserSessionPredicate.create(realm.getId()).client(client.getId()), offline).count(); } @Override @@ -759,7 +744,7 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { return null; } - UserModel user = session.users().getUserById(realm, entity.getUser()); + UserModel user = session.users().getUserById(realm, entity.getUser()); if (user == null) { return null; } @@ -770,12 +755,14 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { AuthenticatedClientSessionAdapter wrap(UserSessionModel userSession, ClientModel client, AuthenticatedClientSessionEntity entity, boolean offline) { InfinispanChangelogBasedTransaction userSessionUpdateTx = getTransaction(offline); InfinispanChangelogBasedTransaction clientSessionUpdateTx = getClientSessionTransaction(offline); - return entity != null ? new AuthenticatedClientSessionAdapter(session,this, entity, client, userSession, clientSessionUpdateTx, offline) : null; + return entity != null ? new AuthenticatedClientSessionAdapter(session, this, entity, client, userSession, clientSessionUpdateTx, offline) : null; } UserSessionEntity getUserSessionEntity(RealmModel realm, UserSessionModel userSession, boolean offline) { if (userSession instanceof UserSessionAdapter) { - if (!userSession.getRealm().equals(realm)) return null; + if (!userSession.getRealm().equals(realm)) { + return null; + } return ((UserSessionAdapter) userSession).getEntity(); } else { return getUserSessionEntity(realm, userSession.getId(), offline); @@ -942,7 +929,7 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { // Import client sessions Cache> clientSessCache = - CacheDecorators.skipCacheLoaders(offline ? offlineClientSessionCache : clientSessionCache); + CacheDecorators.skipCacheLoaders(offline ? offlineClientSessionCache : clientSessionCache); if (importWithExpiration) { importSessionsWithExpiration(clientSessionsById, clientSessCache, @@ -986,8 +973,8 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { } private void importSessionsWithExpiration(Map> sessionsById, - BasicCache cache, SessionFunction lifespanMsCalculator, - SessionFunction maxIdleTimeMsCalculator) { + BasicCache cache, SessionFunction lifespanMsCalculator, + SessionFunction maxIdleTimeMsCalculator) { sessionsById.forEach((id, sessionEntityWrapper) -> { T sessionEntity = sessionEntityWrapper.getEntity(); @@ -996,8 +983,8 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { long lifespan = lifespanMsCalculator.apply(currentRealm, client, sessionEntity); long maxIdle = maxIdleTimeMsCalculator.apply(currentRealm, client, sessionEntity); - if(lifespan != SessionTimeouts.ENTRY_EXPIRED_FLAG - && maxIdle != SessionTimeouts.ENTRY_EXPIRED_FLAG ) { + if (lifespan != SessionTimeouts.ENTRY_EXPIRED_FLAG + && maxIdle != SessionTimeouts.ENTRY_EXPIRED_FLAG) { if (cache instanceof RemoteCache) { Retry.executeWithBackoff((int iteration) -> { @@ -1052,7 +1039,7 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { if (userSession instanceof OfflineUserSessionModel) { // this is a hack so that UserModel doesn't have to be available when offline token is imported. // see related JIRA - KEYCLOAK-5350 and corresponding test - OfflineUserSessionModel oline = (OfflineUserSessionModel)userSession; + OfflineUserSessionModel oline = (OfflineUserSessionModel) userSession; entity.setUser(oline.getUserId()); // NOTE: Hack // We skip calling entity.setLoginUsername(userSession.getLoginUsername()) @@ -1098,12 +1085,12 @@ public class InfinispanUserSessionProvider implements UserSessionProvider { SessionUpdateTask registerClientSessionTask = new RegisterClientSessionTask(clientSession.getClient().getId(), clientSessionId); userSessionUpdateTx.addTask(sessionToImportInto.getId(), registerClientSessionTask); - return new AuthenticatedClientSessionAdapter(session,this, entity, clientSession.getClient(), sessionToImportInto, clientSessionUpdateTx, offline); + return new AuthenticatedClientSessionAdapter(session, this, entity, clientSession.getClient(), sessionToImportInto, clientSessionUpdateTx, offline); } private AuthenticatedClientSessionEntity createAuthenticatedClientSessionInstance(AuthenticatedClientSessionModel clientSession, - String realmId, String clientId, boolean offline) { + String realmId, String clientId, boolean offline) { final UUID clientSessionId = keyGenerator.generateKeyUUID(session, getClientSessionCache(offline)); AuthenticatedClientSessionEntity entity = new AuthenticatedClientSessionEntity(clientSessionId); entity.setRealmId(realmId);