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