com.vmax.servlet.core
Class CachedGraphicsServlet

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--com.vmax.servlet.core.CachedServlet
                    |
                    +--com.vmax.servlet.core.CachedGraphicsServlet
All Implemented Interfaces:
java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
FramerServlet, T2IServlet

public abstract class CachedGraphicsServlet
extends com.vmax.servlet.core.CachedServlet

This class provides basic functinality for Graphics servlets suite. To implement your own graphics servlet you only need to override 3 abstract methods

Author:
virtual_max (aka virtual_max@geocities.com)
See Also:
Serialized Form

Field Summary
static int ALIGN_BOTTOM
          bottom alignment
static int ALIGN_CENTER
          center horizontal alignment
static int ALIGN_LEFT
          Left alignment
static int ALIGN_MIDDLE
          Middle verrtial allignment
static int ALIGN_RIGHT
          right alinment
static int ALIGN_TOP
          TOP alignment
static long DAY
           
static long HOUR
           
static long MONTH
           
static java.lang.String TTL
           
static long WEEK
           
static long YEAR
           
 
Constructor Summary
CachedGraphicsServlet()
           
 
Method Summary
protected  java.lang.String countHash(BasicHttpRequest req, java.lang.String[] parameterNames)
           
 void destroy()
           
 void drawLines(java.awt.Graphics2D graphics, java.util.List lines, java.awt.Rectangle text_rect, java.awt.FontMetrics fm, int align)
          Method to draw text lines
abstract  java.lang.String[] getActualParameterNames()
          This method supposed to return array of parameters names which are actual parameters for your servlet, this is necessary because request may contain other parameters which are not in use.
 int getAlignParameter(BasicHttpRequest req, java.lang.String name, int deft)
          Usefull function to obtain alignment parameters.
 java.lang.String getBasePath()
           
 java.awt.Color getColorParam(BasicHttpRequest req, java.lang.String name, int deft)
          Usefull function to obtain parameter value from request as hex value of color in aarrggbb format and construct Color object.
abstract  java.lang.String getContentType(javax.servlet.http.HttpServletRequest req)
          This method is used to get content type for setting response content type.
protected  long getExpiration(javax.servlet.http.HttpServletRequest req)
           
 java.io.File getFontsDir()
          method to get fonts directory defined with servlet init parameter fonts_dir
 int getHexParam(BasicHttpRequest req, java.lang.String name, int deft)
          Usefull function to obtain String parameter value from request interpreting it as hexadecimal string or use default.
 java.awt.image.BufferedImage getImageParam(BasicHttpRequest req, java.lang.String name)
          Usful method to get parameter value as image name and looad image from backgrounds directory.
 java.awt.FontMetrics getMetrics(java.awt.Font font)
          Function to obtain font metrics for futher dimensions calculations
 int getParam(BasicHttpRequest req, java.lang.String name, int deft)
          Usefull function to obtain int parameter value interpreting it as decimal integer string from request or use default.
 java.lang.String getParam(BasicHttpRequest req, java.lang.String name, java.lang.String deft)
          Usefull function to obtain String parameter value from request or use default.
 java.awt.Dimension getRequiredRect(java.util.List lines, java.awt.FontMetrics fm, int minWidth, int minHeight)
          Method to obtain dimensions required to fit List of lines
static java.io.File getWorkingDir(javax.servlet.ServletContext context, java.lang.String paramName, java.lang.String deft, boolean create)
           
 java.io.File getWorkingDir(java.lang.String paramName, java.lang.String deft, boolean create)
           
 void init(javax.servlet.ServletConfig servletConfig)
           
 java.awt.Font loadFontFromPath(java.lang.String fontpath, int fontSize)
          Function to get Font.
 java.lang.String seekParam(BasicHttpRequest req, java.lang.String name)
           
abstract  void serve(BasicHttpRequest req, java.io.ByteArrayOutputStream baos)
          Basic painting method to implement pinting logic.
protected  void service(BasicHttpRequest req, javax.servlet.http.HttpServletResponse res, long expiration)
           
 void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)
           
 void setRenderingHints(java.awt.Graphics2D g)
          Set optimal rendering parameters for generating graphics
