Search in sources :

Example 6 with EventBusMessage

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

the class BaseCredentialsService method processUpdateRequest.

private Future<EventBusMessage> processUpdateRequest(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();
        update(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 7 with EventBusMessage

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

the class BaseRegistrationService method processAssertRequest.

private Future<EventBusMessage> processAssertRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final String deviceId = request.getDeviceId();
    final String gatewayId = request.getGatewayId();
    if (tenantId == null || deviceId == null) {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else if (gatewayId == null) {
        log.debug("asserting registration of device [{}] with tenant [{}]", deviceId, tenantId);
        final Future<RegistrationResult> result = Future.future();
        assertRegistration(tenantId, deviceId, result.completer());
        return result.map(res -> {
            return request.getResponse(res.getStatus()).setDeviceId(deviceId).setJsonPayload(res.getPayload()).setCacheDirective(res.getCacheDirective());
        });
    } else {
        log.debug("asserting registration of device [{}] with tenant [{}] for gateway [{}]", deviceId, tenantId, gatewayId);
        final Future<RegistrationResult> result = Future.future();
        assertRegistration(tenantId, deviceId, gatewayId, result.completer());
        return result.map(res -> {
            return request.getResponse(res.getStatus()).setDeviceId(deviceId).setJsonPayload(res.getPayload()).setCacheDirective(res.getCacheDirective());
        });
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) Autowired(org.springframework.beans.factory.annotation.Autowired) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) Future(io.vertx.core.Future) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) CompositeFuture(io.vertx.core.CompositeFuture) RegistrationResult(org.eclipse.hono.util.RegistrationResult) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future) CompositeFuture(io.vertx.core.CompositeFuture)

Example 8 with EventBusMessage

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

the class BaseTenantService method processAddRequest.

private Future<EventBusMessage> processAddRequest(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("creating tenant [{}]", tenantId);
        final Future<TenantResult<JsonObject>> addResult = Future.future();
        addNotPresentFieldsWithDefaultValuesForTenant(payload);
        add(tenantId, payload, addResult.completer());
        return addResult.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)

Example 9 with EventBusMessage

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

the class BaseTenantService method processGetRequest.

private Future<EventBusMessage> processGetRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final JsonObject payload = request.getJsonPayload();
    if (tenantId == null && payload == null) {
        log.debug("request does not contain any query parameters");
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else if (payload != null) {
        final String tenantIdFromPayload = getTypesafeValueForField(payload, TenantConstants.FIELD_PAYLOAD_TENANT_ID);
        if (tenantIdFromPayload == null) {
            log.debug("payload does not contain any query parameters");
            return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
        } else {
            log.debug("retrieving tenant [id: {}]", tenantIdFromPayload);
            final Future<TenantResult<JsonObject>> getResult = Future.future();
            get(tenantIdFromPayload, getResult.completer());
            return getResult.map(tr -> {
                return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setTenant(tenantIdFromPayload).setCacheDirective(tr.getCacheDirective());
            });
        }
    } else {
        // deprecated API
        log.debug("retrieving tenant [{}] using deprecated variant of get tenant request", tenantId);
        final Future<TenantResult<JsonObject>> getResult = Future.future();
        get(tenantId, getResult.completer());
        return getResult.map(tr -> {
            return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload()).setTenant(tenantId).setCacheDirective(tr.getCacheDirective());
        });
    }
}
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)

Example 10 with EventBusMessage

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

the class BaseCredentialsServiceTest method testGetFailsForMissingType.

/**
 * Verifies that the base service fails a request for getting credentials
 * with a 400 error code if the type is missing.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetFailsForMissingType(final TestContext ctx) {
    // GIVEN a request for getting credentials that does not specify a type
    final CredentialsObject malformedPayload = new CredentialsObject().setAuthId("bumlux").addSecret(CredentialsObject.emptySecret(null, null));
    final EventBusMessage request = createRequestForPayload(CredentialsConstants.CredentialsAction.get, JsonObject.mapFrom(malformedPayload));
    // WHEN processing the request
    service.processRequest(request).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the response contains a 400 error code
        ctx.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) BeforeClass(org.junit.BeforeClass) CredentialsResult(org.eclipse.hono.util.CredentialsResult) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EventBusMessage(org.eclipse.hono.util.EventBusMessage) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) EventBusMessage(org.eclipse.hono.util.EventBusMessage) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

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