Search in sources :

Example 76 with AsyncHttpClient

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

the class WebsocketClientCamelRouteTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://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) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 77 with AsyncHttpClient

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

the class WebsocketProducerRouteRestartTest method doTestWSHttpCall.

private void doTestWSHttpCall() 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)

Example 78 with AsyncHttpClient

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

the class WebsocketRouteExampleTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://127.0.0.1:" + port + "/echo").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();
    websocket.sendMessage("Beer");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    assertEquals("BeerBeer", received.get(0));
    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 79 with AsyncHttpClient

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

the class WebsocketSSLContextInUriRouteExampleTest 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)

Example 80 with AsyncHttpClient

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

the class MultipartUploadTest method testSendingSmallFilesAndByteArray.

/**
     * Tests that the streaming of a file works.
     * @throws IOException 
     */
@Test(groups = "standalone")
public void testSendingSmallFilesAndByteArray() throws IOException {
    String expectedContents = "filecontent: hello";
    String expectedContents2 = "gzipcontent: hello";
    String expectedContents3 = "filecontent: hello2";
    String testResource1 = "textfile.txt";
    String testResource2 = "gzip.txt.gz";
    String testResource3 = "textfile2.txt";
    File testResource1File = null;
    try {
        testResource1File = getClasspathFile(testResource1);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        fail("unable to find " + testResource1);
    }
    File testResource2File = null;
    try {
        testResource2File = getClasspathFile(testResource2);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        fail("unable to find " + testResource2);
    }
    File testResource3File = null;
    try {
        testResource3File = getClasspathFile(testResource3);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        fail("unable to find " + testResource3);
    }
    List<File> testFiles = new ArrayList<>();
    testFiles.add(testResource1File);
    testFiles.add(testResource2File);
    testFiles.add(testResource3File);
    List<String> expected = new ArrayList<>();
    expected.add(expectedContents);
    expected.add(expectedContents2);
    expected.add(expectedContents3);
    List<Boolean> gzipped = new ArrayList<>();
    gzipped.add(false);
    gzipped.add(true);
    gzipped.add(false);
    boolean tmpFileCreated = false;
    File tmpFile = File.createTempFile("textbytearray", ".txt");
    try (FileOutputStream os = new FileOutputStream(tmpFile)) {
        IOUtils.write(expectedContents.getBytes(UTF_8), os);
        tmpFileCreated = true;
        testFiles.add(tmpFile);
        expected.add(expectedContents);
        gzipped.add(false);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (!tmpFileCreated) {
        fail("Unable to test ByteArrayMultiPart, as unable to write to filesystem the tmp test content");
    }
    try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) {
        RequestBuilder builder = post("http://localhost" + ":" + port1 + "/upload/bob");
        builder.addBodyPart(new FilePart("file1", testResource1File, "text/plain", UTF_8));
        builder.addBodyPart(new FilePart("file2", testResource2File, "application/x-gzip", null));
        builder.addBodyPart(new StringPart("Name", "Dominic"));
        builder.addBodyPart(new FilePart("file3", testResource3File, "text/plain", UTF_8));
        builder.addBodyPart(new StringPart("Age", "3"));
        builder.addBodyPart(new StringPart("Height", "shrimplike"));
        builder.addBodyPart(new StringPart("Hair", "ridiculous"));
        builder.addBodyPart(new ByteArrayPart("file4", expectedContents.getBytes(UTF_8), "text/plain", UTF_8, "bytearray.txt"));
        Request r = builder.build();
        Response res = c.executeRequest(r).get();
        assertEquals(res.getStatusCode(), 200);
        testSentFile(expected, testFiles, res, gzipped);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Download Exception");
    } finally {
        FileUtils.deleteQuietly(tmpFile);
    }
}
Also used : RequestBuilder(org.asynchttpclient.RequestBuilder) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Request(org.asynchttpclient.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileUploadException(org.apache.commons.fileupload.FileUploadException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) FileOutputStream(java.io.FileOutputStream) File(java.io.File) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

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