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