Example usage for io.vertx.core Future map

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

Introduction

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

Prototype

default <V> Future<V> map(V value) 

Source Link

Document

Map the result of a future to a specific value .

When this future succeeds, this value will complete the future returned by this method call.

When this future fails, the failure will be propagated to the returned future.

Usage

From source file:org.eclipse.hono.client.impl.CommandClientImpl.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w w w .jav a2  s . c  o  m*/
 */
@Override
public Future<Buffer> sendCommand(final String command, final Buffer data) {

    Objects.requireNonNull(command);
    Objects.requireNonNull(data);

    final Future<BufferResult> responseTracker = Future.future();
    createAndSendRequest(command, null, data, null, responseTracker.completer(), null);

    return responseTracker.map(response -> {
        if (response.isOk()) {
            return response.getPayload();
        } else {
            throw StatusCodeMapper.from(response);
        }
    });
}

From source file:org.eclipse.hono.deviceregistry.FileBasedTenantService.java

License:Open Source License

Future<Void> saveToFile() {

    if (!getConfig().isSaveToFile()) {
        return Future.succeededFuture();
    } else if (dirty) {
        return checkFileExists(true).compose(s -> {

            final JsonArray tenantsJson = new JsonArray();
            tenants.values().stream().forEach(tenant -> {
                tenantsJson.add(JsonObject.mapFrom(tenant));
            });//from w  w  w .j  a  v  a2  s .c  om

            final Future<Void> writeHandler = Future.future();
            vertx.fileSystem().writeFile(getConfig().getFilename(),
                    Buffer.factory.buffer(tenantsJson.encodePrettily()), writeHandler.completer());
            return writeHandler.map(ok -> {
                dirty = false;
                log.trace("successfully wrote {} tenants to file {}", tenantsJson.size(),
                        getConfig().getFilename());
                return (Void) null;
            }).otherwise(t -> {
                log.warn("could not write tenants to file {}", getConfig().getFilename(), t);
                return (Void) null;
            });
        });
    } else {
        log.trace("tenants registry does not need to be persisted");
        return Future.succeededFuture();
    }
}

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

License:Open Source License

private Future<EventBusMessage> processGetByIdRequest(final EventBusMessage request, final String 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());
    });/*from  www . j  a va 2  s.  com*/
}

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 {/*from  ww w  .j  a  v a2s .  c o 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());
        });//  w ww.  j a va2 s.  co 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 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> 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 {// www  .j  a  v  a2s .  c  o m
        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());
        });
    }
}