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