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.amqp.AbstractAmqpEndpoint.java

License:Open Source License

/**
 * Adds correlation id related properties on a response to be sent in reply to a request.
 * //from   www .  j ava 2s .c  o  m
 * @param request The request to correlate to.
 * @param message The response message.
 */
protected final void addHeadersToResponse(final Message request, final JsonObject message) {
    final boolean isApplicationCorrelationId = MessageHelper.getXOptAppCorrelationId(request);
    logger.debug("registration request [{}] uses application specific correlation ID: {}",
            request.getMessageId(), isApplicationCorrelationId);
    if (isApplicationCorrelationId) {
        message.put(MessageHelper.ANNOTATION_X_OPT_APP_CORRELATION_ID, isApplicationCorrelationId);
    }
    final JsonObject correlationIdJson = encodeIdToJson(getCorrelationId(request));
    message.put(MessageHelper.SYS_PROPERTY_CORRELATION_ID, correlationIdJson);
}

From source file:org.eclipse.hono.service.amqp.BaseEndpoint.java

License:Open Source License

protected final void addHeadersToResponse(final Message request, final JsonObject message) {
    final boolean isApplicationCorrelationId = MessageHelper.getXOptAppCorrelationId(request);
    logger.debug("registration request [{}] uses application specific correlation ID: {}",
            request.getMessageId(), isApplicationCorrelationId);
    if (isApplicationCorrelationId) {
        message.put(MessageHelper.ANNOTATION_X_OPT_APP_CORRELATION_ID, isApplicationCorrelationId);
    }/*from w w w .jav  a 2 s  .  c o  m*/
    final JsonObject correlationIdJson = encodeIdToJson(getCorrelationId(request));
    message.put(MessageHelper.SYS_PROPERTY_CORRELATION_ID, correlationIdJson);
}

From source file:org.eclipse.hono.service.auth.HonoSaslAuthenticator.java

License:Open Source License

private void verify(final String mechanism, final byte[] saslResponse,
        final Handler<AsyncResult<HonoUser>> authResultHandler) {

    JsonObject authRequest = AuthenticationConstants.getAuthenticationRequest(mechanism, saslResponse);
    if (peerCertificateChain != null) {
        authRequest.put(AuthenticationConstants.FIELD_SUBJECT_DN,
                peerCertificateChain[0].getSubjectDN().getName());
    }/*from   w w w.  jav  a2  s .co m*/
    authenticationService.authenticate(authRequest, authResultHandler);
}

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. ja v  a 2  s.  com
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.credentials.CredentialsHttpEndpoint.java

License:Open Source License

private void removeCredentials(final RoutingContext ctx) {

    final String tenantId = getTenantParam(ctx);
    final String type = getTypeParam(ctx);
    final String authId = getAuthIdParam(ctx);

    logger.debug("removeCredentials [tenant: {}, type: {}, authId: {}]", tenantId, type, authId);

    final JsonObject payload = new JsonObject();
    payload.put(CredentialsConstants.FIELD_TYPE, type);
    payload.put(CredentialsConstants.FIELD_AUTH_ID, authId);

    final JsonObject requestMsg = EventBusMessage
            .forOperation(CredentialsConstants.CredentialsAction.remove.toString()).setTenant(tenantId)
            .setJsonPayload(payload).toJson();

    sendAction(ctx, requestMsg,//from  www.ja v a 2  s .co m
            getDefaultResponseHandler(ctx, status -> status == HttpURLConnection.HTTP_NO_CONTENT, null));
}

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

License:Open Source License

private void removeCredentialsForDevice(final RoutingContext ctx) {

    final String tenantId = getTenantParam(ctx);
    final String deviceId = getDeviceIdParam(ctx);

    logger.debug("removeCredentialsForDevice: [tenant: {}, device-id: {}]", tenantId, deviceId);

    final JsonObject payload = new JsonObject();
    payload.put(CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, deviceId);
    payload.put(CredentialsConstants.FIELD_TYPE, CredentialsConstants.SPECIFIER_WILDCARD);

    final JsonObject requestMsg = EventBusMessage
            .forOperation(CredentialsConstants.CredentialsAction.remove.toString()).setTenant(tenantId)
            .setDeviceId(deviceId).setJsonPayload(payload).toJson();

    sendAction(ctx, requestMsg,/*  w w w.  j av  a  2  s.c  o m*/
            getDefaultResponseHandler(ctx, status -> status == HttpURLConnection.HTTP_NO_CONTENT, null));
}

From source file:org.eclipse.hono.service.credentials.impl.FileBasedCredentialsService.java

License:Open Source License

