com.ning.http.client.listener
Class TransferCompletionHandler

java.lang.Object
  extended by com.ning.http.client.AsyncCompletionHandler<Response>
      extended by com.ning.http.client.AsyncCompletionHandlerBase
          extended by com.ning.http.client.listener.TransferCompletionHandler
All Implemented Interfaces:
AsyncHandler<Response>, ProgressAsyncHandler<Response>

public class TransferCompletionHandler
extends AsyncCompletionHandlerBase

A AsyncHandler that can be used to notify a set of TransferListener AsyncHttpClient client = new AsyncHttpClient(); TransferCompletionHandler tl = new TransferCompletionHandler(); tl.addTransferListener(new TransferListener() { public void onRequestHeadersSent(FluentCaseInsensitiveStringsMap headers) { } public void onResponseHeadersReceived(FluentCaseInsensitiveStringsMap headers) { } public void onBytesReceived(ByteBuffer buffer) { } public void onBytesSent(ByteBuffer buffer) { } public void onRequestResponseCompleted() { } public void onThrowable(Throwable t) { } }); Response response = httpClient.prepareGet("http://...").execute(tl).get();


Nested Class Summary
static class TransferCompletionHandler.TransferAdapter
           
 
Nested classes/interfaces inherited from interface com.ning.http.client.AsyncHandler
AsyncHandler.STATE
 
Constructor Summary
TransferCompletionHandler()
          Create a TransferCompletionHandler that will not accumulate bytes.
TransferCompletionHandler(boolean accumulateResponseBytes)
          Create a TransferCompletionHandler that can or cannot accumulate bytes and make it available when Response.getResponseBody() get called.
 
Method Summary
 TransferCompletionHandler addTransferListener(TransferListener t)
          Add a TransferListener
 AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart content)
          Invoked as soon as some response body part are received.
 Response onCompleted(Response response)
          Invoked once the HTTP response processing is finished.
 AsyncHandler.STATE onContentWriteCompleted()
          Invoked when the content (a File, String or FileInputStream has been fully written on the I/O socket.
 AsyncHandler.STATE onContentWriteProgress(long amount, long current, long total)
          Invoked when the I/O operation associated with the Request body as been progressed.
 AsyncHandler.STATE onHeadersReceived(HttpResponseHeaders headers)
          Invoked as soon as the HTTP headers has been received.
 AsyncHandler.STATE onHeaderWriteCompleted()
          Invoked when the content (a File, String or FileInputStream has been fully written on the I/O socket.
 void onThrowable(Throwable t)
          Invoked when an unexpected exception occurs during the processing of the response.
 TransferCompletionHandler removeTransferListener(TransferListener t)
          Remove a TransferListener
 void transferAdapter(TransferCompletionHandler.TransferAdapter transferAdapter)
          Associate a TransferCompletionHandler.TransferAdapter with this listener.
 
Methods inherited from class com.ning.http.client.AsyncCompletionHandler
onCompleted, onStatusReceived
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransferCompletionHandler

public TransferCompletionHandler()
Create a TransferCompletionHandler that will not accumulate bytes. The resulting Response.getResponseBody(), Response.getResponseBodyAsStream() and Response.getResponseBodyExcerpt(int) will throw an IllegalStateException if called.


TransferCompletionHandler

public TransferCompletionHandler(boolean accumulateResponseBytes)
Create a TransferCompletionHandler that can or cannot accumulate bytes and make it available when Response.getResponseBody() get called. The default is false.

Parameters:
accumulateResponseBytes - true to accumulates bytes in memory.
Method Detail

addTransferListener

public TransferCompletionHandler addTransferListener(TransferListener t)
Add a TransferListener

Parameters:
t - a TransferListener
Returns:
this

removeTransferListener

public TransferCompletionHandler removeTransferListener(TransferListener t)
Remove a TransferListener

Parameters:
t - a TransferListener
Returns:
this

transferAdapter

public void transferAdapter(TransferCompletionHandler.TransferAdapter transferAdapter)
Associate a TransferCompletionHandler.TransferAdapter with this listener.

Parameters:
transferAdapter - TransferCompletionHandler.TransferAdapter

onHeadersReceived

public AsyncHandler.STATE onHeadersReceived(HttpResponseHeaders headers)
                                     throws Exception
Invoked as soon as the HTTP headers has been received. Can potentially be invoked morethan once if a broken server sent trailling headers.

Specified by:
onHeadersReceived in interface AsyncHandler<Response>
Overrides:
onHeadersReceived in class AsyncCompletionHandler<Response>
Parameters:
headers - the HTTP headers.
Returns:
a AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.
Throws:
Exception - if something wrong happens

onBodyPartReceived

public AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart content)
                                      throws Exception
Description copied from class: AsyncCompletionHandler
Invoked as soon as some response body part are received. Could be invoked many times.

Specified by:
onBodyPartReceived in interface AsyncHandler<Response>
Overrides:
onBodyPartReceived in class AsyncCompletionHandler<Response>
Parameters:
content - response's body part.
Returns:
a AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.
Throws:
Exception - if something wrong happens

onCompleted

public Response onCompleted(Response response)
                     throws Exception
Description copied from class: AsyncCompletionHandler
Invoked once the HTTP response processing is finished.

Gets always invoked as last callback method.

Overrides:
onCompleted in class AsyncCompletionHandlerBase
Parameters:
response - The Response
Returns:
T Value that will be returned by the associated Future
Throws:
Exception - if something wrong happens

onHeaderWriteCompleted

public AsyncHandler.STATE onHeaderWriteCompleted()
Invoked when the content (a File, String or FileInputStream has been fully written on the I/O socket.

Specified by:
onHeaderWriteCompleted in interface ProgressAsyncHandler<Response>
Overrides:
onHeaderWriteCompleted in class AsyncCompletionHandler<Response>
Returns:
a AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.

onContentWriteCompleted

public AsyncHandler.STATE onContentWriteCompleted()
Invoked when the content (a File, String or FileInputStream has been fully written on the I/O socket.

Specified by:
onContentWriteCompleted in interface ProgressAsyncHandler<Response>
Overrides:
onContentWriteCompleted in class AsyncCompletionHandler<Response>
Returns:
a AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.

onContentWriteProgress

public AsyncHandler.STATE onContentWriteProgress(long amount,
                                                 long current,
                                                 long total)
Invoked when the I/O operation associated with the Request body as been progressed.

Specified by:
onContentWriteProgress in interface ProgressAsyncHandler<Response>
Overrides:
onContentWriteProgress in class AsyncCompletionHandler<Response>
Parameters:
amount - The amount of bytes to transfer.
current - The amount of bytes transferred
total - The total number of bytes transferred
Returns:
a AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.

onThrowable

public void onThrowable(Throwable t)
Description copied from class: AsyncCompletionHandler
Invoked when an unexpected exception occurs during the processing of the response. The exception may have been produced by implementation of onXXXReceived method invokation.

Specified by:
onThrowable in interface AsyncHandler<Response>
Overrides:
onThrowable in class AsyncCompletionHandlerBase
Parameters:
t - a Throwable


Copyright © 2011. All Rights Reserved.