public CachedResource(Long revision, Resource resource) { super(revision, resource.getId()); this.name = resource.getName(); this.displayName = resource.getDisplayName(); this.type = resource.getType(); this.owner = resource.getOwner(); this.iconUri = resource.getIconUri(); this.resourceServerId = resource.getResourceServer().getId(); ownerManagedAccess = resource.isOwnerManagedAccess(); if (resource.isFetched("uris")) { Set<String> data = new HashSet<>(resource.getUris()); this.uris = source -> data; } else { this.uris = new DefaultLazyLoader<>(source -> new HashSet<>(source.getUris()), Collections::emptySet); } if (resource.isFetched("scopes")) { Set<String> data = resource.getScopes().stream().map(Scope::getId).collect(Collectors.toSet()); this.scopesIds = source -> data; } else { this.scopesIds = new DefaultLazyLoader<>(source -> source.getScopes().stream().map(Scope::getId).collect(Collectors.toSet()), Collections::emptySet); } if (resource.isFetched("attributes")) { MultivaluedHashMap<String, String> data = new MultivaluedHashMap<>(resource.getAttributes()); this.attributes = source -> data; } else { this.attributes = new DefaultLazyLoader<>(source -> new MultivaluedHashMap<>(source.getAttributes()), MultivaluedHashMap::new); } }
@Override public List<String> getAttribute(String name) { if (updated != null) return updated.getAttribute(name); List<String> values = cached.getAttributes(modelSupplier).getOrDefault(name, Collections.emptyList()); if (values.isEmpty()) { return null; } return Collections.unmodifiableList(values); }
@Override public void delete(String id) { if (id == null) return; Resource resource = findById(id, null); if (resource == null) return; cache.invalidateObject(id); invalidationEvents.add(ResourceRemovedEvent.create(id, resource.getName(), resource.getType(), resource.getUris(), resource.getOwner(), resource.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toSet()), resource.getResourceServer().getId())); cache.resourceRemoval(id, resource.getName(), resource.getType(), resource.getUris(), resource.getOwner(), resource.getScopes().stream().map(scope -> scope.getId()).collect(Collectors.toSet()), resource.getResourceServer().getId(), invalidations); getResourceStoreDelegate().delete(id); }
@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 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 getOwner() { if (isUpdated()) return updated.getOwner(); return cached.getOwner(); }
@Override public void updateScopes(Set<Scope> scopes) { Resource updated = getDelegateForUpdate(); for (Scope scope : updated.getScopes()) { if (!scopes.contains(scope)) { PermissionTicketStore permissionStore = cacheSession.getPermissionTicketStore(); List<PermissionTicket> permissions = permissionStore.findByScope(scope.getId(), getResourceServer().getId()); for (PermissionTicket permission : permissions) { permissionStore.delete(permission.getId()); } } } PolicyStore policyStore = cacheSession.getPolicyStore(); for (Scope scope : updated.getScopes()) { if (!scopes.contains(scope)) { policyStore.findByResource(getId(), getResourceServer().getId(), policy -> policy.removeScope(scope)); } } cacheSession.registerResourceInvalidation(cached.getId(), cached.getName(), cached.getType(), cached.getUris(modelSupplier), scopes.stream().map(scope1 -> scope1.getId()).collect(Collectors.toSet()), cached.getResourceServerId(), cached.getOwner()); updated.updateScopes(scopes); }
@Override public Map<String, List<String>> getAttributes() { if (updated != null) return updated.getAttributes(); return cached.getAttributes(modelSupplier); }
@Override public String getName() { if (isUpdated()) return updated.getName(); return cached.getName(); }
@Override public String getIconUri() { if (isUpdated()) return updated.getIconUri(); return cached.getIconUri(); }
@Override public String getDisplayName() { if (isUpdated()) return updated.getDisplayName(); return cached.getDisplayName(); }
@Override public List<Scope> getScopes() { if (isUpdated()) return updated.getScopes(); if (scopes != null) return scopes; scopes = new LinkedList<>(); for (String scopeId : cached.getScopesIds(modelSupplier)) { scopes.add(cacheSession.getScopeStore().findById(scopeId, cached.getResourceServerId())); } return scopes = Collections.unmodifiableList(scopes); }
@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 void evaluate(Evaluation evaluation) { ResourcePermission permission = evaluation.getPermission(); Resource resource = permission.getResource(); if (resource != null) { Identity identity = evaluation.getContext().getIdentity(); // no need to evaluate UMA permissions to resource owner resources if (resource.getOwner().equals(identity.getId())) { return; } } super.evaluate(evaluation); } }
@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()); } }