ThingResourceKey(final CharSequence path) { resourceKey = ResourceKey.newInstance(PoliciesResourceType.THING, path); }
ThingResourceKey(final CharSequence path) { resourceKey = ResourceKey.newInstance(PoliciesResourceType.THING, path); }
private static ResourceKey resourceKeyFromUnmatchedPath(final String resource) { // cut off leading "/" if there is one: return resource.startsWith("/") ? ResourceKey.newInstance(resource.substring(1)) : ResourceKey.newInstance(resource); }
/** * Removes the Resource with the given {@code resourceType} and {@code resourcePath} from a copy of this Resources. * * @param resourceType the type of the Resource to be removed. * @param resourcePath the path of the Resource to be removed. * @return a copy of this Resources with {@code resource} removed. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code resourceType} is empty. */ default Resources removeResource(final String resourceType, final CharSequence resourcePath) { return removeResource(ResourceKey.newInstance(resourceType, resourcePath)); }
/** * Returns the Resource with the given {@code resourceType} and {@code resourcePath} or an empty optional. * * @param resourceType the type of the Resource to be retrieved. * @param resourcePath the path of the Resource to be retrieved. * @return the Resource or an empty optional. * @throws NullPointerException if {@code resourcePath} is {@code null}. * @throws IllegalArgumentException if {@code resourceType} is empty. */ default Optional<Resource> getResource(final String resourceType, final CharSequence resourcePath) { return getResource(ResourceKey.newInstance(resourceType, resourcePath)); }
/** * Set the given permissions on the specified {@code resourceType} and {@code resourcePath} in the specified * {@code label} as "revoked" to this builder. * * @param label the label identifying the PolicyEntry to modify. * @param resourceType the type of the Resource to set the permissions on. * @param resourcePath the path of the Resource to set the permissions on. * @param revokedPermissions the Permissions to set as "revoke"ed on the Resource in the label. * @return this builder to allow method chaining. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code label} or {@code resourceType} is empty. */ default PolicyBuilder setRevokedPermissionsFor(final CharSequence label, final String resourceType, final CharSequence resourcePath, final Permissions revokedPermissions) { return setRevokedPermissionsFor(label, ResourceKey.newInstance(resourceType, resourcePath), revokedPermissions); }
public PolicyAssert hasResourceEffectedPermissionsFor(final Label label, final String type, final JsonPointer path, final EffectedPermissions expectedEffectedPermissions) { return hasResourceEffectedPermissionsFor(label, ResourceKey.newInstance(type, path), expectedEffectedPermissions); }
/** * Get read subjects from an enforcer for the resource type {@code things}. * * @param signal the signal to get read subjects for. * @param enforcer the enforcer. * @return read subjects of the signal. */ protected static Set<String> getThingsReadSubjects(final Signal<?> signal, final Enforcer enforcer) { final ResourceKey resourceKey = ResourceKey.newInstance(ThingCommand.RESOURCE_TYPE, signal.getResourcePath()); return enforcer.getSubjectIdsWithPermission(resourceKey, Permission.READ).getGranted(); }
public PolicyAssert hasResourceFor(final Label label, final String type, final JsonPointer path) { return hasResourceFor(label, ResourceKey.newInstance(type, path)); }
/** * Creates a new {@code Resources} from the specified JSON object. * * @param jsonObject the JSON object of which a new Resources instance is to be created. * @return the {@code Resources} 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 * 'Resources' format. */ public static Resources fromJson(final JsonObject jsonObject) { final List<Resource> theResources = jsonObject.stream() .filter(field -> !Objects.equals(field.getKey(), JsonSchemaVersion.getJsonKey())) .map(field -> ImmutableResource.of(ResourceKey.newInstance(field.getKeyName()), field.getValue())) .collect(Collectors.toList()); return of(theResources); }
public PolicyAssert doesNotHaveResourceFor(final Label label, final String type, final JsonPointer path) { return doesNotHaveResourceFor(label, ResourceKey.newInstance(type, path)); }
private static JsonObject getJsonViewForPolicyQueryCommandResponse(final JsonObject responseEntity, final PolicyQueryCommandResponse response, final Enforcer enforcer) { final ResourceKey resourceKey = ResourceKey.newInstance(PolicyCommand.RESOURCE_TYPE, response.getResourcePath()); final AuthorizationContext authorizationContext = response.getDittoHeaders().getAuthorizationContext(); return enforcer.buildJsonView(resourceKey, responseEntity, authorizationContext, POLICY_QUERY_COMMAND_RESPONSE_WHITELIST, Permissions.newInstance(Permission.READ)); }
/** * Restrict view on a JSON object by enforcer. * * @param responseEntity the JSON object to restrict view on. * @param response the response containing the object. * @param enforcer the enforcer. * @return JSON object with view restricted by enforcer. */ private static JsonObject getJsonViewForThingQueryCommandResponse(final JsonObject responseEntity, final ThingQueryCommandResponse response, final Enforcer enforcer) { final ResourceKey resourceKey = ResourceKey.newInstance(ThingCommand.RESOURCE_TYPE, response.getResourcePath()); final AuthorizationContext authorizationContext = response.getDittoHeaders().getAuthorizationContext(); return enforcer.buildJsonView(resourceKey, responseEntity, authorizationContext, THING_QUERY_COMMAND_RESPONSE_WHITELIST, Permissions.newInstance(Permission.READ)); }
/** * Creates a command for deleting a {@code Resource} 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 DeleteResource fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<DeleteResource>(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); return of(policyId, label, ResourceKey.newInstance(resourceKey), dittoHeaders); }); }
/** * Creates a new {@code ResourceDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new ResourceDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourceDeleted} 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 'ResourceDeleted' format. */ public static ResourceDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourceDeleted>(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); return of(policyId, label, ResourceKey.newInstance(resourceKey), revision, timestamp, dittoHeaders); }); }
/** * Creates a command for deleting a {@code Resource} 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 DeleteResource fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<DeleteResource>(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); return of(policyId, label, ResourceKey.newInstance(resourceKey), dittoHeaders); }); }
/** * Creates a new {@code RetrieveResource} from a JSON object. * * @param jsonObject the JSON object of which a new RetrieveResource instance is to be created. * @param dittoHeaders the optional command headers of the request. * @return the {@code RetrieveResources} 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 * format. */ public static RetrieveResource fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<RetrieveResource>(TYPE, jsonObject).deserialize(() -> { final String policyId = jsonObject.getValueOrThrow(PolicyQueryCommand.JsonFields.JSON_POLICY_ID); final Label extractedLabel = Label.of(jsonObject.getValueOrThrow(JSON_LABEL)); final String extractedKey = jsonObject.getValueOrThrow(JSON_RESOURCE_KEY); return of(policyId, extractedLabel, ResourceKey.newInstance(extractedKey), dittoHeaders); }); }
/** * Creates a new {@code ResourceDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new ResourceDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ResourceDeleted} 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 'ResourceDeleted' format. */ public static ResourceDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ResourceDeleted>(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); return of(policyId, label, ResourceKey.newInstance(resourceKey), revision, timestamp, dittoHeaders); }); }
private void publishMessageCommand(final MessageCommand command, final Enforcer enforcer, final ActorRef sender) { final ResourceKey resourceKey = ResourceKey.newInstance(MessageCommand.RESOURCE_TYPE, command.getResourcePath()); final Set<String> messageReaders = enforcer.getSubjectIdsWithPermission(resourceKey, Permission.READ) .getGranted(); final DittoHeaders headersWithReadSubjects = command.getDittoHeaders() .toBuilder() .readSubjects(messageReaders) .build(); final MessageCommand commandWithReadSubjects = command.setDittoHeaders(headersWithReadSubjects); publishToMediator(commandWithReadSubjects, commandWithReadSubjects.getTypePrefix(), sender); // answer the sender immediately for fire-and-forget message commands. getResponseForFireAndForgetMessage(commandWithReadSubjects) .ifPresent(response -> replyToSender(response, sender)); }
/** * 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); }); }