rabbit.handler
Class BaseHandler

java.lang.Object
  extended by rabbit.handler.BaseHandler
All Implemented Interfaces:
Handler, HandlerFactory, AsyncListener, BlockListener, BlockSentListener, HttpHeaderSentListener
Direct Known Subclasses:
GZipHandler, ImageHandler, MultiPartHandler

public class BaseHandler
extends Object
implements Handler, HandlerFactory, HttpHeaderSentListener, BlockListener, BlockSentListener

This class is an implementation of the Handler interface. This handler does no filtering, it only sends the data as effective as it can.

Author:
Robert Olofsson

Field Summary
protected  WritableByteChannel cacheChannel
          The cache channel.
protected  BufferHandle clientHandle
          The client buffer.
protected  Connection con
          The Connection handling the request.
protected  ResourceSource content
          The resource
protected  CacheEntry<HttpHeader,HttpHeader> entry
          The cache entry if available.
protected  boolean mayCache
          May we cache this request.
protected  boolean mayFilter
          May we filter this request
protected  HttpHeader request
          The actual request made.
protected  HttpHeader response
          The actual response.
protected  long size
          The length of the data beeing handled or -1 if unknown.
protected  TrafficLoggerHandler tlh
          The traffic logger handler.
protected  long totalRead
          The total amount of data that we read.
 
Constructor Summary
BaseHandler()
          For creating the factory.
BaseHandler(Connection con, TrafficLoggerHandler tlh, HttpHeader request, BufferHandle clientHandle, HttpHeader response, ResourceSource content, boolean mayCache, boolean mayFilter, long size)
          Create a new BaseHandler for the given request.
 
Method Summary
protected  void addCache()
          Set up the cache stream if available.
 void blockSent()
          The http header has been sent.
 void bufferRead(BufferHandle bufHandle)
          A buffer has been read, the buffer has been flip:ed before this call is made so position and remaining are valid.
 boolean changesContentSize()
          ┬«return false if this handler never modifies the content.
protected  void deleteFile(File f)
           
 void failed(Exception cause)
          Reading failed
protected  void finish(boolean good)
          Close nesseccary channels and adjust the cached files.
protected  void finishData()
          This method is used to finish the data for the resource being sent.
 void finishedRead()
          The resource have been fully transferred
protected  Logger getLogger()
           
 Handler getNewInstance(Connection con, TrafficLoggerHandler tlh, HttpHeader header, BufferHandle bufHandle, HttpHeader webHeader, ResourceSource content, boolean mayCache, boolean mayFilter, long size)
          Get a new Handler for the given request made.
 void handle()
          Handle the request.
 void httpHeaderSent()
          The http header has been sent.
protected  boolean mayCacheFromSize()
          Try to use the resource size to decide if we may cache or not.
protected  boolean mayRestrictCacheSize()
          Check if this handler may force the cached resource to be less than the cache max size.
protected  boolean mayTransfer()
          Check if this handler supports direct transfers.
protected  void prepare()
          This method is used to prepare the data for the resource being sent.
protected  void prepareStream()
          This method is used to prepare the stream for the data being sent.
protected  void removeCache()
           
protected  void send()
           
protected  void sendHeader()
           
protected  void setPartialContent(long got, long shouldbe)
          Mark the current response as a partial response.
 void setup(SProperties properties)
          setup the handler factory.
 void timeout()
          The operation timed out
protected  void writeCache(ByteBuffer buf)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

con

protected Connection con
The Connection handling the request.


tlh

protected TrafficLoggerHandler tlh
The traffic logger handler.


request

protected HttpHeader request
The actual request made.


clientHandle

protected BufferHandle clientHandle
The client buffer.


response

protected HttpHeader response
The actual response.


content

protected ResourceSource content
The resource


entry

protected CacheEntry<HttpHeader,HttpHeader> entry
The cache entry if available.


cacheChannel

protected WritableByteChannel cacheChannel
The cache channel.


mayCache

protected boolean mayCache
May we cache this request.


mayFilter

protected boolean mayFilter
May we filter this request


size

protected long size
The length of the data beeing handled or -1 if unknown.


totalRead

protected long totalRead
The total amount of data that we read.

Constructor Detail

BaseHandler

public BaseHandler()
For creating the factory.


BaseHandler

public BaseHandler(Connection con,
                   TrafficLoggerHandler tlh,
                   HttpHeader request,
                   BufferHandle clientHandle,
                   HttpHeader response,
                   ResourceSource content,
                   boolean mayCache,
                   boolean mayFilter,
                   long size)
