Search in sources :

Example 16 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class BaseCredentialsService method processAddRequest.

private Future<EventBusMessage> processAddRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final CredentialsObject payload = Optional.ofNullable(request.getJsonPayload()).map(json -> json.mapTo(CredentialsObject.class)).orElse(null);
    if (tenantId == null || payload == null) {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else if (payload.isValid()) {
        final Future<CredentialsResult<JsonObject>> result = Future.future();
        add(tenantId, JsonObject.mapFrom(payload), result.completer());
        return result.map(res -> {
            return request.getResponse(res.getStatus()).setDeviceId(payload.getDeviceId()).setCacheDirective(res.getCacheDirective());
        });
    } else {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CredentialsResult(org.eclipse.hono.util.CredentialsResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future) JsonObject(io.vertx.core.json.JsonObject)

Example 17 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class BaseCredentialsService method processGetRequest.

private Future<EventBusMessage> processGetRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final JsonObject payload = request.getJsonPayload();
    if (tenantId == null || payload == null) {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else {
        final String type = getTypesafeValueForField(payload, CredentialsConstants.FIELD_TYPE);
        final String authId = getTypesafeValueForField(payload, CredentialsConstants.FIELD_AUTH_ID);
        final String deviceId = getTypesafeValueForField(payload, CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID);
        if (type == null) {
            return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
        } else if (authId != null && deviceId == null) {
            log.debug("getting credentials [tenant: {}, type: {}, auth-id: {}]", tenantId, type, authId);
            final Future<CredentialsResult<JsonObject>> result = Future.future();
            get(tenantId, type, authId, result.completer());
            return result.map(res -> {
                final String deviceIdFromPayload = Optional.ofNullable(res.getPayload()).map(p -> (String) getTypesafeValueForField(p, TenantConstants.FIELD_PAYLOAD_DEVICE_ID)).orElse(null);
                return request.getResponse(res.getStatus()).setDeviceId(deviceIdFromPayload).setJsonPayload(res.getPayload()).setCacheDirective(res.getCacheDirective());
            });
        } else if (deviceId != null && authId == null) {
            log.debug("getting credentials for device [tenant: {}, device-id: {}]", tenantId, deviceId);
            final Future<CredentialsResult<JsonObject>> result = Future.future();
            getAll(tenantId, deviceId, result.completer());
            return result.map(res -> {
                return request.getResponse(res.getStatus()).setDeviceId(deviceId).setJsonPayload(res.getPayload()).setCacheDirective(res.getCacheDirective());
            });
        } else {
            log.debug("get credentials request contains invalid search criteria [type: {}, device-id: {}, auth-id: {}]", type, deviceId, authId);
            return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CredentialsResult(org.eclipse.hono.util.CredentialsResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future) CredentialsResult(org.eclipse.hono.util.CredentialsResult)

Example 18 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class CredentialsAmqpEndpoint method processRequest.

@Override
public final void processRequest(final Message msg, final ResourceIdentifier targetAddress, final HonoUser clientPrincipal) {
    final EventBusMessage credentialsMsg = EventBusMessage.forOperation(msg).setReplyToAddress(msg).setAppCorrelationId(msg).setCorrelationId(msg).setTenant(targetAddress.getTenantId()).setJsonPayload(msg);
    vertx.eventBus().send(CredentialsConstants.EVENT_BUS_ADDRESS_CREDENTIALS_IN, credentialsMsg.toJson());
}
Also used : EventBusMessage(org.eclipse.hono.util.EventBusMessage)

Example 19 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class RegistrationAmqpEndpoint method processRequest.

@Override
public final void processRequest(final Message msg, final ResourceIdentifier targetAddress, final HonoUser clientPrincipal) {
    final EventBusMessage registrationMsg = EventBusMessage.forOperation(msg).setReplyToAddress(msg).setAppCorrelationId(msg).setCorrelationId(msg).setTenant(targetAddress.getTenantId()).setDeviceId(msg).setGatewayId(msg).setJsonPayload(msg);
    vertx.eventBus().send(RegistrationConstants.EVENT_BUS_ADDRESS_REGISTRATION_IN, registrationMsg.toJson());
}
Also used : EventBusMessage(org.eclipse.hono.util.EventBusMessage)

Example 20 with EventBusMessage

use of org.eclipse.hono.util.EventBusMessage in project hono by eclipse.

the class BaseTenantService method processUpdateRequest.

private Future<EventBusMessage> processUpdateRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final JsonObject payload = getRequestPayload(request.getJsonPayload());
    if (tenantId == null) {
        log.debug("request does not contain mandatory property [{}]", MessageHelper.APP_PROPERTY_TENANT_ID);
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else if (isValidRequestPayload(payload)) {
        log.debug("updating tenant [{}]", tenantId);
        final Future<TenantResult<JsonObject>> updateResult = Future.future();
        addNotPresentFieldsWithDefaultValuesForTenant(payload);
        update(tenantId, payload, updateResult.completer());
        return updateResult.map(tr -> {
            return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setCacheDirective(tr.getCacheDirective());
        });
    } else {
        log.debug("request contains malformed payload");
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TenantResult(org.eclipse.hono.util.TenantResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future)

Aggregations

EventBusMessage (org.eclipse.hono.util.EventBusMessage)29 Future (io.vertx.core.Future)26 JsonObject (io.vertx.core.json.JsonObject)26 HttpURLConnection (java.net.HttpURLConnection)26 Handler (io.vertx.core.Handler)24 AsyncResult (io.vertx.core.AsyncResult)21 ServiceInvocationException (org.eclipse.hono.client.ServiceInvocationException)18 TestContext (io.vertx.ext.unit.TestContext)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)16 Test (org.junit.Test)16 Timeout (org.junit.rules.Timeout)16 RunWith (org.junit.runner.RunWith)16 ServiceConfigProperties (org.eclipse.hono.config.ServiceConfigProperties)14 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)13 CredentialsObject (org.eclipse.hono.util.CredentialsObject)13 CredentialsResult (org.eclipse.hono.util.CredentialsResult)13 BeforeClass (org.junit.BeforeClass)13 TenantConstants (org.eclipse.hono.util.TenantConstants)11 Objects (java.util.Objects)10 ClientErrorException (org.eclipse.hono.client.ClientErrorException)8