Example usage for org.apache.http.concurrent FutureCallback completed

List of usage examples for org.apache.http.concurrent FutureCallback completed

Introduction

In this page you can find the example usage for org.apache.http.concurrent FutureCallback completed.

Prototype

void completed(T t);

Source Link

Usage

From source file:co.paralleluniverse.fibers.httpasyncclient.FiberCloseableHttpAsyncClient.java

private static <T> FutureCallback<T> wrapCallbackWithFuture(final SettableFuture<T> future,
        final FutureCallback<T> callback) {
    return new FutureCallback<T>() {

        @Override//from w  w  w .j a v  a 2  s  .  c  o  m
        public void completed(T result) {
            future.set(result);
            callback.completed(result);
        }

        @Override
        public void failed(Exception ex) {
            future.setException(ex);
            callback.failed(ex);
        }

        @Override
        public void cancelled() {
            future.cancel(true);
            callback.cancelled();
        }
    };
}

From source file:com.clxcommunications.xms.CallbackWrapperTest.java

@Test
public void dropperLogsCompleted() throws Exception {
    FutureCallback<Integer> wrapped = CallbackWrapper.exceptionDropper.wrap(exceptionalCallback);

    wrapped.completed(13);

    assertThat(logger.getLoggingEvents(), is(Arrays.asList(LoggingEvent.error(COMPLETED_EXCEPTION,
            "caught and dropped exception in callback: {}", COMPLETED_EXCEPTION.getMessage()))));
}

From source file:com.ysheng.auth.common.restful.BaseClient.java

/**
 * Performs an asynchronous DELETE operation.
 *
 * @param path The path of the RESTful operation.
 * @param expectedHttpStatus The expected HTTP status of the operation.
 * @param responseHandler The asynchronous response handler.
 * @throws IOException The error that contains detail information.
 *//*  w  w  w  .j a  va  2  s.c  om*/
