Search in sources :

Example 6 with Request

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

the class SimpleAsyncHttpClient method execute.

private Future<Response> execute(RequestBuilder rb, BodyConsumer bodyConsumer, ThrowableHandler throwableHandler) throws IOException {
    if (throwableHandler == null) {
        throwableHandler = defaultThrowableHandler;
    }
    Request request = rb.build();
    ProgressAsyncHandler<Response> handler = new BodyConsumerAsyncHandler(bodyConsumer, throwableHandler, errorDocumentBehaviour, request.getUri(), listener);
    if (resumeEnabled && request.getMethod().equals("GET") && bodyConsumer != null && bodyConsumer instanceof ResumableBodyConsumer) {
        ResumableBodyConsumer fileBodyConsumer = (ResumableBodyConsumer) bodyConsumer;
        long length = fileBodyConsumer.getTransferredBytes();
        fileBodyConsumer.resume();
        handler = new ResumableBodyConsumerAsyncHandler(length, handler);
    }
    return getAsyncHttpClient().executeRequest(request, handler);
}
Also used : Response(org.asynchttpclient.Response) Request(org.asynchttpclient.Request)

Example 7 with Request

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

the class HttpUtilsTest method testGetHostHeaderHasVirtualHost.

@Test
public void testGetHostHeaderHasVirtualHost() {
    Request request = Dsl.get("http://stackoverflow.com/questions/1057564").setVirtualHost("example.com").build();
    Uri uri = Uri.create("http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs");
    String hostHeader = HttpUtils.hostHeader(request, uri);
    assertEquals(hostHeader, "example.com", "Incorrect hostHeader returned");
}
Also used : Request(org.asynchttpclient.Request) Uri(org.asynchttpclient.uri.Uri) Test(org.testng.annotations.Test)

Example 8 with Request

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

the class ChunkingTest method doTestWithInputStreamBodyGenerator.

public void doTestWithInputStreamBodyGenerator(InputStream is) throws Throwable {
    try (AsyncHttpClient c = asyncHttpClient(httpClientBuilder())) {
        RequestBuilder builder = post(getTargetUrl()).setBody(new InputStreamBodyGenerator(is));
        Request r = builder.build();
        final ListenableFuture<Response> responseFuture = c.executeRequest(r);
        waitForAndAssertResponse(responseFuture);
    }
}
Also used : Response(org.asynchttpclient.Response) RequestBuilder(org.asynchttpclient.RequestBuilder) InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) Request(org.asynchttpclient.Request) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 9 with Request

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

the class AhcProducer method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    try {
        // AHC supports async processing
        Request request = getEndpoint().getBinding().prepareRequest(getEndpoint(), exchange);
        log.debug("Executing request {} ", request);
        client.prepareRequest(request).execute(new AhcAsyncHandler(exchange, callback, request.getUrl(), getEndpoint().getBufferSize()));
        return false;
    } catch (Exception e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
}
Also used : Request(org.asynchttpclient.Request)

Example 10 with Request

use of org.asynchttpclient.Request in project tutorials by eugenp.

the class AsyncHttpClientTestCase method givenHttpClient_executeAsyncGetRequestWithListanableFuture.

@Test
public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() {
    // execute an unbound GET request
    Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build();
    ListenableFuture<Response> listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest);
    listenableFuture.addListener(() -> {
        Response response;
        try {
            response = listenableFuture.get(5000, TimeUnit.MILLISECONDS);
            assertEquals(200, response.getStatusCode());
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            e.printStackTrace();
        }
    }, Executors.newCachedThreadPool());
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : Response(org.asynchttpclient.Response) Request(org.asynchttpclient.Request) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

Request (org.asynchttpclient.Request)67 Test (org.testng.annotations.Test)32 Response (org.asynchttpclient.Response)31 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)18 RequestBuilder (org.asynchttpclient.RequestBuilder)17 Test (org.junit.Test)16 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)8 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)8 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)7 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)6 ListenableFuture (org.asynchttpclient.ListenableFuture)6 Uri (org.asynchttpclient.uri.Uri)5 Realm (org.asynchttpclient.Realm)4 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 HttpResponse (io.netty.handler.codec.http.HttpResponse)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3