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

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

Introduction

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

Prototype

public JsonObject put(String key, Object value) 

Source Link

Document

Put an Object into the JSON object with the specified key.

Usage

From source file:org.eclipse.hono.service.registration.RegistrationHttpEndpoint.java

License:Open Source License

private static JsonObject getPayloadForParams(final HttpServerRequest request) {
    JsonObject payload = new JsonObject();
    for (Entry<String, String> param : request.params()) {
        if (PARAM_DEVICE_ID.equalsIgnoreCase(param.getKey())) {
            // add device param captured from URI path - use same name as in JSON structures for that
            payload.put(FIELD_DEVICE_ID, param.getValue());
        } else if (!PARAM_TENANT_ID.equalsIgnoreCase(param.getKey())) { // filter out tenant param captured from URI path
            payload.put(param.getKey(), param.getValue());
        }/*from w  w w . j  a v  a2s  .  c o  m*/
    }
    return payload;
}

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

License:Open Source License

/**
 * Add default values for optional fields that are not filled in the payload.
 * <p>//from  w ww.  j  a  v a2s.  c o  m
 * Payload should be checked for validity first, there is no error handling inside this method anymore.
 * </p>
 *
 * @param checkedPayload The checked payload to add optional fields to.
 * @throws ClassCastException If the {@link TenantConstants#FIELD_ADAPTERS_TYPE} element is not a {@link JsonArray}
 *       or the JsonArray contains elements that are not of type {@link JsonObject}.
 */
protected final void addNotPresentFieldsWithDefaultValuesForTenant(final JsonObject checkedPayload) {
    if (!checkedPayload.containsKey(TenantConstants.FIELD_ENABLED)) {
        log.trace("adding 'enabled' key to payload");
        checkedPayload.put(TenantConstants.FIELD_ENABLED, Boolean.TRUE);
    }

    final JsonArray adapters = checkedPayload.getJsonArray(TenantConstants.FIELD_ADAPTERS);
    if (adapters != null) {
        adapters.forEach(elem -> addNotPresentFieldsWithDefaultValuesForAdapter((JsonObject) elem));
    }
}

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

License:Open Source License

private void addNotPresentFieldsWithDefaultValuesForAdapter(final JsonObject adapter) {
    if (!adapter.containsKey(TenantConstants.FIELD_ENABLED)) {
        log.trace("adding 'enabled' key to payload");
        adapter.put(TenantConstants.FIELD_ENABLED, Boolean.TRUE);
    }/*w  w  w.  ja v a 2 s. c o  m*/

    if (!adapter.containsKey(TenantConstants.FIELD_ADAPTERS_DEVICE_AUTHENTICATION_REQUIRED)) {
        log.trace("adding 'device-authentication-required' key to adapter payload");
        adapter.put(TenantConstants.FIELD_ADAPTERS_DEVICE_AUTHENTICATION_REQUIRED, Boolean.TRUE);
    }
}

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

License:Open Source License

/**
 * Adds registration information for a device.
 * <p>/*  w w  w  .  ja v  a2 s. c o m*/
 * The device will be enabled by default if not specified otherwise
 * in the additional data.
 * 
 * @param tenantId The tenant that the device belongs to.
 * @param deviceId The identifier of the device.
 * @param data Additional properties to register with the device.
 * @param contentType The content type to set on the request.
 * @param expectedStatus The status code indicating a successful outcome.
 * @return A future indicating the outcome of the operation.
 *         The future will succeed if the response contained the expected status code.
 *         Otherwise the future will fail with a {@link ServiceInvocationException}.
 * @throws NullPointerException if the tenant is {@code null}.
 */
public Future<Void> registerDevice(final String tenantId, final String deviceId, final JsonObject data,
        final String contentType, final int expectedStatus) {

    Objects.requireNonNull(tenantId);
    final JsonObject requestJson = Optional.ofNullable(data).map(json -> json.copy()).orElse(null);
    if (deviceId != null && requestJson != null) {
        requestJson.put(RegistrationConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId);
    }
    final String uri = String.format("/%s/%s", RegistrationConstants.REGISTRATION_ENDPOINT, tenantId);
    return httpClient.create(uri, requestJson, contentType, statusCode -> statusCode == expectedStatus);
}

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

License:Open Source License

/**
 * Updates registration information for a device.
 * /*from w  w w  . j  a va 2s.  c om*/
 * @param tenantId The tenant that the device belongs to.
 * @param deviceId The identifier of the device.
 * @param data Additional properties to register with the device.
 * @param contentType The content type to set on the request.
 * @param expectedStatus The status code indicating a successful outcome.
 * @return A future indicating the outcome of the operation.
 *         The future will succeed if the response contained the expected status code.
 *         Otherwise the future will fail with a {@link ServiceInvocationException}.
 * @throws NullPointerException if the tenant is {@code null}.
 */
public Future<Void> updateDevice(final String tenantId, final String deviceId, final JsonObject data,
        final String contentType, final int expectedStatus) {

    Objects.requireNonNull(tenantId);
    final String requestUri = String.format(TEMPLATE_URI_REGISTRATION_INSTANCE, tenantId, deviceId);
    final JsonObject requestJson = data.copy();
    requestJson.put(RegistrationConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId);
    return httpClient.update(requestUri, requestJson, contentType, status -> status == expectedStatus);
}

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

