Search in sources :

Example 1 with ServiceInvocationException

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

the class CrudHttpClient method delete.

/**
 * Deletes a resource using HTTP DELETE.
 *
 * @param uri The resource to delete.
 * @param successPredicate A predicate on the returned HTTP status code for determining success.
 * @return A future that will succeed if the predicate evaluates to {@code true}.
 */
public Future<Void> delete(final String uri, final Predicate<Integer> successPredicate) {
    final Future<Void> result = Future.future();
    vertx.createHttpClient().delete(port, host, uri).handler(response -> {
        if (successPredicate.test(response.statusCode())) {
            result.complete();
        } else {
            result.fail(new ServiceInvocationException(response.statusCode()));
        }
    }).exceptionHandler(result::fail).end();
    return result;
}
Also used : ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException)

Example 2 with ServiceInvocationException

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

the class CrudHttpClient method update.

/**
 * Updates a resource using HTTP PUT.
 *
 * @param uri The resource to update.
 * @param body The content to update the resource with.
 * @param contentType The content type to set in the request.
 * @param successPredicate A predicate on the returned HTTP status code for determining success.
 * @return A future that will succeed if the predicate evaluates to {@code true}.
 */
public Future<Void> update(final String uri, final Buffer body, final String contentType, final Predicate<Integer> successPredicate) {
    final Future<Void> result = Future.future();
    final HttpClientRequest req = vertx.createHttpClient().put(port, host, uri).handler(response -> {
        if (successPredicate.test(response.statusCode())) {
            result.complete();
        } else {
            result.fail(new ServiceInvocationException(response.statusCode()));
        }
    }).exceptionHandler(result::fail);
    if (contentType != null) {
        req.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
    }
    if (body == null) {
        req.end();
    } else {
        req.end(body);
    }
    return result;
}
Also used : Objects(java.util.Objects) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Buffer(io.vertx.core.buffer.Buffer) Predicate(java.util.function.Predicate) Vertx(io.vertx.core.Vertx) Optional(java.util.Optional) HttpHeaders(io.vertx.core.http.HttpHeaders) JsonObject(io.vertx.core.json.JsonObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Future(io.vertx.core.Future) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException)

Example 3 with ServiceInvocationException

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

the class CrudHttpClient method create.

/**
 * Creates a resource using HTTP POST.
 *
 * @param uri The URI to post to.
 * @param body The body to post (may be {@code null}).
 * @param contentType The content type to set in the request (may be {@code null}).
 * @param successPredicate A predicate on the returned HTTP status code for determining success.
 * @return A future that will succeed if the predicate evaluates to {@code true}.
 */
public Future<Void> create(final String uri, final Buffer body, final String contentType, final Predicate<HttpClientResponse> successPredicate) {
    Objects.requireNonNull(uri);
    Objects.requireNonNull(successPredicate);
    final Future<Void> result = Future.future();
    final HttpClientRequest req = vertx.createHttpClient().post(port, host, uri).handler(response -> {
        if (successPredicate.test(response)) {
            result.complete();
        } else {
            result.fail(new ServiceInvocationException(response.statusCode()));
        }
    }).exceptionHandler(result::fail);
    if (contentType != null) {
        req.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
    }
    if (body == null) {
        req.end();
    } else {
        req.end(body);
    }
    return result;
}
Also used : Objects(java.util.Objects) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Buffer(io.vertx.core.buffer.Buffer) Predicate(java.util.function.Predicate) Vertx(io.vertx.core.Vertx) Optional(java.util.Optional) HttpHeaders(io.vertx.core.http.HttpHeaders) JsonObject(io.vertx.core.json.JsonObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Future(io.vertx.core.Future) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException)

Example 4 with ServiceInvocationException

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

the class HttpTestBase method testUploadMessageFailsForDisabledTenant.

/**
 * Verifies that the HTTP adapter rejects messages from a device
 * that belongs to a tenant for which the HTTP adapter has been disabled.
 *
 * @param ctx The test context
 */
@Test
public void testUploadMessageFailsForDisabledTenant(final TestContext ctx) {
    // GIVEN a tenant for which the HTTP adapter is disabled
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final JsonObject adapterDetailsHttp = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, Constants.PROTOCOL_ADAPTER_TYPE_HTTP).put(TenantConstants.FIELD_ENABLED, Boolean.FALSE);
    final TenantObject tenant = TenantObject.from(tenantId, true);
    tenant.addAdapterConfiguration(adapterDetailsHttp);
    final Async setup = ctx.async();
    helper.registry.addTenant(JsonObject.mapFrom(tenant)).compose(ok -> helper.registry.registerDevice(tenantId, deviceId)).setHandler(ctx.asyncAssertSuccess(ok -> setup.complete()));
    setup.await();
    // WHEN a device that belongs to the tenant uploads a message
    send(tenantId, deviceId, Buffer.buffer("hello")).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the message gets rejected by the HTTP adapter
        ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) TenantConstants(org.eclipse.hono.util.TenantConstants) LoggerFactory(org.slf4j.LoggerFactory) MessageConsumer(org.eclipse.hono.client.MessageConsumer) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) CrudHttpClient(org.eclipse.hono.tests.CrudHttpClient) TenantObject(org.eclipse.hono.util.TenantObject) Async(io.vertx.ext.unit.Async) JsonObject(io.vertx.core.json.JsonObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

Example 5 with ServiceInvocationException

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

the class BaseTenantServiceTest method testAddFailsForEmptyAdapterArray.

/**
 * Verifies that the base service fails for a payload that defines an empty adapter array (must be null or has to
 * contain at least one element).
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testAddFailsForEmptyAdapterArray(final TestContext ctx) {
    final JsonObject testPayload = createValidTenantPayload();
    testPayload.put(TenantConstants.FIELD_ADAPTERS, new JsonArray());
    final EventBusMessage msg = createRequest(TenantConstants.TenantAction.add, testPayload);
    tenantService.processRequest(msg).setHandler(ctx.asyncAssertFailure(t -> {
        ctx.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode());
    }));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) TenantResult(org.eclipse.hono.util.TenantResult) BeforeClass(org.junit.BeforeClass) TenantConstants(org.eclipse.hono.util.TenantConstants) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EventBusMessage(org.eclipse.hono.util.EventBusMessage) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) JsonArray(io.vertx.core.json.JsonArray) Timeout(org.junit.rules.Timeout) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) EventBusMessage(org.eclipse.hono.util.EventBusMessage) JsonObject(io.vertx.core.json.JsonObject) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Test(org.junit.Test)

Aggregations

ServiceInvocationException (org.eclipse.hono.client.ServiceInvocationException)19 Future (io.vertx.core.Future)17 JsonObject (io.vertx.core.json.JsonObject)17 HttpURLConnection (java.net.HttpURLConnection)15 TestContext (io.vertx.ext.unit.TestContext)11 EventBusMessage (org.eclipse.hono.util.EventBusMessage)11 BeforeClass (org.junit.BeforeClass)11 Test (org.junit.Test)11 Timeout (org.junit.rules.Timeout)11 AsyncResult (io.vertx.core.AsyncResult)10 Handler (io.vertx.core.Handler)10 ServiceConfigProperties (org.eclipse.hono.config.ServiceConfigProperties)10 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)9 RunWith (org.junit.runner.RunWith)9 Vertx (io.vertx.core.Vertx)7 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)7 CredentialsObject (org.eclipse.hono.util.CredentialsObject)7 Buffer (io.vertx.core.buffer.Buffer)6 Objects (java.util.Objects)6 Optional (java.util.Optional)6