com.jrefinery.chart.demo.servlet
Class BaseImageServlet

java.lang.Object
  |
  +--javax.servlet.GenericServlet
        |
        +--javax.servlet.http.HttpServlet
              |
              +--com.jrefinery.chart.demo.servlet.BaseImageServlet
All Implemented Interfaces:
Constants, java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
Direct Known Subclasses:
ImageDataAvailable, ImageDemoPie, ImageSelectSQL, JFreeServletDemo

public class BaseImageServlet
extends javax.servlet.http.HttpServlet
implements Constants

A Base image servlet generator. Used to provide common base and methods to servlets which need to generate a chart from sql data sources To extend overwrite method : generateSQL. Will attempt to get configuration constants from the firstly the servlet container configuration and then secondly the servlet's initialization parameters. The servlets initialization parameters taking precedence. Available configuration options are a. DBuser - The database user to connect as b. DBpwd - The password for the database user c. DBschema - The schema to utilise d. DBurl - The connection URL to the database e. DBdriver - The JDBC driver to utilise to connect to the database f. CacheType- Specifies the type of chart cache to utilise when generating HTML. (Optional, default of Dynamic) g. TemporaryDir - Specifies the directory to locate chart cache (Optional for Dynamic CacheTypes, mandatory for other types) h. TemporaryUrl - Specifies the url to the temporaryDir (Mandatory for Temp_Url CacheType, Optional for other types) i. HeaderUrl - The url for the header included in html page. may be file:// (Optional) j. FooterUrl - The url for the footer included in html page. may be file:// (Optional) The servlet will check to see if a connection is shared amongst the servlets context already. If not it will create a connection and share via the servlet container. Supports the following url options NAME TYPE NOTES type integer the type of chart to generate. eg moving average, linear fit etc under development. width integer The width of output in pixels. Clipped into the range 10-2000 height integer The height of the output in pixels. Clipped into the range 10-1000 initColor integer Between 0-11, used to indicate the initial shading of the background finalColor integer Between 0-11, used to indicate the final shading of the background title String The chart title xaxistitle String The x axis title yaxistitle String The y axis title zero String if passed a value of 'true' then the chart will include zero showLegend String if passed a value of 'false' then the chart will not include legend output String Selection of the type of output requested. jpeg, gif, svg CAUTION : THE FOLLOWING ARE UNTESTED / UNDER DEVELOPMENT 1. SVG support using apache batik. 2. PDF support 3. Charting options such as moving average, linear fit.

See Also:
JFreeChart, JFreeChartServletDemo, Serialized Form

Field Summary
protected  java.lang.String chartTitle
          Title of the chart
 java.lang.String dbSchema
           
protected  boolean debug
          Whether or not to enable debug information output
protected  int defaultChartType
           
protected static java.text.SimpleDateFormat df
           
static int IMAGE_CACHE_DELETE
          Cache Types : 0 : include through a servlet request to cache, do delete file after initial retrival 1 : include through a servlet request to cache, do not delete file after initial retrival 2 : include image through url reference, , do not delete file after initial retrival 3 : Dynamic (No ImageMap Support) 4 : Dynamic with imagemap support (Note requires chart to be generated twice)
static int IMAGE_CACHE_NO_DELETE
           
static int IMAGE_DYNAMIC_IMAGEMAP
           
static int IMAGE_DYNAMIC_NO_IMAGEMAP
           
static int IMAGE_TEMP_URL
           
protected  java.lang.String servletName
          The servlets name
protected  int sqlServerType
           
protected  java.lang.String xAxisTitle
          Title of the x axis
protected  java.lang.String yAxisTitle
          Title of the y axis
 
Fields inherited from interface com.jrefinery.chart.demo.servlet.Constants
CAUTION_FILE, CONTENT_TYPE, DB_CONNECTION, DB_DRIVER, DB_PASSWORD, DB_SCHEMA, DB_SERVER, DB_URL, DB_USER, DEBUG, FOOTER, FOOTER_FILE, FOOTER_URL, HEADER, HEADER_FILE, HEADER_URL, IMAGE_URL, MYSQL, ORACLE, ORGANISATION, REPLACE_SCHEMA, RESPONSE_HTML, RESPONSE_JPEG, RESPONSE_PDF, RESPONSE_PNG, RESPONSE_PS, RESPONSE_SVG, SHARED_DB, SHARED_DB_OPENER, SHARED_VOYAGES, SHARED_VOYAGES_TIME, TEMP_DIR, TEMP_URL
 