public final void deleteAsync(final String path, final int expectedHttpStatus,
        final FutureCallback<Void> responseHandler) throws IOException {
    restClient.performAsync(RestClient.Method.DELETE, path, null, new FutureCallback<HttpResponse>() {
        @Override
        public void completed(HttpResponse httpResponse) {
            try {
                restClient.checkResponse(httpResponse, expectedHttpStatus);
            } catch (Exception e) {
                responseHandler.failed(e);
            }

            responseHandler.completed(null);
        }

        @Override
        public void failed(Exception e) {
            responseHandler.failed(e);
        }

        @Override
        public void cancelled() {
            responseHandler.cancelled();
        }
    });
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testIngestionFailure() {
    Message msg1 = new Message("Testing the ingestion");
    msg1.addField("vclap_test_id", "11111");
    IngestionRequest request = new IngestionRequest();
    request.addMessage(msg1);//from   www  . j  ava2s  . c o  m
    testIngestionQueryUrlAndHeaders(request);

    HttpResponse response = mock(HttpResponse.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(httpEntity);

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.completed(response);
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        when(httpEntity.getContent()).thenThrow(IOException.class);
        CompletableFuture<IngestionResponse> responseFuture = client.ingest(request);
    } catch (Exception e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(e.getCause() instanceof LogInsightApiException);
        Assert.assertEquals(e.getCause().getMessage(), "Unable to process the query response");
    }
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testIngestionRuntimeFailure() {
    Message msg1 = new Message("Testing the ingestion");
    msg1.addField("vclap_test_id", "11111");
    IngestionRequest request = new IngestionRequest();
    request.addMessage(msg1);/*  w w w . j a  v  a2 s . c o  m*/
    testIngestionQueryUrlAndHeaders(request);

    HttpResponse response = mock(HttpResponse.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(httpEntity);

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.completed(response);
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        when(httpEntity.getContent()).thenThrow(Exception.class);
        CompletableFuture<IngestionResponse> responseFuture = client.ingest(request);
        responseFuture.get(0, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(e.getCause() instanceof LogInsightApiException);
        Assert.assertEquals(e.getCause().getMessage(), "Ingestion failed");
    } catch (Exception e1) {
        Assert.assertTrue(false);
    }
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testIngestion() {
    Message msg1 = new Message("Testing the ingestion");
    msg1.addField("vclap_test_id", "11111");
    IngestionRequest request = new IngestionRequest();
    request.addMessage(msg1);/*from   w w  w . j  a va  2  s. co m*/
    testIngestionQueryUrlAndHeaders(request);

    HttpResponse response = mock(HttpResponse.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(httpEntity);
    StatusLine statusLine = mock(StatusLine.class);
    when(response.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(200);

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.completed(response);
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        InputStream inputStream = IOUtils.toInputStream(SERVER_EXPECTED_RESPONSE_FOR_INGESTION, "UTF-8");
        when(httpEntity.getContent()).thenReturn(inputStream);
        CompletableFuture<IngestionResponse> responseFuture = client.ingest(request);
        Assert.assertTrue("Invalid status in ingestion response",
                "ok".equals(responseFuture.get().getStatus()));
    } catch (Exception e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(false);
    }
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testMessageQueryFailure() {
    MessageQuery mqb = getMessageQueryForTest();
    testMessageQueryUrlAndHeaders(mqb);//from   w  w  w . j  a va 2s.  c o  m
    HttpResponse response = mock(HttpResponse.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(httpEntity);

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            @SuppressWarnings("unchecked")
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.completed(response);
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        when(httpEntity.getContent()).thenThrow(IOException.class);
        CompletableFuture<MessageQueryResponse> responseFuture = client.messageQuery(mqb.toUrlString());
        MessageQueryResponse messages = responseFuture.get(0, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(e.getCause() instanceof IOException);
    }
}

From source file:com.ysheng.auth.common.restful.BaseClient.java

/**
 * Performs an asynchronous GET operation.
 *
 * @param path The path of the RESTful operation.
 * @param expectedHttpStatus The expected HTTP status of the operation.
 * @param responseHandler The asynchronous response handler.
 * @param <T> The type of the response.
 * @throws IOException The error that contains detail information.
 *///from   w w w .  jav  a 2s.  co  m
public final <T> void getAsync(final String path, final int expectedHttpStatus,
        final FutureCallback<T> responseHandler) throws IOException {
    restClient.performAsync(RestClient.Method.GET, path, null, new FutureCallback<HttpResponse>() {
        @Override
        public void completed(HttpResponse httpResponse) {
            T response = null;
            try {
                restClient.checkResponse(httpResponse, expectedHttpStatus);
                response = JsonSerializer.deserialize(httpResponse.getEntity(), new TypeReference<T>() {
                });
            } catch (Exception e) {
                responseHandler.failed(e);
            }

            responseHandler.completed(response);
        }

        @Override
        public void failed(Exception e) {
            responseHandler.failed(e);
        }

        @Override
        public void cancelled() {
            responseHandler.cancelled();
        }
    });
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testMessageQuery() {
    MessageQuery mqb = getMessageQueryForTest();
    testMessageQueryUrlAndHeaders(mqb);/*from  w w  w. ja v a 2s .c  o m*/
    HttpResponse response = mock(HttpResponse.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(httpEntity);

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            @SuppressWarnings("unchecked")
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.completed(response);
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        InputStream inputStream = IOUtils.toInputStream(SERVER_EXPECTED_QUERY_RESPONSE, "UTF-8");
        when(httpEntity.getContent()).thenReturn(inputStream);
        CompletableFuture<MessageQueryResponse> responseFuture = client.messageQuery(mqb.toUrlString());

        MessageQueryResponse messages = responseFuture.get(0, TimeUnit.MILLISECONDS);
        Assert.assertTrue("Invalid number of messages", messages.getEvents().size() <= 100);
    } catch (Exception e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(false);
    }
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testMessageQueryRuntimeFailure() {
    MessageQuery mqb = getMessageQueryForTest();
    testMessageQueryUrlAndHeaders(mqb);//w ww. j ava2s.co  m
    HttpResponse response = mock(HttpResponse.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    when(response.getEntity()).thenReturn(httpEntity);

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            @SuppressWarnings("unchecked")
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.completed(response);
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        when(httpEntity.getContent()).thenThrow(Exception.class);
        CompletableFuture<MessageQueryResponse> responseFuture = client.messageQuery(mqb.toUrlString());
        MessageQueryResponse messages = responseFuture.get(0, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(e.getCause() instanceof LogInsightApiException);
        Assert.assertEquals(e.getCause().getMessage(), "Message query failed");
    } catch (Exception e1) {
        Assert.assertTrue(false);
    }
}