rabbit.handler
Class ImageHandlerBase

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

public abstract class ImageHandlerBase
extends BaseHandler

This handler first downloads the image runs convert on it and then serves the smaller image.

Author:
Robert Olofsson

Nested Class Summary
static class ImageHandlerBase.ImageConversionResult
           
 
Field Summary
protected  File convertedFile
           
 
Fields inherited from class rabbit.handler.BaseHandler
cacheChannel, clientHandle, con, content, entry, mayCache, mayFilter, request, response, size, tlh, totalRead
 
Constructor Summary
ImageHandlerBase()
          For creating the factory.
ImageHandlerBase(Connection con, TrafficLoggerHandler tlh, HttpHeader request, BufferHandle clientHandle, HttpHeader response, ResourceSource content, boolean mayCache, boolean mayFilter, long size, SProperties config, boolean doConvert, int minSizeToConvert)
          Create a new ImageHandler for the given request.
 
Method Summary
protected  void addCache()
          Set up the cache stream if available.
 boolean changesContentSize()
          ®return true this handler modifies the content.
protected  String checkFileType(File typeFile)
           
 void closeStreams(Process ps)
           
protected  void convertImage()
          Convert the image into a small low quality image (normally a jpeg).
protected  void finish(boolean good)
          clear up the mess we made (remove intermediate files etc).
 SProperties getConfig()
           
 boolean getDoConvert()
           
 int getMinSizeToConvert()
           
 void handle()
          Try to convert the image before letting the superclass handle it.
protected abstract  ImageHandlerBase.ImageConversionResult internalConvertImage(String entryName)
          Perform the actual image conversion.
protected  boolean mayCacheFromSize()
          Images needs to be cacheable to be compressed.
protected  boolean mayRestrictCacheSize()
          Check if this handler may force the cached resource to be less than the cache max size.
protected  void readImage()
          Read in the image
protected  void removeCache()
          Remove the cachestream and the cache entry.
 void setDoConvert(boolean doConvert)
           
 void setup(SProperties prop)
          setup the handler factory.
protected  void tryconvert()
          Try to convert the image.
 
Methods inherited from class rabbit.handler.BaseHandler
blockSent, bufferRead, deleteFile, failed, finishData, finishedRead, getLogger, getNewInstance, httpHeaderSent, mayTransfer, prepare, prepareStream, send, sendHeader, setPartialContent, timeout, writeCache
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

convertedFile

protected File convertedFile
Constructor Detail

ImageHandlerBase

public ImageHandlerBase()
For creating the factory.


ImageHandlerBase

public ImageHandlerBase(Connection con,
                        TrafficLoggerHandler tlh,
                        HttpHeader request,
                        BufferHandle clientHandle,
                        HttpHeader response,
                        ResourceSource content,
                        boolean mayCache,
                        boolean mayFilter,
                        long size,
                        SProperties config,
                        boolean doConvert,
                        int minSizeToConvert)
Create a new ImageHandler 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

changesContentSize

public boolean changesContentSize()
®return true this handler modifies the content.

Specified by:
changesContentSize in interface Handler
Overrides:
changesContentSize in class BaseHandler
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.

mayCacheFromSize

protected boolean mayCacheFromSize()
Images needs to be cacheable to be compressed.

Overrides:
mayCacheFromSize in class BaseHandler
Returns:
true

mayRestrictCacheSize

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

Overrides:
mayRestrictCacheSize in class BaseHandler
Returns:
false

handle

public void handle()
Try to convert the image before letting the superclass handle it.

Specified by:
handle in interface Handler
Overrides:
handle in class BaseHandler

addCache

protected void addCache()
Description copied from class: BaseHandler
Set up the cache stream if available.

Overrides:
addCache in class BaseHandler

finish

protected void finish(boolean good)
clear up the mess we made (remove intermediate files etc).

Overrides:
finish in class BaseHandler
Parameters:
good - if true then the connection may be restarted, if false then the connection may not be restared

removeCache

protected void removeCache()
Remove the cachestream and the cache entry.

Overrides:
removeCache in class BaseHandler

tryconvert

protected void tryconvert()
                   throws IOException
Try to convert the image. This is done like this: super.addCache (); readImage(); convertImage(); cacheChannel = null; We have to use the cachefile to convert the image, and if we convert it we dont want to write the file to the cache later on.

Throws:
IOException

readImage

protected void readImage()
                  throws IOException
Read in the image

Throws:
IOException - if reading of the image fails.

closeStreams

public void closeStreams(Process ps)
                  throws IOException
Throws:
IOException

convertImage

protected void convertImage()
Convert the image into a small low quality image (normally a jpeg).

Throws:
IOException - if conversion fails.

internalConvertImage

protected abstract ImageHandlerBase.ImageConversionResult internalConvertImage(String entryName)
                                                                        throws IOException
Perform the actual image conversion.

Parameters:
entryName - the filename of the cache entry to use.
Throws:
IOException

checkFileType

protected String checkFileType(File typeFile)
                        throws IOException
Throws:
IOException

setDoConvert

public void setDoConvert(boolean doConvert)

getDoConvert

public boolean getDoConvert()

getConfig

public SProperties getConfig()

getMinSizeToConvert

public int getMinSizeToConvert()

setup

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

Specified by:
setup in interface HandlerFactory
Overrides:
setup in class BaseHandler