Skip to content

Commit

Permalink
KEYCLOAK-18680 Always close result stream
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlnarik committed Jul 30, 2021
1 parent a412bb7 commit 0cdce13
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import javax.persistence.LockModeType;

import static org.keycloak.models.jpa.PaginationUtils.paginateQuery;
import static org.keycloak.utils.StreamsUtil.closing;

/**
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
Expand Down Expand Up @@ -71,7 +72,7 @@ public long count(Map<PermissionTicket.FilterOption, String> attributes, String

TypedQuery query = entityManager.createQuery(querybuilder);

return query.getResultStream().count();
return closing(query.getResultStream()).count();
}

private List<Predicate> getPredicates(CriteriaBuilder builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ public RoleModel getClientRole(ClientModel client, String name) {
public Map<ClientModel, Set<String>> getAllRedirectUrisOfEnabledClients(RealmModel realm) {
TypedQuery<Map> query = em.createNamedQuery("getAllRedirectUrisOfEnabledClients", Map.class);
query.setParameter("realm", realm.getId());
return query.getResultStream()
.filter(s -> s.get("client") != null)
return closing(query.getResultStream()
.filter(s -> s.get("client") != null))
.collect(
Collectors.groupingBy(
s -> new ClientAdapter(realm, em, session, (ClientEntity) s.get("client")),
Expand Down Expand Up @@ -903,7 +903,7 @@ public Map<String, ClientScopeModel> getClientScopes(RealmModel realm, ClientMod
query.setParameter("clientId", client.getId());
query.setParameter("defaultScope", defaultScope);

return query.getResultStream()
return closing(query.getResultStream())
.map(clientScopeId -> session.clientScopes().getClientScopeById(realm, clientScopeId))
.filter(Objects::nonNull)
.filter(clientScope -> Objects.equals(clientScope.getProtocol(), clientProtocol))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public UserSessionModel loadUserSession(RealmModel realm, String userSessionId,

Set<String> removedClientUUIDs = new HashSet<>();

clientSessionQuery.getResultStream().forEach(clientSession -> {
closing(clientSessionQuery.getResultStream()).forEach(clientSession -> {
boolean added = addClientSessionToAuthenticatedClientSessionsIfPresent(userSession, clientSession);
if (!added) {
// client was removed in the meantime
Expand Down

0 comments on commit 0cdce13

Please sign in to comment.