Example usage for io.vertx.core Future future

List of usage examples for io.vertx.core Future future

Introduction

In this page you can find the example usage for io.vertx.core Future future.

Prototype

future

Source Link

Usage

From source file:org.eclipse.hono.service.tenant.BaseTenantService.java

License:Open Source License

private Future<EventBusMessage> processGetByCaRequest(final EventBusMessage request, final String subjectDn) {

    try {//  ww w  .j  a  v a2  s  .  co  m
        final X500Principal dn = new X500Principal(subjectDn);
        log.debug("retrieving tenant [subject DN: {}]", subjectDn);
        final Future<TenantResult<JsonObject>> getResult = Future.future();
        get(dn, getResult.completer());
        return getResult.map(tr -> {
            final EventBusMessage response = request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload())
                    .setCacheDirective(tr.getCacheDirective());
            if (tr.isOk() && tr.getPayload() != null) {
                response.setTenant((String) getTypesafeValueForField(tr.getPayload(),
                        TenantConstants.FIELD_PAYLOAD_TENANT_ID));
            }
            return response;
        });
    } catch (final IllegalArgumentException e) {
        // the given subject DN is invalid
        log.debug("cannot parse subject DN [{}] provided by client", subjectDn);
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    }
}

From source file:org.eclipse.hono.service.tenant.BaseTenantService.java

License:Open Source License

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());
        });//from  w ww.j a  v a  2s  . c o m
    } else {
        log.debug("request contains malformed payload");
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    }
}

From source file:org.eclipse.hono.service.tenant.BaseTenantService.java

License:Open Source License

private Future<EventBusMessage> processUpdateRequest(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("updating tenant [{}]", tenantId);
        final Future<TenantResult<JsonObject>> updateResult = Future.future();
        addNotPresentFieldsWithDefaultValuesForTenant(payload);
        update(tenantId, payload, updateResult.completer());
        return updateResult.map(tr -> {
            return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload())
                    .setCacheDirective(tr.getCacheDirective());
        });/*from  w w  w. ja va 2s. c  o  m*/
    } else {
        log.debug("request contains malformed payload");
        return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    }
}

From source file:org.eclipse.hono.service.tenant.BaseTenantService.java

License:Open Source License

private Future<EventBusMessage> processRemoveRequest(final EventBusMessage request) {

    final String tenantId = request.getTenant();

    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 {//  w  ww.  j  av a  2 s .  c  om
        log.debug("deleting tenant [{}]", tenantId);
        final Future<TenantResult<JsonObject>> removeResult = Future.future();
        remove(tenantId, removeResult.completer());
        return removeResult.map(tr -> {
            return request.getResponse(tr.getStatus()).setJsonPayload(tr.getPayload())
                    .setCacheDirective(tr.getCacheDirective());
        });
    }
}

From source file:org.eclipse.hono.tests.CrudHttpClient.java

License:Open Source License

/**
 * Gets options for a resource using an HTTP OPTIONS request.
 * //from   w  ww .  j a v a 2 s.co m
 * @param requestOptions The options to use for the request.
 * @param requestHeaders The headers to include 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}. In that case the
 *         future will contain the response headers.
 * @throws NullPointerException if options or predicate are {@code null}.
 */
public Future<MultiMap> options(final RequestOptions requestOptions, final MultiMap requestHeaders,
        final Predicate<Integer> successPredicate) {

    Objects.requireNonNull(requestOptions);
    Objects.requireNonNull(successPredicate);

    final Future<MultiMap> result = Future.future();

    context.runOnContext(go -> {
        final HttpClientRequest req = client.options(requestOptions).handler(response -> {
            if (successPredicate.test(response.statusCode())) {
                result.complete(response.headers());
            } else {
                result.fail(new ServiceInvocationException(response.statusCode()));
            }
        }).exceptionHandler(result::fail);

        if (requestHeaders != null) {
            req.headers().addAll(requestHeaders);
        }
        req.end();
    });
    return result;
}

From source file:org.eclipse.hono.tests.CrudHttpClient.java

License:Open Source License

/**
 * Creates a resource using an HTTP POST request.
 * //from   w ww . j  av  a2 s  .  co m
 * @param requestOptions The options to use for the request.
 * @param body The body to post (may be {@code null}).
 * @param requestHeaders The headers to include 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}.
 * @throws NullPointerException if options or predicate are {@code null}.
 */
