org.xlightweb
Class BodyForwarder

Package class diagram package BodyForwarder
java.lang.Object
  extended by org.xlightweb.BodyForwarder
All Implemented Interfaces:
IBodyDataHandler

public class BodyForwarder
extends Object
implements IBodyDataHandler

Implementation base of a body forwarder. This class handles closing of he data source and exceptions by forwarding the body data. Example:

 
 class ForwardHandler implements IHttpRequestHandler {
 
   private HttpClient httpClient = ...
    
    
   // will be called if request header is received (Default-InvokeOn is HEADER_RECEIVED) 
   public void onRequest(IHttpExchange exchange) throws IOException {
      
      IHttpRequestHeader requestHeader = exchange.getRequest().getRequestHeader();
      NonBlockingBodyDataSource requestBodyChannel = exchange.getRequest().getNonBlockingBody();
      
       // forwards the request (header)
       BodyDataSink dataSink = httpClient.send(requestHeader);
       
       // defines the body forwarder
       BodyForwarder bodyForwarder = new BodyForwarder(requestBodyChannel, dataSink) {
       
          public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
             int available = bodyDataSink.available();
             ByteBuffer[] data  = bodyDataSink.readByteBufferByLength(available);
             
             for (ByteBuffer byteBuffer : data) {
                ByteBuffer copy = byteBuffer.duplicate();
                // ...
             }
             
             bodyDataSink.write(data);
          }
       };
       
       // assign the body forwarder to the body data source 
       requestBodyChannel.setDataHandler(bodyForwarder);
    }
 }
 


Field Summary
 
Fields inherited from interface org.xlightweb.IBodyDataHandler
DEFAULT_EXECUTION_MODE
 
Constructor Summary
BodyForwarder(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink)
          constructor
 
Method Summary
 void onComplete()
          call back which will be executed, if the body data source's end of data is reached
 boolean onData(NonBlockingBodyDataSource bodyDataSource)
          call back.
 void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink)
          call back method which will be called if the body data source contains a least one byte
 void onException(Exception e)
          call back which will be executed, if an exception is occurred
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BodyForwarder

public BodyForwarder(NonBlockingBodyDataSource bodyDataSource,
                     BodyDataSink bodyDataSink)
constructor

Parameters:
bodyDataSource - the body data source
bodyDataSink - the body data sink
Method Detail

onData

public final boolean onData(NonBlockingBodyDataSource bodyDataSource)
                     throws BufferUnderflowException
call back. This method will be called each time body data has been received for the given body

Specified by:
onData in interface IBodyDataHandler
Parameters:
bodyDataSource - the body
Returns:
true, if the event has been handled
Throws:
BufferUnderflowException - if more incoming data is required to process (e.g. delimiter hasn't yet received -> readByDelimiter methods or size of the available, received data is smaller than the required size -> readByLength). The BufferUnderflowException will be swallowed by the framework

onData

public void onData(NonBlockingBodyDataSource bodyDataSource,
                   BodyDataSink bodyDataSink)
            throws BufferUnderflowException,
                   IOException
call back method which will be called if the body data source contains a least one byte

Parameters:
bodyDataSource - the body data source
bodyDataSink - the body data sink
Throws:
BufferUnderflowException - if not enough data is available
IOException - if an exception occurs

onException

public void onException(Exception e)
call back which will be executed, if an exception is occurred

Parameters:
e - the excption

onComplete

public void onComplete()
call back which will be executed, if the body data source's end of data is reached