Java Code Examples for com.ning.http.client.HttpResponseBodyPart#writeTo()

The following examples show how to use com.ning.http.client.HttpResponseBodyPart#writeTo() . 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: TezBodyDeferringAsyncHandler.java    From tez with Apache License 2.0 5 votes vote down vote up
public AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
  // body arrived, flush headers
  if (!responseSet) {
    response = responseBuilder.build();
    responseSet = true;
    headersArrived.countDown();
  }
  bodyPart.writeTo(output);
  return AsyncHandler.STATE.CONTINUE;
}
 
Example 2
Source File: TaskResource.java    From Singularity with Apache License 2.0 4 votes vote down vote up
@Override
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
  bodyPart.writeTo(wrappedOutputStream);
  wrappedOutputStream.flush();
  return STATE.CONTINUE;
}
 
Example 3
Source File: AsyncHttpClientTest.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void doTest(Request request) throws InterruptedException, ExecutionException, IOException {
	final PipedOutputStream pipedOutputStream = new PipedOutputStream();
	final PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);

	AsyncHandler<Response> asyncHandler = new AsyncHandler<Response>() {
		private final Response.ResponseBuilder builder = new Response.ResponseBuilder();

		@Override
		public STATE onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
			content.writeTo(pipedOutputStream);
			return STATE.CONTINUE;
		}

		@Override
		public STATE onStatusReceived(final HttpResponseStatus status) throws Exception {
			builder.accumulate(status);
			return STATE.CONTINUE;
		}

		@Override
		public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
			builder.accumulate(headers);
			return STATE.CONTINUE;
		}

		@Override
		public Response onCompleted() throws Exception {

			LOGGER.info("On complete called!");

			pipedOutputStream.flush();
			pipedOutputStream.close();

			return builder.build();

		}

		@Override
		public void onThrowable(Throwable arg0) {
			// TODO Auto-generated method stub
			LOGGER.error("Error: {}", arg0);
			onTestFailed();
		}

	};

	Future<Void> readingThreadFuture = Executors.newCachedThreadPool().submit(new Callable<Void>() {

		@Override
		public Void call() throws Exception {
			BufferedReader reader = new BufferedReader(new InputStreamReader(pipedInputStream));

			String readPrediction;

			int numPredictionsRead = 0;

			while ((readPrediction = reader.readLine()) != null) {
				//LOGGER.info("Got prediction: {}", readPrediction);
				numPredictionsRead++;
			}

			LOGGER.info("Read a total of {} predictions", numPredictionsRead);
			Assert.assertEquals(roundsOfDataToSubmit * 272274, numPredictionsRead);

			return null;
		}
	});

	Builder config = new AsyncHttpClientConfig.Builder();

	config.setRequestTimeoutInMs(-1); //need to set this to -1, to indicate wait forever. setting to 0 actually means a 0 ms timeout!

	AsyncHttpClient client = new AsyncHttpClient(config.build());

	client.executeRequest(request, asyncHandler).get();

	readingThreadFuture.get(); //verify no exceptions occurred when reading predictions

	client.close();

	Assert.assertFalse(getTestFailed());
}