MDi JLib
1.7.4

com.microdeveloper.servlets.io
Class DbFileDriver

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.microdeveloper.servlets.io.DbFileDriver
All Implemented Interfaces:
Serializable, Servlet, ServletConfig

public abstract class DbFileDriver
extends HttpServlet
implements Servlet

Abstract servlet for SQL output as download responses in the form of a file download. Extend this class as the basis for taking SQL queries and sending the formatted results back to the client in the form of a file download. The type sent is controlled by the response header which is defined in the returnFile method. The connection to the database is managed by the db package and referenced in this class. The connection criteria is derived from the environment by locating the session variables, validated, userId, password, and dbUrlPrefix. If the user is not validated, the standard response is to redirect to the site index.

Version:
1.0
Author:
Gregg Lagnese
See Also:
Serialized Form

Field Summary
protected  String[] credentials
          Full database credential string
protected  String CRLF
          Field CRLF
protected  String ERRAUTHORIZED
          Error response given when the user attempts to run a report for which they have no authorization
protected  String ERRDBCONN
          Error response when the database connection fails
protected  String ERRHEADING
          Error response heading
protected  String ERRINITPARM
          Error response given when an invalid open parameter is processed
protected  String ERRNODATA
          Error response when no data is found that match the report criteria
protected  String ERRNOPARMS
          Error response when no parameters are given and parameters are expected
protected  String ERRSQL
          Error response given when the SQL instruction fails
protected  String ERRVALIDATED
          Error response when the user is not validated.
protected  String outFile
          Fully qualified filename of the desired output file
protected  String parameterForm
          URL to the parameter form if any
protected  HashMap parameterMap
          Parameter map for a given report
protected  String reportName
          Name of the report
protected  HttpSession session
          HTTP session variable
protected  String TITLE
          Report title
 
Constructor Summary
DbFileDriver()
           
 
Method Summary
 void destroy()
          Destroys the servlet.
protected  void doGet(HttpServletRequest request, HttpServletResponse response)
          Handles the HTTP GET method.
protected  void doPost(HttpServletRequest request, HttpServletResponse response)
          Handles the HTTP POST method.
 HashMap getParameterMap(HttpServletRequest request, HttpServletResponse response)
          Returns the parameter map for the servlet
 String getParameterValue(Object key)
          Returns the value for a given parameter.
 String getServletInfo()
          Returns a short description of the servlet.
 void init(ServletConfig config)
          Initializes the servlet.
 boolean isAuthorized(String reportName)
          Determines if the user is authorized to run the report or not
 boolean isKeyInMap(Object key)
          Determines if the parameter object key exists in the parameter map or not
 void parameterPage(HttpServletRequest request, HttpServletResponse response)
          Returns the parameter page for the give report servlet.
protected  void processRequest(HttpServletRequest request, HttpServletResponse response)
          Processes requests for both HTTP GET and POST methods.
 void returnError(String message, HttpServletRequest request, HttpServletResponse response)
          Returns the error response using a standard HTML response template.
abstract  void returnFile(Connection conn, HttpServletRequest request, HttpServletResponse response)
          Returns the result of the SQL query as a file download.
 void writeOutput(PreparedStatement stmt, HttpServletRequest request, HttpServletResponse response)
          Writes the output to the response writer.
 
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, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.Servlet
getServletConfig, service
 

Field Detail

parameterMap

protected HashMap parameterMap
Parameter map for a given report


TITLE

protected final String TITLE
Report title

See Also:
Constant Field Values

ERRHEADING

protected final String ERRHEADING
Error response heading

See Also:
Constant Field Values

ERRDBCONN

protected final String ERRDBCONN
Error response when the database connection fails

See Also:
Constant Field Values

ERRNOPARMS

protected final String ERRNOPARMS
Error response when no parameters are given and parameters are expected

See Also:
Constant Field Values

ERRNODATA

protected final String ERRNODATA
Error response when no data is found that match the report criteria

See Also:
Constant Field Values

ERRSQL

protected final String ERRSQL
Error response given when the SQL instruction fails

See Also:
Constant Field Values

ERRINITPARM

protected final String ERRINITPARM
Error response given when an invalid open parameter is processed

See Also:
Constant Field Values

ERRVALIDATED

protected final String ERRVALIDATED
Error response when the user is not validated. This occurs if the session variables cannot be read which can happen if the servlet session times out or the user tried to invoke the report with logging in.

See Also:
Constant Field Values

ERRAUTHORIZED

protected final String ERRAUTHORIZED
Error response given when the user attempts to run a report for which they have no authorization

