@Override protected String resolveType(final JsonObject jsonObject) { final Supplier<String> command = () -> jsonObject.getValue(Command.JsonFields.ID) .map(cmd -> getTypePrefix() + cmd) // and transform to V2 format // fail if "command" also is not present .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(Command.JsonFields.TYPE.getPointer().toString()) .build()); // if type was not present (was included in V2) take "command" instead return jsonObject.getValue(Command.JsonFields.TYPE).orElseGet(command); }
JsonMissingFieldException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders));
JsonMissingFieldException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders));
Collections.addAll(allFieldNames, missingFieldNameChildren); return fieldName(String.join(".", allFieldNames));
Collections.addAll(allFieldNames, missingFieldNameChildren); return fieldName(String.join(".", allFieldNames));
/** * Creates a response to a {@link RetrieveThing} command from a JSON object. * * @param jsonObject the JSON object of which the response is to be created. * @param dittoHeaders the headers of the preceding command. * @return the response. * @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 RetrieveThingResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<RetrieveThingResponse>(TYPE, jsonObject) .deserialize(statusCode -> { final String thingId = jsonObject.getValueOrThrow(ThingQueryCommandResponse.JsonFields.JSON_THING_ID); final JsonObject extractedThing = jsonObject.getValue(JSON_THING).orElse(null); final String extractedThingPlainJson = jsonObject.getValue(JSON_THING_PLAIN_JSON) .orElseGet(() -> { if (null == extractedThing) { throw JsonMissingFieldException.newBuilder() .fieldName(JSON_THING.getPointer()) .build(); } return extractedThing.toString(); }); return new RetrieveThingResponse(thingId, statusCode, extractedThing, extractedThingPlainJson, dittoHeaders); }); }
/** * Creates a response to a {@link RetrieveThing} command from a JSON object. * * @param jsonObject the JSON object of which the response is to be created. * @param dittoHeaders the headers of the preceding command. * @return the response. * @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 RetrieveThingResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<RetrieveThingResponse>(TYPE, jsonObject) .deserialize(statusCode -> { final String thingId = jsonObject.getValueOrThrow(ThingQueryCommandResponse.JsonFields.JSON_THING_ID); final JsonObject extractedThing = jsonObject.getValue(JSON_THING).orElse(null); final String extractedThingPlainJson = jsonObject.getValue(JSON_THING_PLAIN_JSON) .orElseGet(() -> { if (null == extractedThing) { throw JsonMissingFieldException.newBuilder() .fieldName(JSON_THING.getPointer()) .build(); } return extractedThing.toString(); }); return new RetrieveThingResponse(thingId, statusCode, extractedThing, extractedThingPlainJson, dittoHeaders); }); }
/** * Creates a new {@code Subject} object from the specified JSON object. * * @param subjectIssuerWithId the Subject issuer + Subject ID (separated with a "{@value * SubjectId#ISSUER_DELIMITER}" of the Subject to be created. * @param jsonObject a JSON object which provides the data for the Subject to be created. * @return a new Subject which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws DittoJsonException if {@code jsonObject} * <ul> * <li>is empty,</li> * <li>contains only a field with the schema version</li> * <li>or it contains more than two fields.</li> * </ul> */ public static Subject fromJson(final CharSequence subjectIssuerWithId, final JsonObject jsonObject) { checkNotNull(subjectIssuerWithId, "Subject ID"); checkNotNull(jsonObject, "JSON object"); final String subjectTypeValue = jsonObject.getValue(JsonFields.TYPE) .orElseThrow(() -> new DittoJsonException(JsonMissingFieldException.newBuilder() .message("The 'type' for the 'subject' is missing.") .build())); return of(SubjectId.newInstance(subjectIssuerWithId), ImmutableSubjectType.of(subjectTypeValue)); }
/** * Deserializes the {@link Message} from the JSON representation - the {@code rawPayload} is decoded with Base64. * * @param <T> the type of the message's payload. * @param jsonObject the JsonObjectReader to use for reading the message * @return the Message */ protected static <T> Message<T> deserializeMessageFromJson(final JsonObject jsonObject) { final JsonObject messageObject = jsonObject.getValueOrThrow(MessageCommandResponse.JsonFields.JSON_MESSAGE); final JsonObject messageHeadersObject = messageObject.getValue(MessageCommandResponse.JsonFields.JSON_MESSAGE_HEADERS) .filter(JsonValue::isObject) .map(JsonValue::asObject) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName( MessageCommandResponse.JsonFields.JSON_MESSAGE_HEADERS.getPointer().toString()) .build()); final Optional<JsonValue> messagePayloadOptional = messageObject.getValue(MessageCommand.JsonFields.JSON_MESSAGE_PAYLOAD); final MessageHeaders messageHeaders = MessageHeaders.of(messageHeadersObject); final MessageBuilder<T> messageBuilder = Message.<T>newBuilder(messageHeaders); MessagePayloadSerializer.deserialize(messagePayloadOptional, messageBuilder, messageHeaders); return messageBuilder.build(); }
/** * Creates a new {@code Resource} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the Resource to be created. * @return a new Resource which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws DittoJsonException if {@code jsonObject} * <ul> * <li>is empty,</li> * <li>contains only a field with the schema version</li> * <li>or it contains more than two fields.</li> * </ul> */ public static Resource fromJson(final JsonObject jsonObject) { checkNotNull(jsonObject, "JSON object"); return jsonObject.stream() .filter(field -> !Objects.equals(field.getKey(), JsonSchemaVersion.getJsonKey())) .findFirst() .map(field -> of(ResourceKey.newInstance(field.getKeyName()), field.getValue())) .orElseThrow(() -> new DittoJsonException(JsonMissingFieldException.newBuilder() .message("The JSON object for the 'resource' is missing.") .build())); }
/** * Deserializes the {@link Message} from the JSON representation - the {@code rawPayload} is decoded with Base64. * * @param <T> the type of the message's payload. * @param jsonObject the JsonObjectReader to use for reading the message * @return the Message */ protected static <T> Message<T> deserializeMessageFromJson(final JsonObject jsonObject) { final JsonObject messageObject = jsonObject.getValueOrThrow(MessageCommandResponse.JsonFields.JSON_MESSAGE); final JsonObject messageHeadersObject = messageObject.getValue(MessageCommandResponse.JsonFields.JSON_MESSAGE_HEADERS) .filter(JsonValue::isObject) .map(JsonValue::asObject) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName( MessageCommandResponse.JsonFields.JSON_MESSAGE_HEADERS.getPointer().toString()) .build()); final Optional<JsonValue> messagePayloadOptional = messageObject.getValue(MessageCommand.JsonFields.JSON_MESSAGE_PAYLOAD); final MessageHeaders messageHeaders = MessageHeaders.of(messageHeadersObject); final MessageBuilder<T> messageBuilder = Message.<T>newBuilder(messageHeaders); MessagePayloadSerializer.deserialize(messagePayloadOptional, messageBuilder, messageHeaders); return messageBuilder.build(); }
/** * Creates a new {@code AclEntry} object from the specified JSON object. If, for any reason, the specified JSON * object contains more than one field with Authorization Subject/permissions pairs only the first field is used * while all remaining fields are ignored. * * @param jsonObject a JSON object which provides the data for the ACL entry to be created. * @return a new ACL entry which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws DittoJsonException if {@code jsonObject} <ul> <li>is empty,</li> <li>contains only a field with the schema * version</li> <li>or it contains more than two fields.</li> </ul> */ public static AclEntry fromJson(final JsonObject jsonObject) { checkNotNull(jsonObject, "JSON object"); return jsonObject.stream() .filter(field -> !Objects.equals(field.getKey(), JsonSchemaVersion.getJsonKey())) .findFirst() .map(field -> ImmutableAclEntry.of(field.getKey(), field.getValue())) .orElseThrow(() -> new DittoJsonException(JsonMissingFieldException.newBuilder() .message("The JSON object for 'aclEntry' is missing.") .build())); }
/** * Creates a new {@code AclEntry} object from the specified JSON object. If, for any reason, the specified JSON * object contains more than one field with Authorization Subject/permissions pairs only the first field is used * while all remaining fields are ignored. * * @param jsonObject a JSON object which provides the data for the ACL entry to be created. * @return a new ACL entry which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws DittoJsonException if {@code jsonObject} <ul> <li>is empty,</li> <li>contains only a field with the schema * version</li> <li>or it contains more than two fields.</li> </ul> */ public static AclEntry fromJson(final JsonObject jsonObject) { checkNotNull(jsonObject, "JSON object"); return jsonObject.stream() .filter(field -> !Objects.equals(field.getKey(), JsonSchemaVersion.getJsonKey())) .findFirst() .map(field -> ImmutableAclEntry.of(field.getKey(), field.getValue())) .orElseThrow(() -> new DittoJsonException(JsonMissingFieldException.newBuilder() .message("The JSON object for 'aclEntry' is missing.") .build())); }
private Builder() { super(ERROR_CODE); description(DEFAULT_DESCRIPTION); }
private Builder() { super(ERROR_CODE); description(DEFAULT_DESCRIPTION); }