Search in sources :

Example 46 with JsonArray

use of io.vertx.core.json.JsonArray in project hono by eclipse.

the class FileBasedCredentialsService method remove.

@Override
public void remove(final String tenantId, final String type, final String authId, final Handler<AsyncResult<CredentialsResult<JsonObject>>> resultHandler) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(type);
    Objects.requireNonNull(authId);
    Objects.requireNonNull(resultHandler);
    if (getConfig().isModificationEnabled()) {
        final Map<String, JsonArray> credentialsForTenant = credentials.get(tenantId);
        if (credentialsForTenant == null) {
            resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND)));
        } else {
            final JsonArray credentialsForAuthId = credentialsForTenant.get(authId);
            if (credentialsForAuthId == null) {
                resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND)));
            } else if (removeCredentialsFromCredentialsArray(null, type, credentialsForAuthId)) {
                if (credentialsForAuthId.isEmpty()) {
                    // do not leave empty array as value
                    credentialsForTenant.remove(authId);
                }
                resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_NO_CONTENT)));
            } else {
                resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND)));
            }
        }
    } else {
        resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_FORBIDDEN)));
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray)

Example 47 with JsonArray

use of io.vertx.core.json.JsonArray in project hono by eclipse.

the class FileBasedCredentialsService method removeAll.

@Override
public void removeAll(final String tenantId, final String deviceId, final Handler<AsyncResult<CredentialsResult<JsonObject>>> resultHandler) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(resultHandler);
    if (getConfig().isModificationEnabled()) {
        final Map<String, JsonArray> credentialsForTenant = credentials.get(tenantId);
        if (credentialsForTenant == null) {
            resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND)));
        } else {
            boolean removedAnyElement = false;
            // handled explicitly
            for (JsonArray credentialsForAuthId : credentialsForTenant.values()) {
                if (removeCredentialsFromCredentialsArray(deviceId, CredentialsConstants.SPECIFIER_WILDCARD, credentialsForAuthId)) {
                    removedAnyElement = true;
                }
            }
            // there might be empty credentials arrays left now, so remove them in a second run
            cleanupEmptyCredentialsArrays(credentialsForTenant);
            if (removedAnyElement) {
                dirty = true;
                resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_NO_CONTENT)));
            } else {
                resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND)));
            }
        }
    } else {
        resultHandler.handle(Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_FORBIDDEN)));
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray)

Example 48 with JsonArray

use of io.vertx.core.json.JsonArray in project hono by eclipse.

the class FileBasedCredentialsService method addCredentialsResult.

