Search in sources :

Example 1 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project camel by apache.

the class WebsocketSSLContextInUriRouteExampleTest method createAsyncHttpSSLClient.

protected AsyncHttpClient createAsyncHttpSSLClient() throws IOException, GeneralSecurityException {
    AsyncHttpClient c;
    AsyncHttpClientConfig config;
    DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder();
    SSLContext sslContext = new SSLContextParameters().createSSLContext(context());
    JdkSslContext ssl = new JdkSslContext(sslContext, true, ClientAuth.REQUIRE);
    builder.setSslContext(ssl);
    builder.setAcceptAnyCertificate(true);
    config = builder.build();
    c = new DefaultAsyncHttpClient(config);
    return c;
}
Also used : JdkSslContext(io.netty.handler.ssl.JdkSslContext) RouteBuilder(org.apache.camel.builder.RouteBuilder) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) SSLContext(javax.net.ssl.SSLContext) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters)

Example 2 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project camel by apache.

the class WebsocketSSLRouteExampleTest method createAsyncHttpSSLClient.

protected AsyncHttpClient createAsyncHttpSSLClient() throws IOException, GeneralSecurityException {
    AsyncHttpClient c;
    AsyncHttpClientConfig config;
    DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder();
    SSLContext sslContext = new SSLContextParameters().createSSLContext(context());
    JdkSslContext ssl = new JdkSslContext(sslContext, true, ClientAuth.REQUIRE);
    builder.setSslContext(ssl);
    builder.setAcceptAnyCertificate(true);
    config = builder.build();
    c = new DefaultAsyncHttpClient(config);
    return c;
}
Also used : JdkSslContext(io.netty.handler.ssl.JdkSslContext) RouteBuilder(org.apache.camel.builder.RouteBuilder) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) SSLContext(javax.net.ssl.SSLContext) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters)

Example 3 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project async-http-client by AsyncHttpClient.

the class HttpsProxyTest method testConfigProxy.

@Test(groups = "standalone")
public void testConfigProxy() throws Exception {
    AsyncHttpClientConfig config = //
    config().setFollowRedirect(//
    true).setProxyServer(//
    proxyServer("localhost", port1).build()).setUseInsecureTrustManager(//
    true).build();
    try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) {
        Response r = asyncHttpClient.executeRequest(get(getTargetUrl2())).get();
        assertEquals(r.getStatusCode(), 200);
    }
}
Also used : Response(org.asynchttpclient.Response) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 4 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project async-http-client by AsyncHttpClient.

the class MaxConnectionsInThreads method testMaxConnectionsWithinThreads.

@Test(groups = "standalone")
public void testMaxConnectionsWithinThreads() throws Exception {
    String[] urls = new String[] { getTargetUrl(), getTargetUrl() };
    AsyncHttpClientConfig config = //
    config().setConnectTimeout(//
    1000).setRequestTimeout(//
    5000).setKeepAlive(//
    true).setMaxConnections(//
    1).setMaxConnectionsPerHost(//
    1).build();
    final CountDownLatch inThreadsLatch = new CountDownLatch(2);
    final AtomicInteger failedCount = new AtomicInteger();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        for (final String url : urls) {
            Thread t = new Thread() {

                public void run() {
                    client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {

                        @Override
                        public Response onCompleted(Response response) throws Exception {
                            Response r = super.onCompleted(response);
                            inThreadsLatch.countDown();
                            return r;
                        }

                        @Override
                        public void onThrowable(Throwable t) {
                            super.onThrowable(t);
                            failedCount.incrementAndGet();
                            inThreadsLatch.countDown();
                        }
                    });
                }
            };
            t.start();
        }
        inThreadsLatch.await();
        assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from concurrent threads");
        final CountDownLatch notInThreadsLatch = new CountDownLatch(2);
        failedCount.set(0);
        for (final String url : urls) {
            client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    Response r = super.onCompleted(response);
                    notInThreadsLatch.countDown();
                    return r;
                }

                @Override
                public void onThrowable(Throwable t) {
                    super.onThrowable(t);
                    failedCount.incrementAndGet();
                    notInThreadsLatch.countDown();
                }
            });
        }
        notInThreadsLatch.await();
        assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from main thread");
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) AsyncCompletionHandlerBase(org.asynchttpclient.AsyncCompletionHandlerBase) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 5 with AsyncHttpClientConfig

use of org.asynchttpclient.AsyncHttpClientConfig in project async-http-client by AsyncHttpClient.

the class BodyChunkTest method negativeContentTypeTest.

@Test(groups = "standalone")
public void negativeContentTypeTest() throws Exception {
    AsyncHttpClientConfig config = //
    config().setConnectTimeout(//
    100).setMaxConnections(//
    50).setRequestTimeout(// 5 minutes
    5 * 60 * 1000).build();
    try (AsyncHttpClient client = asyncHttpClient(config)) {
        RequestBuilder requestBuilder = //
        post(getTargetUrl()).setHeader("Content-Type", //
        "message/rfc822").setBody(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())));
        Future<Response> future = client.executeRequest(requestBuilder.build());
        System.out.println("waiting for response");
        Response response = future.get();
        assertEquals(response.getStatusCode(), 200);
        assertEquals(response.getResponseBody(), MY_MESSAGE);
    }
}
Also used : Response(org.asynchttpclient.Response) RequestBuilder(org.asynchttpclient.RequestBuilder) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) ByteArrayInputStream(java.io.ByteArrayInputStream) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest) Test(org.testng.annotations.Test)

Aggregations

AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)9 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)7 Test (org.testng.annotations.Test)7 Response (org.asynchttpclient.Response)6 JdkSslContext (io.netty.handler.ssl.JdkSslContext)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 SSLContext (javax.net.ssl.SSLContext)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)3 ListenableFuture (org.asynchttpclient.ListenableFuture)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 SSLContextParameters (org.apache.camel.util.jsse.SSLContextParameters)2 AsyncCompletionHandlerBase (org.asynchttpclient.AsyncCompletionHandlerBase)2 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1