Search in sources :

Example 1 with CredentialsResult

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

the class BaseCredentialsService method processRemoveRequest.

private Future<EventBusMessage> processRemoveRequest(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) {
            log.debug("remove credentials request does not contain mandatory type parameter");
            return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
        } else if (!type.equals(CredentialsConstants.SPECIFIER_WILDCARD) && authId != null) {
            // delete a single credentials instance
            log.debug("removing specific credentials [tenant: {}, type: {}, auth-id: {}]", tenantId, type, authId);
            final Future<CredentialsResult<JsonObject>> result = Future.future();
            remove(tenantId, type, authId, result.completer());
            return result.map(res -> {
                return request.getResponse(res.getStatus()).setCacheDirective(res.getCacheDirective());
            });
        } else if (deviceId != null && type.equals(CredentialsConstants.SPECIFIER_WILDCARD)) {
            // delete all credentials for device
            log.debug("removing all credentials for device [tenant: {}, device-id: {}]", tenantId, deviceId);
            final Future<CredentialsResult<JsonObject>> result = Future.future();
            removeAll(tenantId, deviceId, result.completer());
            return result.map(res -> {
                return request.getResponse(res.getStatus()).setDeviceId(deviceId).setCacheDirective(res.getCacheDirective());
            });
        } else {
            log.debug("remove credentials request contains invalid search criteria [type: {}, device-id: {}, auth-id: {}]", type, deviceId, authId);
            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) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future) CredentialsResult(org.eclipse.hono.util.CredentialsResult)

Example 2 with CredentialsResult

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

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

the class CredentialsClientImpl method get.

/**
 * Invokes the <em>Get Credentials</em> operation of Hono's
 * <a href="https://www.eclipse.org/hono/api/Credentials-API">Credentials API</a>
 * on the service represented by the <em>sender</em> and <em>receiver</em> links.
 */
@Override
public final Future<CredentialsObject> get(final String type, final String authId) {
    Objects.requireNonNull(type);
    Objects.requireNonNull(authId);
    final Future<CredentialsResult<CredentialsObject>> responseTracker = Future.future();
    final JsonObject specification = new JsonObject().put(CredentialsConstants.FIELD_TYPE, type).put(CredentialsConstants.FIELD_AUTH_ID, authId);
    createAndSendRequest(CredentialsConstants.CredentialsAction.get.toString(), specification, responseTracker.completer());
    return responseTracker.map(response -> {
        switch(response.getStatus()) {
            case HttpURLConnection.HTTP_OK:
                return response.getPayload();
            default:
                throw StatusCodeMapper.from(response);
        }
    });
}
Also used : CredentialsResult(org.eclipse.hono.util.CredentialsResult) JsonObject(io.vertx.core.json.JsonObject)

Example 4 with CredentialsResult

use of org.eclipse.hono.util.CredentialsResult 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));
    }
}
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 5 with CredentialsResult

use of org.eclipse.hono.util.CredentialsResult 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));
        }
    }
}
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) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future) CredentialsResult(org.eclipse.hono.util.CredentialsResult)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)5 CredentialsResult (org.eclipse.hono.util.CredentialsResult)5 AsyncResult (io.vertx.core.AsyncResult)4 Future (io.vertx.core.Future)4 Handler (io.vertx.core.Handler)4 HttpURLConnection (java.net.HttpURLConnection)4 Objects (java.util.Objects)4 Optional (java.util.Optional)4 ClientErrorException (org.eclipse.hono.client.ClientErrorException)4 EventBusService (org.eclipse.hono.service.EventBusService)4 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)4 CredentialsObject (org.eclipse.hono.util.CredentialsObject)4 EventBusMessage (org.eclipse.hono.util.EventBusMessage)4 TenantConstants (org.eclipse.hono.util.TenantConstants)4