/** * Returns the retrieved Policy. * * @return the retrieved Policy. */ public Policy getPolicy() { return PoliciesModelFactory.newPolicy(policy); }
/** * Returns the retrieved Policy. * * @return the retrieved Policy. */ public Policy getPolicy() { return PoliciesModelFactory.newPolicy(policy); }
/** * Returns the retrieved Policy. * * @return the retrieved Policy. */ public Policy getPolicy() { return PoliciesModelFactory.newPolicy(policy); }
/** * Returns the retrieved Policy. * * @return the retrieved Policy. */ public Policy getPolicy() { return PoliciesModelFactory.newPolicy(policy); }
@Override protected Policy createJsonifiableFrom(@Nonnull final JsonObject jsonObject) { return PoliciesModelFactory.newPolicy(jsonObject); }
@Override protected Policy createJsonifiableFrom(@Nonnull final JsonObject jsonObject) { return PoliciesModelFactory.newPolicy(jsonObject); }
/** * Returns a new immutable Policy based on the given JSON string. * * @param jsonString the JSON object representation as String of a Policy. * @return the new initialised {@code Policy}. * @throws org.eclipse.ditto.model.base.exceptions.DittoJsonException if {@code jsonString} cannot be parsed to * {@link Policy}. */ public static Policy newPolicy(final String jsonString) { final JsonObject jsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return newPolicy(jsonObject); }
private static JsonObject substituteInitialPolicy(final JsonObject initialPolicy, final HeaderBasedPlaceholderSubstitutionAlgorithm substitutionAlgorithm, final DittoHeaders dittoHeaders) { Policy existingPolicy; try { existingPolicy = PoliciesModelFactory.newPolicy(initialPolicy); } catch (final RuntimeException e) { // Just log to debug, error is handled somewhere else LOGGER.debug("Failed to parse initial policy.", e); return initialPolicy; } final Policy substitutedPolicy = substitutePolicy(existingPolicy, substitutionAlgorithm, dittoHeaders); return substitutedPolicy.toJson(); }
private static JsonObject substituteInitialPolicy(final JsonObject initialPolicy, final HeaderBasedPlaceholderSubstitutionAlgorithm substitutionAlgorithm, final DittoHeaders dittoHeaders) { Policy existingPolicy; try { existingPolicy = PoliciesModelFactory.newPolicy(initialPolicy); } catch (final RuntimeException e) { // Just log to debug, error is handled somewhere else LOGGER.debug("Failed to parse initial policy.", e); return initialPolicy; } final Policy substitutedPolicy = substitutePolicy(existingPolicy, substitutionAlgorithm, dittoHeaders); return substitutedPolicy.toJson(); } }
/** * Creates a new {@code PolicyModified} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyModified} 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 'PolicyModified' format. */ public static PolicyModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyModified>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy extractedModifiedPolicy = PoliciesModelFactory.newPolicy(policyJsonObject); return of(extractedModifiedPolicy, revision, timestamp, dittoHeaders); }); }
/** * Creates a command for creating a {@code Policy} 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 CreatePolicy fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<CreatePolicy>(TYPE, jsonObject).deserialize(() -> { final Policy policy = PoliciesModelFactory.newPolicy(jsonObject.getValueOrThrow(JSON_POLICY)); return of(policy, dittoHeaders); }); }
/** * Creates a new {@code PolicyCreated} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyCreated instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyCreated} 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 jsonString} was not in the expected 'PolicyCreated' format. */ public static PolicyCreated fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyCreated>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy extractedModifiedPolicy = PoliciesModelFactory.newPolicy(policyJsonObject); return new PolicyCreated(extractedModifiedPolicy, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code PolicyCreated} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyCreated instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyCreated} 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 jsonString} was not in the expected 'PolicyCreated' format. */ public static PolicyCreated fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyCreated>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy extractedModifiedPolicy = PoliciesModelFactory.newPolicy(policyJsonObject); return new PolicyCreated(extractedModifiedPolicy, revision, timestamp, dittoHeaders); }); }
/** * Creates a command for creating a {@code Policy} 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 CreatePolicy fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<CreatePolicy>(TYPE, jsonObject).deserialize(() -> { final Policy policy = PoliciesModelFactory.newPolicy(jsonObject.getValueOrThrow(JSON_POLICY)); return of(policy, dittoHeaders); }); }
/** * Creates a new {@code PolicyModified} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyModified} 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 'PolicyModified' format. */ public static PolicyModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyModified>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy extractedModifiedPolicy = PoliciesModelFactory.newPolicy(policyJsonObject); return of(extractedModifiedPolicy, revision, timestamp, dittoHeaders); }); }
private static Optional<Policy> getInlinedOrDefaultPolicyForCreateThing(final CreateThing createThing) { final Optional<JsonObject> initialPolicy = createThing.getInitialPolicy(); if (initialPolicy.isPresent()) { final JsonObject policyJson = initialPolicy.get(); final JsonObjectBuilder policyJsonBuilder = policyJson.toBuilder(); final Thing thing = createThing.getThing(); if (thing.getPolicyId().isPresent() || !policyJson.contains(Policy.JsonFields.ID.getPointer())) { final String policyId = thing.getPolicyId().orElse(createThing.getThingId()); policyJsonBuilder.set(Policy.JsonFields.ID, policyId); } return Optional.of(PoliciesModelFactory.newPolicy(policyJsonBuilder.build())); } else { return getDefaultPolicy(createThing.getDittoHeaders().getAuthorizationContext(), createThing.getThingId()); } }
/** * Creates a command for modifying a {@code Policy} 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. * @throws org.eclipse.ditto.json.JsonMissingFieldException if {@code jsonObject} did not contain a field for {@link * PolicyModifyCommand.JsonFields#JSON_POLICY_ID} or {@link #JSON_POLICY}. */ public static ModifyPolicy fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyPolicy>(TYPE, jsonObject).deserialize(() -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy policy = PoliciesModelFactory.newPolicy(policyJsonObject); final Optional<String> optionalPolicyId = jsonObject.getValue(PolicyModifyCommand.JsonFields .JSON_POLICY_ID); final String policyId = optionalPolicyId.orElseGet(() -> policy.getId().orElseThrow(() -> new JsonMissingFieldException(PolicyModifyCommand.JsonFields.JSON_POLICY_ID) )); return of(policyId, policy, dittoHeaders); }); }
/** * Creates a command for modifying a {@code Policy} 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. * @throws org.eclipse.ditto.json.JsonMissingFieldException if {@code jsonObject} did not contain a field for {@link * PolicyModifyCommand.JsonFields#JSON_POLICY_ID} or {@link #JSON_POLICY}. */ public static ModifyPolicy fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyPolicy>(TYPE, jsonObject).deserialize(() -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy policy = PoliciesModelFactory.newPolicy(policyJsonObject); final Optional<String> optionalPolicyId = jsonObject.getValue(PolicyModifyCommand.JsonFields .JSON_POLICY_ID); final String policyId = optionalPolicyId.orElseGet(() -> policy.getId().orElseThrow(() -> new JsonMissingFieldException(PolicyModifyCommand.JsonFields.JSON_POLICY_ID) )); return of(policyId, policy, dittoHeaders); }); }
private Optional<Policy> checkInitialPolicy(final CreateThing createThing, final JsonObject inlinedPolicy, final ActorRef sender) { try { // Java doesn't permit conversion of this early return into assignment to final variable. return Optional.of(PoliciesModelFactory.newPolicy(inlinedPolicy)); } catch (final JsonRuntimeException | DittoJsonException e) { final String thingId = createThing.getThingId(); final DittoRuntimeException error = PolicyInvalidException.newBuilderForCause(e, thingId) .dittoHeaders(createThing.getDittoHeaders()) .build(); replyToSender(error, sender); } catch (final DittoRuntimeException e) { final DittoHeaders dittoHeaders = createThing.getDittoHeaders(); if (e instanceof PolicyException) { // user error; no need to log stack trace. replyToSender(e.setDittoHeaders(dittoHeaders), sender); } else { reportError("Error during creation of inline policy from JSON", sender, e, dittoHeaders); } } return Optional.empty(); }
private Route policyEntry(final RequestContext ctx, final DittoHeaders dittoHeaders, final String policyId) { return pathEndOrSingleSlash(() -> Directives.route( get(() -> // GET /policies/<policyId> handlePerRequest(ctx, RetrievePolicy.of(policyId, dittoHeaders)) ), put(() -> // PUT /policies/<policyId> extractDataBytes(payloadSource -> handlePerRequest(ctx, dittoHeaders, payloadSource, policyJson -> ModifyPolicy .of(policyId, PoliciesModelFactory.newPolicy( createPolicyJsonObjectForPut(policyJson, policyId)), dittoHeaders) ) ) ), delete(() -> // DELETE /policies/<policyId> handlePerRequest(ctx, DeletePolicy.of(policyId, dittoHeaders)) ) ) ); }