Create a new BaseHandler for the given request.

Parameters:
con - the Connection handling the request.
request - the actual request made.
clientHandle - the client side buffer.
response - the actual response.
content - the resource.
mayCache - May we cache this request?
mayFilter - May we filter this request?
size - the size of the data beeing handled.
Method Detail

getNewInstance

public Handler getNewInstance(Connection con,
                              TrafficLoggerHandler tlh,
                              HttpHeader header,
                              BufferHandle bufHandle,
                              HttpHeader webHeader,
                              ResourceSource content,
                              boolean mayCache,
                              boolean mayFilter,
                              long size)
Description copied from interface: HandlerFactory
Get a new Handler for the given request made.

Specified by:
getNewInstance in interface HandlerFactory
Parameters:
con - the Connection handling the request.
tlh - the Traffic logger handler.
header - the request.
bufHandle - the client side buffer handle (may contain the next request).
webHeader - the response.
content - the resource.
mayCache - if the handler may cache the response.
mayFilter - if the handler may filter the response.
size - the Size of the data beeing handled (-1 = unknown length).

getLogger

protected Logger getLogger()

handle

public void handle()
Handle the request. A request is made in these steps: sendHeader (); addCache (); prepare (); send (); finishData (); finish (); Note that finish is always called, no matter what exceptions are thrown. The middle steps are most probably only performed if the previous steps have all succeded

Specified by:
handle in interface Handler

changesContentSize

public boolean changesContentSize()
®return false if this handler never modifies the content.

Specified by:
changesContentSize in interface Handler
Returns:
true if Content-Lenght may be changed by this handler typically used for handlers that may modify the content. Return false if this handler will not change the size.

sendHeader

protected void sendHeader()

httpHeaderSent

public void httpHeaderSent()
Description copied from interface: HttpHeaderSentListener
The http header has been sent.

Specified by:
httpHeaderSent in interface HttpHeaderSentListener

prepare

protected void prepare()
This method is used to prepare the data for the resource being sent. This method does nothing here.


finishData

protected void finishData()
This method is used to finish the data for the resource being sent. This method will send an end chunk if needed and then call finish


setPartialContent

protected void setPartialContent(long got,
                                 long shouldbe)
Mark the current response as a partial response.


finish

protected void finish(boolean good)
Close nesseccary channels and adjust the cached files. If you override this one, remember to call super.finish ()!

Parameters:
good - if true then the connection may be restarted, if false then the connection may not be restared

mayCacheFromSize

protected boolean mayCacheFromSize()
Try to use the resource size to decide if we may cache or not. If the size is known and the size is bigger than the maximum cache size, then we dont want to cache the resource.


mayRestrictCacheSize

protected boolean mayRestrictCacheSize()
Check if this handler may force the cached resource to be less than the cache max size.

Returns:
true

addCache

protected void addCache()
Set up the cache stream if available.


prepareStream

protected void prepareStream()
This method is used to prepare the stream for the data being sent. This method does nothing here.


mayTransfer

protected boolean mayTransfer()
Check if this handler supports direct transfers.

Returns:
this handler always return true.

send

protected void send()

writeCache

protected void writeCache(ByteBuffer buf)
                   throws IOException
Throws:
IOException

bufferRead

public void bufferRead(BufferHandle bufHandle)
Description copied from interface: BlockListener
A buffer has been read, the buffer has been flip:ed before this call is made so position and remaining are valid.

Specified by:
bufferRead in interface BlockListener

blockSent

public void blockSent()
Description copied from interface: BlockSentListener
The http header has been sent.

Specified by:
blockSent in interface BlockSentListener

finishedRead

public void finishedRead()
Description copied from interface: BlockListener
The resource have been fully transferred

Specified by:
finishedRead in interface BlockListener

deleteFile

protected void deleteFile(File f)

removeCache

protected void removeCache()

failed

public void failed(Exception cause)
Description copied from interface: AsyncListener
Reading failed

Specified by:
failed in interface AsyncListener
Parameters:
cause - the real reason the operation failed.

timeout

public void timeout()
Description copied from interface: AsyncListener
The operation timed out

Specified by:
timeout in interface AsyncListener

setup

public void setup(SProperties properties)
Description copied from interface: HandlerFactory
setup the handler factory.

Specified by:
setup in interface HandlerFactory