/** * Returns the Resource with the given {@code resourceType} and {@code resourcePath} or an empty optional. * * @param resourceType the type of the Resource to be retrieved. * @param resourcePath the path of the Resource to be retrieved. * @return the Resource or an empty optional. * @throws NullPointerException if {@code resourcePath} is {@code null}. * @throws IllegalArgumentException if {@code resourceType} is empty. */ default Optional<Resource> getResource(final String resourceType, final CharSequence resourcePath) { return getResource(ResourceKey.newInstance(resourceType, resourcePath)); }
/** * Returns all non hidden marked fields of this Resources. * * @return a JSON object representation of this Resources including only non hidden marked fields. */ @Override default JsonObject toJson() { return toJson(FieldType.notHidden()); }
/** * Creates a response to a {@code RetrieveResources} command. * * @param policyId the Policy ID of the retrieved resources. * @param label the Label of the PolicyEntry. * @param resources the retrieved Resources. * @param dittoHeaders the headers of the preceding command. * @return the response. * @throws NullPointerException if any argument is {@code null}. */ public static RetrieveResourcesResponse of(final String policyId, final Label label, final Resources resources, final DittoHeaders dittoHeaders) { return new RetrieveResourcesResponse(policyId, label, checkNotNull(resources, "Resources").toJson( dittoHeaders.getSchemaVersion().orElse(resources.getLatestSchemaVersion())), HttpStatusCode.OK, dittoHeaders); }
final List<ResourceKey> rks = resources.stream() .map(Resource::getResourceKey) .collect(Collectors.toList()); final long resourcesLength = resources.toJsonString() .length() + 5L; return policyLength + resourcesLength;
private boolean hasPermissionGranted(final PolicyEntry policyEntry) { return policyEntry.getResources().stream() // .anyMatch(resource -> { final boolean isRootResource = ROOT_RESOURCE.equals(resource.getResourceKey()); final boolean containsGrantedPermissions = resource.getEffectedPermissions() .getGrantedPermissions() .contains(Permission.MIN_REQUIRED_POLICY_PERMISSIONS); return isRootResource && containsGrantedPermissions; }); }
@Override public ImmutablePolicyBuilder setResourcesFor(final CharSequence label, final Resources resources) { checkNotNull(resources, "Resources to be set"); final Map<ResourceKey, Permissions> grantedMap = retrieveGrantedPermissions(label); final Map<ResourceKey, Permissions> revokedMap = retrieveRevokedPermissions(label); resources.forEach(resource -> { final ResourceKey resourceKey = resource.getResourceKey(); final EffectedPermissions effectedPermissions = resource.getEffectedPermissions(); grantedMap.put(resourceKey, effectedPermissions.getGrantedPermissions()); revokedMap.put(resourceKey, effectedPermissions.getRevokedPermissions()); }); return this; }
@Override public Policy setResourceFor(final CharSequence label, final Resource resource) { final Label lbl = Label.of(label); checkNotNull(resource, "resource to set to the Policy entry"); final Map<Label, PolicyEntry> entriesCopy = copyEntries(); final PolicyEntry modifiedEntry; if (!entriesCopy.containsKey(lbl)) { modifiedEntry = newPolicyEntry(label, PoliciesModelFactory.emptySubjects(), newResources(resource)); } else { final PolicyEntry policyEntry = entriesCopy.get(lbl); final Resources modifiedResources = policyEntry.getResources().setResource(resource); modifiedEntry = newPolicyEntry(label, policyEntry.getSubjects(), modifiedResources); } entriesCopy.put(lbl, modifiedEntry); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }
private ModifyResources(final String policyId, final Label label, final Resources resources, final DittoHeaders dittoHeaders) { super(TYPE, dittoHeaders); PolicyIdValidator.getInstance().accept(policyId, dittoHeaders); this.policyId = policyId; this.label = label; this.resources = resources; PolicyCommandSizeValidator.getInstance().ensureValidSize(() -> resources.toJsonString().length(), () -> dittoHeaders); }
/** * Removes the Resource with the given {@code resourceType} and {@code resourcePath} from a copy of this Resources. * * @param resourceType the type of the Resource to be removed. * @param resourcePath the path of the Resource to be removed. * @return a copy of this Resources with {@code resource} removed. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code resourceType} is empty. */ default Resources removeResource(final String resourceType, final CharSequence resourcePath) { return removeResource(ResourceKey.newInstance(resourceType, resourcePath)); }
@Override public ImmutablePolicyBuilder setResourceFor(final CharSequence label, final Resource resource) { return setResourcesFor(label, Resources.newInstance(resource)); }
final List<ResourceKey> rks = resources.stream() .map(Resource::getResourceKey) .collect(Collectors.toList()); final long resourcesLength = resources.toJsonString() .length() + 5L; return policyLength + resourcesLength;
private boolean hasPermissionRevoked(final PolicyEntry policyEntry) { return policyEntry.getResources().stream() // .anyMatch(resource -> { final boolean isRootResource = ROOT_RESOURCE.equals(resource.getResourceKey()); final boolean containsRevokedPermissions = resource.getEffectedPermissions() .getRevokedPermissions() .contains(Permission.MIN_REQUIRED_POLICY_PERMISSIONS); return isRootResource && containsRevokedPermissions; }); }
}); policyEntry.getResources().forEach(resource -> { final ResourceNode rootChild = (ResourceNode) parentNode.getChild(resource.getType()) .orElseGet(() -> {
.map(policyEntry -> PoliciesModelFactory.newPolicyEntry(rc.getLabel(), policyEntry.getSubjects(), policyEntry.getResources().setResource(rc.getResource()))) .ifPresent(modifiedPolicyEntry -> policy = policy.toBuilder() .set(modifiedPolicyEntry) .map(policyEntry -> PoliciesModelFactory.newPolicyEntry(rm.getLabel(), policyEntry.getSubjects(), policyEntry.getResources().setResource(rm.getResource()))) .ifPresent(modifiedPolicyEntry -> policy = policy.toBuilder() .set(modifiedPolicyEntry)
private ModifyResources(final String policyId, final Label label, final Resources resources, final DittoHeaders dittoHeaders) { super(TYPE, dittoHeaders); PolicyIdValidator.getInstance().accept(policyId, dittoHeaders); this.policyId = policyId; this.label = label; this.resources = resources; PolicyCommandSizeValidator.getInstance().ensureValidSize(() -> resources.toJsonString().length(), () -> dittoHeaders); }
@Override public Policy removeResourceFor(final CharSequence label, final ResourceKey resourceKey) { final Label lbl = Label.of(label); Policy result = this; final PolicyEntry existingEntry = entries.get(lbl); if (null != existingEntry) { final Resources existingResources = existingEntry.getResources(); final Resources newResources = existingResources.removeResource(resourceKey); if (!Objects.equals(existingResources, newResources)) { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(lbl, newPolicyEntry(lbl, existingEntry.getSubjects(), newResources)); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } } return result; }
@Override protected Optional<Resource> determineETagEntity(final DeleteResource command) { return Optional.ofNullable(policy) .flatMap(p -> p.getEntryFor(command.getLabel())) .map(PolicyEntry::getResources) .flatMap(r -> r.getResource(command.getResourceKey())); } }
@Override public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) { return Optional.of(resources.toJson(schemaVersion, FieldType.regularOrSpecial())); }
/** * Creates a response to a {@code RetrieveResources} command. * * @param policyId the Policy ID of the retrieved resources. * @param label the Label of the PolicyEntry. * @param resources the retrieved Resources. * @param dittoHeaders the headers of the preceding command. * @return the response. * @throws NullPointerException if any argument is {@code null}. */ public static RetrieveResourcesResponse of(final String policyId, final Label label, final Resources resources, final DittoHeaders dittoHeaders) { return new RetrieveResourcesResponse(policyId, label, checkNotNull(resources, "Resources").toJson( dittoHeaders.getSchemaVersion().orElse(resources.getLatestSchemaVersion())), HttpStatusCode.OK, dittoHeaders); }
private boolean hasPermissionGranted(final PolicyEntry policyEntry) { return policyEntry.getResources().stream() // .anyMatch(resource -> { final boolean isRootResource = ROOT_RESOURCE.equals(resource.getResourceKey()); final boolean containsGrantedPermissions = resource.getEffectedPermissions() .getGrantedPermissions() .contains(Permission.MIN_REQUIRED_POLICY_PERMISSIONS); return isRootResource && containsGrantedPermissions; }); }