/** * Returns the retrieved Policy entry. * * @return the retrieved Policy entry. */ public PolicyEntry getPolicyEntry() { return PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntry); }
/** * Returns the retrieved Policy entry. * * @return the retrieved Policy entry. */ public PolicyEntry getPolicyEntry() { return PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntry); }
/** * Returns a new {@code PolicyEntry} with the specified {@code label}, {@code subjects} and {@code resources}. * * @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 {@code PolicyEntry}. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code label} is empty. */ static PolicyEntry newInstance(final CharSequence label, final Iterable<Subject> subjects, final Iterable<Resource> resources) { return PoliciesModelFactory.newPolicyEntry(label, subjects, resources); }
@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 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); }
/** * Returns a new immutable Iterable of Policy entries based on the given JSON object. * * @param jsonObject the JSON object representation of Policy entries. * @return the new initialised {@code Iterable} of {@code PolicyEntry}s. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.model.base.exceptions.DittoJsonException if {@code jsonObject} cannot be parsed to * {@link Iterable} of {@link PolicyEntry}s. */ public static Iterable<PolicyEntry> newPolicyEntries(final JsonObject jsonObject) { checkNotNull(jsonObject, "JSON object"); return jsonObject.stream() .map(jsonField -> newPolicyEntry(jsonField.getKey(), jsonField.getValue())) .collect(Collectors.toSet()); }
@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); }
/** * Creates a response to a {@code ModifyPolicyEntry} command from a JSON object. * * @param jsonObject the JSON object of which the response is to be created. * @param dittoHeaders the headers of the preceding command. * @return the response. * @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 ModifyPolicyEntryResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<ModifyPolicyEntryResponse>(TYPE, jsonObject) .deserialize((statusCode) -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID); final Optional<String> readLabel = jsonObject.getValue(JSON_LABEL); final PolicyEntry extractedPolicyEntryCreated = jsonObject.getValue(JSON_POLICY_ENTRY) .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(obj -> readLabel.map(s -> PoliciesModelFactory.newPolicyEntry(s, obj)).orElse(null)) .orElse(null); return new ModifyPolicyEntryResponse(policyId, statusCode, extractedPolicyEntryCreated, dittoHeaders); }); }
/** * Creates a response to a {@code ModifyPolicyEntry} command from a JSON object. * * @param jsonObject the JSON object of which the response is to be created. * @param dittoHeaders the headers of the preceding command. * @return the response. * @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 ModifyPolicyEntryResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<ModifyPolicyEntryResponse>(TYPE, jsonObject) .deserialize((statusCode) -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID); final Optional<String> readLabel = jsonObject.getValue(JSON_LABEL); final PolicyEntry extractedPolicyEntryCreated = jsonObject.getValue(JSON_POLICY_ENTRY) .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(obj -> readLabel.map(s -> PoliciesModelFactory.newPolicyEntry(s, obj)).orElse(null)) .orElse(null); return new ModifyPolicyEntryResponse(policyId, statusCode, extractedPolicyEntryCreated, dittoHeaders); }); }
@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 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); }
@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 setSubjectFor(final CharSequence label, final Subject subject) { final Label lbl = Label.of(label); checkNotNull(subject, "subject to set to the Policy entry"); final Policy result; final PolicyEntry existingPolicyEntry = entries.get(lbl); if (null != existingPolicyEntry) { final Subjects existingSubjects = existingPolicyEntry.getSubjects(); final Subjects newSubjects = existingSubjects.setSubject(subject); 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); } else { result = this; } } else { result = setSubjectsFor(label, Subjects.newInstance(subject)); } return result; }
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 command for modifying a {@code PolicyEntry} from a JSON object. * * @param jsonObject the JSON object of which the command is to be created. * @param dittoHeaders the headers of the command. * @return the command. * @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 ModifyPolicyEntry fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyPolicyEntry>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommand.JsonFields.JSON_POLICY_ID); final String policyEntryLabel = jsonObject.getValueOrThrow(JSON_LABEL); final JsonObject policyEntryJsonObject = jsonObject.getValueOrThrow(JSON_POLICY_ENTRY); final PolicyEntry policyEntry = PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntryJsonObject); return of(policyId, policyEntry, dittoHeaders); }); }
/** * Creates a new {@code PolicyEntryCreated} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyEntryCreated instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyEntryCreated} 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 'PolicyEntryCreated' * format. */ public static PolicyEntryCreated fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyEntryCreated>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final String policyEntryLabel = jsonObject.getValueOrThrow(JSON_LABEL); final JsonObject policyEntryJsonObject = jsonObject.getValueOrThrow(JSON_POLICY_ENTRY); final PolicyEntry extractedModifiedPolicyEntry = PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntryJsonObject); return of(policyId, extractedModifiedPolicyEntry, revision, timestamp, dittoHeaders); }); }
/** * Creates a command for modifying a {@code PolicyEntry} from a JSON object. * * @param jsonObject the JSON object of which the command is to be created. * @param dittoHeaders the headers of the command. * @return the command. * @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 ModifyPolicyEntry fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyPolicyEntry>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommand.JsonFields.JSON_POLICY_ID); final String policyEntryLabel = jsonObject.getValueOrThrow(JSON_LABEL); final JsonObject policyEntryJsonObject = jsonObject.getValueOrThrow(JSON_POLICY_ENTRY); final PolicyEntry policyEntry = PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntryJsonObject); return of(policyId, policyEntry, dittoHeaders); }); }
/** * Creates a new {@code PolicyEntryModified} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyEntryModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyEntryModified} 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 'PolicyEntryModified' * format. */ public static PolicyEntryModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyEntryModified>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final String policyEntryLabel = jsonObject.getValueOrThrow(JSON_LABEL); final JsonObject policyEntryJsonObject = jsonObject.getValueOrThrow(JSON_POLICY_ENTRY); final PolicyEntry extractedModifiedPolicyEntry = PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntryJsonObject); return of(policyId, extractedModifiedPolicyEntry, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code PolicyEntryModified} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyEntryModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyEntryModified} 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 'PolicyEntryModified' * format. */ public static PolicyEntryModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyEntryModified>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final String policyEntryLabel = jsonObject.getValueOrThrow(JSON_LABEL); final JsonObject policyEntryJsonObject = jsonObject.getValueOrThrow(JSON_POLICY_ENTRY); final PolicyEntry extractedModifiedPolicyEntry = PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntryJsonObject); return of(policyId, extractedModifiedPolicyEntry, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code PolicyEntryCreated} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyEntryCreated instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyEntryCreated} 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 'PolicyEntryCreated' * format. */ public static PolicyEntryCreated fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyEntryCreated>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final String policyEntryLabel = jsonObject.getValueOrThrow(JSON_LABEL); final JsonObject policyEntryJsonObject = jsonObject.getValueOrThrow(JSON_POLICY_ENTRY); final PolicyEntry extractedModifiedPolicyEntry = PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntryJsonObject); return of(policyId, extractedModifiedPolicyEntry, revision, timestamp, dittoHeaders); }); }