protected void loadCredentialsData() {
    if (filename != null) {
        final FileSystem fs = vertx.fileSystem();
        log.debug("trying to load credentials information from file {}", filename);
        if (fs.existsBlocking(filename)) {
            final AtomicInteger credentialsCount = new AtomicInteger();
            fs.readFile(filename, readAttempt -> {
                if (readAttempt.succeeded()) {
                    JsonArray allObjects = new JsonArray(new String(readAttempt.result().getBytes()));
                    for (Object obj : allObjects) {
                        JsonObject tenant = (JsonObject) obj;
                        String tenantId = tenant.getString(FIELD_TENANT);
                        Map<String, JsonArray> credentialsMap = new HashMap<>();
                        for (Object credentialsObj : tenant.getJsonArray(ARRAY_CREDENTIALS)) {
                            JsonObject credentials = (JsonObject) credentialsObj;
                            JsonArray authIdCredentials;
                            if (credentialsMap.containsKey(credentials.getString(FIELD_AUTH_ID))) {
                                authIdCredentials = credentialsMap.get(credentials.getString(FIELD_AUTH_ID));
                            } else {
                                authIdCredentials = new JsonArray();
                            }/*from  ww  w  .j a v a2  s .  c  om*/
                            authIdCredentials.add(credentials);
                            credentialsMap.put(credentials.getString(FIELD_AUTH_ID), authIdCredentials);
                            credentialsCount.incrementAndGet();
                        }
                        credentials.put(tenantId, credentialsMap);
                    }
                    log.info("successfully loaded {} credentials from file [{}]", credentialsCount.get(),
                            filename);
                } else {
                    log.warn("could not load credentials from file [{}]", filename, readAttempt.cause());
                }
            });
        } else {
            log.debug("credentials file {} does not exist (yet)", filename);
        }
    }
}

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

License:Open Source License

/**
 * Gets the payload from a request message.
 * <p>//from   w  ww  .j ava  2s .  co m
 * The returned JSON object contains the given payload (if not {@code null}).
 * If the given payload does not contain an <em>enabled</em> property, then
 * it is added with value {@code true} to the returned object.
 * 
 * @param payload The payload from the request message.
 * @return The payload (never {@code null}).
 */
protected final JsonObject getRequestPayload(final JsonObject payload) {

    final JsonObject result = Optional.ofNullable(payload).orElse(new JsonObject());
    final Boolean enabled = getTypesafeValueForField(result, RequestResponseApiConstants.FIELD_ENABLED);
    if (enabled == null) {
        log.debug("adding 'enabled=true' property to request payload");
        result.put(RequestResponseApiConstants.FIELD_ENABLED, Boolean.TRUE);
    }
    return result;
}

From source file:org.eclipse.hono.service.monitoring.AbstractMessageSenderConnectionEventProducer.java

License:Open Source License

private Future<?> sendNotificationEvent(final Device authenticatedDevice, final String protocolAdapter,
        final String remoteId, final String cause, final JsonObject data) {

    if (authenticatedDevice == null) {
        // we only handle authenticated devices
        return Future.succeededFuture();
    }//from   w  ww.  ja v  a  2 s .  com

    // get an assertion

    final Future<String> assertionFuture = this.deviceRegistryClient
            .getOrCreateRegistrationClient(authenticatedDevice.getTenantId()).compose(registrationClient -> {
                return registrationClient.assertRegistration(authenticatedDevice.getDeviceId())
                        .map(registration -> {
                            return registration.getString(RegistrationConstants.FIELD_ASSERTION);
                        });
            });

    // get a sender

    final Future<MessageSender> senderFuture = getOrCreateSender(authenticatedDevice);

    // send message with assertion and sender

    return CompositeFuture.all(assertionFuture, senderFuture).compose(f -> {
        final String deviceId = authenticatedDevice.getDeviceId();

        final JsonObject payload = new JsonObject();
        payload.put("cause", cause);
        payload.put("remote-id", remoteId);
        payload.put("source", protocolAdapter);

        if (data != null) {
            payload.put("data", data);
        }

        return senderFuture.result().send(deviceId, payload.encode().getBytes(StandardCharsets.UTF_8),
                EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE, assertionFuture.result(), v -> {
                });
    });
}

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

License:Open Source License

private JsonObject getRequestPayload(final JsonObject request) {

    final JsonObject payload = request.getJsonObject(RegistrationConstants.FIELD_PAYLOAD, new JsonObject());
    Boolean enabled = payload.getBoolean(FIELD_ENABLED);
    if (enabled == null) {
        log.debug("adding 'enabled' key to payload");
        payload.put(FIELD_ENABLED, Boolean.TRUE);
    }/*from ww w. ja  va2 s  .c  o  m*/
    return payload;
}