/** * Returns the Subject with the given {@code issuer} and {@code subject} or an empty optional. * * @param issuer the SubjectId's {@code issuer} of the Subject to be retrieved. * @param subject the character sequence for the SubjectId's {@code subject} of the Subject to be retrieved. * @return the Subject with the given subjectId or an empty optional. * @throws NullPointerException if {@code subjectId} is {@code null}. * @throws IllegalArgumentException if {@code subject} is empty. */ default Optional<Subject> getSubject(final SubjectIssuer issuer, final CharSequence subject) { return getSubject(SubjectId.newInstance(issuer, subject)); }
static Subjects substituteSubjects(final Subjects subjects, final HeaderBasedPlaceholderSubstitutionAlgorithm substitutionAlgorithm, final DittoHeaders dittoHeaders) { Subjects newSubjects = subjects; for (final Subject subject : subjects) { final String subjectId = subject.getId().toString(); final String substitutedSubjectId = substitutionAlgorithm.substitute(subjectId, dittoHeaders); if (!subjectId.equals(substitutedSubjectId)) { final Subject newSubject = Subject.newInstance(substitutedSubjectId, subject.getType()); newSubjects = newSubjects.removeSubject(subjectId).setSubject(newSubject); } } return newSubjects; }
/** * Returns all non hidden marked fields of this subjects. * * @return a JSON object representation of this subjects including only non hidden marked fields. */ @Override default JsonObject toJson() { return toJson(FieldType.notHidden()); }
private void putAllSubjects(final PolicyEntry policyEntry) { final Subjects entrySubjects = policyEntry.getSubjects(); final Map<SubjectId, Subject> subjectsMap = new LinkedHashMap<>(entrySubjects.getSize()); entrySubjects.forEach(entrySubject -> subjectsMap.put(entrySubject.getId(), entrySubject)); subjects.put(policyEntry.getLabel(), subjectsMap); }
/** * Creates a response to a {@code RetrieveSubjects} command. * * @param policyId the Policy ID of the retrieved subjects. * @param label the Label of the PolicyEntry. * @param subjects the retrieved Subjects. * @param dittoHeaders the headers of the preceding command. * @return the response. * @throws NullPointerException if any argument is {@code null}. */ public static RetrieveSubjectsResponse of(final String policyId, final Label label, final Subjects subjects, final DittoHeaders dittoHeaders) { return new RetrieveSubjectsResponse(policyId, label, checkNotNull(subjects, "Subjects").toJson( dittoHeaders.getSchemaVersion().orElse(subjects.getLatestSchemaVersion())), HttpStatusCode.OK, dittoHeaders); }
@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; }
.newPolicyEntry(sc.getLabel(), policyEntry.getSubjects().setSubject(sc.getSubject()), policyEntry.getResources())) .ifPresent(modifiedPolicyEntry -> policy = policy.toBuilder() .newPolicyEntry(sm.getLabel(), policyEntry.getSubjects().setSubject(sm.getSubject()), policyEntry.getResources())) .ifPresent(modifiedPolicyEntry -> policy = policy.toBuilder()
/** * Removes the Subject with the given identifier from a copy of this Subjects. * * @param subjectIssuerWithId the Subject issuer + Subject ID (separated with a "{@value * SubjectId#ISSUER_DELIMITER}") of the Subject to be removed. * @return a copy of this Subjects with {@code subject} removed. * @throws NullPointerException if {@code subjectIssuerWithId} is {@code null}. * @throws IllegalArgumentException if {@code subjectIssuerWithId} is empty. */ default Subjects removeSubject(final CharSequence subjectIssuerWithId) { return removeSubject(SubjectId.newInstance(subjectIssuerWithId)); }
@Override public ImmutablePolicyBuilder setSubjectsFor(final CharSequence label, final Subjects subjects) { checkNotNull(subjects, "Subjects to be set"); final Map<SubjectId, Subject> existingSubject = retrieveExistingSubjects(label); subjects.forEach(subject -> existingSubject.put(subject.getId(), subject)); return this; }
private static Collection<String> getSubjectIds(final Subjects subjects) { return subjects.stream() .map(Subject::getId) .map(SubjectId::toString) .collect(Collectors.toSet()); }
/** * Creates a response to a {@code RetrieveSubjects} command. * * @param policyId the Policy ID of the retrieved subjects. * @param label the Label of the PolicyEntry. * @param subjects the retrieved Subjects. * @param dittoHeaders the headers of the preceding command. * @return the response. * @throws NullPointerException if any argument is {@code null}. */ public static RetrieveSubjectsResponse of(final String policyId, final Label label, final Subjects subjects, final DittoHeaders dittoHeaders) { return new RetrieveSubjectsResponse(policyId, label, checkNotNull(subjects, "Subjects").toJson( dittoHeaders.getSchemaVersion().orElse(subjects.getLatestSchemaVersion())), HttpStatusCode.OK, dittoHeaders); }
.newPolicyEntry(sc.getLabel(), policyEntry.getSubjects().setSubject(sc.getSubject()), policyEntry.getResources())) .ifPresent(modifiedPolicyEntry -> policy = policy.toBuilder() .newPolicyEntry(sm.getLabel(), policyEntry.getSubjects().setSubject(sm.getSubject()), policyEntry.getResources())) .ifPresent(modifiedPolicyEntry -> policy = policy.toBuilder()
/** * Removes the Subject with the given {@code issuer} and {@code subject} from a copy of this Subjects. * * @param issuer the SubjectId's {@code issuer} of the Subject to be removed. * @param subject the character sequence for the SubjectId's {@code subject} of the Subject to be removed. * @return a copy of this Subjects with {@code subject} removed. * @throws NullPointerException if {@code subjectId} is {@code null}. * @throws IllegalArgumentException if {@code subject} is empty. */ default Subjects removeSubject(final SubjectIssuer issuer, final CharSequence subject) { return removeSubject(SubjectId.newInstance(issuer, subject)); }
policyEntry.getSubjects().forEach(subject -> { final PolicyTreeNode parentNode = Optional.ofNullable(tree.get(subject.getId().toString())). orElseGet(() -> {
/** * Returns the Subject with the given subjectIssuerWithId or an empty optional. * * @param subjectIssuerWithId the Subject issuer + Subject ID (separated with a "{@value * SubjectId#ISSUER_DELIMITER}") of the Subject to be retrieved. * @return the Subject with the given subjectIssuerWithId or an empty optional. * @throws NullPointerException if {@code subjectIssuerWithId} is {@code null}. * @throws IllegalArgumentException if {@code subjectIssuerWithId} is empty. */ default Optional<Subject> getSubject(final CharSequence subjectIssuerWithId) { return getSubject(SubjectId.newInstance(subjectIssuerWithId)); }
@Override public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) { return Optional.of(subjects.toJson(schemaVersion, FieldType.regularOrSpecial())); }
@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 protected Optional<Subject> determineETagEntity(final RetrieveSubject command) { return Optional.ofNullable(policy) .flatMap(p -> p.getEntryFor(command.getLabel())) .map(PolicyEntry::getSubjects) .flatMap(s -> s.getSubject(command.getSubjectId())); } }
@Override public Optional<JsonValue> getEntity(final JsonSchemaVersion schemaVersion) { return Optional.of(subjects.toJson(schemaVersion, FieldType.regularOrSpecial())); }
@Override protected Optional<Subject> determineETagEntity(final DeleteSubject command) { return Optional.ofNullable(policy) .flatMap(p -> p.getEntryFor(command.getLabel())) .map(PolicyEntry::getSubjects) .flatMap(s -> s.getSubject(command.getSubjectId())); } }