Example usage for io.vertx.core.json JsonObject getValue

List of usage examples for io.vertx.core.json JsonObject getValue

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getValue.

Prototype

public Object getValue(String key) 

Source Link

Document

Get the value with the specified key, as an Object with types respecting the limitations of JSON.

Usage

From source file:org.eclipse.hono.service.credentials.BaseCredentialsService.java

License:Open Source License

private boolean containsValidTimestampIfPresentForField(final JsonObject payload, final String field) {

    final Object value = payload.getValue(field);
    if (value == null) {
        return true;
    } else if (String.class.isInstance(value)) {
        return isValidTimestamp((String) value);
    } else {/*w ww  . ja  va  2s . co m*/
        return false;
    }
}

From source file:org.eclipse.hono.service.credentials.BaseCredentialsService.java

License:Open Source License

/**
 * Gets the payload from the request and ensures that the enabled flag is contained.
 *
 * @param request The request from which the payload is tried to be extracted. Must not be null.
 * @return The payload as JsonObject (if found). Null otherwise.
 *///from   w w w.  j a  v a2  s  .  c o m
private JsonObject getRequestPayload(final JsonObject request) {

    if (request == null) {
        return null;
    } else {
        JsonObject payload = null;
        Object payloadObject = request.getValue(CredentialsConstants.FIELD_PAYLOAD);
        if (JsonObject.class.isInstance(payloadObject)) {
            payload = (JsonObject) payloadObject;
            if (!payload.containsKey(FIELD_ENABLED)) {
                log.debug("adding 'enabled' key to payload");
                payload.put(FIELD_ENABLED, Boolean.TRUE);
            }
        }
        return payload;
    }
}

From source file:org.eclipse.hono.service.EventBusService.java

License:Open Source License

/**
 * Gets a property value of a given type from a JSON object.
 * /* w w w .j  a va 2s  .  co  m*/
 * @param payload The object to get the property from.
 * @param field The name of the property.
 * @param <T> The type of the field.
 * @return The property value or {@code null} if no such property exists or is not of the expected type.
 * @throws NullPointerException if any of the parameters is {@code null}.
 */
@SuppressWarnings({ "unchecked" })
protected final <T> T getTypesafeValueForField(final JsonObject payload, final String field) {

    Objects.requireNonNull(payload);
    Objects.requireNonNull(field);

    try {
        return (T) payload.getValue(field);
    } catch (ClassCastException e) {
        return null;
    }
}

From source file:org.eclipse.hono.service.tenant.BaseTenantService.java

License:Open Source License

/**
 * Check the request payload for validity.
 *
 * @param payload The payload to check.// www  .  j ava  2 s . c  o m
 * @return boolean The result of the check : {@link Boolean#TRUE} if the payload is valid, {@link Boolean#FALSE} otherwise.
 * @throws NullPointerException If the payload is {@code null}.
 */
private boolean isValidRequestPayload(final JsonObject payload) {

    final Object adaptersObj = payload.getValue(TenantConstants.FIELD_ADAPTERS);
    if (adaptersObj == null) {
        // all adapters enabled with default config
        return true;
    } else if (adaptersObj instanceof JsonArray) {

        final JsonArray adapters = (JsonArray) adaptersObj;
        if (adapters.size() == 0) {
            // if given, adapters config array must not be empty
            return false;
        } else {
            return !adapters.stream().anyMatch(obj -> {
                return !(obj instanceof JsonObject)
                        || !((JsonObject) obj).containsKey(TenantConstants.FIELD_ADAPTERS_TYPE);
            });
        }
    } else {
        // malformed payload
        return false;
    }
}

From source file:org.eclipse.hono.service.tenant.TenantHttpEndpoint.java

License:Open Source License

/**
 * Check if the payload (that was put to the RoutingContext ctx with the
 * key {@link #KEY_REQUEST_BODY}) contains a value for the key {@link TenantConstants#FIELD_PAYLOAD_TENANT_ID} that is not null.
 *
 * @param ctx The routing context to retrieve the JSON request body from.
 *//* ww  w .j ava 2s  . c  o m*/
protected void checkPayloadForTenantId(final RoutingContext ctx) {

    final JsonObject payload = ctx.get(KEY_REQUEST_BODY);

    final Object tenantId = payload.getValue(TenantConstants.FIELD_PAYLOAD_TENANT_ID);

    if (tenantId == null) {
        ctx.response().setStatusMessage(
                String.format("'%s' param is required", TenantConstants.FIELD_PAYLOAD_TENANT_ID));
        ctx.fail(HttpURLConnection.HTTP_BAD_REQUEST);
    } else if (!(tenantId instanceof String)) {
        ctx.response().setStatusMessage(
                String.format("'%s' must be a string", TenantConstants.FIELD_PAYLOAD_TENANT_ID));
        ctx.fail(HttpURLConnection.HTTP_BAD_REQUEST);
    }
    ctx.next();
}

From source file:org.eclipse.hono.tests.IntegrationTestSupport.java

License:Open Source License

