Example usage for io.vertx.core Future succeededFuture

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

Introduction

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

Prototype

static <T> Future<T> succeededFuture(T result) 

Source Link

Document

Created a succeeded future with the specified result.

Usage

From source file:io.knotx.repository.impl.FilesystemRepositoryConnectorProxyImpl.java

License:Apache License

@Override
public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) {
    final String localFilePath = catalogue + StringUtils.stripStart(request.getPath(), "/");
    final Optional<String> contentType = Optional.ofNullable(MimeMapping.getMimeTypeForFilename(localFilePath));

    LOGGER.trace("Fetching file `{}` from local repository.", localFilePath);

    ObservableFuture<Buffer> fileObservable = RxHelper.observableFuture();
    fileObservable/*w w  w  .  j  a va 2s.com*/
            .map(buffer -> new ClientResponse().setStatusCode(HttpResponseStatus.OK.code())
                    .setHeaders(headers(contentType)).setBody(buffer))
            .defaultIfEmpty(new ClientResponse().setStatusCode(HttpResponseStatus.NOT_FOUND.code()))
            .subscribe(response -> result.handle(Future.succeededFuture(response)), error -> {
                LOGGER.error(ERROR_MESSAGE, error);
                result.handle(Future.succeededFuture(processError(error)));
            });

    fileSystem.readFile(localFilePath, fileObservable.toHandler());
}

From source file:io.knotx.repository.impl.RepositoryConnectorProxyImpl.java

License:Apache License

@Override
public void process(ClientRequest request, Handler<AsyncResult<ClientResponse>> result) {
    MultiMap requestHeaders = getFilteredHeaders(request.getHeaders());
    String repoUri = buildRepoUri(request);

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("GET Http Repository: http://{}:{}{} with headers [{}]",
                clientDestination.getString("domain"), clientDestination.getInteger("port"), repoUri,
                DataObjectsUtil.toString(requestHeaders));
    }/*from   w w w. ja  va 2 s . c o m*/

    RxHelper.get(httpClient, clientDestination.getInteger("port"), clientDestination.getString("domain"),
            repoUri, requestHeaders).doOnNext(this::traceHttpResponse).flatMap(this::processResponse)
            .subscribe(response -> result.handle(Future.succeededFuture(response)), error -> {
                LOGGER.error(ERROR_MESSAGE, error);
                result.handle(Future.succeededFuture(toErrorResponse()));
            });
}

From source file:io.knotx.server.MockKnotProxy.java

License:Apache License

@Override
public void process(KnotContext knotContext, Handler<AsyncResult<KnotContext>> result) {
    if (knot != null) {
        knot.call(knotContext);//from  ww  w  .  java 2 s.  com
    }
    result.handle(Future.succeededFuture(knotContext));
}

From source file:io.nonobot.core.client.impl.BotClientImpl.java

License:Apache License

