rabbit.handler
Class BaseHandler

java.lang.Object
  extended by rabbit.handler.BaseHandler
All Implemented Interfaces:
Handler, HandlerFactory, Logger
Direct Known Subclasses:
GZIPHandler, ImageHandler, MultiPartHandler

public class BaseHandler
extends java.lang.Object
implements Handler, HandlerFactory, Logger

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


Field Summary
protected  java.io.OutputStream cacheStream
          The cache stream if available.
protected  MultiOutputStream clientstream
          The stream to send data to.
protected  Connection con
          The Connection handling the request.
protected  HTTPInputStream contentstream
          The stream to read data from.
protected  NCacheEntry 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.
 
Fields inherited from interface rabbit.util.Logger
ALL, DEBUG, ERROR, FATAL, INFO, MSG, WARN
 
Constructor Summary
BaseHandler()
          For creating the factory.
BaseHandler(Connection con, HTTPHeader request, HTTPHeader response, HTTPInputStream contentstream, MultiOutputStream clientstream, boolean maycache, boolean mayfilter, long size)
          Create a new BaseHansler for the given request.
 
Method Summary
protected  void addCacheStream()
          Set up the cache stream if available.
protected  boolean clientStreamHasCache()
          Check if the client stream also have a cache stream.
protected  void finish()
          Close nesseccary files and adjust the cached files.
protected  void finishStream()
          This method is used to finish the stream for the data being sent.
static HandlerFactory getFactory()
          Get a HandlerFactory that creates BaseHandlers.
 Handler getNewInstance(Connection connection, HTTPHeader header, HTTPHeader webheader, HTTPInputStream contentStream, MultiOutputStream out, boolean maycache, boolean mayfilter, long size)
          Get a new Handler for the given request made.
 void handle()
          Handle the request.
 void logError(int type, java.lang.String error)
          Log an error of given type and with given message.
 void logError(java.lang.String error)
          Log an error.
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  void prepareStream()
          This method is used to prepare the stream for the data being sent.
protected  void removeCache(java.lang.Exception e)
          Remove the cachestream and the cache entry.
protected  void send()
          Send the actual data.
protected  void setPartialContent(long got, long shouldbe)
          Mark the current response as a partial response.
 void setup(Logger logger, SProperties properties)
          Setup the factory, this method does nothing in this class.
protected  void writeData(byte[] v, int off, int len)
          Write the data to the client.
protected  void writeHeader()
          Write the response header
 
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.


request

protected HTTPHeader request
The actual request made.


response

protected HTTPHeader response
The actual response.


contentstream

protected HTTPInputStream contentstream
The stream to read data from.


clientstream

protected MultiOutputStream clientstream
The stream to send data to.


maycache

protected boolean maycache
May we cache this request.


mayfilter

protected boolean mayfilter
May we filter this request


entry

protected NCacheEntry entry
The cache entry if available.


cacheStream

protected java.io.OutputStream cacheStream
The cache stream if available.


size

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

Constructor Detail

BaseHandler

public BaseHandler()
For creating the factory.


BaseHandler

public BaseHandler(Connection con,
                   HTTPHeader request,
                   HTTPHeader response,
                   HTTPInputStream contentstream,
                   MultiOutputStream clientstream,
                   boolean maycache,
                   boolean mayfilter,
                   long size)
Create a new BaseHansler for the given request.

Parameters:
con - the Connection handling the request.
request - the actual request made.
response - the actual response.
contentstream - the stream to read data from.
clientstream - the stream to write data to.
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 connection,
                              HTTPHeader header,
                              HTTPHeader webheader,
                              HTTPInputStream contentStream,
                              MultiOutputStream out,
                              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:
connection - the Connection handling the request.
header - the request.
webheader - the response.
contentStream - the data to read.
out - the client (and possibly the cache).
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).

getFactory

public static HandlerFactory getFactory()
Get a HandlerFactory that creates BaseHandlers.


writeHeader

protected void writeHeader()
                    throws java.io.IOException
Write the response header

Throws:
java.io.IOException - if writing the response fails.

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

addCacheStream

protected void addCacheStream()
                       throws java.io.IOException
Set up the cache stream if available.

Throws:
java.io.IOException - if a cachestream couldnt be set up.

prepareStream

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

Throws:
java.io.IOException

writeData

protected void writeData(byte[] v,
                         int off,
                         int len)
                  throws java.io.IOException
Write the data to the client. Handle that the cached resource gets too big.

Throws:
java.io.IOException

send

protected void send()
             throws java.io.IOException
Send the actual data.

Throws:
java.io.IOException - if reading or writing of the data fails.

setPartialContent

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


finishStream

protected void finishStream()
                     throws java.io.IOException
This method is used to finish the stream for the data being sent. This method does nothing here.

Throws:
java.io.IOException

clientStreamHasCache

protected boolean clientStreamHasCache()
Check if the client stream also have a cache stream.


finish

protected void finish()
               throws java.io.IOException
Close nesseccary files and adjust the cached files. If you override this one, remember to call super.finish ()!

Throws:
java.io.IOException - if closing the files does.

handle

public void handle()
            throws java.io.IOException
Handle the request. A request is made in these steps: writeHeader (); addCacheStream (); prepareStream (); send (); finishStream (); finish ();

Specified by:
handle in interface Handler
Throws:
java.io.IOException - if any of the underlying methods does.

removeCache

protected void removeCache(java.lang.Exception e)
Remove the cachestream and the cache entry. Use this to clean up resources held, but not released due to IOExceptions.

Parameters:
e - the Exception that happened

setup

public void setup(Logger logger,
                  SProperties properties)
Setup the factory, this method does nothing in this class.

Specified by:
setup in interface HandlerFactory

logError

public void logError(java.lang.String error)
Description copied from interface: Logger
Log an error. Same as logError (ERROR, error);

Specified by:
logError in interface Logger

logError

public void logError(int type,
                     java.lang.String error)
Description copied from interface: Logger
Log an error of given type and with given message.

Specified by:
logError in interface Logger
Parameters:
type - the error level.
error - the error message.