Search in sources :

Example 6 with ClientErrorException

use of org.eclipse.hono.client.ClientErrorException 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());
        });
    }
}
Also used : ClientErrorException(org.eclipse.hono.client.ClientErrorException) RegistrationResult(org.eclipse.hono.util.RegistrationResult)

Example 7 with ClientErrorException

use of org.eclipse.hono.client.ClientErrorException 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));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TenantResult(org.eclipse.hono.util.TenantResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future)

Example 8 with ClientErrorException

use of org.eclipse.hono.client.ClientErrorException 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());
        });
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TenantResult(org.eclipse.hono.util.TenantResult) TenantConstants(org.eclipse.hono.util.TenantConstants) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ClientErrorException(org.eclipse.hono.client.ClientErrorException) EventBusMessage(org.eclipse.hono.util.EventBusMessage) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Objects(java.util.Objects) EventBusService(org.eclipse.hono.service.EventBusService) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Future(io.vertx.core.Future)

Example 9 with ClientErrorException

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

the class CredentialsApiAuthProviderTest method testAuthenticateFailsWith401ForMalformedCredentials.

/**
 * Verifies that the auth provider fails an authentication request with a 401
 * {@code ClientErrorException} if the credentials cannot be parsed.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAuthenticateFailsWith401ForMalformedCredentials(final TestContext ctx) {
    // WHEN trying to authenticate using malformed credentials
    // that do not contain a tenant
    final JsonObject authInfo = new JsonObject().put("username", "no-tenant").put("password", "secret");
    provider.authenticate(authInfo, ctx.asyncAssertFailure(t -> {
        // THEN authentication fails with a 401 client error
        ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ClientErrorException(org.eclipse.hono.client.ClientErrorException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CredentialsClient(org.eclipse.hono.client.CredentialsClient) Rule(org.junit.Rule) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

Example 10 with ClientErrorException

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

the class CredentialsApiAuthProviderTest method testAuthenticateFailsWith401ForNonExistingAuthId.

/**
 * Verifies that the auth provider fails an authentication request with a 401
 * {@code ClientErrorException} if the auth-id is unknown.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAuthenticateFailsWith401ForNonExistingAuthId(final TestContext ctx) {
    // WHEN trying to authenticate using an auth-id that is not known
    final JsonObject authInfo = new JsonObject().put("username", "unknown@TENANT").put("password", "secret");
    when(credentialsClient.isOpen()).thenReturn(Boolean.TRUE);
    when(credentialsClient.get(anyString(), eq("unknown"))).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND)));
    provider.authenticate(authInfo, ctx.asyncAssertFailure(t -> {
        // THEN authentication fails with a 401 client error
        ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) ClientErrorException(org.eclipse.hono.client.ClientErrorException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CredentialsClient(org.eclipse.hono.client.CredentialsClient) Rule(org.junit.Rule) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

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