/** * Returns the retrieved Policy. * * @return the retrieved Policy. */ public Policy getPolicy() { return PoliciesModelFactory.newPolicy(policy); }
/** * Returns the retrieved Policy entry. * * @return the retrieved Policy entry. */ public PolicyEntry getPolicyEntry() { return PoliciesModelFactory.newPolicyEntry(policyEntryLabel, policyEntry); }
/** * Returns a mutable builder with a fluent API for an immutable {@code Policy}. The builder is initialised * with the Policy entries of this instance. * * @return the new builder. */ default PolicyBuilder toBuilder() { return PoliciesModelFactory.newPolicyBuilder(this); }
/** * Returns a new {@link Resource} with the specified {@code resourceType}, {@code resourcePath} and * {@code effectedPermissions}. * * @param resourceType the type of the new Resource to create. * @param resourcePath the path of the new Resource to create. * @param effectedPermissions the EffectedPermissions of the new Resource to create. * @return the new {@link Resource}. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code resourceType} is empty. */ public static Resource newResource(final CharSequence resourceType, final CharSequence resourcePath, final EffectedPermissions effectedPermissions) { return newResource(newResourceKey(resourceType, resourcePath), effectedPermissions); }
/** * 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)); }
@Nonnull private Resources getFinalResources(final CharSequence label) { final Map<ResourceKey, Permissions> grantedMap = retrieveGrantedPermissions(label); final Map<ResourceKey, Permissions> revokedMap = retrieveRevokedPermissions(label); final Collection<ResourceKey> allResourceKeys = new LinkedHashSet<>(grantedMap.keySet()); allResourceKeys.addAll(revokedMap.keySet()); final Collection<Resource> resourcesList = allResourceKeys.stream() .map(resourceKey -> { final Permissions granted = grantedMap.get(resourceKey); final Permissions revoked = revokedMap.get(resourceKey); final EffectedPermissions ep = PoliciesModelFactory.newEffectedPermissions(granted, revoked); return PoliciesModelFactory.newResource(resourceKey, ep); }) .collect(Collectors.toList()); return PoliciesModelFactory.newResources(resourcesList); }
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); }
/** * Returns the retrieved Subject. * * @return the retrieved Subject. */ public Subject getSubject() { return PoliciesModelFactory.newSubject(subjectId, subject); }
/** * Returns the retrieved Resource. * * @return the retrieved Resource. */ public Resource getResource() { return PoliciesModelFactory.newResource(resourceKey, resource); }
/** * Returns the retrieved Subjects. * * @return the retrieved Subjects. */ public Subjects getSubjects() { return PoliciesModelFactory.newSubjects(subjects); }
@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); }
/** * Returns the retrieved Resources. * * @return the retrieved Resources. */ public Resources getResources() { return PoliciesModelFactory.newResources(resources); }
/** * Returns the retrieved Policy entries. * * @return the retrieved Policy entries. */ public Iterable<PolicyEntry> getPolicyEntries() { return PoliciesModelFactory.newPolicyEntries(policyEntries); }
/** * Creates a response to a {@code ModifySubject} 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 ModifySubjectResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<ModifySubjectResponse>(TYPE, jsonObject) .deserialize((statusCode) -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID); final Label label = PoliciesModelFactory.newLabel(jsonObject.getValueOrThrow(JSON_LABEL)); final Optional<String> extractedSubjectId = jsonObject.getValue(JSON_SUBJECT_ID); final Subject extractedSubjectCreated = jsonObject.getValue(JSON_SUBJECT) .map(JsonValue::asObject) .map(obj -> extractedSubjectId.map(s -> PoliciesModelFactory.newSubject(s, obj)) .orElse(null)) .orElse(null); return new ModifySubjectResponse(policyId, label, extractedSubjectCreated, statusCode, dittoHeaders); }); }
/** * Creates a command for modifying {@code Subjects} of a {@code Policy}'s {@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 ModifySubjects fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifySubjects>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommand.JsonFields.JSON_POLICY_ID); final Label label = PoliciesModelFactory.newLabel(jsonObject.getValueOrThrow(JSON_LABEL)); final Subjects subjects = PoliciesModelFactory.newSubjects(jsonObject.getValueOrThrow(JSON_SUBJECTS)); return of(policyId, label, subjects, dittoHeaders); }); }
/** * Creates a command for modifying {@code Resources} of a {@code Policy}'s {@code PolicyEntry} from a JSON string. * * @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 ModifyResources fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyResources>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommand.JsonFields.JSON_POLICY_ID); final Label label = PoliciesModelFactory.newLabel(jsonObject.getValueOrThrow(JSON_LABEL)); final JsonObject resourcesJsonObject = jsonObject.getValueOrThrow(JSON_RESOURCES); final Resources resources = PoliciesModelFactory.newResources(resourcesJsonObject); return of(policyId, label, resources, dittoHeaders); }); }
/** * Creates a command for modifying {@code Resource} of a {@code Policy}'s {@code PolicyEntry} from a JSON string. * * @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 ModifyResource fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyResource>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommand.JsonFields.JSON_POLICY_ID); final Label label = PoliciesModelFactory.newLabel(jsonObject.getValueOrThrow(JSON_LABEL)); final String resourceKey = jsonObject.getValueOrThrow(JSON_RESOURCE_KEY); final JsonObject resourceJsonObject = jsonObject.getValueOrThrow(JSON_RESOURCE); final Resource resource = PoliciesModelFactory.newResource(ResourceKey.newInstance(resourceKey), resourceJsonObject); return of(policyId, label, resource, dittoHeaders); }); }
@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); }
private static Subject createSubjectForPut(final String jsonString, final CharSequence subjectId) { final JsonObject jsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); final String subjectTypeString = jsonObject.getValueOrThrow(Subject.JsonFields.TYPE); final SubjectType subjectType = PoliciesModelFactory.newSubjectType(subjectTypeString); return PoliciesModelFactory.newSubject(SubjectId.newInstance(subjectId), subjectType); }