See Also:
Constant Field Values

parameterForm

protected String parameterForm
URL to the parameter form if any


credentials

protected String[] credentials
Full database credential string


session

protected HttpSession session
HTTP session variable


reportName

protected String reportName
Name of the report


outFile

protected String outFile
Fully qualified filename of the desired output file


CRLF

protected final String CRLF
Field CRLF

Constructor Detail

DbFileDriver

public DbFileDriver()
Method Detail

init

public void init(ServletConfig config)
          throws ServletException
Initializes the servlet.

Specified by:
init in interface Servlet
Overrides:
init in class GenericServlet
Parameters:
config - Determines where to locate the configuration parameters for a given report
Throws:
ServletException - Thrown when a servlet exception occurs

destroy

public void destroy()
Destroys the servlet.

Specified by:
destroy in interface Servlet
Overrides:
destroy in class GenericServlet

returnFile

public abstract void returnFile(Connection conn,
                                HttpServletRequest request,
                                HttpServletResponse response)
                         throws ServletException,
                                IOException,
                                SQLException
Returns the result of the SQL query as a file download.

Parameters:
conn - the database connection object
request - servlet request
response - servlet response
Throws:
ServletException - Thrown when a servlet exception occurs
IOException
SQLException

getServletInfo

public String getServletInfo()
Returns a short description of the servlet.

Specified by:
getServletInfo in interface Servlet
Overrides:
getServletInfo in class GenericServlet
Returns:
servlet descriptive information

processRequest

protected void processRequest(HttpServletRequest request,
                              HttpServletResponse response)
                       throws ServletException,
                              IOException
Processes requests for both HTTP GET and POST methods.

Parameters:
request - servlet request
response - servlet response
Throws:
ServletException - Thrown when a servlet exception occurs
IOException

doGet

protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
              throws ServletException,
                     IOException
Handles the HTTP GET method.

Overrides:
doGet in class HttpServlet
Parameters:
request - servlet request
response - servlet response
Throws:
ServletException - Thrown when a servlet exception occurs
IOException

doPost

protected void doPost(HttpServletRequest request,
                      HttpServletResponse response)
               throws ServletException,
                      IOException
Handles the HTTP POST method.

Overrides:
doPost in class HttpServlet
Parameters:
request - servlet request
response - servlet response
Throws:
ServletException - Thrown when a servlet exception occurs
IOException

returnError

public void returnError(String message,
                        HttpServletRequest request,
                        HttpServletResponse response)
                 throws ServletException,
                        IOException
Returns the error response using a standard HTML response template.

Parameters:
message - the error message or html to return
request - servlet request
response - servlet response
Throws:
ServletException - Thrown when a servlet exception occurs
IOException

getParameterValue

public String getParameterValue(Object key)
Returns the value for a given parameter.

Parameters:
key - the parameter object name
Returns:
the parameter value

isKeyInMap

public boolean isKeyInMap(Object key)
Determines if the parameter object key exists in the parameter map or not

Parameters:
key - the parameter object name
Returns:
true if the key exists, otherwise false

isAuthorized

public boolean isAuthorized(String reportName)
Determines if the user is authorized to run the report or not

Parameters:
reportName - Name of the report to check authorization for
Returns:
true if authorized, otherwise false

getParameterMap

public HashMap getParameterMap(HttpServletRequest request,
                               HttpServletResponse response)
                        throws ServletException
Returns the parameter map for the servlet

Parameters:
request - servlet request
response - servlet response
Returns:
Hashmap consisting of the parameters for the report
Throws:
ServletException - Thrown when a servlet exception occurs

parameterPage

public void parameterPage(HttpServletRequest request,
                          HttpServletResponse response)
                   throws ServletException,
                          IOException
Returns the parameter page for the give report servlet. If no parameter page is given and this method is invoked, it will return an error page.

Parameters:
request - servlet request
response - servlet response
Throws:
ServletException - Thrown when a servlet exception occurs
IOException

writeOutput

public void writeOutput(PreparedStatement stmt,
                        HttpServletRequest request,
                        HttpServletResponse response)
                 throws ServletException,
                        IOException,
                        SQLException
Writes the output to the response writer.

Parameters:
stmt - an opened but not executed prepared statement (java.sql.PreparedStatement)
request - the HttpServletRequest request
response - the HttpServletResponse response
Throws:
ServletException - if an error occurs reading or writing the request or response
IOException
SQLException

MDi JLib
1.7.4

Copyright©2001-2007 MicroDeveloper, Inc. All Rights Reserved.