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