Search in sources :

Example 41 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.

the class VertxImpl method createHttpClient.

public HttpClient createHttpClient(HttpClientOptions options) {
    CloseFuture closeFuture = new CloseFuture();
    HttpClient client;
    if (options.isShared()) {
        client = createSharedClient(SharedHttpClient.SHARED_MAP_NAME, options.getName(), closeFuture, cf -> createHttpClient(options, cf));
        client = new SharedHttpClient(this, closeFuture, client);
    } else {
        client = createHttpClient(options, closeFuture);
    }
    resolveCloseFuture().add(closeFuture);
    return client;
}
Also used : HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) NetServerImpl(io.vertx.core.net.impl.NetServerImpl) DnsResolverProvider(io.vertx.core.impl.resolver.DnsResolverProvider) HttpClientImpl(io.vertx.core.http.impl.HttpClientImpl) HttpServer(io.vertx.core.http.HttpServer) NetClientImpl(io.vertx.core.net.impl.NetClientImpl) VertxThreadFactory(io.vertx.core.spi.VertxThreadFactory) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) InetAddress(java.net.InetAddress) EventBus(io.vertx.core.eventbus.EventBus) WindowsFileSystem(io.vertx.core.file.impl.WindowsFileSystem) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) Logger(io.vertx.core.impl.logging.Logger) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) java.util.concurrent(java.util.concurrent) Set(java.util.Set) MetricsProvider(io.vertx.core.spi.metrics.MetricsProvider) EventLoop(io.netty.channel.EventLoop) ResourceLeakDetector(io.netty.util.ResourceLeakDetector) Future(io.vertx.core.Future) ServerID(io.vertx.core.net.impl.ServerID) InetSocketAddress(java.net.InetSocketAddress) NetClientOptions(io.vertx.core.net.NetClientOptions) DnsClient(io.vertx.core.dns.DnsClient) AddressResolverGroup(io.netty.resolver.AddressResolverGroup) NetServerOptions(io.vertx.core.net.NetServerOptions) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) FileSystem(io.vertx.core.file.FileSystem) HttpClient(io.vertx.core.http.HttpClient) DatagramSocketImpl(io.vertx.core.datagram.impl.DatagramSocketImpl) VerticleFactory(io.vertx.core.spi.VerticleFactory) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) DnsClientImpl(io.vertx.core.dns.impl.DnsClientImpl) DnsClientOptions(io.vertx.core.dns.DnsClientOptions) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) SharedData(io.vertx.core.shareddata.SharedData) VertxMetrics(io.vertx.core.spi.metrics.VertxMetrics) ArrayList(java.util.ArrayList) SharedDataImpl(io.vertx.core.shareddata.impl.SharedDataImpl) EventBusInternal(io.vertx.core.eventbus.impl.EventBusInternal) FileResolver(io.vertx.core.spi.file.FileResolver) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) WeakReference(java.lang.ref.WeakReference) Metrics(io.vertx.core.spi.metrics.Metrics) EventBusImpl(io.vertx.core.eventbus.impl.EventBusImpl) EventLoopGroup(io.netty.channel.EventLoopGroup) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) Transport(io.vertx.core.net.impl.transport.Transport) NodeSelector(io.vertx.core.spi.cluster.NodeSelector) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) ExecutorServiceFactory(io.vertx.core.spi.ExecutorServiceFactory) VertxTracer(io.vertx.core.spi.tracing.VertxTracer) IOException(java.io.IOException) SharedHttpClient(io.vertx.core.http.impl.SharedHttpClient) File(java.io.File) FileSystemImpl(io.vertx.core.file.impl.FileSystemImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) NetServer(io.vertx.core.net.NetServer) TCPServerBase(io.vertx.core.net.impl.TCPServerBase) HttpServerOptions(io.vertx.core.http.HttpServerOptions) ClusteredEventBus(io.vertx.core.eventbus.impl.clustered.ClusteredEventBus) HttpClient(io.vertx.core.http.HttpClient) SharedHttpClient(io.vertx.core.http.impl.SharedHttpClient) SharedHttpClient(io.vertx.core.http.impl.SharedHttpClient)

