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)));
}
}
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)));
}
}
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);
}
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;
}
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();
}
}
Aggregations