/** * @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); }
/** * Executes the given Supplier. An occurring {@link JsonRuntimeException}, {@code IllegalArgumentException}, * {@code UnsupportedOperationException}, or {@code NullPointerException} is caught, wrapped and re-thrown as * {@code DittoJsonException}. * * @param supplier the supplier which potentially throws a {@code JsonRuntimeException}. * @param <T> the type of results the supplier returns. * @return the results of the supplier. * @throws DittoJsonException if a {@code JsonRuntimeException} occurred. */ public static <T> T wrapJsonRuntimeException(final Supplier<T> supplier) { try { return supplier.get(); } catch (final JsonRuntimeException | IllegalArgumentException | NullPointerException | UnsupportedOperationException e) { throw new DittoJsonException(e); } // "ditto-json" library also throws IllegalArgumentException when for example strings which may not be empty // (e.g. keys) are empty // "ditto-json" library also throws NullPointerException when for example non-nullable objects are null }
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 Resource} object based on the given {@code resourceKey} and {@code jsonValue}. * * @param resourceKey the JSON key which is assumed to be the path of a Resource prefixed with a type. * @param jsonValue the JSON value containing the effected permissions for the Resource. This value is supposed to * be a {@link JsonObject}. * @return a new {@code Resource} object. * @throws NullPointerException if any argument is {@code null}. * @throws DittoJsonException if {@code jsonValue} is not a JSON object or the JSON has not the expected format. */ public static Resource of(final ResourceKey resourceKey, final JsonValue jsonValue) { checkNotNull(jsonValue, "JSON value"); final EffectedPermissions effectedPermissions = Optional.of(jsonValue) .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(object -> wrapJsonRuntimeException(() -> ImmutableEffectedPermissions.fromJson(object))) .orElseThrow(() -> new DittoJsonException(JsonParseException.newBuilder() .message("The JSON object for the 'permissions' (grant/revoke) of the 'resource' '" + resourceKey + "' is missing or not an object.") .build())); return of(resourceKey, effectedPermissions); }
}) .match(JsonRuntimeException.class, jre -> { final DittoJsonException dittoJsonException = new DittoJsonException(jre); final DittoHeaders dittoHeaders = dittoJsonException.getDittoHeaders(); enhanceLogWithCorrelationId(dittoHeaders.getCorrelationId(), () -> LOGGER.info("DittoJsonException in gateway RootRoute: {}", dittoJsonException.getMessage())); return complete(HttpResponse.create().withStatus(dittoJsonException.getStatusCode().toInt()) .withEntity(ContentTypes.APPLICATION_JSON, ByteString.fromString(dittoJsonException.toJsonString()))); }) .matchAny(throwable -> {
throw new DittoJsonException(cause, initialInternalHeaders); final JsonifiableAdaptable jsonifiableAdaptable = wrapJsonRuntimeException(cmdString,
/** * @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); }
/** * Executes the given Function. An occurring {@link JsonRuntimeException}, * {@code IllegalArgumentException} or {@code NullPointerException} is caught, wrapped and re-thrown as * {@code DittoJsonException}. * * @param function to function which potentially throws a {@code JsonRuntimeException}. * @param input the input to the function. * @param <I> the type of the input to the function. * @param <T> the type of results the function returns. * @return the results of the function. * @throws DittoJsonException if a {@code JsonRuntimeException} occurred. */ public static <I, T> T wrapJsonRuntimeException(final Function<I, T> function, final I input) { try { return function.apply(input); } catch (final JsonRuntimeException | IllegalArgumentException | NullPointerException e) { throw new DittoJsonException(e); } // "ditto-json" library also throws IllegalArgumentException when for example strings which may not be empty // (e.g. keys) are empty // "ditto-json" library also throws NullPointerException when for example non-nullable objects are null }
@Override public T parse(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return DittoJsonException.wrapJsonRuntimeException(jsonObject, dittoHeaders, this::fromJson); }
/** * Executes the given Function. An occurring {@link JsonRuntimeException}, * {@code IllegalArgumentException} or {@code NullPointerException} is caught, wrapped and re-thrown as * {@code DittoJsonException}. * * @param function to function which potentially throws a {@code JsonRuntimeException}. * @param input the input to the function. * @param <I> the type of the input to the function. * @param <T> the type of results the function returns. * @return the results of the function. * @throws DittoJsonException if a {@code JsonRuntimeException} occurred. */ public static <I, T> T wrapJsonRuntimeException(final Function<I, T> function, final I input) { try { return function.apply(input); } catch (final JsonRuntimeException | IllegalArgumentException | NullPointerException e) { throw new DittoJsonException(e); } // "ditto-json" library also throws IllegalArgumentException when for example strings which may not be empty // (e.g. keys) are empty // "ditto-json" library also throws NullPointerException when for example non-nullable objects are null }
@Override public T parse(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return DittoJsonException.wrapJsonRuntimeException(jsonObject, dittoHeaders, this::fromJson); }
/** * Executes the given Function with the given argument and DittoHeaders. An occurring {@link * JsonRuntimeException}, {@code IllegalArgumentException} or {@code NullPointerException} is caught, wrapped and * re-thrown as {@code DittoJsonException} with the given DittoHeaders. * * @param argument the first argument that is passed to the given {@code function}. * @param dittoHeaders the command headers that are applied as second argument to the given {@code function} and * that are passed to the {@code DittoJsonException} in case any error is thrown by the {@code function}. * @param function the supplier which potentially throws a {@code JsonRuntimeException}. * @param <T> the type of the first argument for the given {@code function}. * @param <R> the type of results the {@code function} returns. * @return the results of the function. * @throws DittoJsonException if a {@code JsonRuntimeException} occurred. */ public static <T, R> R wrapJsonRuntimeException(final T argument, final DittoHeaders dittoHeaders, final BiFunction<T, DittoHeaders, R> function) { try { return function.apply(argument, dittoHeaders); } catch (final JsonRuntimeException | IllegalArgumentException | NullPointerException e) { throw new DittoJsonException(e, dittoHeaders); } // "ditto-json" library also throws IllegalArgumentException when for example strings which may not be empty // (e.g. keys) are empty // "ditto-json" library also throws NullPointerException when for example non-nullable objects are null }
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); }
/** * Executes the given Supplier. An occurring {@link JsonRuntimeException}, {@code IllegalArgumentException}, * {@code UnsupportedOperationException}, or {@code NullPointerException} is caught, wrapped and re-thrown as * {@code DittoJsonException}. * * @param supplier the supplier which potentially throws a {@code JsonRuntimeException}. * @param <T> the type of results the supplier returns. * @return the results of the supplier. * @throws DittoJsonException if a {@code JsonRuntimeException} occurred. */ public static <T> T wrapJsonRuntimeException(final Supplier<T> supplier) { try { return supplier.get(); } catch (final JsonRuntimeException | IllegalArgumentException | NullPointerException | UnsupportedOperationException e) { throw new DittoJsonException(e); } // "ditto-json" library also throws IllegalArgumentException when for example strings which may not be empty // (e.g. keys) are empty // "ditto-json" library also throws NullPointerException when for example non-nullable objects are null }
/** * 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)); }
/** * Executes the given Function with the given argument and DittoHeaders. An occurring {@link * JsonRuntimeException}, {@code IllegalArgumentException} or {@code NullPointerException} is caught, wrapped and * re-thrown as {@code DittoJsonException} with the given DittoHeaders. * * @param argument the first argument that is passed to the given {@code function}. * @param dittoHeaders the command headers that are applied as second argument to the given {@code function} and * that are passed to the {@code DittoJsonException} in case any error is thrown by the {@code function}. * @param function the supplier which potentially throws a {@code JsonRuntimeException}. * @param <T> the type of the first argument for the given {@code function}. * @param <R> the type of results the {@code function} returns. * @return the results of the function. * @throws DittoJsonException if a {@code JsonRuntimeException} occurred. */ public static <T, R> R wrapJsonRuntimeException(final T argument, final DittoHeaders dittoHeaders, final BiFunction<T, DittoHeaders, R> function) { try { return function.apply(argument, dittoHeaders); } catch (final JsonRuntimeException | IllegalArgumentException | NullPointerException e) { throw new DittoJsonException(e, dittoHeaders); } // "ditto-json" library also throws IllegalArgumentException when for example strings which may not be empty // (e.g. keys) are empty // "ditto-json" library also throws NullPointerException when for example non-nullable objects are null }
/** * 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)); }
@Override public ThingSearchCommand parse(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { try { return super.parse(jsonObject, dittoHeaders); } catch (final JsonRuntimeException | IllegalArgumentException | NullPointerException | ClassCastException e) { throw new DittoJsonException(e, dittoHeaders); } } }
/** * 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)); }
@Override public ThingSearchCommand parse(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { try { return super.parse(jsonObject, dittoHeaders); } catch (final JsonRuntimeException | IllegalArgumentException | NullPointerException | ClassCastException e) { throw new DittoJsonException(e, dittoHeaders); } } }