/** * Sets message, description and link from a JSON object of it has matching fields with valid values. * * @param jsonObject The JSON object to read from. * @throws NullPointerException if {@code jsonObject} is {@code null}. */ public DittoRuntimeExceptionBuilder<T> loadJson(final JsonObject jsonObject) { jsonObject.getValue(MESSAGE).ifPresent(this::message); jsonObject.getValue(DESCRIPTION).ifPresent(this::description); jsonObject.getValue(HREF).map(URI::create).ifPresent(this::href); return this; }
/** * Sets message, description and link from a JSON object of it has matching fields with valid values. * * @param jsonObject The JSON object to read from. * @throws NullPointerException if {@code jsonObject} is {@code null}. */ public DittoRuntimeExceptionBuilder<T> loadJson(final JsonObject jsonObject) { jsonObject.getValue(MESSAGE).ifPresent(this::message); jsonObject.getValue(DESCRIPTION).ifPresent(this::description); jsonObject.getValue(HREF).map(URI::create).ifPresent(this::href); return this; }
@SuppressWarnings("squid:CallToDeprecatedMethod") private JsonObject migrateComplex(final JsonObject jsonObject) { return jsonObject.getValue(Event.JsonFields.ID) .map(migrationMappings::get) .map(migration -> migration.apply(jsonObject)) .orElse(jsonObject); }
@SuppressWarnings("squid:CallToDeprecatedMethod") private JsonObject migrateComplex(final JsonObject jsonObject) { return jsonObject.getValue(Event.JsonFields.ID) .map(migrationMappings::get) .map(migration -> migration.apply(jsonObject)) .orElse(jsonObject); }
static ClientCertificateCredentials fromJson(final JsonObject jsonObject) { final Builder builder = newBuilder(); jsonObject.getValue(JsonFields.CLIENT_CERTIFICATE).ifPresent(builder::clientCertificate); jsonObject.getValue(JsonFields.CLIENT_KEY).ifPresent(builder::clientKey); return builder.build(); }
private static Set<String> getTags(final JsonObject jsonObject) { return jsonObject.getValue(JsonFields.TAGS) .map(array -> array.stream() .filter(JsonValue::isString) .map(JsonValue::asString) .collect(Collectors.toSet())) .orElse(Collections.emptySet()); }
private static JsonObject renameField(final CharSequence oldFieldName, final CharSequence newFieldName, final JsonObject jsonObject) { return jsonObject.getValue(oldFieldName) .map(value -> jsonObject.setValue(newFieldName, value)) .orElse(jsonObject); }
private ImmutableJsonWebToken(final String authorizationString) { super(authorizationString); authorizationSubjects = getBody().getValue(JsonFields.USER_ID) .map(Collections::singletonList) .orElse(Collections.emptyList()); }
private static Set<Target> getTargets(final JsonObject jsonObject, final ConnectionType type) { return jsonObject.getValue(JsonFields.TARGETS) .map(array -> array.stream() .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(valueAsObject -> ConnectivityModelFactory.targetFromJson(valueAsObject, type)) .collect(Collectors.toSet())) .orElse(Collections.emptySet()); }
private static Set<Target> getTargets(final JsonObject jsonObject, final ConnectionType type) { return jsonObject.getValue(JsonFields.TARGETS) .map(array -> array.stream() .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(valueAsObject -> ConnectivityModelFactory.targetFromJson(valueAsObject, type)) .collect(Collectors.toSet())) .orElse(Collections.emptySet()); }
@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 static boolean needsSourceFilterMigration(final JsonObject connectionJsonObject) { final Optional<JsonArray> sources = connectionJsonObject.getValue(Connection.JsonFields.SOURCES); return sources.isPresent() && sources.get().stream().filter(JsonValue::isObject) .map(JsonValue::asObject).anyMatch(source -> source.contains("filters")); }
private static String policyIdOrPlaceholderForCreateThingFrom(final Adaptable adaptable) { return adaptable.getPayload().getValue() .map(JsonValue::asObject) .flatMap(o -> o.getValue(CreateThing.JSON_COPY_POLICY_FROM)) .orElse(null); }
private static String getCopyPolicyFrom(final String jsonString) { final JsonObject inputJson = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return inputJson.getValue(ModifyThing.JSON_COPY_POLICY_FROM) .orElse(null); }
private static void handleObject(final String path, final JsonValue value, final List<Document> flatAttributes) { final JsonObject jsonObject = value.asObject(); jsonObject.getKeys().forEach(key -> { final String newPath = path + PersistenceConstants.SLASH + key; final JsonValue innerValue = jsonObject.getValue(key).orElse(null); toFlatAttributesList(newPath, innerValue, flatAttributes); }); }
/** * A "payload" object was wrapping the events payload until the introduction of "cr-commands 1.0.0". This field has * to be used as fallback for already persisted events with "things-model" < 3.0.0. Removing this workaround is * possible if we are sure that no "old" events are ever loaded again! */ private static JsonObject migratePayload(final JsonObject jsonObject) { return jsonObject.getValue(PAYLOAD) .map(obj -> jsonObject.remove(PAYLOAD.getPointer()).setAll(obj)) .orElse(jsonObject); }
@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); }
@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 static String getMessage(final JsonObject jsonObject) { return jsonObject.getValue(DittoJsonException.JsonFields.MESSAGE) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(DittoRuntimeException.JsonFields.MESSAGE.getPointer().toString()).build()); }
private CommandResponse unfixCorrelationId(final CommandResponse response) { final String correlationId = response.getDittoHeaders() .getCorrelationId() .flatMap(s -> decodeCommandCorrelationId(s).getValue(ORIGINAL_CORRELATION_ID)) .map(JsonValue::asString) .orElse(null); final DittoHeaders dittoHeaders = response.getDittoHeaders().toBuilder() .correlationId(correlationId) .build(); return response.setDittoHeaders(dittoHeaders); }