public BotClientImpl(Vertx vertx, Context context, ClientOptions options,
        Handler<AsyncResult<BotClient>> handler) {

    this.name = options.getName();
    this.inboundAddress = "bots." + name + ".inbound";
    this.outboundAddress = "bots." + name + ".outbound";

    // Default names
    alias(Arrays.asList(name, "@" + name));

    vertx.eventBus().<JsonObject>consumer(outboundAddress, msg -> {
        String chatId = msg.body().getString("chatId");
        String body = msg.body().getString("body");
        handle(new Message() {
            @Override//from   w ww.j av  a2  s. co  m
            public String chatId() {
                return chatId;
            }

            @Override
            public String body() {
                return body;
            }
        });
    }).completionHandler(ar -> {
        if (ar.succeeded()) {
            handler.handle(Future.succeededFuture(this));
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });

    this.context = context;
    this.vertx = vertx;
}

From source file:io.reactiverse.pgclient.impl.PgConnectionBase.java

License:Apache License

public C prepare(String sql, Handler<AsyncResult<PgPreparedQuery>> handler) {
    schedule(new PrepareStatementCommand(sql), cr -> {
        if (cr.succeeded()) {
            handler.handle(Future.succeededFuture(new PgPreparedQueryImpl(conn, context, cr.result())));
        } else {/*ww  w .  j  a  v a2  s  .  c om*/
            handler.handle(Future.failedFuture(cr.cause()));
        }
    });
    return (C) this;
}

From source file:io.reactiverse.pgclient.impl.PgConnectionFactory.java

License:Apache License

public void connect(Handler<AsyncResult<SocketConnection>> handler) {
    switch (sslMode) {
    case DISABLE:
        doConnect(false, handler);/*  www. j a  va 2  s . c om*/
        break;
    case ALLOW:
        doConnect(false, ar -> {
            if (ar.succeeded()) {
                handler.handle(Future.succeededFuture(ar.result()));
            } else {
                doConnect(true, handler);
            }
        });
        break;
    case PREFER:
        doConnect(true, ar -> {
            if (ar.succeeded()) {
                handler.handle(Future.succeededFuture(ar.result()));
            } else {
                doConnect(false, handler);
            }
        });
        break;
    case VERIFY_FULL:
        if (hostnameVerificationAlgorithm == null || hostnameVerificationAlgorithm.isEmpty()) {
            handler.handle(Future.failedFuture(new IllegalArgumentException(
                    "Host verification algorithm must be specified under verify-full sslmode")));
            return;
        }
    case VERIFY_CA:
        if (trustOptions == null) {
            handler.handle(Future.failedFuture(new IllegalArgumentException(
                    "Trust options must be specified under verify-full or verify-ca sslmode")));
            return;
        }
    case REQUIRE:
        doConnect(true, handler);
        break;
    default:
        throw new IllegalArgumentException("Unsupported SSL mode");
    }
}

From source file:io.reactiverse.pgclient.impl.PgConnectionFactory.java

License:Apache License

private void doConnect(boolean ssl, Handler<AsyncResult<SocketConnection>> handler) {
    if (Vertx.currentContext() != ctx) {
        throw new IllegalStateException();
    }//from   w  w w. j  a  v  a  2 s . c o m
    SocketAddress socketAddress;
    if (!isUsingDomainSocket) {
        socketAddress = SocketAddress.inetSocketAddress(port, host);
    } else {
        socketAddress = SocketAddress.domainSocketAddress(host + "/.s.PGSQL." + port);
    }

    Future<NetSocket> future = Future.<NetSocket>future().setHandler(ar -> {
        if (ar.succeeded()) {
            NetSocketInternal socket = (NetSocketInternal) ar.result();
            SocketConnection conn = newSocketConnection(socket);

            if (ssl && !isUsingDomainSocket) {
                // upgrade connection to SSL if needed
                conn.upgradeToSSLConnection(ar2 -> {
                    if (ar2.succeeded()) {
                        handler.handle(Future.succeededFuture(conn));
                    } else {
                        handler.handle(Future.failedFuture(ar2.cause()));
                    }
                });
            } else {
                handler.handle(Future.succeededFuture(conn));
            }
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });

    try {
        client.connect(socketAddress, null, future);
    } catch (Exception e) {
        // Client is closed
        future.fail(e);
    }
}

From source file:io.reactiverse.pgclient.impl.PgPoolImpl.java

License:Apache License

@Override
public void begin(Handler<AsyncResult<PgTransaction>> handler) {
    getConnection(ar -> {//  w w w. j  a va  2 s .  c o  m
        if (ar.succeeded()) {
            PgConnectionImpl conn = (PgConnectionImpl) ar.result();
            PgTransaction tx = conn.begin(true);
            handler.handle(Future.succeededFuture(tx));
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });
}

From source file:io.reactiverse.pgclient.PgClient.java

License:Apache License

/**
 * Connects to the database and returns the connection if that succeeds.
 * <p/>/*from  ww w. jav a2 s.c  o m*/
 * The connection interracts directly with the database is not a proxy, so closing the
 * connection will close the underlying connection to the database.
 *
 * @param vertx the vertx instance
 * @param options the connect options
 * @param handler the handler called with the connection or the failure
 */
static void connect(Vertx vertx, PgConnectOptions options, Handler<AsyncResult<PgConnection>> handler) {
    Context ctx = Vertx.currentContext();
    if (ctx != null) {
        PgConnectionFactory client = new PgConnectionFactory(ctx, false, options);
        client.create(ar -> {
            if (ar.succeeded()) {
                Connection conn = ar.result();
                PgConnectionImpl p = new PgConnectionImpl(client, ctx, conn);
                conn.init(p);
                handler.handle(Future.succeededFuture(p));
            } else {
                handler.handle(Future.failedFuture(ar.cause()));
            }
        });
    } else {
        vertx.runOnContext(v -> {
            if (options.isUsingDomainSocket() && !vertx.isNativeTransportEnabled()) {
                handler.handle(Future.failedFuture("Native transport is not available"));
            } else {
                connect(vertx, options, handler);
            }
        });
    }
}

From source file:io.reactiverse.pgclient.pool.ConnectionQueue.java

License:Apache License

void connect(SimpleConnection conn) {
    poll().handle(Future.succeededFuture(conn));
}