@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof Resource)) return false; Resource that = (Resource) o; return that.getId().equals(getId()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof Resource)) return false; Resource that = (Resource) o; return that.getId().equals(getId()); }
@Override public String getId() { if (isUpdated()) return updated.getId(); return cached.getId(); }
@Override public List<Resource> findByType(String type, String resourceServerId) { if (type == null) return Collections.emptyList(); String cacheKey = getResourceByTypeCacheKey(type, resourceServerId); return cacheQuery(cacheKey, ResourceListQuery.class, () -> getResourceStoreDelegate().findByType(type, resourceServerId), (revision, resources) -> new ResourceListQuery(revision, cacheKey, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId); }
@Override public List<Resource> findByScope(List<String> ids, String resourceServerId) { if (ids == null) return null; List<Resource> result = new ArrayList<>(); for (String id : ids) { String cacheKey = getResourceByScopeCacheKey(id, resourceServerId); result.addAll(cacheQuery(cacheKey, ResourceScopeListQuery.class, () -> getResourceStoreDelegate().findByScope(Arrays.asList(id), resourceServerId), (revision, resources) -> new ResourceScopeListQuery(revision, cacheKey, id, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId)); } return result; }
@Override public void findByScope(List<String> ids, String resourceServerId, Consumer<Resource> consumer) { if (ids == null) return; for (String id : ids) { String cacheKey = getResourceByScopeCacheKey(id, resourceServerId); cacheQuery(cacheKey, ResourceScopeListQuery.class, () -> getResourceStoreDelegate().findByScope(Arrays.asList(id), resourceServerId), (revision, resources) -> new ResourceScopeListQuery(revision, cacheKey, id, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, consumer); } }
@Override public Resource findByName(String name, String ownerId, String resourceServerId) { if (name == null) return null; String cacheKey = getResourceByNameCacheKey(name, ownerId, resourceServerId); List<Resource> result = cacheQuery(cacheKey, ResourceListQuery.class, () -> { Resource resource = getResourceStoreDelegate().findByName(name, ownerId, resourceServerId); if (resource == null) { return Collections.emptyList(); } return Arrays.asList(resource); }, (revision, resources) -> new ResourceListQuery(revision, cacheKey, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId); if (result.isEmpty()) { return null; } return result.get(0); }
public static ResourceEntity toEntity(EntityManager em, Resource resource) { if (resource instanceof ResourceAdapter) { return ((ResourceAdapter)resource).getEntity(); } else { return em.getReference(ResourceEntity.class, resource.getId()); } }
@Override public void findByOwner(String ownerId, String resourceServerId, Consumer<Resource> consumer) { String cacheKey = getResourceByOwnerCacheKey(ownerId, resourceServerId); cacheQuery(cacheKey, ResourceListQuery.class, () -> getResourceStoreDelegate().findByOwner(ownerId, resourceServerId), (revision, resources) -> new ResourceListQuery(revision, cacheKey, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, consumer); }
@Override public void findByType(String type, String resourceServerId, Consumer<Resource> consumer) { if (type == null) return; String cacheKey = getResourceByTypeCacheKey(type, resourceServerId); cacheQuery(cacheKey, ResourceListQuery.class, () -> getResourceStoreDelegate().findByType(type, resourceServerId), (revision, resources) -> new ResourceListQuery(revision, cacheKey, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId, consumer); }
@Override public List<Resource> findByOwner(String ownerId, String resourceServerId) { String cacheKey = getResourceByOwnerCacheKey(ownerId, resourceServerId); return cacheQuery(cacheKey, ResourceListQuery.class, () -> getResourceStoreDelegate().findByOwner(ownerId, resourceServerId), (revision, resources) -> new ResourceListQuery(revision, cacheKey, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId); }
@Override public List<Resource> findByUri(String uri, String resourceServerId) { if (uri == null) return null; String cacheKey = getResourceByUriCacheKey(uri, resourceServerId); return cacheQuery(cacheKey, ResourceListQuery.class, () -> getResourceStoreDelegate().findByUri(uri, resourceServerId), (revision, resources) -> new ResourceListQuery(revision, cacheKey, resources.stream().map(resource -> resource.getId()).collect(Collectors.toSet()), resourceServerId), resourceServerId); }
@Override public void addResource(Resource resource) { getMongoEntity().addResource(resource.getId()); updateMongoEntity(); }
@Override public void removeResource(Resource resource) { getMongoEntity().removeResource(resource.getId()); updateMongoEntity(); } }
@Override public Resource create(String id, String name, ResourceServer resourceServer, String owner) { Resource resource = getResourceStoreDelegate().create(id, name, resourceServer, owner); Resource cached = findById(resource.getId(), resourceServer.getId()); registerResourceInvalidation(resource.getId(), resource.getName(), resource.getType(), resource.getUris(), resource.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toSet()), resourceServer.getId(), resource.getOwner()); if (cached == null) { cached = findById(resource.getId(), resourceServer.getId()); } return cached; }
@Override public void addResource(Resource resource) { getDelegateForUpdate(); HashSet<String> resources = new HashSet<>(); resources.add(resource.getId()); cacheSession.registerPolicyInvalidation(cached.getId(), cached.getName(), resources, cached.getScopesIds(modelSupplier), cached.getConfig(modelSupplier).get("defaultResourceType"), cached.getResourceServerId()); updated.addResource(resource); }
@Override public void removeResource(Resource resource) { getDelegateForUpdate(); HashSet<String> resources = new HashSet<>(); resources.add(resource.getId()); cacheSession.registerPolicyInvalidation(cached.getId(), cached.getName(), resources, cached.getScopesIds(modelSupplier), cached.getConfig(modelSupplier).get("defaultResourceType"), cached.getResourceServerId()); updated.removeResource(resource); }
void cacheResource(Resource model) { String id = model.getId(); if (cache.getCache().containsKey(id)) { return; } Long loaded = cache.getCurrentRevision(id); if (!modelMightExist(id)) { return; } if (invalidations.contains(id)) return; cache.addRevisioned(new CachedResource(loaded, model), startupRevision); }
@Override public PermissionTicket create(String resourceId, String scopeId, String requester, ResourceServer resourceServer) { PermissionTicket created = getPermissionTicketStoreDelegate().create(resourceId, scopeId, requester, resourceServer); registerPermissionTicketInvalidation(created.getId(), created.getOwner(), created.getRequester(), created.getResource().getId(), scopeId, created.getResourceServer().getId()); return created; }
@Override public void delete(String id) { if (id == null) return; PermissionTicket permission = findById(id, null); if (permission == null) return; cache.invalidateObject(id); String scopeId = null; if (permission.getScope() != null) { scopeId = permission.getScope().getId(); } invalidationEvents.add(PermissionTicketRemovedEvent.create(id, permission.getOwner(), permission.getRequester(), permission.getResource().getId(), scopeId, permission.getResourceServer().getId())); cache.permissionTicketRemoval(id, permission.getOwner(), permission.getRequester(), permission.getResource().getId(), scopeId, permission.getResourceServer().getId(), invalidations); getPermissionTicketStoreDelegate().delete(id); UserManagedPermissionUtil.removePolicy(permission, StoreFactoryCacheSession.this); }