Search in sources :

Example 26 with ClientErrorException

use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.

the class CredentialsApiAuthProvider method authenticate.

@Override
public final void authenticate(final DeviceCredentials deviceCredentials, final Handler<AsyncResult<Device>> resultHandler) {
    Objects.requireNonNull(deviceCredentials);
    Objects.requireNonNull(resultHandler);
    Future<Device> validationResult = Future.future();
    validationResult.setHandler(resultHandler);
    getCredentialsForDevice(deviceCredentials).recover(t -> {
        final ServiceInvocationException e = (ServiceInvocationException) t;
        if (e.getErrorCode() == HttpURLConnection.HTTP_NOT_FOUND) {
            return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "bad credentials"));
        } else {
            return Future.failedFuture(t);
        }
    }).map(credentialsOnRecord -> {
        if (deviceCredentials.validate(credentialsOnRecord)) {
            return new Device(deviceCredentials.getTenantId(), credentialsOnRecord.getDeviceId());
        } else {
            throw new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "invalid credentials");
        }
    }).setHandler(resultHandler);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Vertx(io.vertx.core.Vertx) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Autowired(org.springframework.beans.factory.annotation.Autowired) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Objects(java.util.Objects) Status(io.vertx.ext.healthchecks.Status) CredentialsClient(org.eclipse.hono.client.CredentialsClient) User(io.vertx.ext.auth.User) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) HonoClient(org.eclipse.hono.client.HonoClient) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException)

Example 27 with ClientErrorException

use of org.eclipse.hono.client.ClientErrorException 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 28 with ClientErrorException

use of org.eclipse.hono.client.ClientErrorException 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)

Example 29 with ClientErrorException

use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.

the class BaseRegistrationService method processUpdateRequest.

private Future<EventBusMessage> processUpdateRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final String deviceId = request.getDeviceId();
    final JsonObject payload = getRequestPayload(request.getJsonPayload());
    if (tenantId == null || deviceId == null) {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else {
        log.debug("updating registration information for device [{}] of tenant [{}]", deviceId, tenantId);
        final Future<RegistrationResult> result = Future.future();
        updateDevice(tenantId, deviceId, payload, result.completer());
        return result.map(res -> {
            return request.getResponse(res.getStatus()).setDeviceId(deviceId).setCacheDirective(res.getCacheDirective());
        });
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RegistrationResult(org.eclipse.hono.util.RegistrationResult)

Example 30 with ClientErrorException

use of org.eclipse.hono.client.ClientErrorException in project hono by eclipse.

the class BaseRegistrationService method processRegisterRequest.

private Future<EventBusMessage> processRegisterRequest(final EventBusMessage request) {
    final String tenantId = request.getTenant();
    final String deviceId = request.getDeviceId();
    final JsonObject payload = getRequestPayload(request.getJsonPayload());
    if (tenantId == null || deviceId == null) {
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else {
        log.debug("registering device [{}] for tenant [{}]", deviceId, tenantId);
        final Future<RegistrationResult> result = Future.future();
        addDevice(tenantId, deviceId, payload, result.completer());
        return result.map(res -> {
            return request.getResponse(res.getStatus()).setDeviceId(deviceId).setCacheDirective(res.getCacheDirective());
        });
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RegistrationResult(org.eclipse.hono.util.RegistrationResult)

Aggregations

ClientErrorException (org.eclipse.hono.client.ClientErrorException)32 JsonObject (io.vertx.core.json.JsonObject)19 Future (io.vertx.core.Future)16 HttpURLConnection (java.net.HttpURLConnection)16 Handler (io.vertx.core.Handler)14 AsyncResult (io.vertx.core.AsyncResult)13 Objects (java.util.Objects)12 ServerErrorException (org.eclipse.hono.client.ServerErrorException)10 TenantConstants (org.eclipse.hono.util.TenantConstants)9 EventBusService (org.eclipse.hono.service.EventBusService)8 EventBusMessage (org.eclipse.hono.util.EventBusMessage)8 Vertx (io.vertx.core.Vertx)6 Buffer (io.vertx.core.buffer.Buffer)6 ProtonDelivery (io.vertx.proton.ProtonDelivery)6 HonoClient (org.eclipse.hono.client.HonoClient)6 MqttEndpoint (io.vertx.mqtt.MqttEndpoint)5 MqttServer (io.vertx.mqtt.MqttServer)5 Optional (java.util.Optional)5 TimeUnit (java.util.concurrent.TimeUnit)5 Message (org.apache.qpid.proton.message.Message)5