/** * Returns the retrieved Resources. * * @return the retrieved Resources. */ public Resources getResources() { return PoliciesModelFactory.newResources(resources); }
/** * Returns a new {@code Resources} containing the given resources. * * @param resources the {@link Resource}s to be contained in the new Resources. * @return the new {@code Resources}. * @throws NullPointerException if {@code resources} is {@code null}. */ static Resources newInstance(final Iterable<Resource> resources) { return PoliciesModelFactory.newResources(resources); }
/** * Returns a new {@code Resources} containing the given resource. * * @param resource the {@link Resource} to be contained in the new Resources. * @param furtherResources further {@link Resource}s to be contained in the new Resources. * @return the new {@code Resources}. * @throws NullPointerException if any argument is {@code null}. */ static Resources newInstance(final Resource resource, final Resource... furtherResources) { return PoliciesModelFactory.newResources(resource, furtherResources); }
/** * Returns the retrieved Resources. * * @return the retrieved Resources. */ public Resources getResources() { return PoliciesModelFactory.newResources(resources); }
/** * Returns a new {@link Resources} containing the given resource. * * @param resource the {@link Resource} to be contained in the new Resources. * @param furtherResources further {@link Resource}s to be contained in the new Resources. * @return the new {@code Resources}. * @throws NullPointerException if any argument is {@code null}. */ public static Resources newResources(final Resource resource, final Resource... furtherResources) { checkNotNull(resource, "mandatory resource"); checkNotNull(furtherResources, "additional resources"); final Collection<Resource> allResources = new ArrayList<>(1 + furtherResources.length); allResources.add(resource); Collections.addAll(allResources, furtherResources); return newResources(allResources); }
@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); }
/** * 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 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); }
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 {@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 new {@code ResourcesModified} from a JSON object. * * @param jsonObject the JSON object from which a new ResourcesModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourcesModified} 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 'ResourcesModified' * format. */ public static ResourcesModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourcesModified>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final Label label = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); final JsonObject resourcesJsonObject = jsonObject.getValueOrThrow(JSON_RESOURCES); final Resources extractedModifiedResources = PoliciesModelFactory.newResources(resourcesJsonObject); return of(policyId, label, extractedModifiedResources, revision, timestamp, 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 new {@code ResourcesModified} from a JSON object. * * @param jsonObject the JSON object from which a new ResourcesModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourcesModified} 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 'ResourcesModified' * format. */ public static ResourcesModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourcesModified>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final Label label = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); final JsonObject resourcesJsonObject = jsonObject.getValueOrThrow(JSON_RESOURCES); final Resources extractedModifiedResources = PoliciesModelFactory.newResources(resourcesJsonObject); return of(policyId, label, extractedModifiedResources, revision, timestamp, dittoHeaders); }); }
/** * 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); } }
private Route thingsEntryPolicyEntryResources(final RequestContext ctx, final DittoHeaders dittoHeaders, final String policyId) { return rawPathPrefix(mergeDoubleSlashes().concat(PathMatchers.segment()), label -> rawPathPrefix(mergeDoubleSlashes().concat(PATH_SUFFIX_RESOURCES), () -> pathEndOrSingleSlash(() -> route( get(() -> // GET /entries/<label>/resources handlePerRequest(ctx, RetrieveResources.of(policyId, Label.of(label), dittoHeaders)) ), put(() -> // PUT /entries/<label>/resources extractDataBytes(payloadSource -> handlePerRequest(ctx, dittoHeaders, payloadSource, policyEntryResourcesJson -> ModifyResources.of(policyId, Label.of( label), PoliciesModelFactory.newResources( JsonFactory.newObject( policyEntryResourcesJson)), dittoHeaders))) ) ) ) ) ); }