Search in sources :

Example 1 with RegistrationResult

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

the class DeviceRegistrationIT method testAssertRegistrationSucceeds.

/**
 * Verifies that a the device registry issues an assertion for a registered device.
 *
 * @throws Exception if the test fails.
 */
@Test
public void testAssertRegistrationSucceeds() throws Exception {
    String deviceId = getRandomDeviceId();
    registration.register(deviceId, HTTP_CREATED).get(DEFAULT_TEST_TIMEOUT, TimeUnit.MILLISECONDS);
    RegistrationResult result = registration.assertRegistration(deviceId, HTTP_OK).get(DEFAULT_TEST_TIMEOUT, TimeUnit.MILLISECONDS);
    assertTrue(result.getPayload().containsKey(RegistrationConstants.FIELD_ASSERTION));
    assertThat("Did not receive responses to all requests", registration.getCorrelationHelperSize(), is(0));
}
Also used : RegistrationResult(org.eclipse.hono.util.RegistrationResult) Test(org.junit.Test)

Example 2 with RegistrationResult

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

the class BaseRegistrationService method assertRegistration.

/**
 * {@inheritDoc}
 * <p>
 * Subclasses may override this method in order to implement a more sophisticated approach for asserting registration status, e.g.
 * using cached information etc.
 */
@Override
public void assertRegistration(final String tenantId, final String deviceId, final String gatewayId, final Handler<AsyncResult<RegistrationResult>> resultHandler) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(gatewayId);
    Objects.requireNonNull(resultHandler);
    Future<RegistrationResult> deviceInfoTracker = Future.future();
    Future<RegistrationResult> gatewayInfoTracker = Future.future();
    getDevice(tenantId, deviceId, deviceInfoTracker.completer());
    getDevice(tenantId, gatewayId, gatewayInfoTracker.completer());
    CompositeFuture.all(deviceInfoTracker, gatewayInfoTracker).compose(ok -> {
        final RegistrationResult deviceResult = deviceInfoTracker.result();
        final RegistrationResult gatewayResult = gatewayInfoTracker.result();
        if (!isDeviceEnabled(deviceResult)) {
            return Future.succeededFuture(RegistrationResult.from(HttpURLConnection.HTTP_NOT_FOUND));
        } else if (!isDeviceEnabled(gatewayResult)) {
            return Future.succeededFuture(RegistrationResult.from(HttpURLConnection.HTTP_FORBIDDEN));
        } else {
            final JsonObject deviceData = deviceResult.getPayload().getJsonObject(RegistrationConstants.FIELD_DATA, new JsonObject());
            final JsonObject gatewayData = gatewayResult.getPayload().getJsonObject(RegistrationConstants.FIELD_DATA, new JsonObject());
            if (isGatewayAuthorized(gatewayId, gatewayData, deviceId, deviceData)) {
                return Future.succeededFuture(RegistrationResult.from(HttpURLConnection.HTTP_OK, getAssertionPayload(tenantId, deviceId, deviceData), CacheDirective.maxAgeDirective(assertionFactory.getAssertionLifetime())));
            } else {
                return Future.succeededFuture(RegistrationResult.from(HttpURLConnection.HTTP_FORBIDDEN));
            }
        }
    }).setHandler(resultHandler);
}
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) JsonObject(io.vertx.core.json.JsonObject) RegistrationResult(org.eclipse.hono.util.RegistrationResult)

Example 3 with RegistrationResult

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

the class BaseRegistrationService method processGetRequest.

private Future<EventBusMessage> processGetRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final String deviceId = request.getDeviceId();
    if (tenantId == null || deviceId == null) {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else {
        log.debug("retrieving device [{}] of tenant [{}]", deviceId, tenantId);
        final Future<RegistrationResult> result = Future.future();
        getDevice(tenantId, deviceId, result.completer());
        return result.map(res -> {
            return request.getResponse(res.getStatus()).setDeviceId(deviceId).setJsonPayload(res.getPayload()).setCacheDirective(res.getCacheDirective());
        });
    }
}
Also used : ClientErrorException(org.eclipse.hono.client.ClientErrorException) RegistrationResult(org.eclipse.hono.util.RegistrationResult)

Example 4 with RegistrationResult

use of org.eclipse.hono.util.RegistrationResult 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 5 with RegistrationResult

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

the class BaseRegistrationService method processDeregisterRequest.

private Future<EventBusMessage> processDeregisterRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final String deviceId = request.getDeviceId();
    if (tenantId == null || deviceId == null) {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else {
        log.debug("deregistering device [{}] of tenant [{}]", deviceId, tenantId);
        final Future<RegistrationResult> result = Future.future();
        removeDevice(tenantId, deviceId, result.completer());
        return result.map(res -> {
            return request.getResponse(res.getStatus()).setDeviceId(deviceId).setCacheDirective(res.getCacheDirective());
        });
    }
}
Also used : ClientErrorException(org.eclipse.hono.client.ClientErrorException) RegistrationResult(org.eclipse.hono.util.RegistrationResult)

Aggregations

RegistrationResult (org.eclipse.hono.util.RegistrationResult)12 JsonObject (io.vertx.core.json.JsonObject)7 ClientErrorException (org.eclipse.hono.client.ClientErrorException)6 Test (org.junit.Test)5 Handler (io.vertx.core.Handler)3 HttpURLConnection (java.net.HttpURLConnection)3 CacheDirective (org.eclipse.hono.util.CacheDirective)3 RegistrationConstants (org.eclipse.hono.util.RegistrationConstants)3 AsyncResult (io.vertx.core.AsyncResult)2 CompositeFuture (io.vertx.core.CompositeFuture)2 Future (io.vertx.core.Future)2 Objects (java.util.Objects)2 EventBusService (org.eclipse.hono.service.EventBusService)2 EventBusMessage (org.eclipse.hono.util.EventBusMessage)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Qualifier (org.springframework.beans.factory.annotation.Qualifier)2 Jwts (io.jsonwebtoken.Jwts)1 SignatureAlgorithm (io.jsonwebtoken.SignatureAlgorithm)1 Context (io.vertx.core.Context)1 Vertx (io.vertx.core.Vertx)1