JsonParseException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders));
protected static FeatureProperties featurePropertiesFrom(final Adaptable adaptable) { return adaptable.getPayload() .getValue() .map(JsonValue::asObject) .map(ThingsModelFactory::newFeatureProperties) .orElseThrow(() -> JsonParseException.newBuilder().build()); }
JsonParseException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders));
} catch (final Exception e) { throw JsonParseException.newBuilder() .message("Could not transform JSON: " + e.getMessage()) .cause(e) .build();
if (!jsonValue.isObject()) { throw new DittoJsonException(JsonParseException.newBuilder() .message(MessageFormat.format("<{0}> is not a JSON object!", jsonValue)) .build());
/** * Creates a new JSON array from the given string. * * @param jsonString the string that represents the JSON array. * @return the JSON array that has been created from the string. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty. * @throws JsonParseException if {@code jsonString} does not contain a valid JSON array. */ public static JsonArray newArray(final String jsonString) { requireNonNull(jsonString, "The JSON string to create JSON array from must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException("The JSON string to create a JSON array from must not be empty!"); } if (isJsonNullLiteralString(jsonString)) { return nullArray(); } else { final JsonValue jsonValue = JsonValueParser.fromString().apply(jsonString); if (!jsonValue.isArray()) { final String msgPattern = "<{0}> is not a valid JSON array!"; throw JsonParseException.newBuilder().message(MessageFormat.format(msgPattern, jsonString)).build(); } return jsonValue.asArray(); } }
/** * Creates a JSON object from the given string. * * @param jsonString the string that represents the JSON object. * @return the JSON object that has been created from the string. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty. * @throws JsonParseException if {@code jsonString} does not contain a valid JSON object. */ public static JsonObject newObject(final String jsonString) { requireNonNull(jsonString, "The JSON string to create a JSON object from must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException("The JSON string to create a JSON object from must not be empty!"); } if (isJsonNullLiteralString(jsonString)) { return nullObject(); } else { final JsonValue jsonValue = JsonValueParser.fromString().apply(jsonString); if (!jsonValue.isObject()) { final String msgPattern = "<{0}> is not a valid JSON object!"; throw JsonParseException.newBuilder().message(MessageFormat.format(msgPattern, jsonString)).build(); } return jsonValue.asObject(); } }
/** * Creates a new {@code SudoRetrieveModifiedThingTags} from a JSON object. * * @param jsonObject the JSON object of which a new SudoRetrieveModifiedThingTags is to be created. * @param dittoHeaders the optional command headers of the request. * @return the SudoRetrieveModifiedThingTags which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws JsonMissingFieldException if the passed in {@code jsonObject} was not in the expected format. */ public static SudoStreamModifiedEntities fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { try { final Instant start = Instant.parse(jsonObject.getValueOrThrow(JSON_START)); final Instant end = Instant.parse(jsonObject.getValueOrThrow(JSON_END)); final Integer burst = jsonObject.getValue(JSON_BURST).orElse(null); final Long timeoutMillis = jsonObject.getValue(JSON_TIMEOUT_MILLIS).orElse(null); return SudoStreamModifiedEntities.of(start, end, burst, timeoutMillis, dittoHeaders); } catch (final DateTimeParseException e) { throw JsonParseException.newBuilder() .message("A given instant is not a valid timestamp.") .description(jsonObject.toString()) .cause(e) .build(); } }
/** * Creates a new {@code Resource} object based on the given {@code resourceKey} and {@code jsonValue}. * * @param resourceKey the JSON key which is assumed to be the path of a Resource prefixed with a type. * @param jsonValue the JSON value containing the effected permissions for the Resource. This value is supposed to * be a {@link JsonObject}. * @return a new {@code Resource} object. * @throws NullPointerException if any argument is {@code null}. * @throws DittoJsonException if {@code jsonValue} is not a JSON object or the JSON has not the expected format. */ public static Resource of(final ResourceKey resourceKey, final JsonValue jsonValue) { checkNotNull(jsonValue, "JSON value"); final EffectedPermissions effectedPermissions = Optional.of(jsonValue) .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(object -> wrapJsonRuntimeException(() -> ImmutableEffectedPermissions.fromJson(object))) .orElseThrow(() -> new DittoJsonException(JsonParseException.newBuilder() .message("The JSON object for the 'permissions' (grant/revoke) of the 'resource' '" + resourceKey + "' is missing or not an object.") .build())); return of(resourceKey, effectedPermissions); }
private static void validate(final CharSequence jsonKey, final JsonValue jsonValue) { checkNotNull(jsonKey, "JSON key"); checkNotNull(jsonValue, "JSON value"); final String msgTemplate = "Expected for Authorization Subject ''{0}'' a JSON object containing all of {1}" + " but got <{2}>!"; final Supplier<String> descriptionSupplier = () -> MessageFormat.format(msgTemplate, jsonKey, Permission.allToString(), jsonValue); if (!jsonValue.isObject()) { throw new DittoJsonException(JsonParseException.newBuilder() // .description(descriptionSupplier) // .build()); } final JsonObject permissionsJsonObject = jsonValue.asObject(); if (permissionsJsonObject.isEmpty()) { throw AclEntryInvalidException.newBuilder() // .description(descriptionSupplier) // .build(); } }
private static void validate(final CharSequence jsonKey, final JsonValue jsonValue) { checkNotNull(jsonKey, "JSON key"); checkNotNull(jsonValue, "JSON value"); final String msgTemplate = "Expected for Authorization Subject ''{0}'' a JSON object containing all of {1}" + " but got <{2}>!"; final Supplier<String> descriptionSupplier = () -> MessageFormat.format(msgTemplate, jsonKey, Permission.allToString(), jsonValue); if (!jsonValue.isObject()) { throw new DittoJsonException(JsonParseException.newBuilder() // .description(descriptionSupplier) // .build()); } final JsonObject permissionsJsonObject = jsonValue.asObject(); if (permissionsJsonObject.isEmpty()) { throw AclEntryInvalidException.newBuilder() // .description(descriptionSupplier) // .build(); } }
/** * Creates a new {@code ConnectionData} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the ConnectionData to be created. * @return a new ConnectionData which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object. */ static ConnectionData fromJson(final JsonObject jsonObject) { final JsonObject readAmqpConnection = jsonObject.getValueOrThrow(AMQP_CONNECTION); final AmqpConnection amqpConnection = AmqpBridgeModelFactory.connectionFromJson(readAmqpConnection); final String readConnectionStatus = jsonObject.getValueOrThrow(CONNECTION_STATUS); final ConnectionStatus connectionStatus = ConnectionStatus.forName(readConnectionStatus).orElseThrow(() -> JsonParseException.newBuilder() .message("Could not create ConnectionStatus from: " + jsonObject) .build()); return new ConnectionData(amqpConnection, connectionStatus); }
/** * Deserialize credentials from JSON. * * @param jsonObject credentials in JSON format. * @return deserialized credentials. */ static Credentials fromJson(final JsonObject jsonObject) { final String type = jsonObject.getValueOrThrow(JsonFields.TYPE); final Function<JsonObject, Credentials> deserializer = JsonFields.DESERIALIZER_MAP.get(type); if (deserializer == null) { throw JsonParseException.newBuilder() .message(String.format("Unknown credential type <%s>", type)) .description("Original JSON: " + jsonObject.toString()) .build(); } return deserializer.apply(jsonObject); }
/** * Deserialize credentials from JSON. * * @param jsonObject credentials in JSON format. * @return deserialized credentials. */ static Credentials fromJson(final JsonObject jsonObject) { final String type = jsonObject.getValueOrThrow(JsonFields.TYPE); final Function<JsonObject, Credentials> deserializer = JsonFields.DESERIALIZER_MAP.get(type); if (deserializer == null) { throw JsonParseException.newBuilder() .message(String.format("Unknown credential type <%s>", type)) .description("Original JSON: " + jsonObject.toString()) .build(); } return deserializer.apply(jsonObject); }
protected static JsonValue featurePropertyValueFrom(final Adaptable adaptable) { return adaptable.getPayload().getValue().orElseThrow(() -> JsonParseException.newBuilder().build()); }
protected static JsonValue attributeValueFrom(final Adaptable adaptable) { return adaptable.getPayload().getValue().orElseThrow(() -> JsonParseException.newBuilder().build()); }
protected static JsonValue attributeValueFrom(final Adaptable adaptable) { return adaptable.getPayload().getValue().orElseThrow(() -> JsonParseException.newBuilder().build()); }
protected static JsonValue featurePropertyValueFrom(final Adaptable adaptable) { return adaptable.getPayload().getValue().orElseThrow(() -> JsonParseException.newBuilder().build()); }
private Builder() { super(ERROR_CODE); description(DEFAULT_DESCRIPTION); }
protected static AclEntry aclEntryFrom(final Adaptable adaptable) { return adaptable.getPayload() .getValue() .map(permissions -> AccessControlListModelFactory .newAclEntry(leafValue(adaptable.getPayload().getPath()), permissions)) .orElseThrow(() -> JsonParseException.newBuilder().build()); }