Search in sources :

Example 1 with Node

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Node(io.gravitee.node.api.Node) UuidString(io.gravitee.rest.api.service.common.UuidString) HttpHeaders(io.gravitee.common.http.HttpHeaders) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Collection(java.util.Collection) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) HttpProviderConfiguration(io.gravitee.rest.api.services.dictionary.provider.http.configuration.HttpProviderConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Objects(java.util.Objects) NodeUtils(io.gravitee.node.api.utils.NodeUtils) JoltMapper(io.gravitee.rest.api.services.dictionary.provider.http.mapper.JoltMapper) Buffer(io.vertx.core.buffer.Buffer) DynamicProperty(io.gravitee.rest.api.services.dictionary.model.DynamicProperty) Provider(io.gravitee.rest.api.services.dictionary.provider.Provider) AsyncResult(io.vertx.core.AsyncResult) URI(java.net.URI) Handler(io.vertx.core.Handler) StringUtils(org.springframework.util.StringUtils) Handler(io.vertx.core.Handler) UuidString(io.gravitee.rest.api.service.common.UuidString) URI(java.net.URI) Collection(java.util.Collection) AsyncResult(io.vertx.core.AsyncResult)

Example 2 with Node

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Node(io.gravitee.node.api.Node) UuidString(io.gravitee.rest.api.service.common.UuidString) HttpHeaders(io.gravitee.common.http.HttpHeaders) Promise(io.vertx.core.Promise) Collection(java.util.Collection) Provider(io.gravitee.rest.api.services.dynamicproperties.provider.Provider) Vertx(io.vertx.core.Vertx) DynamicProperty(io.gravitee.rest.api.services.dynamicproperties.model.DynamicProperty) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) DynamicPropertyService(io.gravitee.definition.model.services.dynamicproperty.DynamicPropertyService) io.vertx.core.http(io.vertx.core.http) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Objects(java.util.Objects) NodeUtils(io.gravitee.node.api.utils.NodeUtils) Buffer(io.vertx.core.buffer.Buffer) HttpDynamicPropertyProviderConfiguration(io.gravitee.definition.model.services.dynamicproperty.http.HttpDynamicPropertyProviderConfiguration) JoltMapper(io.gravitee.rest.api.services.dynamicproperties.provider.http.mapper.JoltMapper) AsyncResult(io.vertx.core.AsyncResult) URI(java.net.URI) Handler(io.vertx.core.Handler) StringUtils(org.springframework.util.StringUtils) Handler(io.vertx.core.Handler) UuidString(io.gravitee.rest.api.service.common.UuidString) URI(java.net.URI) Collection(java.util.Collection) AsyncResult(io.vertx.core.AsyncResult)

Aggregations

HttpHeaders (io.gravitee.common.http.HttpHeaders)2 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)2 Node (io.gravitee.node.api.Node)2 NodeUtils (io.gravitee.node.api.utils.NodeUtils)2 UuidString (io.gravitee.rest.api.service.common.UuidString)2 AsyncResult (io.vertx.core.AsyncResult)2 Handler (io.vertx.core.Handler)2 Promise (io.vertx.core.Promise)2 Vertx (io.vertx.core.Vertx)2 Buffer (io.vertx.core.buffer.Buffer)2 io.vertx.core.http (io.vertx.core.http)2 URI (java.net.URI)2 Collection (java.util.Collection)2 Objects (java.util.Objects)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Function (java.util.function.Function)2 StringUtils (org.springframework.util.StringUtils)2 DynamicPropertyService (io.gravitee.definition.model.services.dynamicproperty.DynamicPropertyService)1 HttpDynamicPropertyProviderConfiguration (io.gravitee.definition.model.services.dynamicproperty.http.HttpDynamicPropertyProviderConfiguration)1 DynamicProperty (io.gravitee.rest.api.services.dictionary.model.DynamicProperty)1