use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class FileBasedTenantService method saveToFile.
Future<Void> saveToFile() {
if (!getConfig().isSaveToFile()) {
return Future.succeededFuture();
} else if (dirty) {
return checkFileExists(true).compose(s -> {
final JsonArray tenantsJson = new JsonArray();
tenants.values().stream().forEach(tenant -> {
tenantsJson.add(JsonObject.mapFrom(tenant));
});
final Future<Void> writeHandler = Future.future();
vertx.fileSystem().writeFile(getConfig().getFilename(), Buffer.factory.buffer(tenantsJson.encodePrettily()), writeHandler.completer());
return writeHandler.map(ok -> {
dirty = false;
log.trace("successfully wrote {} tenants to file {}", tenantsJson.size(), getConfig().getFilename());
return (Void) null;
}).otherwise(t -> {
log.warn("could not write tenants to file {}", getConfig().getFilename(), t);
return (Void) null;
});
});
} else {
log.trace("tenants registry does not need to be persisted");
return Future.succeededFuture();
}
}
use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class FileBasedTenantService method addAll.
private Future<Void> addAll(final Buffer tenantsBuffer) {
final Future<Void> result = Future.future();
try {
if (tenantsBuffer.length() > 0) {
int tenantCount = 0;
final JsonArray allObjects = tenantsBuffer.toJsonArray();
for (final Object obj : allObjects) {
if (JsonObject.class.isInstance(obj)) {
tenantCount++;
addTenant((JsonObject) obj);
}
}
log.info("successfully loaded {} tenants from file [{}]", tenantCount, getConfig().getFilename());
}
result.complete();
} catch (final DecodeException e) {
log.warn("cannot read malformed JSON from tenants file [{}]", getConfig().getFilename());
result.fail(e);
}
return result;
}
use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class FileBasedCredentialsServiceTest method testGetCredentialsSucceedsForExistingCredentials.
/**
* Verifies that the service returns existing credentials.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetCredentialsSucceedsForExistingCredentials(final TestContext ctx) {
register(svc, "tenant", "device", "myId", "myType", new JsonArray(), ctx);
final Async get = ctx.async();
svc.get("tenant", "myType", "myId", ctx.asyncAssertSuccess(s -> {
assertThat(s.getStatus(), is(HttpURLConnection.HTTP_OK));
assertThat(s.getPayload().getString(CredentialsConstants.FIELD_AUTH_ID), is("myId"));
assertThat(s.getPayload().getString(CredentialsConstants.FIELD_TYPE), is("myType"));
get.complete();
}));
get.await(2000);
}
use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class FileBasedTenantServiceTest method buildTenantPayload.
/**
* Creates a tenant object for a tenantId.
* <p>
* The tenant object created contains configurations for the http and the mqtt adapter.
*
* @param tenantId The tenant identifier.
* @return The tenant object.
*/
private static JsonObject buildTenantPayload(final String tenantId) {
final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_HTTP).put(TenantConstants.FIELD_ADAPTERS_DEVICE_AUTHENTICATION_REQUIRED, Boolean.TRUE).put(TenantConstants.FIELD_ENABLED, Boolean.TRUE);
final JsonObject adapterDetailsMqtt = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_MQTT).put(TenantConstants.FIELD_ADAPTERS_DEVICE_AUTHENTICATION_REQUIRED, Boolean.TRUE).put(TenantConstants.FIELD_ENABLED, Boolean.TRUE);
final JsonObject tenantPayload = new JsonObject().put(TenantConstants.FIELD_PAYLOAD_TENANT_ID, tenantId).put(TenantConstants.FIELD_ENABLED, Boolean.TRUE).put(TenantConstants.FIELD_ADAPTERS, new JsonArray().add(adapterDetailsHttp).add(adapterDetailsMqtt));
return tenantPayload;
}
use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class BaseTenantService method addNotPresentFieldsWithDefaultValuesForTenant.
/**
* Add default values for optional fields that are not filled in the payload.
* <p>
* 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));
}
}
Aggregations