/** * Returns a new {@code Resource} with the specified {@code resourceKey} and {@code effectedPermissions}. * * @param resourceKey the ResourceKey of the new Resource to create. * @param effectedPermissions the EffectedPermissions of the new Resource to create. * @return the new {@code Resource}. * @throws NullPointerException if any argument is {@code null}. */ static Resource newInstance(final ResourceKey resourceKey, final JsonValue effectedPermissions) { return PoliciesModelFactory.newResource(resourceKey, effectedPermissions); }
/** * 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); }
@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 the retrieved Resource. * * @return the retrieved Resource. */ public Resource getResource() { return PoliciesModelFactory.newResource(resourceKey, resource); }
/** * Returns a new {@code Resource} with the specified {@code resourceKey} and {@code effectedPermissions}. * * @param resourceKey the ResourceKey of the new Resource to create. * @param effectedPermissions the EffectedPermissions of the new Resource to create. * @return the new {@code Resource}. * @throws NullPointerException if any argument is {@code null}. */ static Resource newInstance(final ResourceKey resourceKey, final EffectedPermissions effectedPermissions) { return PoliciesModelFactory.newResource(resourceKey, effectedPermissions); }
/** * Returns the retrieved Resource. * * @return the retrieved Resource. */ public Resource getResource() { return PoliciesModelFactory.newResource(resourceKey, resource); }
/** * Returns a new {@code 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 {@code Resource}. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code resourceType} is empty. */ static Resource newInstance(final CharSequence resourceType, final CharSequence resourcePath, final EffectedPermissions effectedPermissions) { return PoliciesModelFactory.newResource(resourceType, resourcePath, effectedPermissions); }
/** * Set the given {@link EffectedPermissions} on the specified resource key in the specified label. * * @param label the label identifying the PolicyEntry to modify. * @param resourceKey the ResourceKey to set the effected permissions on. * @param effectedPermissions the EffectedPermissions to set on the resource in the label. * @return a copy of this Policy with the changed state. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code label} is empty. */ default Policy setEffectedPermissionsFor(final CharSequence label, final ResourceKey resourceKey, final EffectedPermissions effectedPermissions) { return setResourceFor(label, PoliciesModelFactory.newResource(resourceKey, effectedPermissions)); }
private static Resource createResourceForPut(final String jsonString, final ResourceKey resourceKey) { final JsonObject jsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return PoliciesModelFactory.newResource(resourceKey, jsonObject); }
/** * Creates a response to a {@code ModifyResource} 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 ModifyResourceResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<ModifyResourceResponse>(TYPE, jsonObject) .deserialize((statusCode) -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID); final String stringLabel = jsonObject.getValueOrThrow(JSON_LABEL); final Label label = PoliciesModelFactory.newLabel(stringLabel); final Optional<ResourceKey> extractedResourceKey = jsonObject.getValue(JSON_RESOURCE_KEY) .map(ResourceKey::newInstance); final Resource extractedResourceCreated = jsonObject.getValue(JSON_RESOURCE) .map(JsonValue::asObject) .map(obj -> extractedResourceKey.map( resourceKey -> PoliciesModelFactory.newResource(resourceKey, obj)).orElse(null)) .orElse(null); return new ModifyResourceResponse(policyId, label, extractedResourceCreated, statusCode, dittoHeaders); }); }
/** * Creates a response to a {@code ModifyResource} 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 ModifyResourceResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<ModifyResourceResponse>(TYPE, jsonObject) .deserialize((statusCode) -> { final String policyId = jsonObject.getValueOrThrow(PolicyModifyCommandResponse.JsonFields.JSON_POLICY_ID); final String stringLabel = jsonObject.getValueOrThrow(JSON_LABEL); final Label label = PoliciesModelFactory.newLabel(stringLabel); final Optional<ResourceKey> extractedResourceKey = jsonObject.getValue(JSON_RESOURCE_KEY) .map(ResourceKey::newInstance); final Resource extractedResourceCreated = jsonObject.getValue(JSON_RESOURCE) .map(JsonValue::asObject) .map(obj -> extractedResourceKey.map( resourceKey -> PoliciesModelFactory.newResource(resourceKey, obj)).orElse(null)) .orElse(null); return new ModifyResourceResponse(policyId, label, extractedResourceCreated, statusCode, 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); }); }
/** * Creates a new {@code ResourceCreated} from a JSON object. * * @param jsonObject the JSON object from which a new ResourceCreated instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourceCreated} 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 'ResourceCreated' format. */ public static ResourceCreated fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourceCreated>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final Label label = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); final ResourceKey resourceKey = ResourceKey.newInstance(jsonObject.getValueOrThrow(JSON_RESOURCE_KEY)); final JsonObject resourceJsonObject = jsonObject.getValueOrThrow(JSON_RESOURCE); final Resource extractedCreatedResource = PoliciesModelFactory.newResource(resourceKey, resourceJsonObject); return of(policyId, label, extractedCreatedResource, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code ResourceCreated} from a JSON object. * * @param jsonObject the JSON object from which a new ResourceCreated instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourceCreated} 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 'ResourceCreated' format. */ public static ResourceCreated fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourceCreated>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final Label label = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); final ResourceKey resourceKey = ResourceKey.newInstance(jsonObject.getValueOrThrow(JSON_RESOURCE_KEY)); final JsonObject resourceJsonObject = jsonObject.getValueOrThrow(JSON_RESOURCE); final Resource extractedCreatedResource = PoliciesModelFactory.newResource(resourceKey, resourceJsonObject); return of(policyId, label, extractedCreatedResource, revision, timestamp, 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); }); }
/** * Creates a new {@code ResourceModified} from a JSON object. * * @param jsonObject the JSON object from which a new ResourceModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourceModified} 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 'ResourceModified' * format. */ public static ResourceModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourceModified>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final Label label = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); final String resourceKey = jsonObject.getValueOrThrow(JSON_RESOURCE_KEY); final JsonObject resourceJsonObject = jsonObject.getValueOrThrow(JSON_RESOURCE); final Resource extractedModifiedResource = PoliciesModelFactory.newResource(ResourceKey.newInstance(resourceKey), resourceJsonObject); return of(policyId, label, extractedModifiedResource, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code ResourceModified} from a JSON object. * * @param jsonObject the JSON object from which a new ResourceModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourceModified} 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 'ResourceModified' * format. */ public static ResourceModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourceModified>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String policyId = jsonObject.getValueOrThrow(JsonFields.POLICY_ID); final Label label = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); final String resourceKey = jsonObject.getValueOrThrow(JSON_RESOURCE_KEY); final JsonObject resourceJsonObject = jsonObject.getValueOrThrow(JSON_RESOURCE); final Resource extractedModifiedResource = PoliciesModelFactory.newResource(ResourceKey.newInstance(resourceKey), resourceJsonObject); return of(policyId, label, extractedModifiedResource, revision, timestamp, dittoHeaders); }); }