Constructor Summary
BaseImageServlet()
           
 
Method Summary
protected  JFreeChart createCategoryChart(int type, java.lang.String sql)
          Create a chart
protected  JFreeChart createChart(javax.servlet.http.HttpServletRequest request)
           
protected  JFreeChart createPieChart(int type, java.lang.String sql)
          Create a chart
protected  JFreeChart createXYChart(int type, java.lang.String sql)
          Create an XY chart
 void destroy()
          Shutdown the servlet
protected  void doCache(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
static void doErrorPage(javax.servlet.http.HttpServletResponse response, java.lang.String message)
          Write an error page as the reponse.
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Basic servlet method, answers requests from the browser.
protected  void doHtml(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
protected  void doImage(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Processes the HTTP request.
static boolean existsURL(java.lang.String URLName)
          This function will return true if a given URL exists by using the HTTP status code.
protected  java.lang.String generateSQL(javax.servlet.http.HttpServletRequest request)
          Generate the SQL required from the http request Override this method to generate the appropriate sql for a request.
static java.awt.Color getColor(int color)
          Gets the color attribute of the passed integer
protected static int getImageOutputType(javax.servlet.http.HttpServletRequest request)
           
protected static java.awt.Dimension getImageSize(javax.servlet.http.HttpServletRequest request)
           
static void includeFile(java.io.OutputStream out, java.io.File file)
          write the contents of a file to an output stream.
static void includeUrl(java.io.PrintWriter out, java.lang.String url)
          Include contents of a given url in output
 void init(javax.servlet.ServletConfig config)
          Override init() to set up data used by invocations of this servlet.
protected  void modifyChart(JFreeChart chart, javax.servlet.http.HttpServletRequest request)
          Modify a chart based on request parameters Override this method if you would like to modify the generated chart, Generally for chart look and feel.
protected  void openConnection()
          Open the database connection
protected  void statusMessage(java.lang.String message)
          Write a status message to the server console (stdout) if debug enabled
protected  boolean testConnection()
          Test the database connection is available
protected  ChartRenderingInfo writeChart(JFreeChart chart, java.io.OutputStream out, int outputType, java.awt.Dimension size)
           
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, 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

df

protected static final java.text.SimpleDateFormat df

sqlServerType

protected int sqlServerType

servletName

protected java.lang.String servletName
The servlets name

debug

protected boolean debug
Whether or not to enable debug information output

chartTitle

protected java.lang.String chartTitle
Title of the chart

xAxisTitle

protected java.lang.String xAxisTitle
Title of the x axis

yAxisTitle

protected java.lang.String yAxisTitle
Title of the y axis

defaultChartType

protected int defaultChartType

dbSchema

public java.lang.String dbSchema

IMAGE_CACHE_DELETE

public static final int IMAGE_CACHE_DELETE
Cache Types : 0 : include through a servlet request to cache, do delete file after initial retrival 1 : include through a servlet request to cache, do not delete file after initial retrival 2 : include image through url reference, , do not delete file after initial retrival 3 : Dynamic (No ImageMap Support) 4 : Dynamic with imagemap support (Note requires chart to be generated twice)

IMAGE_CACHE_NO_DELETE

public static final int IMAGE_CACHE_NO_DELETE

IMAGE_TEMP_URL

public static final int IMAGE_TEMP_URL

IMAGE_DYNAMIC_IMAGEMAP

public static final int IMAGE_DYNAMIC_IMAGEMAP

IMAGE_DYNAMIC_NO_IMAGEMAP

public static final int IMAGE_DYNAMIC_NO_IMAGEMAP
Constructor Detail

BaseImageServlet

public BaseImageServlet()
Method Detail

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  java.io.IOException
Basic servlet method, answers requests from the browser. Implementation is passed off to the doPost method.
Overrides:
doGet in class javax.servlet.http.HttpServlet
Parameters:
request - HTTPServletRequest
response - HTTPServletResponse
Throws:
javax.servlet.ServletException - Description of the Exception
java.io.IOException - Description of the Exception

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException,
                   java.io.IOException
Processes the HTTP request. Supports different types of output requests, which are specified through the 'output' parameter of the request. The types of output requests supported are: HTML - Generates the HTML output CACHE - Retrieves an image from the cache IMAGE - Generates and returns the image. (The default if none specified) Each output type is handled by calling the appropriate method: doHTML, doCache, and doImage.
Overrides:
doPost in class javax.servlet.http.HttpServlet
Parameters:
request - Description of the Parameter
response - Description of the Parameter
Throws:
javax.servlet.ServletException - Description of the Exception
java.io.IOException - Description of the Exception

modifyChart

protected void modifyChart(JFreeChart chart,
                           javax.servlet.http.HttpServletRequest request)
Modify a chart based on request parameters Override this method if you would like to modify the generated chart, Generally for chart look and feel.
Parameters:
chart - The Chart

generateSQL

protected java.lang.String generateSQL(javax.servlet.http.HttpServletRequest request)
Generate the SQL required from the http request Override this method to generate the appropriate sql for a request.
Parameters:
request - The servlets request parameters
Returns:
The generated sql string

createXYChart

protected JFreeChart createXYChart(int type,
                                   java.lang.String sql)
Create an XY chart
Parameters:
type - Type of chart to create. Currently not supported.
sql - SQL to execute. 1st column is x values, following columns are y values
Returns:
The chart of the data

createPieChart

protected JFreeChart createPieChart(int type,
                                    java.lang.String sql)
Create a chart
Parameters:
type - Type of chart to create. Currently not supported.
sql - SQL to execute. 1st column is category, followed by value
Returns:
The chart of the data

createCategoryChart

protected JFreeChart createCategoryChart(int type,
                                         java.lang.String sql)
Create a chart
Parameters:
type - Type of chart to create. Currently not supported.
sql - SQL to execute. 1st column is category, followed by value
Returns:
The chart of the data

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
Override init() to set up data used by invocations of this servlet.
Overrides:
init in class javax.servlet.GenericServlet
Parameters:
config - Description of the Parameter
Throws:
javax.servlet.ServletException - Description of the Exception

doHtml

protected void doHtml(javax.servlet.http.HttpServletRequest request,
                      javax.servlet.http.HttpServletResponse response)
               throws javax.servlet.ServletException,
                      java.io.IOException
Parameters:
request -  
response -  
Throws:
javax.servlet.ServletException -  
java.io.IOException -  

doCache

protected void doCache(javax.servlet.http.HttpServletRequest request,
                       javax.servlet.http.HttpServletResponse response)
                throws javax.servlet.ServletException,
                       java.io.IOException

doImage

protected void doImage(javax.servlet.http.HttpServletRequest request,
                       javax.servlet.http.HttpServletResponse response)
                throws javax.servlet.ServletException,
                       java.io.IOException
Parameters:
request -  
response -  
Throws:
javax.servlet.ServletException -  
java.io.IOException -  

writeChart

protected ChartRenderingInfo writeChart(JFreeChart chart,
                                        java.io.OutputStream out,
                                        int outputType,
                                        java.awt.Dimension size)

createChart

protected JFreeChart createChart(javax.servlet.http.HttpServletRequest request)

destroy

public void destroy()
Shutdown the servlet
Overrides:
destroy in class javax.servlet.GenericServlet

openConnection

protected void openConnection()
Open the database connection

testConnection

protected boolean testConnection()
Test the database connection is available
Returns:
Status of database connection

statusMessage

protected void statusMessage(java.lang.String message)
Write a status message to the server console (stdout) if debug enabled
Parameters:
message - the message to be written

doErrorPage

public static void doErrorPage(javax.servlet.http.HttpServletResponse response,
                               java.lang.String message)
Write an error page as the reponse.
Parameters:
response - The servlet response
message - The error message to be displayed

includeUrl

public static void includeUrl(java.io.PrintWriter out,
                              java.lang.String url)
Include contents of a given url in output
Parameters:
out - the stream to be written to
url - the url to be included

includeFile

public static void includeFile(java.io.OutputStream out,
                               java.io.File file)
write the contents of a file to an output stream.
Parameters:
out - the stream to be written to
file - the file to be included in the output

existsURL

public static boolean existsURL(java.lang.String URLName)
This function will return true if a given URL exists by using the HTTP status code. This is useful to bypass an error page generated by a Web server when trying to a non-existing page.

getColor

public static java.awt.Color getColor(int color)
Gets the color attribute of the passed integer
Parameters:
color - The colour index
Returns:
The color value

getImageOutputType

protected static int getImageOutputType(javax.servlet.http.HttpServletRequest request)

getImageSize

protected static java.awt.Dimension getImageSize(javax.servlet.http.HttpServletRequest request)