org.xlightweb
Class FutureResponseHandler

Package class diagram package FutureResponseHandler
java.lang.Object
  extended by org.xlightweb.FutureResponseHandler
All Implemented Interfaces:
Future<IHttpResponse>, IFutureResponse, IHttpResponseHandler, IHttpSocketTimeoutHandler

public class FutureResponseHandler
extends Object
implements IFutureResponse, IHttpResponseHandler, IHttpSocketTimeoutHandler

A response handler implementation which supports a future behavior. Typically this response handler will be used, if the send method is required to send the request, but the response should be read in a blocking behavior. Example:

   FutureResponseHandler future = new FutureResponseHandler();
    
   HttpRequestHeader header = new HttpRequestHeader("POST", url, "application/octet-stream");
   
   BodyDataSink bodyDataSink = httpClient.send(header, future);
   bodyDataSink.transferFrom(source);
   bodyDataSink.close();
   
   IHttpResponse response = future.getResponse(); // blocks until the response header is received
   if (response.getStatus() != 200) {
      throw new IOException("got status " + response.getStatus());
   }
 


Field Summary
 
Fields inherited from interface org.xlightweb.IHttpResponseHandler
DEFAULT_EXECUTION_MODE, DEFAULT_INVOKE_ON_MODE
 
Constructor Summary
FutureResponseHandler()
           
 
Method Summary
 boolean cancel(boolean mayInterruptIfRunning)
          Attempts to cancel execution of receiving the response.
 IHttpResponse get()
          blocking call to retrieve the response.
 IHttpResponse get(long timeout, TimeUnit unit)
          blocking call to retrieve the response.
 IHttpResponse getResponse()
          blocking call to retrieve the response.
 IHttpResponse getResponse(long timeout, TimeUnit unit)
          blocking call to retrieve the response.
 boolean isCancelled()
          Returns true if this task was cancelled before it completed normally.
 boolean isDone()
          Returns true if this task completed.
 void onException(IOException ioe)
          call back method which will be called if an io exception occurs
 void onException(SocketTimeoutException stoe)
          call back method which will be called if an socket timeout exception occurs
 void onResponse(IHttpResponse response)
          call back method which will be called if the response is received
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FutureResponseHandler

public FutureResponseHandler()
Method Detail

onResponse

public void onResponse(IHttpResponse response)
                throws IOException
call back method which will be called if the response is received

Specified by:
onResponse in interface IHttpResponseHandler
Parameters:
response - the response
Throws:
IOException

onException

public void onException(IOException ioe)
                 throws IOException
call back method which will be called if an io exception occurs

Specified by:
onException in interface IHttpResponseHandler
Parameters:
ioe - the io exception
Throws:
IOException

onException

public void onException(SocketTimeoutException stoe)
call back method which will be called if an socket timeout exception occurs

Specified by:
onException in interface IHttpSocketTimeoutHandler

getResponse

public IHttpResponse getResponse()
                          throws IOException,
                                 InterruptedException,
                                 SocketTimeoutException
blocking call to retrieve the response. Often this method will be used instead (Future.get()

Specified by:
getResponse in interface IFutureResponse
Returns:
the response
Throws:
IOException - if an ioe exception occurs
SocketTimeoutException - if an socket timeout exception occurs
InterruptedException - if the current thread was interrupted while waiting

getResponse

public IHttpResponse getResponse(long timeout,
                                 TimeUnit unit)
                          throws IOException,
                                 SocketTimeoutException
blocking call to retrieve the response. Often this method will be used instead (Future.get(long, TimeUnit)

Specified by:
getResponse in interface IFutureResponse
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the response
Throws:
IOException - if an ioe exception occurs
SocketTimeoutException - if an socket timeout exception occurs

get

public IHttpResponse get()
                  throws InterruptedException,
                         ExecutionException
blocking call to retrieve the response. Often the IFutureResponse.getResponse() method will be used instead of this method.

This method exists for compatibility reasons

Specified by:
get in interface Future<IHttpResponse>
Specified by:
get in interface IFutureResponse
Throws:
InterruptedException
ExecutionException

get

public IHttpResponse get(long timeout,
                         TimeUnit unit)
                  throws InterruptedException,
                         ExecutionException,
                         TimeoutException
blocking call to retrieve the response. Often the IFutureResponse.getResponse(long, TimeUnit) method will be used instead of this method.

This method exists for compatibility reasons

Specified by:
get in interface Future<IHttpResponse>
Specified by:
get in interface IFutureResponse
Throws:
InterruptedException
ExecutionException
TimeoutException

isDone

public boolean isDone()
Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation.

Specified by:
isDone in interface Future<IHttpResponse>
Specified by:
isDone in interface IFutureResponse
Returns:
true if this task completed.

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of receiving the response. This attempt will fail if the response has already received, already been cancelled, or could not be cancelled for some other reason. The mayInterruptIfRunning parameter determines whether the receiving process should be interrupted

Specified by:
cancel in interface Future<IHttpResponse>
Specified by:
cancel in interface IFutureResponse
Parameters:
mayInterruptIfRunning - true if the receiving process should be interrupted
Returns:
false if the receiving process could not be cancelled, typically because it has already completed normally; true otherwise

isCancelled

public boolean isCancelled()
Returns true if this task was cancelled before it completed normally.

Specified by:
isCancelled in interface Future<IHttpResponse>
Specified by:
isCancelled in interface IFutureResponse
Returns:
true if task was cancelled before it completed