public Future<MultiMap> create(final RequestOptions requestOptions, final Buffer body,
        final MultiMap requestHeaders, final Predicate<Integer> successPredicate) {

    Objects.requireNonNull(requestOptions);
    Objects.requireNonNull(successPredicate);

    final Future<MultiMap> result = Future.future();

    context.runOnContext(go -> {
        final HttpClientRequest req = client.post(requestOptions).handler(response -> {
            if (successPredicate.test(response.statusCode())) {
                result.complete(response.headers());
            } else {
                result.fail(new ServiceInvocationException(response.statusCode()));
            }
        }).exceptionHandler(result::fail);

        if (requestHeaders != null) {
            req.headers().addAll(requestHeaders);
        }
        if (body == null) {
            req.end();
        } else {
            req.end(body);
        }
    });
    return result;
}

From source file:org.eclipse.hono.tests.CrudHttpClient.java

License:Open Source License

/**
 * Updates a resource using an HTTP PUT request.
 * /*  w  w  w . j a v a  2s  . c  om*/
 * @param requestOptions The options to use for the request.
 * @param body The content to update the resource with.
 * @param requestHeaders The headers to include in the request.
 * @param successPredicate A predicate on the response for determining success.
 * @return A future that will succeed if the predicate evaluates to {@code true}.
 * @throws NullPointerException if options or predicate are {@code null}.
 */
public Future<MultiMap> update(final RequestOptions requestOptions, final Buffer body,
        final MultiMap requestHeaders, final Predicate<Integer> successPredicate) {

    Objects.requireNonNull(requestOptions);
    Objects.requireNonNull(successPredicate);

    final Future<MultiMap> result = Future.future();

    context.runOnContext(go -> {
        final HttpClientRequest req = client.put(requestOptions).handler(response -> {
            if (successPredicate.test(response.statusCode())) {
                result.complete(response.headers());
            } else {
                result.fail(new ServiceInvocationException(response.statusCode()));
            }
        }).exceptionHandler(result::fail);

        if (requestHeaders != null) {
            req.headers().addAll(requestHeaders);
        }
        if (body == null) {
            req.end();
        } else {
            req.end(body);
        }
    });
    return result;
}

From source file:org.eclipse.hono.tests.CrudHttpClient.java

License:Open Source License

/**
 * Retrieves a resource representation using an HTTP GET request.
 * //from  ww w.ja  v a  2  s  .  c o  m
 * @param requestOptions The options to use for 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}. In that case the
 *         future will contain the response body.
 * @throws NullPointerException if options or predicate are {@code null}.
 */
public Future<Buffer> get(final RequestOptions requestOptions, final Predicate<Integer> successPredicate) {

    Objects.requireNonNull(requestOptions);
    Objects.requireNonNull(successPredicate);

    final Future<Buffer> result = Future.future();

    client.get(requestOptions).handler(response -> {
        if (successPredicate.test(response.statusCode())) {
            response.bodyHandler(body -> result.complete(body));
        } else {
            result.fail(new ServiceInvocationException(response.statusCode()));
        }
    }).exceptionHandler(result::fail).end();

    return result;
}

From source file:org.eclipse.hono.tests.CrudHttpClient.java

License:Open Source License

/**
 * Deletes a resource using an HTTP DELETE request.
 * //from ww  w  .  ja  v  a2  s . co m
 * @param requestOptions The options to use for 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}.
 * @throws NullPointerException if options or predicate are {@code null}.
 */
public Future<Void> delete(final RequestOptions requestOptions, final Predicate<Integer> successPredicate) {

    Objects.requireNonNull(requestOptions);
    Objects.requireNonNull(successPredicate);

    final Future<Void> result = Future.future();

    context.runOnContext(go -> {
        client.delete(requestOptions).handler(response -> {
            if (successPredicate.test(response.statusCode())) {
                result.complete();
            } else {
                result.fail(new ServiceInvocationException(response.statusCode()));
            }
        }).exceptionHandler(result::fail).end();
    });

    return result;
}

From source file:org.eclipse.hono.tests.mqtt.EventMqttIT.java

License:Open Source License

@Override
protected Future<Void> send(final String tenantId, final String deviceId, final Buffer payload,
        final boolean useShortTopicName) {

    final String topic = String.format(TOPIC_TEMPLATE,
            useShortTopicName ? EventConstants.EVENT_ENDPOINT_SHORT : EventConstants.EVENT_ENDPOINT, tenantId,
            deviceId);/*from   ww  w.j  ava2s. co  m*/
    final Future<Void> result = Future.future();
    mqttClient.publishCompletionHandler(id -> result.complete());
    mqttClient.publish(topic, payload, MqttQoS.AT_LEAST_ONCE, false, false);
    return result;
}