private CredentialsResult<JsonObject> addCredentialsResult(final String tenantId, final JsonObject credentialsToAdd) {
    String authId = credentialsToAdd.getString(CredentialsConstants.FIELD_AUTH_ID);
    String type = credentialsToAdd.getString(CredentialsConstants.FIELD_TYPE);
    log.debug("adding credentials for device [tenant-id: {}, auth-id: {}, type: {}]", tenantId, authId, type);
    final Map<String, JsonArray> credentialsForTenant = getCredentialsForTenant(tenantId);
    final JsonArray authIdCredentials = getAuthIdCredentials(authId, credentialsForTenant);
    // check if credentials already exist with the type and auth-id from the payload
    for (Object credentialsObj : authIdCredentials) {
        final JsonObject credentials = (JsonObject) credentialsObj;
        if (credentials.getString(CredentialsConstants.FIELD_TYPE).equals(type)) {
            return CredentialsResult.from(HttpURLConnection.HTTP_CONFLICT);
        }
    }
    authIdCredentials.add(credentialsToAdd);
    dirty = true;
    return CredentialsResult.from(HttpURLConnection.HTTP_CREATED);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Example 49 with JsonArray

use of io.vertx.core.json.JsonArray in project hono by eclipse.

the class FileBasedRegistrationService method addAll.

private Future<Void> addAll(final Buffer deviceIdentities) {
    Future<Void> result = Future.future();
    try {
        int deviceCount = 0;
        JsonArray allObjects = deviceIdentities.toJsonArray();
        for (Object obj : allObjects) {
            if (JsonObject.class.isInstance(obj)) {
                deviceCount += addDevicesForTenant((JsonObject) obj);
            }
        }
        log.info("successfully loaded {} device identities from file [{}]", deviceCount, getConfig().getFilename());
        result.complete();
    } catch (DecodeException e) {
        log.warn("cannot read malformed JSON from device identity file [{}]", getConfig().getFilename());
        result.fail(e);
    }
    return result;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) DecodeException(io.vertx.core.json.DecodeException)

Example 50 with JsonArray

use of io.vertx.core.json.JsonArray in project hono by eclipse.

the class FileBasedRegistrationService method saveToFile.

Future<Void> saveToFile() {
    if (!getConfig().isSaveToFile()) {
        return Future.succeededFuture();
    } else if (dirty) {
        return checkFileExists(true).compose(s -> {
            final AtomicInteger idCount = new AtomicInteger();
            final JsonArray tenants = new JsonArray();
            for (Entry<String, Map<String, JsonObject>> entry : identities.entrySet()) {
                final JsonArray devices = new JsonArray();
                for (Entry<String, JsonObject> deviceEntry : entry.getValue().entrySet()) {
                    devices.add(new JsonObject().put(FIELD_PAYLOAD_DEVICE_ID, deviceEntry.getKey()).put(FIELD_DATA, deviceEntry.getValue()));
                    idCount.incrementAndGet();
                }
                tenants.add(new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_DEVICES, devices));
            }
            Future<Void> writeHandler = Future.future();
            vertx.fileSystem().writeFile(getConfig().getFilename(), Buffer.factory.buffer(tenants.encodePrettily()), writeHandler.completer());
            return writeHandler.map(ok -> {
                dirty = false;
                log.trace("successfully wrote {} device identities to file {}", idCount.get(), getConfig().getFilename());
                return (Void) null;
            }).otherwise(t -> {
                log.warn("could not write device identities to file {}", getConfig().getFilename(), t);
                return (Void) null;
            });
        });
    } else {
        log.trace("registry does not need to be persisted");
        return Future.succeededFuture();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) FIELD_ENABLED(org.eclipse.hono.util.RequestResponseApiConstants.FIELD_ENABLED) DecodeException(io.vertx.core.json.DecodeException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) BaseRegistrationService(org.eclipse.hono.service.registration.BaseRegistrationService) Future(io.vertx.core.Future) Objects(java.util.Objects) FIELD_PAYLOAD_DEVICE_ID(org.eclipse.hono.util.RequestResponseApiConstants.FIELD_PAYLOAD_DEVICE_ID) JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) RegistrationResult(org.eclipse.hono.util.RegistrationResult) FIELD_DATA(org.eclipse.hono.util.RegistrationConstants.FIELD_DATA) Entry(java.util.Map.Entry) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Repository(org.springframework.stereotype.Repository) Handler(io.vertx.core.Handler) JsonArray(io.vertx.core.json.JsonArray) Entry(java.util.Map.Entry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future)

Aggregations

JsonArray (io.vertx.core.json.JsonArray)535 JsonObject (io.vertx.core.json.JsonObject)379 Test (org.junit.Test)185 List (java.util.List)69 ArrayList (java.util.ArrayList)66 Map (java.util.Map)52 Handler (io.vertx.core.Handler)49 HashMap (java.util.HashMap)42 Collectors (java.util.stream.Collectors)42 Test (org.junit.jupiter.api.Test)41 Future (io.vertx.core.Future)37 IOException (java.io.IOException)35 AsyncResult (io.vertx.core.AsyncResult)34 Buffer (io.vertx.core.buffer.Buffer)33 HttpURLConnection (java.net.HttpURLConnection)30 StandardCharsets (java.nio.charset.StandardCharsets)29 RoutingContext (io.vertx.ext.web.RoutingContext)26 Objects (java.util.Objects)25 Instant (java.time.Instant)24 Rule (org.junit.Rule)22