@Override protected JsonMissingFieldException doBuild(final String errorCode, @Nullable final String message, @Nullable final String description, @Nullable final Throwable cause, @Nullable final URI href) { return new JsonMissingFieldException(message, description, cause, href); }
@Override protected JsonMissingFieldException doBuild(final String errorCode, @Nullable final String message, @Nullable final String description, @Nullable final Throwable cause, @Nullable final URI href) { return new JsonMissingFieldException(message, description, cause, href); }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { return getValue(fieldDefinition).orElseThrow(() -> new JsonMissingFieldException(fieldDefinition)); }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { throw new JsonMissingFieldException(fieldDefinition.getPointer()); }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { throw new JsonMissingFieldException(fieldDefinition.getPointer()); }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { throw new JsonMissingFieldException(fieldDefinition.getPointer()); }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { return getValue(fieldDefinition).orElseThrow(() -> new JsonMissingFieldException(fieldDefinition)); }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { throw new JsonMissingFieldException(fieldDefinition.getPointer()); }
@Override protected String resolveType(final JsonObject jsonObject) { /* * If type was not present (was included in V2) take "event" instead and transform to V2 format. * Fail if "event" also is not present. */ return jsonObject.getValue(Event.JsonFields.TYPE) .orElseGet(() -> extractTypeV1(jsonObject) .orElseThrow(() -> new JsonMissingFieldException(Event.JsonFields.TYPE))); }
private void validateCommandType() { final JsonFieldDefinition<String> typeFieldDefinition = Command.JsonFields.TYPE; final Optional<String> actualCommandTypeOptional = jsonObject.getValue(typeFieldDefinition); final String actualCommandType = actualCommandTypeOptional.orElseGet(() -> { // "type" was introduced in V2, if not present, take "command" instead. final String commandTypePrefix = expectedCommandType.split(":")[0]; return jsonObject.getValue(Command.JsonFields.ID) .map(id -> commandTypePrefix + ':' + id) .orElseThrow(() -> new JsonMissingFieldException(typeFieldDefinition)); }); if (!expectedCommandType.equals(actualCommandType)) { final String msgPattern = "Command JSON was not a <{0}> command but a <{1}>!"; final String msg = MessageFormat.format(msgPattern, expectedCommandType, actualCommandType); throw new DittoJsonException(new JsonParseException(msg)); } }
private void validateCommandType() { final JsonFieldDefinition<String> typeFieldDefinition = Command.JsonFields.TYPE; final Optional<String> actualCommandTypeOptional = jsonObject.getValue(typeFieldDefinition); final String actualCommandType = actualCommandTypeOptional.orElseGet(() -> { // "type" was introduced in V2, if not present, take "command" instead. final String commandTypePrefix = expectedCommandType.split(":")[0]; return jsonObject.getValue(Command.JsonFields.ID) .map(id -> commandTypePrefix + ':' + id) .orElseThrow(() -> new JsonMissingFieldException(typeFieldDefinition)); }); if (!expectedCommandType.equals(actualCommandType)) { final String msgPattern = "Command JSON was not a <{0}> command but a <{1}>!"; final String msg = MessageFormat.format(msgPattern, expectedCommandType, actualCommandType); throw new DittoJsonException(new JsonParseException(msg)); } }
/** * Returns a new {@code JsonWebKey} for the given {@code jsonObject}. * * @param jsonObject the object containing the JSON Web Key. * @return the JsonWebKey. * @throws JsonMissingFieldException if any {@code JsonField} is missing. */ public static JsonWebKey fromJson(final JsonObject jsonObject) { final String type = jsonObject.getValueOrThrow(JsonFields.KEY_TYPE); final String algorithm = jsonObject.getValue(JsonFields.KEY_ALGORITHM).orElse(null); final String usage = jsonObject.getValue(JsonFields.KEY_USAGE).orElse(null); final String id = jsonObject.getValueOrThrow(JsonFields.KEY_ID); final BigInteger modulus = jsonObject.getValue(JsonFields.KEY_MODULUS) .map(string -> string.getBytes(StandardCharsets.UTF_8)) .map(DECODER::decode) .map(bytes -> new BigInteger(1, bytes)) .orElseThrow(() -> new JsonMissingFieldException(JsonFields.KEY_MODULUS.getPointer())); final BigInteger exponent = jsonObject.getValue(JsonFields.KEY_EXPONENT) .map(string -> string.getBytes(StandardCharsets.UTF_8)) .map(DECODER::decode) .map(bytes -> new BigInteger(1, bytes)) .orElseThrow(() -> new JsonMissingFieldException(JsonFields.KEY_EXPONENT.getPointer())); return of(type, algorithm, usage, id, modulus, exponent); }
private void validateEventType() { final String type = jsonObject.getValue(Event.JsonFields.TYPE) .orElseGet(() -> // if type was not present (was included in V2) // take event instead and transform to V2 format, fail if "event" is not present, too extractEventTypeV1() .orElseThrow(() -> new JsonMissingFieldException(Event.JsonFields.TYPE.getPointer())) ); if (!expectedType.equals(type)) { final String msgPattern = "Event JSON was not a <{0}> event but a <{1}>!"; final String msg = MessageFormat.format(msgPattern, expectedType, type); throw new DittoJsonException(new JsonParseException(msg)); } }
/** * Creates a new {@code ConnectivityErrorResponse} containing the causing {@code DittoRuntimeException} which is * deserialized from the passed {@code jsonObject} using a special {@code ConnectivityErrorRegistry}. * * @param connectivityErrorRegistry the special {@code ConnectivityErrorRegistry} to use for deserializing the * DittoRuntimeException. * @param jsonObject the JSON representation of the causing {@code DittoRuntimeException}. * @param dittoHeaders the DittoHeaders to use. * @return the ConnectivityErrorResponse. */ public static ConnectivityErrorResponse fromJson(final ConnectivityErrorRegistry connectivityErrorRegistry, final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final JsonObject payload = jsonObject.getValue(ConnectivityCommandResponse.JsonFields.PAYLOAD) .map(JsonValue::asObject) .orElseThrow( () -> new JsonMissingFieldException(ConnectivityCommandResponse.JsonFields.PAYLOAD.getPointer())); final DittoRuntimeException exception = connectivityErrorRegistry.parse(payload, dittoHeaders); return of(exception, dittoHeaders); }
/** * Creates a new {@code SearchErrorResponse} containing the causing {@code DittoRuntimeException} which is * deserialized from the passed {@code jsonObject} using a special {@code SearchErrorRegistry}. * * @param searchErrorRegistry the special {@code SearchErrorRegistry} to use for deserializing the * DittoRuntimeException. * @param jsonObject the JSON representation of the causing {@code DittoRuntimeException}. * @param dittoHeaders the DittoHeaders to use. * @return the SearchErrorResponse. */ public static SearchErrorResponse fromJson(final ThingSearchErrorRegistry searchErrorRegistry, final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final JsonObject payload = jsonObject.getValue(JsonFields.PAYLOAD) .map(JsonValue::asObject) .orElseThrow(() -> new JsonMissingFieldException(JsonFields.PAYLOAD.getPointer())); final DittoRuntimeException exception = searchErrorRegistry.parse(payload, dittoHeaders); return of(exception, dittoHeaders); }
/** * Creates a new {@code ConnectivityErrorResponse} containing the causing {@code DittoRuntimeException} which is * deserialized from the passed {@code jsonObject} using a special {@code ConnectivityErrorRegistry}. * * @param connectivityErrorRegistry the special {@code ConnectivityErrorRegistry} to use for deserializing the * DittoRuntimeException. * @param jsonObject the JSON representation of the causing {@code DittoRuntimeException}. * @param dittoHeaders the DittoHeaders to use. * @return the ConnectivityErrorResponse. */ public static ConnectivityErrorResponse fromJson(final ConnectivityErrorRegistry connectivityErrorRegistry, final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final JsonObject payload = jsonObject.getValue(ConnectivityCommandResponse.JsonFields.PAYLOAD) .map(JsonValue::asObject) .orElseThrow( () -> new JsonMissingFieldException(ConnectivityCommandResponse.JsonFields.PAYLOAD.getPointer())); final DittoRuntimeException exception = connectivityErrorRegistry.parse(payload, dittoHeaders); return of(exception, dittoHeaders); }
/** * Creates a new {@code SearchErrorResponse} containing the causing {@code DittoRuntimeException} which is * deserialized from the passed {@code jsonObject} using a special {@code SearchErrorRegistry}. * * @param searchErrorRegistry the special {@code SearchErrorRegistry} to use for deserializing the * DittoRuntimeException. * @param jsonObject the JSON representation of the causing {@code DittoRuntimeException}. * @param dittoHeaders the DittoHeaders to use. * @return the SearchErrorResponse. */ public static SearchErrorResponse fromJson(final ThingSearchErrorRegistry searchErrorRegistry, final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final JsonObject payload = jsonObject.getValue(JsonFields.PAYLOAD) .map(JsonValue::asObject) .orElseThrow(() -> new JsonMissingFieldException(JsonFields.PAYLOAD.getPointer())); final DittoRuntimeException exception = searchErrorRegistry.parse(payload, dittoHeaders); return of(exception, dittoHeaders); }
private CompletableFuture<JsonArray> mapResponseToJsonArray(final HttpResponse response) { final CompletionStage<JsonObject> body = response.entity().getDataBytes().fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .map(JsonFactory::readFrom) .map(JsonValue::asObject) .runWith(Sink.head(), httpClient.getActorMaterializer()); final JsonPointer keysPointer = JsonPointer.of("keys"); return body.toCompletableFuture() .thenApply(jsonObject -> jsonObject.getValue(keysPointer).map(JsonValue::asArray) .orElseThrow(() -> new JsonMissingFieldException(keysPointer))) .exceptionally(t -> { throw new IllegalStateException("Failed to extract public keys from JSON response: " + body, t); }); }
/** * Creates a new {@code ModifyThing} 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 * ThingModifyCommand.JsonFields#JSON_THING_ID} or {@link #JSON_THING}. */ public static ModifyThing fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyThing>(TYPE, jsonObject).deserialize(() -> { final JsonObject thingJsonObject = jsonObject.getValueOrThrow(JSON_THING); final Thing extractedThing = ThingsModelFactory.newThing(thingJsonObject); final JsonObject initialPolicyObject = jsonObject.getValue(JSON_INITIAL_POLICY).orElse(null); final String policyIdOrPlaceholder = jsonObject.getValue(JSON_POLICY_ID_OR_PLACEHOLDER).orElse(null); final Optional<String> optionalThingId = jsonObject.getValue(ThingModifyCommand.JsonFields.JSON_THING_ID); final String thingId = optionalThingId.orElseGet(() -> extractedThing.getId().orElseThrow(() -> new JsonMissingFieldException(ThingModifyCommand.JsonFields.JSON_THING_ID) )); return of(thingId, extractedThing, initialPolicyObject, policyIdOrPlaceholder, 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); }); }