Search in sources :

Example 71 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    // Note! in real-life you wouldn't often set trust all to true as it could leave you open to man in the middle attacks.
    HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setTrustAll(true);
    vertx.createHttpClient(options).getNow(8443, "localhost", "/", resp -> {
        System.out.println("Got response " + resp.statusCode() + " with protocol " + resp.version());
        resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1")));
    });
}
Also used : HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 72 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    // Note! in real-life you wouldn't often set trust all to true as it could leave you open to man in the middle attacks.
    HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setTrustAll(true);
    HttpClientRequest request = vertx.createHttpClient(options).get(8443, "localhost", "/");
    request.handler(resp -> {
        // Print custom frames received from server
        resp.customFrameHandler(frame -> {
            System.out.println("Got frame from server " + frame.payload().toString("UTF-8"));
        });
    });
    request.sendHead(version -> {
        // Once head has been sent we can send custom frames
        vertx.setPeriodic(1000, timerID -> {
            System.out.println("Sending ping frame to server");
            request.writeCustomFrame(10, 0, Buffer.buffer("ping"));
        });
    });
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 73 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions 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)

Example 74 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-web by vert-x3.

the class StaticHandlerTest method testNoHttp2Push.

@Test
public void testNoHttp2Push() throws Exception {
    stat.setWebRoot("webroot/somedir3");
    router.route().handler(stat);
    HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem")));
    http2Server.requestHandler(router).listen(8443);
    HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem"));
    HttpClient client = vertx.createHttpClient(options);
    HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> {
        assertEquals(200, resp.statusCode());
        assertEquals(HttpVersion.HTTP_2, resp.version());
        resp.bodyHandler(this::assertNotNull);
        testComplete();
    });
    request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> {
        fail();
    }));
    request.end();
    await();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Utils(io.vertx.ext.web.impl.Utils) HttpVersion(io.vertx.core.http.HttpVersion) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) WebTestBase(io.vertx.ext.web.WebTestBase) DateFormat(java.text.DateFormat) Set(java.util.Set) Test(org.junit.Test) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Http2PushMapping(io.vertx.ext.web.Http2PushMapping) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 75 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-web by vert-x3.

the class StaticHandlerTest method testHttp2Push.

@Test
public void testHttp2Push() throws Exception {
    List<Http2PushMapping> mappings = new ArrayList<>();
    mappings.add(new Http2PushMapping("style.css", "style", false));
    mappings.add(new Http2PushMapping("coin.png", "image", false));
    stat.setHttp2PushMapping(mappings).setWebRoot("webroot/somedir3");
    router.route().handler(stat);
    HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem")));
    http2Server.requestHandler(router).listen(8443);
    HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem"));
    HttpClient client = vertx.createHttpClient(options);
    HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> {
        assertEquals(200, resp.statusCode());
        assertEquals(HttpVersion.HTTP_2, resp.version());
        resp.bodyHandler(this::assertNotNull);
    });
    CountDownLatch latch = new CountDownLatch(2);
    request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> {
        assertNotNull(pushedResp);
        pushedResp.bodyHandler(this::assertNotNull);
        latch.countDown();
    }));
    request.end();
    latch.await();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Utils(io.vertx.ext.web.impl.Utils) HttpVersion(io.vertx.core.http.HttpVersion) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) WebTestBase(io.vertx.ext.web.WebTestBase) DateFormat(java.text.DateFormat) Set(java.util.Set) Test(org.junit.Test) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Http2PushMapping(io.vertx.ext.web.Http2PushMapping) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Http2PushMapping(io.vertx.ext.web.Http2PushMapping) HttpClient(io.vertx.core.http.HttpClient) ArrayList(java.util.ArrayList) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) CountDownLatch(java.util.concurrent.CountDownLatch) PemTrustOptions(io.vertx.core.net.PemTrustOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Aggregations

HttpClientOptions (io.vertx.core.http.HttpClientOptions)101 Test (org.junit.Test)51 HttpClient (io.vertx.core.http.HttpClient)27 HttpClientRequest (io.vertx.core.http.HttpClientRequest)23 HttpServerOptions (io.vertx.core.http.HttpServerOptions)19 CountDownLatch (java.util.concurrent.CountDownLatch)15 HttpMethod (io.vertx.core.http.HttpMethod)14 SSLCustom (org.apache.servicecomb.foundation.ssl.SSLCustom)14 SSLOption (org.apache.servicecomb.foundation.ssl.SSLOption)14 HttpVersion (io.vertx.core.http.HttpVersion)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Vertx (io.vertx.core.Vertx)11 MockUp (mockit.MockUp)11 DeploymentOptions (io.vertx.core.DeploymentOptions)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 Buffer (io.vertx.core.buffer.Buffer)9 HttpServer (io.vertx.core.http.HttpServer)8 HttpServerResponse (io.vertx.core.http.HttpServerResponse)8 VertxOptions (io.vertx.core.VertxOptions)7 VertxInternal (io.vertx.core.impl.VertxInternal)7