private static String[] splitTypeWithPath(final String typeWithPath) { final String[] split = typeWithPath.split(ResourceKey.KEY_DELIMITER, 2); if (split.length < 2) { throw new DittoJsonException(JsonParseException.newBuilder() .message("The provided string was not in the expected format 'type:path'") .build()); } return split; }
private static ConnectionStatus getConnectionStatusOrThrow(final JsonObject jsonObject) { final String readConnectionStatus = jsonObject.getValueOrThrow(JsonFields.CONNECTION_STATUS); return ConnectionStatus.forName(readConnectionStatus) .orElseThrow(() -> JsonParseException.newBuilder() .message(MessageFormat.format("Connection status <{0}> is invalid!", readConnectionStatus)) .build()); }
private static ConnectionStatus getConnectionStatusOrThrow(final JsonObject jsonObject) { final String readConnectionStatus = jsonObject.getValueOrThrow(JsonFields.CONNECTION_STATUS); return ConnectionStatus.forName(readConnectionStatus) .orElseThrow(() -> JsonParseException.newBuilder() .message(MessageFormat.format("Connection status <{0}> is invalid!", readConnectionStatus)) .build()); }
/** * 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); }
private static ConnectionType getConnectionTypeOrThrow(final JsonObject jsonObject) { final String readConnectionType = jsonObject.getValueOrThrow(JsonFields.CONNECTION_TYPE); return ConnectionType.forName(readConnectionType) .orElseThrow(() -> JsonParseException.newBuilder() .message(MessageFormat.format("Connection type <{0}> is invalid!", readConnectionType)) .build()); }
/** * 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); }
private static ConnectionType getConnectionTypeOrThrow(final JsonObject jsonObject) { final String readConnectionType = jsonObject.getValueOrThrow(JsonFields.CONNECTION_TYPE); return ConnectionType.forName(readConnectionType) .orElseThrow(() -> JsonParseException.newBuilder() .message(MessageFormat.format("Connection type <{0}> is invalid!", readConnectionType)) .build()); }
private static Instant tryToParseModified(final CharSequence dateTime) { try { return Instant.parse(dateTime); } catch (final DateTimeParseException e) { final String msgPattern ="The JSON object's field <{0>' is not in ISO-8601 format as expected!"; throw JsonParseException.newBuilder() .message(MessageFormat.format(msgPattern, Thing.JsonFields.MODIFIED.getPointer())) .cause(e) .build(); } }
private static Instant tryToParseModified(final CharSequence dateTime) { try { return Instant.parse(dateTime); } catch (final DateTimeParseException e) { final String msgPattern ="The JSON object's field <{0>' is not in ISO-8601 format as expected!"; throw JsonParseException.newBuilder() .message(MessageFormat.format(msgPattern, Thing.JsonFields.MODIFIED.getPointer())) .cause(e) .build(); } }
private static com.eclipsesource.json.JsonValue tryToRead(final String json) { try { return Json.parse(json); } catch (final ParseException | StackOverflowError e) { throw JsonParseException.newBuilder() .message(MessageFormat.format("Failed to parse ''{0}''!", json)) .cause(e) .build(); } }
private static <T> T tryToParseJsonValue(final String jsonString, final DittoJsonHandler<?, ?, T> dittoJsonHandler) { try { return parseJsonValue(jsonString, dittoJsonHandler); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message(MessageFormat.format("Failed to parse JSON string ''{0}''!", jsonString)) .cause(e) .build(); } }
private static com.eclipsesource.json.JsonValue tryToRead(final Reader reader) { try { return Json.parse(reader); } catch (final ParseException | IOException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to parse JSON from reader!") .cause(e) .build(); } }
private static JsonValue tryToReadJsonValueFrom(final Reader reader) { try { return readJsonValueFrom(reader); } catch (final ParseException | IOException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to parse JSON value from reader!") .cause(e) .build(); } }
private static com.eclipsesource.json.JsonArray tryToReadMinimalJsonArrayFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asArray(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON array from string!") .cause(e) .build(); } }
private static com.eclipsesource.json.JsonObject tryToReadJsonObjectFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asObject(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON object from string!") .cause(e) .build(); } }
@SuppressWarnings("squid:S1166") private static String tryToDecodeString(final String s, final JsonParseOptions jsonParseOptions) { try { return decode(s, jsonParseOptions); } catch (final Exception e) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(s) // .description("Check if the field selector is correctly URL encoded.") // .cause(e) // .build(); } }
@SuppressWarnings("squid:S1166") private static String tryToDecodeString(final String s, final JsonParseOptions jsonParseOptions) { try { return decode(s, jsonParseOptions); } catch (final Exception e) { throw JsonFieldSelectorInvalidException.newBuilder() // .fieldSelector(s) // .description("Check if the field selector is correctly URL encoded.") // .cause(e) // .build(); } }
private static DittoRuntimeException buildError(final WithDittoHeaders withDittoHeaders) { final JsonParseException jsonException = JsonParseException.newBuilder() .message("JSON contains forbidden character '\\u0000'") .build(); return new DittoJsonException(jsonException).setDittoHeaders(withDittoHeaders.getDittoHeaders()); } }
/** * 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); }
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(); } }