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