Search in sources :

Example 1 with VertxCompletableFuture

use of io.gravitee.management.services.dynamicproperties.provider.http.vertx.VertxCompletableFuture in project gravitee-management-rest-api by gravitee-io.

the class HttpProvider method get.

@Override
public CompletableFuture<Collection<DynamicProperty>> get() {
    CompletableFuture<Buffer> future = new VertxCompletableFuture<>(vertx);
    URI requestUri = URI.create(dpConfiguration.getUrl());
    boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());
    final HttpClientOptions options = new HttpClientOptions().setSsl(ssl).setTrustAll(true).setMaxPoolSize(1).setKeepAlive(false).setTcpKeepAlive(false).setConnectTimeout(2000);
    final HttpClient httpClient = vertx.createHttpClient(options);
    final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
    try {
        HttpClientRequest request = httpClient.request(HttpMethod.GET, port, requestUri.getHost(), requestUri.toString());
        request.handler(response -> {
            if (response.statusCode() == HttpStatusCode.OK_200) {
                response.bodyHandler(buffer -> {
                    future.complete(buffer);
                    // Close client
                    httpClient.close();
                });
            } else {
                future.complete(null);
            }
        });
        request.exceptionHandler(event -> {
            try {
                future.completeExceptionally(event);
                // Close client
                httpClient.close();
            } catch (IllegalStateException ise) {
            // Do not take care about exception when closing client
            }
        });
        request.end();
    } catch (Exception ex) {
        logger.error("Unable to look for dynamic properties", ex);
        future.completeExceptionally(ex);
    }
    return future.thenApply(buffer -> {
        if (buffer == null) {
            return null;
        }
        return mapper.map(buffer.toString());
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) VertxCompletableFuture(io.gravitee.management.services.dynamicproperties.provider.http.vertx.VertxCompletableFuture) URI(java.net.URI) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Aggregations

VertxCompletableFuture (io.gravitee.management.services.dynamicproperties.provider.http.vertx.VertxCompletableFuture)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpClient (io.vertx.core.http.HttpClient)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 URI (java.net.URI)1