Example 42 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.

the class NettyCompatTest method testHttp2.

@Test
public void testHttp2() {
    vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setSslEngineOptions(new OpenSSLEngineOptions()).setKeyCertOptions(Cert.SERVER_JKS.get())).requestHandler(req -> req.response().end("OK")).listen(8443, "localhost", onSuccess(s -> {
        HttpClient client = vertx.createHttpClient(new HttpClientOptions().setSsl(true).setSslEngineOptions(new OpenSSLEngineOptions()).setTrustStoreOptions(Trust.SERVER_JKS.get()));
        client.request(HttpMethod.GET, 8443, "localhost", "/somepath", onSuccess(req -> {
            req.send(onSuccess(resp -> {
                resp.bodyHandler(buff -> {
                    assertEquals("OK", buff.toString());
                    testComplete();
                });
            }));
        }));
    }));
    await();
}
Also used : OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) HttpMethod(io.vertx.core.http.HttpMethod) Cert(io.vertx.test.tls.Cert) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Trust(io.vertx.test.tls.Trust) Test(org.junit.Test) HttpClientOptions(io.vertx.core.http.HttpClientOptions) VertxTestBase(io.vertx.test.core.VertxTestBase) HttpClient(io.vertx.core.http.HttpClient) HttpClient(io.vertx.core.http.HttpClient) HttpServerOptions(io.vertx.core.http.HttpServerOptions) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 43 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.

the class SSLEngineTest method doTest.