License:Open Source License

public static JsonObject getCredentialsJson(final String subject, String tenantId, final JsonObject payload) {
    final JsonObject msg = new JsonObject();
    msg.put(MessageHelper.SYS_PROPERTY_SUBJECT, subject);
    msg.put(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
    if (payload != null) {
        msg.put(FIELD_PAYLOAD, payload);
    }/* ww w  .j  av a2 s .c  o m*/
    return msg;
}

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

License:Open Source License

public static JsonObject getReply(final int status, final String tenantId, final String deviceId,
        final JsonObject payload) {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.put(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
    if (deviceId != null) {
        jsonObject.put(MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId);
    }/*from   ww w.j  a v a  2 s. c o m*/
    jsonObject.put(MessageHelper.APP_PROPERTY_STATUS, Integer.toString(status));
    if (payload != null) {
        jsonObject.put(FIELD_PAYLOAD, payload);
    }
    return jsonObject;
}

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

License:Open Source License

/**
 * Creates an otherwise empty secret for a <em>not-before</em> and
 * a <em>not-after</em> instant.
 * /*from   w w  w. j  ava 2  s .c om*/
 * @param notBefore The point in time from which on the credentials are valid
 *            or {@code null} if there is no such constraint.
 * @param notAfter The point in time until the credentials are valid
 *            or {@code null} if there is no such constraint.
 * @return The secret.
 * @throws IllegalArgumentException if not-before is not before not-after.
 */
public static JsonObject emptySecret(final Instant notBefore, final Instant notAfter) {
    if (notBefore != null && notAfter != null && !notBefore.isBefore(notAfter)) {
        throw new IllegalArgumentException("not before must be before not after");
    } else {
        final JsonObject secret = new JsonObject();
        if (notBefore != null) {
            secret.put(CredentialsConstants.FIELD_SECRETS_NOT_BEFORE,
                    DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(notBefore.atOffset(ZoneOffset.UTC)));
        }
        if (notAfter != null) {
            secret.put(CredentialsConstants.FIELD_SECRETS_NOT_AFTER,
                    DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(notAfter.atOffset(ZoneOffset.UTC)));
        }
        return secret;
    }
}

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

License:Open Source License

/**
 * Creates a hashed-password secret.//from   w  w  w  . ja  va  2  s .  com
 * 
 * @param password The password.
 * @param hashAlgorithm The algorithm to use for creating the password hash.
 * @param notBefore The point in time from which on the secret is valid.
 * @param notAfter The point in time until the secret is valid.
 * @param salt The salt to use for creating the password hash.
 * @return The secret.
 * @throws NullPointerException if any of password or hash algorithm is {@code null}.
 * @throws IllegalArgumentException if the <em>not-before</em> instant does not lie
 *                                  before the <em>not after</em> instant or if the
 *                                  algorithm is not supported.
 */
public static JsonObject hashedPasswordSecret(final String password, final String hashAlgorithm,
        final Instant notBefore, final Instant notAfter, final byte[] salt) {

    Objects.requireNonNull(password);
    Objects.requireNonNull(hashAlgorithm);

    try {
        final JsonObject secret = emptySecret(notBefore, notAfter);
        secret.put(CredentialsConstants.FIELD_SECRETS_HASH_FUNCTION, hashAlgorithm);
        if (salt != null) {
            secret.put(CredentialsConstants.FIELD_SECRETS_SALT, Base64.getEncoder().encodeToString(salt));
        }
        secret.put(CredentialsConstants.FIELD_SECRETS_PWD_HASH,
                Base64.getEncoder().encodeToString(getHashedPassword(hashAlgorithm, salt, password)));
        return secret;
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("unsupported hash algorithm");
    }

}

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

License:Open Source License

/**
 * Creates a credentials object for a device and auth ID.
 * <p>/*www  . jav a  2s  .  c  o m*/
 * The credentials created are of type <em>psk</em>.
 * 
 * @param deviceId The device identifier.
 * @param authId The authentication identifier.
 * @param key The shared key.
 * @param notBefore The point in time from which on the credentials are valid.
 * @param notAfter The point in time until the credentials are valid.
 * @return The credentials.
 * @throws NullPointerException if any of device ID, authentication ID or password
 *                              is {@code null}.
 * @throws IllegalArgumentException if the <em>not-before</em> instant does not lie
 *                                  before the <em>not after</em> instant.
 */
public static CredentialsObject fromPresharedKey(final String deviceId, final String authId, final byte[] key,
        final Instant notBefore, final Instant notAfter) {

    Objects.requireNonNull(key);
    final CredentialsObject result = new CredentialsObject(deviceId, authId,
            CredentialsConstants.SECRETS_TYPE_PRESHARED_KEY);
    final JsonObject secret = emptySecret(notBefore, notAfter);
    secret.put(CredentialsConstants.FIELD_SECRETS_KEY, Base64.getEncoder().encodeToString(key));
    result.addSecret(secret);
    return result;
}