/** * Constructs a new {@code JsonMissingFieldException} object for the JsonPointer of the specified * JsonFieldDefinition. * * @param fieldDefinition provides the JsonPointer which refers to the missing field. * @throws NullPointerException if {@code jsonFieldDefinition} is {@code null}. */ public JsonMissingFieldException(final JsonFieldDefinition<?> fieldDefinition) { this(requireNonNull(fieldDefinition, "The JSON Field Definition must not be null!").getPointer()); }
private void addFieldDefinition(@Nullable final JsonFieldDefinition fieldDefinition) { if (null != fieldDefinition) { addPointer(fieldDefinition.getPointer()); } }
@Override public ImmutableJsonObjectBuilder remove(final JsonFieldDefinition fieldDefinition) { requireNonNull(fieldDefinition, "The field definition must not be null!"); return remove(fieldDefinition.getPointer()); }
@Override public JsonFieldSelectorBuilder removeFieldDefinition(@Nullable final JsonFieldDefinition fieldDefinition) { if (null == fieldDefinition) { return this; } return removePointer(fieldDefinition.getPointer()); }
@Override public ImmutableJsonObjectBuilder remove(final JsonFieldDefinition fieldDefinition) { requireNonNull(fieldDefinition, "The field definition must not be null!"); return remove(fieldDefinition.getPointer()); }
@SuppressWarnings("squid:CallToDeprecatedMethod") private static JsonObject migrateId(final JsonObject jsonObject) { return jsonObject.getValue(Event.JsonFields.ID) .map(name -> name.replaceFirst("thing", "")) .map(Introspector::decapitalize) .map(name -> ThingEvent.TYPE_PREFIX + name) .map(JsonValue::of) .map(type -> jsonObject.setValue(Event.JsonFields.TYPE.getPointer(), type)) .orElse(jsonObject); }
private Certificate getClientCertificate(final String certificatePem) { final byte[] asciiBytes = certificatePem.getBytes(StandardCharsets.US_ASCII); try { return X509_CERTIFICATE_FACTORY.generateCertificate(new ByteArrayInputStream(asciiBytes)); } catch (final CertificateException e) { final JsonPointer errorLocation = Connection.JsonFields.CREDENTIALS.getPointer() .append(ClientCertificateCredentials.JsonFields.CLIENT_CERTIFICATE.getPointer()); throw badFormat(errorLocation, "CERTIFICATE", "DER") .build(); } }
@Override public <T> Optional<T> getValue(final JsonFieldDefinition<T> fieldDefinition) { checkFieldDefinition(fieldDefinition); return getValueForPointer(fieldDefinition.getPointer()).map(fieldDefinition::mapValue); }
@Override public <T> Optional<T> getValue(final JsonFieldDefinition<T> fieldDefinition) { checkFieldDefinition(fieldDefinition); return getValueForPointer(fieldDefinition.getPointer()).map(fieldDefinition::mapValue); }
@Override public <T> JsonObject set(final JsonFieldDefinition<T> fieldDefinition, @Nullable final T value) { requireNonNull(fieldDefinition, "The JSON field definition to set the value for must not be null!"); final JsonPointer pointer = fieldDefinition.getPointer(); final JsonKey leafKey = pointer.getLeaf().orElseThrow(() -> { final String msgTemplate = "The pointer of the field definition <{0}> must not be empty!"; return new IllegalArgumentException(MessageFormat.format(msgTemplate, fieldDefinition)); }); final JsonField field = JsonFactory.newField(leafKey, JsonFactory.getAppropriateValue(value), fieldDefinition); return setFieldInHierarchy(this, pointer, field); }
@Override public JsonObject get(final JsonFieldDefinition fieldDefinition) { checkFieldDefinition(fieldDefinition); return get(fieldDefinition.getPointer()); }
@Override public JsonPointer getResourcePath() { final String path = Thing.JsonFields.POLICY_ID.getPointer().toString(); return JsonPointer.of(path); }
@Override public <T> JsonObjectBuilder set(final JsonFieldDefinition<T> fieldDefinition, @Nullable final T value, final Predicate<JsonField> predicate) { requireNonNull(fieldDefinition, "The definition of the JSON field to set the value for must not be null!"); checkPredicate(predicate); final JsonPointer pointer = fieldDefinition.getPointer(); pointer.getLeaf() .map(leafKey -> JsonFactory.newField(leafKey, JsonFactory.getAppropriateValue(value), fieldDefinition)) .filter(predicate) .ifPresent(jsonField -> setFieldInHierarchy(this, pointer, jsonField)); return this; }
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(); } }
/** * Verifies that the JSON object contains a field which is denoted by the specified JSON field definition and which has * the specified value. * * @param jsonFieldDefinition points to a field which is expected to be a part of the checked JSON object. * @param expectedValue the expected value of the field which is denoted by {@code jsonPointer}. * @return this assertion object. */ public JsonObjectAssert contains(final JsonFieldDefinition jsonFieldDefinition, final String expectedValue) { return contains(jsonFieldDefinition.getPointer(), expectedValue); }
@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); }
private Event createEventFrom(final JsonValue json) { final JsonObject jsonObject = json.asObject() .setValue(Event.JsonFields.REVISION.getPointer(), Event.DEFAULT_REVISION); return eventRegistry.parse(migrateComplex(migratePayload(jsonObject)), DittoHeaders.empty()); }
private PolicyEvent createEventFrom(final JsonValue json) { final JsonObject jsonObject = json.asObject() .setValue(Event.JsonFields.REVISION.getPointer(), Event.DEFAULT_REVISION); return eventRegistry.parse(migrateComplex(migratePayload(jsonObject)), DittoHeaders.empty()); }
private PolicyEvent createEventFrom(final JsonValue json) { final JsonObject jsonObject = json.asObject() .setValue(Event.JsonFields.REVISION.getPointer(), Event.DEFAULT_REVISION); return eventRegistry.parse(migrateComplex(migratePayload(jsonObject)), DittoHeaders.empty()); }