private void doTest(SSLEngineOptions engine, boolean useAlpn, HttpVersion version, String error, String expectedSslContext, boolean expectCause) {
    server.close();
    HttpServerOptions options = new HttpServerOptions().setSslEngineOptions(engine).setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setKeyCertOptions(Cert.SERVER_PEM.get()).setSsl(true).setUseAlpn(useAlpn);
    server = vertx.createHttpServer(options);
    server.requestHandler(req -> {
        assertEquals(req.version(), version);
        assertTrue(req.isSSL());
        req.response().end();
    });
    try {
        startServer();
        if (error != null) {
            fail("Was expecting failure: " + error);
        }
    } catch (Exception e) {
        if (error == null) {
            fail(e);
        } else {
            assertEquals(error, e.getMessage());
            if (expectCause) {
                assertNotSame(e, e.getCause());
            }
            return;
        }
    }
    SSLHelper sslHelper = ((HttpServerImpl) server).sslHelper();
    SslContext ctx = sslHelper.getContext((VertxInternal) vertx);
    switch(expectedSslContext != null ? expectedSslContext : "jdk") {
        case "jdk":
            assertTrue(ctx instanceof JdkSslContext);
            break;
        case "openssl":
            assertTrue(ctx instanceof OpenSslContext);
            break;
    }
    client = vertx.createHttpClient(new HttpClientOptions().setSslEngineOptions(engine).setSsl(true).setUseAlpn(useAlpn).setTrustAll(true).setProtocolVersion(version));
    client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(req -> {
        req.send(onSuccess(resp -> {
            assertEquals(200, resp.statusCode());
            testComplete();
        }));
    }));
    await();
}
Also used : SSLHelper(io.vertx.core.net.impl.SSLHelper) VertxException(io.vertx.core.VertxException) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) SSLEngineOptions(io.vertx.core.net.SSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext) Test(org.junit.Test) SSLHelper(io.vertx.core.net.impl.SSLHelper) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) HttpTestBase(io.vertx.core.http.HttpTestBase) HttpVersion(io.vertx.core.http.HttpVersion) HttpMethod(io.vertx.core.http.HttpMethod) Cert(io.vertx.test.tls.Cert) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) JdkSslContext(io.netty.handler.ssl.JdkSslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext) HttpServerOptions(io.vertx.core.http.HttpServerOptions) VertxException(io.vertx.core.VertxException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpServerImpl(io.vertx.core.http.impl.HttpServerImpl) JdkSslContext(io.netty.handler.ssl.JdkSslContext) SslContext(io.netty.handler.ssl.SslContext) OpenSslContext(io.netty.handler.ssl.OpenSslContext)

Example 44 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vert.x by eclipse.

the class VertxTest method testFinalizeHttpClient.

@Test
public void testFinalizeHttpClient() throws Exception {
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    try {
        CountDownLatch latch = new CountDownLatch(1);
        AtomicReference<NetSocket> socketRef = new AtomicReference<>();
        vertx.createNetServer().connectHandler(socketRef::set).listen(8080, "localhost").onComplete(onSuccess(server -> latch.countDown()));
        awaitLatch(latch);
        AtomicBoolean closed = new AtomicBoolean();
        // No keep alive so the connection is not held in the pool ????
        CloseFuture closeFuture = new CloseFuture();
        closeFuture.future().onComplete(ar -> closed.set(true));
        HttpClient client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false), closeFuture);
        vertx.addCloseHook(closeFuture);
        client.request(HttpMethod.GET, 8080, "localhost", "/").compose(HttpClientRequest::send).onComplete(onFailure(err -> {
        }));
        WeakReference<HttpClient> ref = new WeakReference<>(client);
        closeFuture = null;
        client = null;
        assertWaitUntil(() -> socketRef.get() != null);
        for (int i = 0; i < 10; i++) {
            Thread.sleep(10);
            RUNNER.runSystemGC();
            assertFalse(closed.get());
            assertNotNull(ref.get());
        }
        socketRef.get().close();
        long now = System.currentTimeMillis();
        while (true) {
            assertTrue(System.currentTimeMillis() - now < 20_000);
            RUNNER.runSystemGC();
            if (ref.get() == null) {
                assertTrue(closed.get());
                break;
            }
        }
    } finally {
        vertx.close(ar -> {
            testComplete();
        });
    }
    await();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) VertxInternal(io.vertx.core.impl.VertxInternal) URL(java.net.URL) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) RepeatRule(io.vertx.test.core.RepeatRule) OptionsBuilder(org.openjdk.jmh.runner.options.OptionsBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) URLClassLoader(java.net.URLClassLoader) Rule(org.junit.Rule) AsyncTestBase(io.vertx.test.core.AsyncTestBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) CloseFuture(io.vertx.core.impl.CloseFuture) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) Runner(org.openjdk.jmh.runner.Runner) WeakReference(java.lang.ref.WeakReference) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) CloseFuture(io.vertx.core.impl.CloseFuture) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientOptions(io.vertx.core.http.HttpClientOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(io.vertx.core.http.HttpClient) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 45 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project java-chassis by ServiceComb.

the class DefaultMonitorDataPublisher method createHttpClientOptions.

private HttpClientOptions createHttpClientOptions() {
    HttpClientOptions httpClientOptions = new HttpClientOptions();
    if (MonitorConstant.isProxyEnable()) {
        ProxyOptions proxy = new ProxyOptions();
        proxy.setHost(MonitorConstant.getProxyHost());
        proxy.setPort(MonitorConstant.getProxyPort());
        proxy.setUsername(MonitorConstant.getProxyUsername());
        proxy.setPassword(MonitorConstant.getProxyPasswd());
        httpClientOptions.setProxyOptions(proxy);
    }
    httpClientOptions.setConnectTimeout(MonitorConstant.getConnectionTimeout());
    if (MonitorConstant.sslEnabled()) {
        SSLOptionFactory factory = SSLOptionFactory.createSSLOptionFactory(SSL_KEY, null);
        SSLOption sslOption;
        if (factory == null) {
            sslOption = SSLOption.buildFromYaml(SSL_KEY);
        } else {
            sslOption = factory.createSSLOption();
        }
        SSLCustom sslCustom = SSLCustom.createSSLCustom(sslOption.getSslCustomClass());
        VertxTLSBuilder.buildHttpClientOptions(sslOption, sslCustom, httpClientOptions);
    }
    return httpClientOptions;
}
Also used : ProxyOptions(io.vertx.core.net.ProxyOptions) SSLOptionFactory(org.apache.servicecomb.foundation.ssl.SSLOptionFactory) SSLOption(org.apache.servicecomb.foundation.ssl.SSLOption) SSLCustom(org.apache.servicecomb.foundation.ssl.SSLCustom) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

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