@Override public boolean contains(final CharSequence label) { return entries.containsKey(Label.of(label)); }
@Override public Optional<PolicyEntry> getEntryFor(final CharSequence label) { return Optional.ofNullable(entries.get(Label.of(label))); }
private static Map<ResourceKey, Permissions> getPermissions(final CharSequence l, final Map<Label, Map<ResourceKey, Permissions>> permissionsMap) { return permissionsMap.computeIfAbsent(Label.of(l), k -> new LinkedHashMap<>()); }
private Map<SubjectId, Subject> retrieveExistingSubjects(final CharSequence label) { return subjects.computeIfAbsent(Label.of(label), l -> new LinkedHashMap<>()); }
/** * Returns a {@code label} scoped PolicyBuilder where the {@link Label} can be omitted in the builder methods. * * @param label the Label to scope to. * @return the label scoped PolicyBuilder. */ default LabelScoped forLabel(final CharSequence label) { return forLabel(Label.of(label)); }
@Override public Policy removeEntry(final CharSequence label) { final Label lbl = Label.of(label); if (!entries.containsKey(lbl)) { return this; } final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.remove(lbl); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }
@Override public ImmutablePolicyBuilder remove(final CharSequence label) { checkNotNull(label, "label of the entry to be removed"); removePolicyEntryFor(Label.of(label)); return this; }
@Override public Optional<EffectedPermissions> getEffectedPermissionsFor(final CharSequence label, final SubjectId subjectId, final ResourceKey resourceKey) { final Label lbl = Label.of(label); Optional<EffectedPermissions> result = Optional.empty(); final PolicyEntry policyEntry = entries.get(lbl); if (null != policyEntry) { final Subjects subjects = policyEntry.getSubjects(); final Optional<Subject> subjectOptional = subjects.getSubject(subjectId); if (subjectOptional.isPresent()) { final Resources resources = policyEntry.getResources(); result = resources.getResource(resourceKey).map(Resource::getEffectedPermissions); } } return result; }
/** * Returns a new immutable {@link PolicyEntry} with the given authorization subject and permissions. * * @param label the Label of the PolicyEntry to create. * @param subjects the Subjects contained in the PolicyEntry to create. * @param resources the Resources of the PolicyEntry to create. * @return the new Policy entry. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code label} is empty. */ public static PolicyEntry newPolicyEntry(final CharSequence label, final Iterable<Subject> subjects, final Iterable<Resource> resources) { return ImmutablePolicyEntry.of(Label.of(label), newSubjects(subjects), newResources(resources)); }
@Override public Policy removeSubjectFor(final CharSequence label, final SubjectId subjectId) { final Label lbl = Label.of(label); Policy result = this; final PolicyEntry existingPolicyEntry = entries.get(lbl); if (null != existingPolicyEntry) { final Subjects existingSubjects = existingPolicyEntry.getSubjects(); final Subjects newSubjects = existingSubjects.removeSubject(subjectId); if (!Objects.equals(existingSubjects, newSubjects)) { final Map<Label, PolicyEntry> entriesCopy = copyEntries(); entriesCopy.put(lbl, newPolicyEntry(lbl, newSubjects, existingPolicyEntry.getResources())); result = new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); } } return result; }
@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 public Policy setSubjectsFor(final CharSequence label, final Subjects subjects) { final Label lbl = Label.of(label); checkNotNull(subjects, "subjects to set to the Policy entry"); final Map<Label, PolicyEntry> entriesCopy = copyEntries(); final PolicyEntry modifiedEntry; if (!entriesCopy.containsKey(lbl)) { modifiedEntry = newPolicyEntry(lbl, subjects, emptyResources()); } else { final PolicyEntry policyEntry = entriesCopy.get(lbl); modifiedEntry = newPolicyEntry(lbl, subjects, policyEntry.getResources()); } entriesCopy.put(lbl, modifiedEntry); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }
@Override public Policy setResourcesFor(final CharSequence label, final Resources resources) { final Label lbl = Label.of(label); checkNotNull(resources, "resources to set to the Policy entry"); final Map<Label, PolicyEntry> entriesCopy = copyEntries(); final PolicyEntry policyEntry = entriesCopy.get(lbl); final PolicyEntry modifiedEntry; if (null == policyEntry) { modifiedEntry = newPolicyEntry(lbl, PoliciesModelFactory.emptySubjects(), resources); } else { modifiedEntry = newPolicyEntry(lbl, policyEntry.getSubjects(), resources); } entriesCopy.put(lbl, modifiedEntry); return new ImmutablePolicy(policyId, entriesCopy, lifecycle, revision, modified); }
@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); }
/** * Creates a new {@code RetrievePolicyEntry} from a JSON object. * * @param jsonObject the JSON object of which a new RetrievePolicyEntry instance is to be created. * @param dittoHeaders the optional command headers of the request. * @return the {@code RetrievePolicyEntry} which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static RetrievePolicyEntry fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<RetrievePolicyEntry>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyQueryCommand.JsonFields.JSON_POLICY_ID); final Label extractedLabel = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); return of(policyId, extractedLabel, dittoHeaders); }); }
/** * Creates a new {@code PolicyEntryDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyEntryDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyDeleted} which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected 'PolicyEntryDeleted' * format. */ public static PolicyEntryDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyEntryDeleted>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final Label extractedLabel = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); return of(policyId, extractedLabel, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code RetrievePolicyEntry} from a JSON object. * * @param jsonObject the JSON object of which a new RetrievePolicyEntry instance is to be created. * @param dittoHeaders the optional command headers of the request. * @return the {@code RetrievePolicyEntry} which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static RetrievePolicyEntry fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<RetrievePolicyEntry>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyQueryCommand.JsonFields.JSON_POLICY_ID); final Label extractedLabel = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); return of(policyId, extractedLabel, dittoHeaders); }); }
/** * Creates a new {@code RetrieveSubjects} from a JSON object. * * @param jsonObject the JSON object of which a new RetrieveSubjects instance is to be created. * @param dittoHeaders the optional command headers of the request. * @return the {@code RetrieveSubjects} which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static RetrieveSubjects fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<RetrieveSubjects>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyQueryCommand.JsonFields.JSON_POLICY_ID); final Label extractedLabel = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); return of(policyId, extractedLabel, dittoHeaders); }); }
private static PolicyEntry createPolicyEntryForPut(final String jsonString, final CharSequence labelString) { final JsonObject jsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); final Subjects subjects = PoliciesModelFactory.newSubjects(jsonObject.getValueOrThrow(PolicyEntry.JsonFields.SUBJECTS)); final Resources resources = PoliciesModelFactory.newResources(jsonObject.getValueOrThrow(PolicyEntry.JsonFields.RESOURCES)); return PoliciesModelFactory.newPolicyEntry(Label.of(labelString), subjects, resources); }
/** * Creates a new {@code ResourceDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new ResourceDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourceDeleted} which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected 'ResourceDeleted' format. */ public static ResourceDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourceDeleted>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final Label label = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); final String resourceKey = jsonObject.getValueOrThrow(JSON_RESOURCE_KEY); return of(policyId, label, ResourceKey.newInstance(resourceKey), revision, timestamp, dittoHeaders); }); }