php.java.servlet
Class CGIServlet.CGIEnvironment

java.lang.Object
  extended by php.java.servlet.CGIServlet.CGIEnvironment
Direct Known Subclasses:
FastCGIServlet.CGIEnvironment
Enclosing class:
CGIServlet

protected class CGIServlet.CGIEnvironment
extends java.lang.Object

Encapsulates the CGI environment and rules to derive that environment from the servlet container and request information.

Since:
Tomcat 4.0
Author:
Martin Dengler [root@martindengler.com]

Field Summary
protected  java.lang.String command
          cgi command to be invoked
protected  javax.servlet.ServletContext context
          context of the enclosing servlet
protected  java.lang.String contextPath
          context path of enclosing servlet
protected  java.util.HashMap environment
          derived cgi environment
protected  java.lang.String pathInfo
          pathInfo for the current request
protected  java.lang.String servletPath
          servlet URI of the enclosing servlet
protected  java.lang.String webAppRootDir
          real file system directory of the enclosing servlet's web app
protected  java.io.File workingDirectory
          cgi command's desired working directory
 
Constructor Summary
protected CGIServlet.CGIEnvironment(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, javax.servlet.ServletContext context)
          Creates a CGIEnvironment and derives the necessary environment, query parameters, working directory, cgi command, etc.
 
Method Summary
protected  java.lang.String blanksToString(java.lang.String couldBeBlank, java.lang.String subForBlanks)
          Converts blank strings to another string
protected  java.lang.String[] findCGI(java.lang.String pathInfo, java.lang.String webAppRootDir, java.lang.String contextPath, java.lang.String servletPath, java.lang.String cgiPathPrefix)
          Resolves core information about the cgi script.
 void init(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
           
protected  java.lang.String nullsToBlanks(java.lang.String s)
          Converts null strings to blank strings ("")
protected  java.lang.String nullsToString(java.lang.String couldBeNull, java.lang.String subForNulls)
          Converts null strings to another string
protected  boolean setCGIEnvironment(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          Constructs the CGI environment to be supplied to the invoked CGI script; relies heavliy on Servlet API methods and findCGI
protected  void setPathInfo(javax.servlet.http.HttpServletRequest req, java.util.HashMap envp, java.lang.String sCGIFullName)
           
protected  void setupFromContext(javax.servlet.ServletContext context)
          Uses the ServletContext to set some CGI variables
protected  void setupFromRequest(javax.servlet.http.HttpServletRequest req)
          Uses the HttpServletRequest to set most CGI variables
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

protected javax.servlet.ServletContext context
context of the enclosing servlet


contextPath

protected java.lang.String contextPath
context path of enclosing servlet


servletPath

protected java.lang.String servletPath
servlet URI of the enclosing servlet


pathInfo

protected java.lang.String pathInfo
pathInfo for the current request


webAppRootDir

protected java.lang.String webAppRootDir
real file system directory of the enclosing servlet's web app


environment

protected java.util.HashMap environment
derived cgi environment


command

protected java.lang.String command
cgi command to be invoked


workingDirectory

protected java.io.File workingDirectory
cgi command's desired working directory

Constructor Detail

CGIServlet.CGIEnvironment

protected CGIServlet.CGIEnvironment(javax.servlet.http.HttpServletRequest req,
                                    javax.servlet.http.HttpServletResponse res,
                                    javax.servlet.ServletContext context)
Creates a CGIEnvironment and derives the necessary environment, query parameters, working directory, cgi command, etc.

Parameters:
req - HttpServletRequest for information provided by the Servlet API
res - HttpServletResponse
context - ServletContext for information provided by the Servlet API
Method Detail

init

public void init(javax.servlet.http.HttpServletRequest req,
                 javax.servlet.http.HttpServletResponse res)

setupFromContext

protected void setupFromContext(javax.servlet.ServletContext context)
Uses the ServletContext to set some CGI variables

Parameters:
context - ServletContext for information provided by the Servlet API

setupFromRequest

protected void setupFromRequest(javax.servlet.http.HttpServletRequest req)
Uses the HttpServletRequest to set most CGI variables

Parameters:
req - HttpServletRequest for information provided by the Servlet API

findCGI

protected java.lang.String[] findCGI(java.lang.String pathInfo,
                                     java.lang.String webAppRootDir,
                                     java.lang.String contextPath,
                                     java.lang.String servletPath,
                                     java.lang.String cgiPathPrefix)
Resolves core information about the cgi script.

Example URI:

 /servlet/cgigateway/dir1/realCGIscript/pathinfo1 

CGI search algorithm: search the real path below <my-webapp-root> and find the first non-directory in the getPathTranslated("/"), reading/searching from left-to-right.

The CGI search path will start at webAppRootDir + File.separator + cgiPathPrefix (or webAppRootDir alone if cgiPathPrefix is null).

cgiPathPrefix is defined by setting this servlet's cgiPathPrefix init parameter

Parameters:
pathInfo - String from HttpServletRequest.getPathInfo()
webAppRootDir - String from context.getRealPath("/")
contextPath - String as from HttpServletRequest.getContextPath()
servletPath - String as from HttpServletRequest.getServletPath()
cgiPathPrefix - subdirectory of webAppRootDir below which the web app's CGIs may be stored; can be null. The CGI search path will start at webAppRootDir + File.separator + cgiPathPrefix (or webAppRootDir alone if cgiPathPrefix is null). cgiPathPrefix is defined by setting the servlet's cgiPathPrefix init parameter.
Returns:
  • path - full file-system path to valid cgi script, or null if no cgi was found
  • scriptName - CGI variable SCRIPT_NAME; the full URL path to valid cgi script or null if no cgi was found
  • cgiName - servlet pathInfo fragment corresponding to the cgi script itself, or null if not found
  • name - simple name (no directories) of the cgi script, or null if no cgi was found
author Martin Dengler [root@martindengler.com]
Since:
Tomcat 4.0

setCGIEnvironment

protected boolean setCGIEnvironment(javax.servlet.http.HttpServletRequest req,
                                    javax.servlet.http.HttpServletResponse res)
Constructs the CGI environment to be supplied to the invoked CGI script; relies heavliy on Servlet API methods and findCGI

Parameters:
res - HttpServletResponse
req - HttpServletRequest request associated with the CGI invokation
Returns:
true if environment was set OK, false if there was a problem and no environment was set

setPathInfo

protected void setPathInfo(javax.servlet.http.HttpServletRequest req,
                           java.util.HashMap envp,
                           java.lang.String sCGIFullName)

nullsToBlanks

protected java.lang.String nullsToBlanks(java.lang.String s)
Converts null strings to blank strings ("")

Parameters:
s - string to be converted if necessary
Returns:
a non-null string, either the original or the empty string ("") if the original was null

nullsToString

protected java.lang.String nullsToString(java.lang.String couldBeNull,
                                         java.lang.String subForNulls)
Converts null strings to another string

Parameters:
couldBeNull - string to be converted if necessary
subForNulls - string to return instead of a null string
Returns:
a non-null string, either the original or the substitute string if the original was null

blanksToString

protected java.lang.String blanksToString(java.lang.String couldBeBlank,
                                          java.lang.String subForBlanks)
Converts blank strings to another string

Parameters:
couldBeBlank - string to be converted if necessary
subForBlanks - string to return instead of a blank string
Returns:
a non-null string, either the original or the substitute string if the original was null or empty ("")