/**
 * A simple implementation of subtree containment: all entries of the JsonObject that is tested to be contained
 * must be contained in the other JsonObject as well. Nested JsonObjects are treated the same by recursively calling
 * this method to test the containment.//from w  w w  .j  av  a2 s.c  o  m
 * JsonArrays are tested for containment as well: all elements in a JsonArray belonging to the contained JsonObject
 * must be present in the corresponding JsonArray of the other JsonObject as well. The sequence of the array elements
 * is not important (suitable for the current tests).
 * @param jsonObject The JsonObject that must fully contain the other JsonObject (but may contain more entries as well).
 * @param jsonObjectToBeContained The JsonObject that needs to be fully contained inside the other JsonObject.
 * @return The result of the containment test.
 */
public static boolean testJsonObjectToBeContained(final JsonObject jsonObject,
        final JsonObject jsonObjectToBeContained) {
    if (jsonObjectToBeContained == null) {
        return true;
    }
    if (jsonObject == null) {
        return false;
    }
    final AtomicBoolean containResult = new AtomicBoolean(true);

    jsonObjectToBeContained.forEach(entry -> {
        if (!jsonObject.containsKey(entry.getKey())) {
            containResult.set(false);
        } else {
            if (entry.getValue() == null) {
                if (jsonObject.getValue(entry.getKey()) != null) {
                    containResult.set(false);
                }
            } else if (entry.getValue() instanceof JsonObject) {
                if (!(jsonObject.getValue(entry.getKey()) instanceof JsonObject)) {
                    containResult.set(false);
                } else {
                    if (!testJsonObjectToBeContained((JsonObject) entry.getValue(),
                            (JsonObject) jsonObject.getValue(entry.getKey()))) {
                        containResult.set(false);
                    }
                }
            } else if (entry.getValue() instanceof JsonArray) {
                if (!(jsonObject.getValue(entry.getKey()) instanceof JsonArray)) {
                    containResult.set(false);
                } else {
                    // compare two JsonArrays
                    final JsonArray biggerArray = (JsonArray) jsonObject.getValue(entry.getKey());
                    final JsonArray smallerArray = (JsonArray) entry.getValue();

                    if (!testJsonArrayToBeContained(biggerArray, smallerArray)) {
                        containResult.set(false);
                    }
                }
            } else {
                if (!entry.getValue().equals(jsonObject.getValue(entry.getKey()))) {
                    containResult.set(false);
                }
            }
        }
    });
    return containResult.get();
}

From source file:org.eclipse.hono.util.CredentialsObject.java

License:Open Source License

private boolean containsValidTimestampIfPresentForField(final JsonObject secret, final String field) {

    final Object value = secret.getValue(field);
    if (value == null) {
        return true;
    } else if (String.class.isInstance(value)) {
        return getInstant((String) value) != null;
    } else {/*from  ww w .j  a va  2  s.  co m*/
        return false;
    }
}

From source file:org.eclipse.hono.util.CredentialsObject.java

License:Open Source License

private static Instant getInstant(final JsonObject secret, final String field) {

    final Object value = secret.getValue(field);
    if (String.class.isInstance(value)) {
        return getInstant((String) value);
    } else {/*ww w . j  a  v a2 s. c o  m*/
        return null;
    }
}

From source file:org.eclipse.hono.util.TenantObject.java

License:Open Source License

@SuppressWarnings("unchecked")
private <T> T getProperty(final JsonObject parent, final String name) {
    final Object value = parent.getValue(Objects.requireNonNull(name));
    try {// www .  j ava2s .c o  m
        return (T) value;
    } catch (ClassCastException e) {
        return null;
    }
}

From source file:org.eclipse.hono.util.TenantObject.java

License:Open Source License

/**
 * Gets the trusted certificate authority configured for this tenant.
 * <p>// www.j  a  v a  2  s  . co  m
 * This method tries to extract the certificate from the data contained in
 * the JSON object of the <em>trusted-ca</em> property.
 * The value of the JSON object's <em>cert</em> property is expected to contain
 * the Base64 encoded <em>binary</em> DER-encoding of the certificate, i.e. the same
 * value as the <em>printable</em> form but without the leading
 * {@code -----BEGIN CERTIFICATE-----} and trailing {@code -----END CERTIFICATE-----}.
 * 
 * @return The certificate or {@code null} if no certificate authority
 *         has been set or the certificate is not DER encoded.
 */
@JsonIgnore
public X509Certificate getTrustedCertificateAuthority() {

    final JsonObject trustedCa = getProperty(TenantConstants.FIELD_PAYLOAD_TRUSTED_CA);
    if (trustedCa == null) {
        return null;
    } else {
        return Optional.ofNullable(trustedCa.getValue(TenantConstants.FIELD_PAYLOAD_CERT)).map(obj -> {
            if (obj instanceof String) {
                try {
                    final CertificateFactory factory = CertificateFactory.getInstance("X.509");
                    final byte[] derEncodedCert = Base64.getDecoder().decode(((String) obj));
                    return (X509Certificate) factory
                            .generateCertificate(new ByteArrayInputStream(derEncodedCert));
                } catch (CertificateException e) {
                    return null;
                }
            } else {
                return null;
            }
        }).orElse(null);
    }
}