Search in sources :

Example 1 with AsyncHttpClient

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

the class WebsocketProducerRouteExampleTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/shop").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    // Send message to the direct endpoint
    producer.sendBodyAndHeader("Beer on stock at Apache Mall", WebsocketConstants.SEND_TO_ALL, "true");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    Object r = received.get(0);
    assertTrue(r instanceof String);
    assertEquals("Beer on stock at Apache Mall", r);
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 2 with AsyncHttpClient

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

the class WebsocketProducerRouteExampleTest method testWSBytesHttpCall.

@Test
public void testWSBytesHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/shop").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new DefaultWebSocketListener() {

        @Override
        public void onMessage(byte[] message) {
            received.add(message);
            log.info("received --> " + Arrays.toString(message));
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    // Send message to the direct endpoint
    byte[] testmessage = "Beer on stock at Apache Mall".getBytes("utf-8");
    producer.sendBodyAndHeader(testmessage, WebsocketConstants.SEND_TO_ALL, "true");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    Object r = received.get(0);
    assertTrue(r instanceof byte[]);
    assertArrayEquals(testmessage, (byte[]) r);
    websocket.close();
    c.close();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) DefaultWebSocketListener(org.asynchttpclient.ws.DefaultWebSocketListener) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 3 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient 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 4 with AsyncHttpClient

use of org.asynchttpclient.AsyncHttpClient 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 5 with AsyncHttpClient

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

the class WebsocketSSLRouteExampleTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = createAsyncHttpSSLClient();
    WebSocket websocket = c.prepareGet("wss://127.0.0.1:" + port + "/test").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    getMockEndpoint("mock:client").expectedBodiesReceived("Hello from WS client");
    websocket.sendMessage("Hello from WS client");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertMockEndpointsSatisfied();
    assertEquals(10, received.size());
    for (int i = 0; i < 10; i++) {
        assertEquals(">> Welcome on board!", received.get(i));
    }
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Aggregations

AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)146 Test (org.testng.annotations.Test)119 Response (org.asynchttpclient.Response)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)66 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)31 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RequestBuilder (org.asynchttpclient.RequestBuilder)16 IOException (java.io.IOException)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 ExecutionException (java.util.concurrent.ExecutionException)13 Request (org.asynchttpclient.Request)13 WebSocket (org.asynchttpclient.ws.WebSocket)12 Test (org.junit.Test)11 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 File (java.io.File)9 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7