protected  void setResponseHeaders(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          If you need to add specific headers to response, than override this method
 java.util.ArrayList splitText(java.lang.String text, java.awt.FontMetrics fm, int w)
          Function to split text in multiline in desired width using given fontMetrics.
static void tileBackground(java.awt.image.BufferedImage im, java.awt.Graphics2D graphics, java.awt.image.BufferedImage bgim, int align)
          Method to tile background image over given graphics object
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
 
Methods inherited from class javax.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALIGN_TOP

public static final int ALIGN_TOP
TOP alignment

See Also:
Constant Field Values

ALIGN_MIDDLE

public static final int ALIGN_MIDDLE
Middle verrtial allignment

See Also:
Constant Field Values

ALIGN_BOTTOM

public static final int ALIGN_BOTTOM
bottom alignment

See Also:
Constant Field Values

ALIGN_LEFT

public static final int ALIGN_LEFT
Left alignment

See Also:
Constant Field Values

ALIGN_CENTER

public static final int ALIGN_CENTER
center horizontal alignment

See Also:
Constant Field Values

ALIGN_RIGHT

public static final int ALIGN_RIGHT
right alinment

See Also:
Constant Field Values

TTL

public static final java.lang.String TTL
See Also:
Constant Field Values

YEAR

public static final long YEAR
See Also:
Constant Field Values

MONTH

public static final long MONTH
See Also:
Constant Field Values

WEEK

public static final long WEEK
See Also:
Constant Field Values

DAY

public static final long DAY
See Also:
Constant Field Values

HOUR

public static final long HOUR
See Also:
Constant Field Values
Constructor Detail

CachedGraphicsServlet

public CachedGraphicsServlet()
Method Detail

getActualParameterNames

public abstract java.lang.String[] getActualParameterNames()
This method supposed to return array of parameters names which are actual parameters for your servlet, this is necessary because request may contain other parameters which are not in use. This parameters values will be used for generating cache entry key.

Overrides:
getActualParameterNames in class com.vmax.servlet.core.CachedServlet
Returns:
array of strings containing parameter names.

getContentType

public abstract java.lang.String getContentType(javax.servlet.http.HttpServletRequest req)
This method is used to get content type for setting response content type.

Overrides:
getContentType in class com.vmax.servlet.core.CachedServlet
Parameters:
req - - original http request
Returns:
content type to set in response

serve

public abstract void serve(BasicHttpRequest req,
                           java.io.ByteArrayOutputStream baos)
                    throws java.lang.Exception
Basic painting method to implement pinting logic.

Overrides:
serve in class com.vmax.servlet.core.CachedServlet
Parameters:
req - - request wrapper
baos - - output stream to write image data
Throws:
java.lang.Exception

init

public void init(javax.servlet.ServletConfig servletConfig)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class com.vmax.servlet.core.CachedServlet
javax.servlet.ServletException

setRenderingHints

public void setRenderingHints(java.awt.Graphics2D g)
Set optimal rendering parameters for generating graphics

Parameters:
g -

loadFontFromPath

public java.awt.Font loadFontFromPath(java.lang.String fontpath,
                                      int fontSize)
                               throws java.io.IOException,
                                      java.awt.FontFormatException
Function to get Font. Path may be absolute or relative to project base directory

Parameters:
fontpath - - name of font file or path to font file, relative to fonts_dir
fontSize - - font sixe in pixels
Returns:
- Font object.
Throws:
java.io.IOException
java.awt.FontFormatException

getMetrics

public java.awt.FontMetrics getMetrics(java.awt.Font font)
Function to obtain font metrics for futher dimensions calculations


getColorParam

public java.awt.Color getColorParam(BasicHttpRequest req,
                                    java.lang.String name,
                                    int deft)
Usefull function to obtain parameter value from request as hex value of color in aarrggbb format and construct Color object. This function seek for parameter in the following order:
first - parameter passed in traditional way (GET or POST)
second - parameter passed via path info as <servletName>/param1/value1/param2/value2
third - check for properties file optionaly specified with wtk_defaults parameter for default of this value forth - attempt to get default value from serlet init parameters
last - use default value passed to function

Parameters:
req - - request
name - - parameter name
deft - - default value
Returns:
- Color object

getParam

public java.lang.String getParam(BasicHttpRequest req,
                                 java.lang.String name,
                                 java.lang.String deft)
Usefull function to obtain String parameter value from request or use default. This function seek for parameter in the following order:
first - parameter passed in traditional way (GET or POST)
second - parameter passed via path info as <servletName>/param1/value1/param2/value2
third - check for properties file optionaly specified with wtk_defaults parameter for default of this value forth - attempt to get default value from serlet init parameters
last - use default value passed to function

Parameters:
req - - request
name - - parameter name
deft - - default value
Returns:
- parameter value

getParam

public int getParam(BasicHttpRequest req,
                    java.lang.String name,
                    int deft)
Usefull function to obtain int parameter value interpreting it as decimal integer string from request or use default. This function seek for parameter in the following order:
first - parameter passed in traditional way (GET or POST)
second - parameter passed via path info as <servletName>/param1/value1/param2/value2
third - check for properties file optionaly specified with wtk_defaults parameter for default of this value forth - attempt to get default value from serlet init parameters
last - use default value passed to function

Parameters:
req - - request
name - - parameter name
deft - - default value
Returns:
- integer parameter value

getHexParam

public int getHexParam(BasicHttpRequest req,
                       java.lang.String name,
                       int deft)
Usefull function to obtain String parameter value from request interpreting it as hexadecimal string or use default. This function seek for parameter in the following order:
first - parameter passed in traditional way (GET or POST)
second - parameter passed via path info as <servletName>/param1/value1/param2/value2
third - check for properties file optionaly specified with wtk_defaults parameter for default of this value forth - attempt to get default value from serlet init parameters
last - use default value passed to function

Parameters:
req - - request
name - - parameter name
deft - - default value
Returns:
- hex parameter value

getAlignParameter

public int getAlignParameter(BasicHttpRequest req,
                             java.lang.String name,
                             int deft)
Usefull function to obtain alignment parameters. It check string parameter value for keywords
top,middle,bottom,left,right and center. This function seek for parameter in the following order:
first - parameter passed in traditional way (GET or POST)
second - parameter passed via path info as <servletName>/param1/value1/param2/value2
third - check for properties file optionaly specified with wtk_defaults parameter for default of this value forth - attempt to get default value from serlet init parameters
last - use default value passed to function

Parameters:
req - - request
name - - parameter name
deft - - default value
Returns:
- alligment integer value, combination (by OR) if alingment constants defined in this class

splitText

public java.util.ArrayList splitText(java.lang.String text,
                                     java.awt.FontMetrics fm,
                                     int w)
Function to split text in multiline in desired width using given fontMetrics. This function also trims spaces and force split when \n character is found. When width == 0 then split only on new line chars.

Parameters:
text -
fm -
w -
Returns:
- arrayList of text rows

getRequiredRect

public java.awt.Dimension getRequiredRect(java.util.List lines,
                                          java.awt.FontMetrics fm,
                                          int minWidth,
                                          int minHeight)
Method to obtain dimensions required to fit List of lines

Parameters:
lines - - ordered List of text lines
fm - - font metrics to use for text dimensions calculations
minWidth - - minimum Width to return
minHeight - - minimumHeight to return
Returns:
Dimension enough to fit text rows but not less then spicified minimums.

drawLines

public void drawLines(java.awt.Graphics2D graphics,
                      java.util.List lines,
                      java.awt.Rectangle text_rect,
                      java.awt.FontMetrics fm,
                      int align)
Method to draw text lines

Parameters:
graphics - - graphics object to draw on.
lines - - ordered List of lines
text_rect - - rectangle to define offsets and dimensions of text alignment bounds.
fm - - font metrics object for size calculations
align - - alignment parameter

getImageParam

public java.awt.image.BufferedImage getImageParam(BasicHttpRequest req,
                                                  java.lang.String name)
Usful method to get parameter value as image name and looad image from backgrounds directory.

Parameters:
req - - http request
name - - image file name
Returns:
- Buffered image or null

tileBackground

public static void tileBackground(java.awt.image.BufferedImage im,
                                  java.awt.Graphics2D graphics,
                                  java.awt.image.BufferedImage bgim,
                                  int align)
Method to tile background image over given graphics object

Parameters:
im - - image to paint over, passed only to obtain it's dimensions
graphics - - graphics object to draw on
bgim - - background image to tile
align - - background alignment.

getFontsDir

public java.io.File getFontsDir()
method to get fonts directory defined with servlet init parameter fonts_dir

Returns:
directory

setResponseHeaders

protected void setResponseHeaders(javax.servlet.http.HttpServletRequest req,
                                  javax.servlet.http.HttpServletResponse res)
If you need to add specific headers to response, than override this method

Parameters:
req -
res -

countHash

protected java.lang.String countHash(BasicHttpRequest req,
                                     java.lang.String[] parameterNames)
                              throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException

getWorkingDir

public java.io.File getWorkingDir(java.lang.String paramName,
                                  java.lang.String deft,
                                  boolean create)

getWorkingDir

public static java.io.File getWorkingDir(javax.servlet.ServletContext context,
                                         java.lang.String paramName,
                                         java.lang.String deft,
                                         boolean create)

service

protected void service(BasicHttpRequest req,
                       javax.servlet.http.HttpServletResponse res,
                       long expiration)
                throws javax.servlet.ServletException,
                       java.io.IOException
javax.servlet.ServletException
java.io.IOException

service

public void service(javax.servlet.ServletRequest req,
                    javax.servlet.ServletResponse res)
             throws javax.servlet.ServletException,
                    java.io.IOException
Specified by:
service in interface javax.servlet.Servlet
Overrides:
service in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException

getExpiration

protected long getExpiration(javax.servlet.http.HttpServletRequest req)

getBasePath

public java.lang.String getBasePath()

destroy

public void destroy()
Specified by:
destroy in interface javax.servlet.Servlet
Overrides:
destroy in class javax.servlet.GenericServlet

seekParam

public java.lang.String seekParam(BasicHttpRequest req,
                                  java.lang.String name)