/** * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty. * @throws JsonTypeNotParsableException if the {@code jsonObject}'s {@code type} was unknown to the parser. * @throws DittoJsonException if {@code jsonString} does not contain a valid JSON object. */ @Override public T parse(final String jsonString, final DittoHeaders dittoHeaders) { return DittoJsonException.wrapJsonRuntimeException(jsonString, dittoHeaders, this::fromJson); }
/** * Returns a new immutable {@link AuthorizationContext} based on the given JSON object. * * @param jsonObject the JSON object representation of an authorization context. * @return the new {@code AuthorizationContext}. * @throws DittoJsonException if {@code jsonObject} is {@code null} or cannot be parsed. */ public static AuthorizationContext newAuthContext(final JsonObject jsonObject) { return DittoJsonException.wrapJsonRuntimeException(() -> ImmutableAuthorizationContext.fromJson(jsonObject)); }
/** * Returns a new immutable {@link SearchResult} which is initialised with the values of the given JSON object. * * @param jsonObject provides the initial values of the result. * @return the new immutable initialised {@code SearchResult}. * @throws NullPointerException if {@code jsonObject} is {@code null}. */ public static SearchResult newSearchResult(final JsonObject jsonObject) { return wrapJsonRuntimeException(() -> ImmutableSearchResult.fromJson(jsonObject)); }
/** * Returns a new immutable {@link SearchResult} which is initialised with the values of the given JSON object. * * @param jsonObject provides the initial values of the result. * @return the new immutable initialised {@code SearchResult}. * @throws NullPointerException if {@code jsonObject} is {@code null}. */ public static SearchResult newSearchResult(final JsonObject jsonObject) { return wrapJsonRuntimeException(() -> ImmutableSearchResult.fromJson(jsonObject)); }
/** * Returns a new immutable {@link AuthorizationContext} based on the given JSON object. * * @param jsonObject the JSON object representation of an authorization context. * @return the new {@code AuthorizationContext}. * @throws DittoJsonException if {@code jsonObject} is {@code null} or cannot be parsed. */ public static AuthorizationContext newAuthContext(final JsonObject jsonObject) { return DittoJsonException.wrapJsonRuntimeException(() -> ImmutableAuthorizationContext.fromJson(jsonObject)); }
private static JsonObject createInlinePolicyJson(final String jsonString) { final JsonObject inputJson = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return inputJson.getValue(Policy.INLINED_FIELD_NAME) .map(jsonValue -> wrapJsonRuntimeException(jsonValue::asObject)) .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); }
@Override public Optional<Adaptable> apply(final ExternalMessage message) { return Optional.ofNullable( message.getTextPayload() .orElseGet(() -> message.getBytePayload() .map(b -> StandardCharsets.UTF_8.decode(b).toString()) .orElse(null)) ).map(plainString -> DittoJsonException.wrapJsonRuntimeException(() -> { final JsonObject jsonObject = JsonFactory.readFrom(plainString).asObject(); return ProtocolFactory.jsonifiableAdaptableFromJson(jsonObject); })); } }
/** * Returns a new immutable {@link Features} based on the given JSON string. * * @param jsonString provides the initial values of the result. * @return the new immutable initialised {@code Features}. * @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Features}. */ public static Features newFeatures(final String jsonString) { final JsonObject featuresJsonObject = DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return newFeatures(featuresJsonObject); }
public static PolicyErrorResponse fromJson(final String jsonString, final DittoHeaders dittoHeaders) { final JsonObject jsonObject = DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return fromJson(jsonObject, dittoHeaders); }
/** * Returns a new immutable Iterable of Policy entries based on the given JSON string. * * @param jsonString the JSON object representation as String of Policy entries. * @return the new initialised {@code Policy}. * @throws org.eclipse.ditto.model.base.exceptions.DittoJsonException if {@code jsonObject} cannot be parsed to * {@link Iterable} of {@link PolicyEntry}s. */ public static Iterable<PolicyEntry> newPolicyEntries(final String jsonString) { final JsonObject jsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return newPolicyEntries(jsonObject); }
public static PolicyErrorResponse fromJson(final String jsonString, final DittoHeaders dittoHeaders) { final JsonObject jsonObject = DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return fromJson(jsonObject, dittoHeaders); }
/** * Returns a new immutable {@link Features} based on the given JSON string. * * @param jsonString provides the initial values of the result. * @return the new immutable initialised {@code Features}. * @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Features}. */ public static Features newFeatures(final String jsonString) { final JsonObject featuresJsonObject = DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return newFeatures(featuresJsonObject); }
/** * Returns a new immutable {@link Thing} based on the given JSON string. * * @param jsonString the JSON string representation of a Thing. * @return the new Thing. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws DittoJsonException if {@code jsonString} cannot be parsed to {@code Thing}. */ public static Thing newThing(final String jsonString) { final JsonObject jsonObject = DittoJsonException.wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); return newThingBuilder(jsonObject).build(); }
@Override public Optional<Adaptable> map(final ExternalMessage message) { final String payload = extractPayloadAsString(message); final JsonifiableAdaptable jsonifiableAdaptable = DittoJsonException.wrapJsonRuntimeException(() -> ProtocolFactory.jsonifiableAdaptableFromJson(JsonFactory.newObject(payload)) ); final DittoHeaders mergedHeaders = mergeHeaders(message, jsonifiableAdaptable); return Optional.of( ProtocolFactory.newAdaptableBuilder(jsonifiableAdaptable).withHeaders(mergedHeaders).build()); }
@Override public Optional<Adaptable> map(final ExternalMessage message) { final String payload = extractPayloadAsString(message); final JsonifiableAdaptable jsonifiableAdaptable = DittoJsonException.wrapJsonRuntimeException(() -> ProtocolFactory.jsonifiableAdaptableFromJson(JsonFactory.newObject(payload)) ); final DittoHeaders mergedHeaders = mergeHeaders(message, jsonifiableAdaptable); return Optional.of( ProtocolFactory.newAdaptableBuilder(jsonifiableAdaptable).withHeaders(mergedHeaders).build()); }
private static Subject createSubjectForPut(final String jsonString, final CharSequence subjectId) { final JsonObject jsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); final String subjectTypeString = jsonObject.getValueOrThrow(Subject.JsonFields.TYPE); final SubjectType subjectType = PoliciesModelFactory.newSubjectType(subjectTypeString); return PoliciesModelFactory.newSubject(SubjectId.newInstance(subjectId), subjectType); }
private static JsonObject createPolicyJsonObjectForPut(final String jsonString, final String policyId) { final JsonObject policyJsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); policyJsonObject.getValue(Policy.JsonFields.ID.getPointer()) .ifPresent(policyIdJsonValue -> { if (!policyIdJsonValue.isString() || !policyId.equals(policyIdJsonValue.asString())) { throw PolicyIdNotExplicitlySettableException.newBuilder().build(); } }); return policyJsonObject.setValue(Policy.JsonFields.ID.getPointer(), JsonValue.of(policyId)); }
private static PolicyEntry createPolicyEntryForPut(final String jsonString, final CharSequence labelString) { final JsonObject jsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); final Subjects subjects = PoliciesModelFactory.newSubjects(jsonObject.getValueOrThrow(PolicyEntry.JsonFields.SUBJECTS)); final Resources resources = PoliciesModelFactory.newResources(jsonObject.getValueOrThrow(PolicyEntry.JsonFields.RESOURCES)); return PoliciesModelFactory.newPolicyEntry(Label.of(labelString), subjects, resources); }
private static Thing createThingForPost(final String jsonString) { final JsonObject inputJson = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); if (inputJson.contains(Thing.JsonFields.ID.getPointer())) { throw ThingIdNotExplicitlySettableException.newBuilder(true).build(); } final String thingId = ThingBuilder.generateRandomThingId(); final JsonObjectBuilder outputJsonBuilder = inputJson.toBuilder(); outputJsonBuilder.set(Thing.JsonFields.ID.getPointer(), thingId); return ThingsModelFactory.newThingBuilder(outputJsonBuilder.build()) .setId(thingId) .build(); }