Search in sources :

Example 26 with Callback

use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.

the class AbstractIOSessionPool method getSessionInternal.

private void getSessionInternal(final PoolEntry poolEntry, final boolean requestNew, final T namedEndpoint, final Timeout connectTimeout, final FutureCallback<IOSession> callback) {
    synchronized (poolEntry) {
        if (poolEntry.session != null && requestNew) {
            closeSession(poolEntry.session, CloseMode.GRACEFUL);
            poolEntry.session = null;
        }
        if (poolEntry.session != null && !poolEntry.session.isOpen()) {
            poolEntry.session = null;
        }
        if (poolEntry.session != null) {
            callback.completed(poolEntry.session);
        } else {
            poolEntry.requestQueue.add(callback);
            if (poolEntry.sessionFuture != null && poolEntry.sessionFuture.isDone()) {
                poolEntry.sessionFuture = null;
            }
            if (poolEntry.sessionFuture == null) {
                poolEntry.sessionFuture = connectSession(namedEndpoint, connectTimeout, new FutureCallback<IOSession>() {

                    @Override
                    public void completed(final IOSession result) {
                        synchronized (poolEntry) {
                            poolEntry.session = result;
                            for (; ; ) {
                                final FutureCallback<IOSession> callback = poolEntry.requestQueue.poll();
                                if (callback != null) {
                                    callback.completed(result);
                                } else {
                                    break;
                                }
                            }
                        }
                    }

                    @Override
                    public void failed(final Exception ex) {
                        synchronized (poolEntry) {
                            poolEntry.session = null;
                            for (; ; ) {
                                final FutureCallback<IOSession> callback = poolEntry.requestQueue.poll();
                                if (callback != null) {
                                    callback.failed(ex);
                                } else {
                                    break;
                                }
                            }
                        }
                    }

                    @Override
                    public void cancelled() {
                        failed(new ConnectionClosedException("Connection request cancelled"));
                    }
                });
            }
        }
    }
}
Also used : ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException)

Example 27 with Callback

use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.

the class TestBasicFuture method testCompletedWithTimeout.

@Test
public void testCompletedWithTimeout() throws Exception {
    final FutureCallback<Object> callback = Mockito.mock(FutureCallback.class);
    final BasicFuture<Object> future = new BasicFuture<>(callback);
    Assertions.assertFalse(future.isDone());
    final Object result = new Object();
    final Exception boom = new Exception();
    future.completed(result);
    future.failed(boom);
    Mockito.verify(callback).completed(result);
    Mockito.verify(callback, Mockito.never()).failed(Mockito.any());
    Mockito.verify(callback, Mockito.never()).cancelled();
    Assertions.assertSame(result, future.get(1, TimeUnit.MILLISECONDS));
    Assertions.assertTrue(future.isDone());
    Assertions.assertFalse(future.isCancelled());
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) TimeoutValueException(org.apache.hc.core5.util.TimeoutValueException) Test(org.junit.jupiter.api.Test)

Example 28 with Callback

use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.

the class TestBasicFuture method testFailed.

@Test
public void testFailed() throws Exception {
    final FutureCallback<Object> callback = Mockito.mock(FutureCallback.class);
    final BasicFuture<Object> future = new BasicFuture<>(callback);
    final Object result = new Object();
    final Exception boom = new Exception();
    future.failed(boom);
    future.completed(result);
    Mockito.verify(callback, Mockito.never()).completed(Mockito.any());
    Mockito.verify(callback).failed(boom);
    Mockito.verify(callback, Mockito.never()).cancelled();
    try {
        future.get();
    } catch (final ExecutionException ex) {
        Assertions.assertSame(boom, ex.getCause());
    }
    Assertions.assertTrue(future.isDone());
    Assertions.assertFalse(future.isCancelled());
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) TimeoutValueException(org.apache.hc.core5.util.TimeoutValueException) Test(org.junit.jupiter.api.Test)

Example 29 with Callback

use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.

the class AbstractBinAsyncEntityConsumer method streamStart.

@Override
public final void streamStart(final EntityDetails entityDetails, final FutureCallback<T> resultCallback) throws IOException, HttpException {
    Args.notNull(resultCallback, "Result callback");
    this.resultCallback = resultCallback;
    try {
        final ContentType contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType()) : null;
        streamStart(contentType);
    } catch (final UnsupportedCharsetException ex) {
        throw new UnsupportedEncodingException(ex.getMessage());
    }
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 30 with Callback

use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.

the class TestHttpRequestExecutor method testExecutionEntityEnclosingRequestWithExpectContinueMultiple1xxResponses.

@Test
public void testExecutionEntityEnclosingRequestWithExpectContinueMultiple1xxResponses() throws Exception {
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpRequestExecutor executor = new HttpRequestExecutor();
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/");
    request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE);
    final HttpEntity entity = Mockito.mock(HttpEntity.class);
    request.setEntity(entity);
    executor.preProcess(request, httprocessor, context);
    Mockito.verify(httprocessor).process(request, request.getEntity(), context);
    Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(110, "Huh?"), new BasicClassicHttpResponse(100, "Continue"), new BasicClassicHttpResponse(111, "Huh?"), new BasicClassicHttpResponse(200, "OK"));
    Mockito.when(conn.isDataAvailable(ArgumentMatchers.any(Timeout.class))).thenReturn(Boolean.TRUE);
    final HttpResponseInformationCallback callback = Mockito.mock(HttpResponseInformationCallback.class);
    final ClassicHttpResponse response = executor.execute(request, conn, callback, context);
    Mockito.verify(conn).sendRequestHeader(request);
    Mockito.verify(conn).sendRequestEntity(request);
    Mockito.verify(conn, Mockito.times(2)).flush();
    Mockito.verify(conn, Mockito.times(2)).isDataAvailable(Timeout.ofMilliseconds(3000));
    Mockito.verify(conn, Mockito.times(4)).receiveResponseHeader();
    Mockito.verify(conn).receiveResponseEntity(response);
    final ArgumentCaptor<HttpResponse> responseCaptor = ArgumentCaptor.forClass(HttpResponse.class);
    Mockito.verify(callback, Mockito.times(2)).execute(responseCaptor.capture(), ArgumentMatchers.eq(conn), ArgumentMatchers.eq(context));
    final List<HttpResponse> infos = responseCaptor.getAllValues();
    Assertions.assertNotNull(infos);
    Assertions.assertEquals(2, infos.size());
    final HttpResponse info1 = infos.get(0);
    Assertions.assertNotNull(info1);
    Assertions.assertEquals(110, info1.getCode());
    final HttpResponse info2 = infos.get(1);
    Assertions.assertNotNull(info2);
    Assertions.assertEquals(111, info2.getCode());
    executor.postProcess(response, httprocessor, context);
    Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) Timeout(org.apache.hc.core5.util.Timeout) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) HttpResponseInformationCallback(org.apache.hc.core5.http.io.HttpResponseInformationCallback) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)10 IOException (java.io.IOException)6 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)6 CancellationException (java.util.concurrent.CancellationException)5 TimeoutException (java.util.concurrent.TimeoutException)5 HttpResponse (org.apache.hc.core5.http.HttpResponse)5 ExecutionException (java.util.concurrent.ExecutionException)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)4 TimeoutValueException (org.apache.hc.core5.util.TimeoutValueException)4 Stopwatch (com.google.common.base.Stopwatch)3 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)3 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)3 ContentType (org.apache.hc.core5.http.ContentType)3 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)3 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2