/** * 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)); }
/** * Creates a new {@code PolicyEntry} object from the specified JSON object. * * @param label the Label for the PolicyEntry to create. * @param jsonObject a JSON object which provides the data for the Policy entry to be created. If there are more * than one entries in the given JSON object, it'll take the first in the object. * @return a new Policy entry which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code label} or {@code jsonObject} is {@code null}. * @throws IllegalArgumentException if {@code label} is empty. * @throws DittoJsonException if {@code jsonObject} * <ul> * <li>is empty or</li> * <li>contains only a field with the schema version.</li> * </ul> */ public static PolicyEntry fromJson(final CharSequence label, final JsonObject jsonObject) { checkNotNull(jsonObject, "JSON object"); final Label lbl = Label.of(label); try { final JsonObject subjectsJsonObject = jsonObject.getValueOrThrow(JsonFields.SUBJECTS); final Subjects subjectsFromJson = PoliciesModelFactory.newSubjects(subjectsJsonObject); final JsonObject resourcesJsonObject = jsonObject.getValueOrThrow(JsonFields.RESOURCES); final Resources resourcesFromJson = PoliciesModelFactory.newResources(resourcesJsonObject); return of(lbl, subjectsFromJson, resourcesFromJson); } catch (final JsonMissingFieldException e) { throw new DittoJsonException(e); } }