/** * Returns a new empty builder for a {@code Policy}. * * @param id the ID of the new Policy. * @return the new builder. * @throws PolicyIdInvalidException if {@code policyId} did not comply to {@link Policy#ID_REGEX}. */ public static ImmutablePolicyBuilder of(final CharSequence id) { return new ImmutablePolicyBuilder().setId(id); }
private Map<ResourceKey, Permissions> retrieveGrantedPermissions(final CharSequence label) { return getPermissions(label, grantedPermissions); }
/** * Returns a mutable builder with a fluent API for an immutable {@link Policy}. * * @param id the ID of the new Policy. * @return the new builder. * @throws PolicyIdInvalidException if {@code id} is invalid. */ public static PolicyBuilder newPolicyBuilder(final CharSequence id) { return ImmutablePolicyBuilder.of(id); }
/** * Returns a new builder for a {@code Policy} based on the given {@code existingPolicy}. * * @param existingPolicy the existing Policy to instantiate the builder with. * @return the new builder. * @throws NullPointerException if {@code existingPolicy} is {@code null}. * @throws PolicyIdInvalidException if {@code policyId} did not comply to {@link Policy#ID_REGEX}. */ public static PolicyBuilder of(final Policy existingPolicy) { checkNotNull(existingPolicy, "existing Policy"); @SuppressWarnings("ConstantConditions") final ImmutablePolicyBuilder result = new ImmutablePolicyBuilder() .setLifecycle(existingPolicy.getLifecycle().orElse(null)) .setRevision(existingPolicy.getRevision().orElse(null)) .setModified(existingPolicy.getModified().orElse(null)); existingPolicy.getId().ifPresent(result::setId); existingPolicy.forEach(result::set); return result; }
@Override public Policy build() { final Collection<Label> allLabels = getAllLabels(); final Collection<PolicyEntry> policyEntries = allLabels.stream() .map(lbl -> PoliciesModelFactory.newPolicyEntry(lbl, getFinalSubjects(lbl), getFinalResources(lbl))) .collect(Collectors.toList()); return ImmutablePolicy.of(id, lifecycle, revision, modified, policyEntries); }
@Override public ImmutablePolicyBuilder setPermissionsFor(final CharSequence label, final ResourceKey resourceKey, final EffectedPermissions effectedPermissions) { checkResourceKey(resourceKey); checkNotNull(effectedPermissions, "permissions to be set"); retrieveGrantedPermissions(label).put(resourceKey, effectedPermissions.getGrantedPermissions()); retrieveRevokedPermissions(label).put(resourceKey, effectedPermissions.getRevokedPermissions()); return this; }
@Override public ImmutablePolicyBuilder setGrantedPermissionsFor(final CharSequence label, final ResourceKey resourceKey, final Permissions grantedPermissions) { checkResourceKey(resourceKey); checkNotNull(revokedPermissions, "granted permissions"); retrieveGrantedPermissions(label).put(resourceKey, grantedPermissions); return this; }
@Override public ImmutablePolicyBuilder setRevokedPermissionsFor(final CharSequence label, final ResourceKey resourceKey, final Permissions revokedPermissions) { checkResourceKey(resourceKey); checkNotNull(revokedPermissions, "revoked permissions"); retrieveRevokedPermissions(label).put(resourceKey, revokedPermissions); return this; }
private void setPolicyEntry(final PolicyEntry entry) { putAllSubjects(entry); final Label label = entry.getLabel(); grantedPermissions.put(label, new LinkedHashMap<>()); revokedPermissions.put(label, new LinkedHashMap<>()); setResourcesFor(entry.getLabel(), entry.getResources()); }
@Override public ImmutablePolicyBuilder removeResourceFor(final CharSequence label, final Resource resource) { checkNotNull(resource, "the resource to be removed"); return removeResourceFor(label, resource.getResourceKey()); }
@Override public ImmutablePolicyBuilder remove(final CharSequence label) { checkNotNull(label, "label of the entry to be removed"); removePolicyEntryFor(Label.of(label)); return this; }
/** * Returns a new builder for a {@code Policy} which is initialised with the given entries. Be aware: if there are * several entries with the same {@link Label} in the given Iterable, later entries will replace earlier ones. * * @param id the ID of the new Policy. * @param policyEntries the initials entries of the new builder. * @return the new builder. * @throws NullPointerException if {@code policyEntries} is null; * @throws PolicyIdInvalidException if {@code policyId} did not comply to {@link Policy#ID_REGEX}. */ public static PolicyBuilder of(final CharSequence id, final Iterable<PolicyEntry> policyEntries) { checkNotNull(policyEntries, "initial Policy entries"); final ImmutablePolicyBuilder result = new ImmutablePolicyBuilder(); result.setId(id); policyEntries.forEach(result::set); return result; }
/** * Returns a mutable builder with a fluent API for an immutable {@link Policy}. The builder is initialised * with the given Policy entries. * * @param id the ID of the new Policy. * @param policyEntries the initial entries of the new builder. * @return the new builder. * @throws NullPointerException if {@code policyEntries} is {@code null}. * @throws org.eclipse.ditto.model.policies.PolicyIdInvalidException if {@code id} is invalid. */ public static PolicyBuilder newPolicyBuilder(final CharSequence id, final Iterable<PolicyEntry> policyEntries) { return ImmutablePolicyBuilder.of(id, policyEntries); }
private Map<ResourceKey, Permissions> retrieveRevokedPermissions(final CharSequence label) { return getPermissions(label, revokedPermissions); }
/** * Returns a mutable builder for a {@code Policy} based on the given {@code existingPolicy}. * * @param existingPolicy the existing Policy to instantiate the builder with. * @return the new builder. */ public static PolicyBuilder newPolicyBuilder(final Policy existingPolicy) { return ImmutablePolicyBuilder.of(existingPolicy); }