Example usage for io.vertx.core.http HttpClient request

List of usage examples for io.vertx.core.http HttpClient request

Introduction

In this page you can find the example usage for io.vertx.core.http HttpClient request.

Prototype

HttpClientRequest request(HttpMethod method, String requestURI,
        Handler<AsyncResult<HttpClientResponse>> responseHandler);

Source Link

Document

Create an HTTP request to send to the server at the default host and port, specifying a response handler to receive the response

Usage

From source file:com.hubrick.vertx.rest.impl.DefaultRestClientRequest.java

License:Apache License

public DefaultRestClientRequest(Vertx vertx, DefaultRestClient restClient, HttpClient httpClient,
        List<HttpMessageConverter> httpMessageConverters, HttpMethod method, String uri, Class<T> responseClass,
        Handler<RestClientResponse<T>> responseHandler, Long timeoutInMillis,
        RequestCacheOptions requestCacheOptions, MultiMap globalHeaders,
        @Nullable Handler<Throwable> exceptionHandler) {
    checkNotNull(vertx, "vertx must not be null");
    checkNotNull(restClient, "restClient must not be null");
    checkNotNull(httpClient, "httpClient must not be null");
    checkNotNull(httpMessageConverters, "dataMappers must not be null");
    checkArgument(!httpMessageConverters.isEmpty(), "dataMappers must not be empty");
    checkNotNull(globalHeaders, "globalHeaders must not be null");

    this.vertx = vertx;
    this.restClient = restClient;
    this.httpClient = httpClient;
    this.method = method;
    this.uri = uri;
    this.httpMessageConverters = httpMessageConverters;
    this.responseHandler = responseHandler;
    this.globalHeaders = globalHeaders;

    httpClientRequest = httpClient.request(method, uri, (HttpClientResponse httpClientResponse) -> {
        handleResponse(httpClientResponse, responseClass);
    });/*from  ww  w  . j ava2s .c o  m*/

    this.requestCacheOptions = requestCacheOptions;
    this.timeoutInMillis = timeoutInMillis;

    if (exceptionHandler != null) {
        exceptionHandler(exceptionHandler);
    }
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example33(Vertx vertx) {
    HttpClient client = vertx.createHttpClient();

    client.request(HttpMethod.GET, "some-uri", response -> {
        System.out.println("Received response with status code " + response.statusCode());
    }).end();/*w  ww .j a  v  a2  s  .c  o m*/

    client.request(HttpMethod.POST, "foo-uri", response -> {
        System.out.println("Received response with status code " + response.statusCode());
    }).end("some-data");
}

From source file:org.entcore.feeder.dictionary.structures.PostImport.java

License:Open Source License

private void wsCall(JsonObject object) {
    for (String url : object.fieldNames()) {
        final JsonArray endpoints = object.getJsonArray(url);
        if (endpoints == null || endpoints.size() < 1)
            continue;
        try {/*from  www .  j a  v  a2  s .c  o m*/
            final URI uri = new URI(url);
            HttpClientOptions options = new HttpClientOptions().setDefaultHost(uri.getHost())
                    .setDefaultPort(uri.getPort()).setMaxPoolSize(16).setSsl("https".equals(uri.getScheme()))
                    .setConnectTimeout(10000).setKeepAlive(false);
            final HttpClient client = vertx.createHttpClient(options);

            final Handler[] handlers = new Handler[endpoints.size() + 1];
            handlers[handlers.length - 1] = new Handler<Void>() {
                @Override
                public void handle(Void v) {
                    client.close();
                }
            };
            for (int i = endpoints.size() - 1; i >= 0; i--) {
                final int ji = i;
                handlers[i] = new Handler<Void>() {
                    @Override
                    public void handle(Void v) {
                        final JsonObject j = endpoints.getJsonObject(ji);
                        logger.info("endpoint : " + j.encode());
                        final HttpClientRequest req = client.request(HttpMethod.valueOf(j.getString("method")),
                                j.getString("uri"), new Handler<HttpClientResponse>() {
                                    @Override
                                    public void handle(HttpClientResponse resp) {
                                        if (resp.statusCode() >= 300) {
                                            logger.warn("Endpoint " + j.encode() + " error : "
                                                    + resp.statusCode() + " " + resp.statusMessage());
                                        }
                                        handlers[ji + 1].handle(null);
                                    }
                                });
                        JsonObject headers = j.getJsonObject("headers");
                        if (headers != null && headers.size() > 0) {
                            for (String h : headers.fieldNames()) {
                                req.putHeader(h, headers.getString(h));
                            }
                        }
                        req.exceptionHandler(
                                e -> logger.error("Error in ws call post import : " + j.encode(), e));
                        if (j.getString("body") != null) {
                            req.end(j.getString("body"));
                        } else {
                            req.end();
                        }
                    }
                };
            }
            handlers[0].handle(null);
        } catch (URISyntaxException e) {
            logger.error("Invalid uri in ws call after import : " + url, e);
        }
    }
}