rabbit.handler
Class ImageHandler

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

public class ImageHandler
extends BaseHandler

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

Author:
Robert Olofsson

Field Summary
protected  File convertedFile
           
 
Fields inherited from class rabbit.handler.BaseHandler
cacheChannel, con, content, entry, mayCache, mayFilter, request, response, size, tlh, totalRead
 
Constructor Summary
ImageHandler()
          For creating the factory.
ImageHandler(Connection con, TrafficLoggerHandler tlh, HttpHeader request, HttpHeader response, ResourceSource content, boolean mayCache, boolean mayFilter, long size, SProperties config, boolean doConvert, int minSizeToConvert, ImageConverter imageConverter)
          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)
           
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()
          Get the current configuration of this handler
 boolean getDoConvert()
          Get the convert flag
 int getMinSizeToConvert()
          Only try to convert images larger than this size
 Handler getNewInstance(Connection con, TrafficLoggerHandler tlh, HttpHeader header, HttpHeader webHeader, ResourceSource content, boolean mayCache, boolean mayFilter, long size)
          Get a new Handler for the given request made.
 void handle()
          Try to convert the image before letting the superclass handle it.
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)
          Śet the convert flag
 void setup(SProperties prop, HttpProxy proxy)
          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, httpHeaderSent, mayTransfer, prepare, 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

ImageHandler

public ImageHandler()
For creating the factory.


ImageHandler

public ImageHandler(Connection con,
                    TrafficLoggerHandler tlh,
                    HttpHeader request,
                    HttpHeader response,
                    ResourceSource content,
                    boolean mayCache,
                    boolean mayFilter,
                    long size,
                    SProperties config,
                    boolean doConvert,
                    int minSizeToConvert,
                    ImageConverter imageConverter)
Create a new ImageHandler for the given request.

Parameters:
con - the Connection handling the request.
tlh - the logger for the data traffic
request - the actual request made.
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.
config - the configuration of this handler
doConvert - image comprssion will only be attempted if true
minSizeToConvert - images less than this many bytes are not compressed
imageConverter - the actual converter to use
Method Detail

getNewInstance

public Handler getNewInstance(Connection con,
                              TrafficLoggerHandler tlh,
                              HttpHeader header,
                              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
Overrides:
getNewInstance in class BaseHandler
Parameters:
con - the Connection handling the request.
tlh - the Traffic logger handler.
header - the 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).
Returns:
the new Handler

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()
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.


readImage

protected void readImage()
Read in the image


convertImage

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


checkFileType

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

setDoConvert

public void setDoConvert(boolean doConvert)
Śet the convert flag

Parameters:
doConvert - if true then image conversion will be tried

getDoConvert

public boolean getDoConvert()
Get the convert flag

Returns:
true if image conversion should be used

getConfig

public SProperties getConfig()
Get the current configuration of this handler

Returns:
the current configuration

getMinSizeToConvert

public int getMinSizeToConvert()
Only try to convert images larger than this size

Returns:
the minimum size of images to run conversion on

setup

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

Specified by:
setup in interface HandlerFactory
Overrides:
setup in class BaseHandler
Parameters:
prop - the properties for this factory
proxy - the HttpProxy using this HandlerFactory