use of io.gravitee.node.api.Node in project gravitee-management-rest-api by gravitee-io.
the class HttpProvider method get.
@Override
public CompletableFuture<Collection<DynamicProperty>> get() {
Promise<Buffer> promise = Promise.promise();
URI requestUri = URI.create(configuration.getUrl());
boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());
final HttpClientOptions clientOptions = new HttpClientOptions().setSsl(ssl).setTrustAll(true).setMaxPoolSize(1).setKeepAlive(false).setTcpKeepAlive(false).setConnectTimeout(2000);
final HttpClient httpClient = vertx.createHttpClient(clientOptions);
final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
String relativeUri = (requestUri.getRawQuery() == null) ? requestUri.getRawPath() : requestUri.getRawPath() + '?' + requestUri.getRawQuery();
RequestOptions options = new RequestOptions().setMethod(configuration.getMethod() != null ? configuration.getMethod() : HttpMethod.GET).setHost(requestUri.getHost()).setPort(port).setURI(relativeUri);
// headers
options.putHeader(HttpHeaders.USER_AGENT, NodeUtils.userAgent(node));
options.putHeader("X-Gravitee-Request-Id", UuidString.generateRandom());
if (configuration.getHeaders() != null) {
configuration.getHeaders().forEach(httpHeader -> options.putHeader(httpHeader.getName(), httpHeader.getValue()));
}
httpClient.request(options).onFailure(new Handler<Throwable>() {
@Override
public void handle(Throwable event) {
promise.fail(event);
// Close client
httpClient.close();
}
}).onSuccess(new Handler<HttpClientRequest>() {
@Override
public void handle(HttpClientRequest request) {
request.response(new Handler<AsyncResult<HttpClientResponse>>() {
@Override
public void handle(AsyncResult<HttpClientResponse> asyncResponse) {
if (asyncResponse.failed()) {
promise.fail(asyncResponse.cause());
// Close client
httpClient.close();
} else {
final HttpClientResponse response = asyncResponse.result();
if (response.statusCode() == HttpStatusCode.OK_200) {
response.bodyHandler(buffer -> {
promise.complete(buffer);
// Close client
httpClient.close();
});
} else {
promise.complete(null);
// Close client
httpClient.close();
}
}
}
}).exceptionHandler(new Handler<Throwable>() {
@Override
public void handle(Throwable throwable) {
promise.fail(throwable);
// Close client
httpClient.close();
}
});
if (!StringUtils.isEmpty(configuration.getBody())) {
request.end(configuration.getBody());
} else {
request.end();
}
}
});
return promise.future().map(new Function<Buffer, Collection<DynamicProperty>>() {
@Override
public Collection<DynamicProperty> apply(Buffer buffer) {
if (buffer == null) {
return null;
}
return mapper.map(buffer.toString());
}
}).toCompletionStage().toCompletableFuture();
}
use of io.gravitee.node.api.Node in project gravitee-management-rest-api by gravitee-io.
the class HttpProvider method get.
@Override
public CompletableFuture<Collection<DynamicProperty>> get() {
Promise<Buffer> promise = Promise.promise();
URI requestUri = URI.create(dpConfiguration.getUrl());
boolean ssl = HTTPS_SCHEME.equalsIgnoreCase(requestUri.getScheme());
final HttpClientOptions clientOptions = new HttpClientOptions().setSsl(ssl).setTrustAll(true).setMaxPoolSize(1).setKeepAlive(false).setTcpKeepAlive(false).setConnectTimeout(2000);
final HttpClient httpClient = vertx.createHttpClient(clientOptions);
final int port = requestUri.getPort() != -1 ? requestUri.getPort() : (HTTPS_SCHEME.equals(requestUri.getScheme()) ? 443 : 80);
String relativeUri = (requestUri.getRawQuery() == null) ? requestUri.getRawPath() : requestUri.getRawPath() + '?' + requestUri.getRawQuery();
RequestOptions options = new RequestOptions().setMethod(HttpMethod.valueOf(dpConfiguration.getMethod().name())).setHost(requestUri.getHost()).setPort(port).setURI(relativeUri);
// headers
options.putHeader(HttpHeaders.USER_AGENT, NodeUtils.userAgent(node));
options.putHeader("X-Gravitee-Request-Id", UuidString.generateRandom());
if (dpConfiguration.getHeaders() != null) {
dpConfiguration.getHeaders().forEach(httpHeader -> options.putHeader(httpHeader.getName(), httpHeader.getValue()));
}
httpClient.request(options).onFailure(new Handler<Throwable>() {
@Override
public void handle(Throwable event) {
promise.fail(event);
// Close client
httpClient.close();
}
}).onSuccess(new Handler<HttpClientRequest>() {
@Override
public void handle(HttpClientRequest request) {
request.response(new Handler<AsyncResult<HttpClientResponse>>() {
@Override
public void handle(AsyncResult<HttpClientResponse> asyncResponse) {
if (asyncResponse.failed()) {
promise.fail(asyncResponse.cause());
// Close client
httpClient.close();
} else {
final HttpClientResponse response = asyncResponse.result();
if (response.statusCode() == HttpStatusCode.OK_200) {
response.bodyHandler(buffer -> {
promise.complete(buffer);
// Close client
httpClient.close();
});
} else {
promise.complete(null);
// Close client
httpClient.close();
}
}
}
}).exceptionHandler(new Handler<Throwable>() {
@Override
public void handle(Throwable throwable) {
promise.fail(throwable);
// Close client
httpClient.close();
}
});
if (!StringUtils.isEmpty(dpConfiguration.getBody())) {
request.end(dpConfiguration.getBody());
} else {
request.end();
}
}
});
return promise.future().map(new Function<Buffer, Collection<DynamicProperty>>() {
@Override
public Collection<DynamicProperty> apply(Buffer buffer) {
if (buffer == null) {
return null;
}
return mapper.map(buffer.toString());
}
}).toCompletionStage().toCompletableFuture();
}
Aggregations