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