org.asynchttpclient.HttpResponseBodyPart Java Examples

The following examples show how to use org.asynchttpclient.HttpResponseBodyPart. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdlsAsyncFileReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public AsyncHandler.State onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
  if (isErrorResponse) {
    return super.onBodyPartReceived(content);
  }

  outputBuffer.writeBytes(content.getBodyByteBuffer());
  return AsyncHandler.State.CONTINUE;
}
 
Example #2
Source File: BAOSBasedCompletionHandler.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public State onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
  if (requestFailed) {
    return super.onBodyPartReceived(content);
  }
  baos.write(content.getBodyPartBytes());
  return State.CONTINUE;
}
 
Example #3
Source File: BufferBasedCompletionHandler.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public AsyncHandler.State onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
  if (requestFailed) {
    return super.onBodyPartReceived(content);
  }
  outputBuffer.writeBytes(content.getBodyByteBuffer());
  return AsyncHandler.State.CONTINUE;
}
 
Example #4
Source File: TracingAsyncHandler.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Override
public State onBodyPartReceived(final HttpResponseBodyPart bodyPart) throws Exception {
  return handler.onBodyPartReceived(bodyPart);
}
 
Example #5
Source File: TestAzureAsyncReader.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private void testSuccessHttpMode(boolean isSecure) {
  // Prepare response
  AsyncHttpClient client = mock(AsyncHttpClient.class);
  Response response = mock(Response.class);
  HttpResponseStatus status = mock(HttpResponseStatus.class);
  when(status.getStatusCode()).thenReturn(206);

  CompletableFuture<Response> future = CompletableFuture.completedFuture(response);
  ListenableFuture<Response> resFuture = mock(ListenableFuture.class);
  when(resFuture.toCompletableFuture()).thenReturn(future);
  LocalDateTime versionDate = LocalDateTime.now(ZoneId.of("GMT")).minusDays(2);

  byte[] responseBytes = getRandomBytes(20);
  HttpResponseBodyPart responsePart = mock(HttpResponseBodyPart.class);
  when(responsePart.getBodyByteBuffer()).thenReturn(ByteBuffer.wrap(responseBytes));

  when(client.executeRequest(any(Request.class), any(AsyncCompletionHandler.class))).then(invocationOnMock -> {
    // Validate URL
    Request req = invocationOnMock.getArgument(0, Request.class);
    String expectedPrefix = isSecure ? "https" : "http";
    assertEquals("Invalid request url",
      expectedPrefix + "://account.dfs.core.windows.net/container/directory%2Ffile_00.parquet", req.getUrl());

    // Validate Headers
    assertEquals("Invalid blob range header", "bytes=0-19", req.getHeaders().get("Range"));
    LocalDateTime dateHeaderVal = LocalDateTime.parse(req.getHeaders().get("Date"), DATE_RFC1123_FORMATTER);
    long dateHeaderDiffInSecs = Math.abs(dateHeaderVal.until(LocalDateTime.now(ZoneId.of("GMT")), ChronoUnit.SECONDS));
    assertTrue("Date header not set correctly", dateHeaderDiffInSecs < 2);

    LocalDateTime versionHeaderVal = LocalDateTime.parse(req.getHeaders().get("If-Unmodified-Since"), DATE_RFC1123_FORMATTER);
    assertEquals("Version header not set correctly", 0, versionHeaderVal.until(versionDate, ChronoUnit.SECONDS));

    assertEquals("Authz header not set correctly", req.getHeaders().get("Authorization"), "Bearer testtoken");
    assertNotNull(req.getHeaders().get("x-ms-client-request-id"));

    // Fill in response
    AsyncCompletionHandler<Response> responseHandler = invocationOnMock.getArgument(1, AsyncCompletionHandler.class);
    assertEquals(responseHandler.getClass(), BufferBasedCompletionHandler.class);

    responseHandler.onBodyPartReceived(responsePart);
    responseHandler.onStatusReceived(status);
    responseHandler.onCompleted(response);
    return resFuture;
  });

  AzureAsyncReader azureAsyncReader = spy(new AzureAsyncReader(
    "account", new Path("container/directory/file_00.parquet"),
    getMockAuthTokenProvider(), String.valueOf(versionDate.atZone(ZoneId.of("GMT")).toInstant().toEpochMilli()),
    isSecure, client
  ));

  try {
    ByteBuf buf = Unpooled.buffer(20);
    azureAsyncReader.readFully(0, buf, 0, 20).get();
    assertEquals(new String(buf.array()), new String(responseBytes));
    verify(azureAsyncReader).read(0, buf, 0, 20, 0);
  } catch (Exception e) {
    fail(e.getMessage());
  }
}
 
Example #6
Source File: RandomBytesResponseDispatcher.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public HttpResponseBodyPart getNextBodyPart() {
  HttpResponseBodyPart responseBodyPart = mock(HttpResponseBodyPart.class);
  when(responseBodyPart.getBodyPartBytes()).thenReturn(getNextChunk());
  return responseBodyPart;
}
 
Example #7
Source File: AsyncHttpClientLiveTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() {

    // execute an unbound GET request
    Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build();

    HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler<Integer>() {

        int responseStatusCode = -1;

        @Override
        public State onStatusReceived(HttpResponseStatus responseStatus) {
            responseStatusCode = responseStatus.getStatusCode();
            return State.CONTINUE;
        }

        @Override
        public State onHeadersReceived(HttpHeaders headers) {
            return State.CONTINUE;
        }

        @Override
        public State onBodyPartReceived(HttpResponseBodyPart bodyPart) {
            return State.CONTINUE;
        }

        @Override
        public void onThrowable(Throwable t) {

        }

        @Override
        public Integer onCompleted() {
            assertEquals(200, responseStatusCode);
            return responseStatusCode;
        }
    });

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: WebhookTest.java    From attic-aurora with Apache License 2.0 4 votes vote down vote up
@Override
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
  return handler.onBodyPartReceived(bodyPart);
}