KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > CGIProcessEnvironment


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.catalina.util;
30
31 import java.lang.Process JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.Writer JavaDoc;
34 import java.io.Reader JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.io.BufferedWriter JavaDoc;
37 import java.io.BufferedReader JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.io.InputStreamReader JavaDoc;
41 import java.io.OutputStreamWriter JavaDoc;
42 import java.io.BufferedInputStream JavaDoc;
43 import java.io.BufferedOutputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.net.URLEncoder JavaDoc;
46 import java.util.Hashtable JavaDoc;
47 import java.util.Vector JavaDoc;
48 import java.util.Enumeration JavaDoc;
49 import java.util.StringTokenizer JavaDoc;
50 import java.util.Locale JavaDoc;
51 import java.util.Date JavaDoc;
52 import javax.servlet.ServletException JavaDoc;
53 import javax.servlet.ServletOutputStream JavaDoc;
54 import javax.servlet.ServletContext JavaDoc;
55 import javax.servlet.ServletConfig JavaDoc;
56 import javax.servlet.http.HttpServlet JavaDoc;
57 import javax.servlet.http.HttpServletRequest JavaDoc;
58 import javax.servlet.http.HttpServletResponse JavaDoc;
59 import javax.servlet.http.HttpSession JavaDoc;
60 import javax.servlet.http.Cookie JavaDoc;
61 import org.apache.catalina.Context;
62 import org.apache.catalina.Wrapper;
63
64
65
66 // import org.apache.catalina.util.StringManager;
67

68
69
70 /**
71  * Encapsulates the CGI Process' environment and rules to derive
72  * that environment from the servlet container and request information.
73  * @author Martin Dengler [root@martindengler.com]
74  * @version $Revision: 1.3 $, $Date: 2006/03/12 01:27:08 $
75  * @since Tomcat 4.0
76  */

77
78 public class CGIProcessEnvironment extends ProcessEnvironment JavaDoc {
79
80
81     private static com.sun.org.apache.commons.logging.Log log=
82         com.sun.org.apache.commons.logging.LogFactory.getLog( CGIProcessEnvironment.class );
83
84     /** cgi command's query parameters */
85     private Hashtable JavaDoc queryParameters = null;
86
87     /**
88      * The CGI search path will start at
89      * webAppRootDir + File.separator + cgiPathPrefix
90      * (or webAppRootDir alone if cgiPathPrefix is
91      * null)
92      */

93     private String JavaDoc cgiPathPrefix = null;
94
95
96     /**
97      * Creates a ProcessEnvironment and derives the necessary environment,
98      * working directory, command, etc. The cgi path prefix is initialized
99      * to "" (the empty string).
100      *
101      * @param req HttpServletRequest for information provided by
102      * the Servlet API
103      * @param context ServletContext for information provided by
104      * the Servlet API
105      */

106     public CGIProcessEnvironment(HttpServletRequest JavaDoc req,
107         ServletContext JavaDoc context) {
108             this(req, context, "");
109     }
110
111
112
113     /**
114      * Creates a ProcessEnvironment and derives the necessary environment,
115      * working directory, command, etc.
116      * @param req HttpServletRequest for information provided by
117      * the Servlet API
118      * @param context ServletContext for information provided by
119      * the Servlet API
120      * @param cgiPathPrefix subdirectory of webAppRootDir below which the
121      * web app's CGIs may be stored; can be null or "".
122      */

123     public CGIProcessEnvironment(HttpServletRequest JavaDoc req,
124         ServletContext JavaDoc context, String JavaDoc cgiPathPrefix) {
125             this(req, context, cgiPathPrefix, 0);
126     }
127
128
129
130     /**
131      * Creates a ProcessEnvironment and derives the necessary environment,
132      * working directory, command, etc.
133      * @param req HttpServletRequest for information provided by
134      * the Servlet API
135      * @param context ServletContext for information provided by
136      * the Servlet API
137      * @param debug int debug level (0 == none, 6 == lots)
138      */

139     public CGIProcessEnvironment(HttpServletRequest JavaDoc req,
140         ServletContext JavaDoc context, int debug) {
141             this(req, context, "", 0);
142     }
143
144
145
146
147     /**
148      * Creates a ProcessEnvironment and derives the necessary environment,
149      * working directory, command, etc.
150      * @param req HttpServletRequest for information provided by
151      * the Servlet API
152      * @param context ServletContext for information provided by
153      * the Servlet API
154      * @param cgiPathPrefix subdirectory of webAppRootDir below which the
155      * web app's CGIs may be stored; can be null or "".
156      * @param debug int debug level (0 == none, 6 == lots)
157      */

158     public CGIProcessEnvironment(HttpServletRequest JavaDoc req,
159         ServletContext JavaDoc context, String JavaDoc cgiPathPrefix, int debug) {
160             super(req, context, debug);
161             this.cgiPathPrefix = cgiPathPrefix;
162             queryParameters = new Hashtable JavaDoc();
163             Enumeration JavaDoc paramNames = req.getParameterNames();
164             while (paramNames != null && paramNames.hasMoreElements()) {
165                 String JavaDoc param = paramNames.nextElement().toString();
166                 if (param != null) {
167                     queryParameters.put(param,
168                         URLEncoder.encode(req.getParameter(param)));
169                 }
170             }
171             this.valid = deriveProcessEnvironment(req);
172     }
173
174
175
176     /**
177      * Constructs the CGI environment to be supplied to the invoked CGI
178      * script; relies heavliy on Servlet API methods and findCGI
179      * @param HttpServletRequest request associated with the CGI invokation
180      * @return true if environment was set OK, false if there was a problem
181      * and no environment was set
182      */

183     protected boolean deriveProcessEnvironment(HttpServletRequest JavaDoc req) {
184         /*
185          * This method is slightly ugly; c'est la vie.
186          * "You cannot stop [ugliness], you can only hope to contain [it]"
187          * (apologies to Marv Albert regarding MJ)
188          */

189
190         Hashtable JavaDoc envp;
191         super.deriveProcessEnvironment(req);
192         envp = getEnvironment();
193
194         String JavaDoc sPathInfoOrig = null;
195         String JavaDoc sPathTranslatedOrig = null;
196         String JavaDoc sPathInfoCGI = null;
197         String JavaDoc sPathTranslatedCGI = null;
198         String JavaDoc sCGIFullPath = null;
199         String JavaDoc sCGIScriptName = null;
200         String JavaDoc sCGIFullName = null;
201         String JavaDoc sCGIName = null;
202         String JavaDoc[] sCGINames;
203         sPathInfoOrig = this.pathInfo;
204         sPathInfoOrig = sPathInfoOrig == null ? "" : sPathInfoOrig;
205         sPathTranslatedOrig = req.getPathTranslated();
206         sPathTranslatedOrig = sPathTranslatedOrig == null ? "" :
207             sPathTranslatedOrig;
208             sCGINames =
209                 findCGI(sPathInfoOrig, getWebAppRootDir(), getContextPath(),
210                 getServletPath(), cgiPathPrefix);
211         sCGIFullPath = sCGINames[0];
212         sCGIScriptName = sCGINames[1];
213         sCGIFullName = sCGINames[2];
214         sCGIName = sCGINames[3];
215         if (sCGIFullPath == null || sCGIScriptName == null
216             || sCGIFullName == null || sCGIName == null) {
217                 return false;
218         }
219         envp.put("SERVER_SOFTWARE", "TOMCAT");
220         envp.put("SERVER_NAME", nullsToBlanks(req.getServerName()));
221         envp.put("GATEWAY_INTERFACE", "CGI/1.1");
222         envp.put("SERVER_PROTOCOL", nullsToBlanks(req.getProtocol()));
223         int port = req.getServerPort();
224         Integer JavaDoc iPort = (port == 0 ? new Integer JavaDoc(-1) : new Integer JavaDoc(port));
225         envp.put("SERVER_PORT", iPort.toString());
226         envp.put("REQUEST_METHOD", nullsToBlanks(req.getMethod()));
227
228         /*-
229         * PATH_INFO should be determined by using sCGIFullName:
230         * 1) Let sCGIFullName not end in a "/" (see method findCGI)
231         * 2) Let sCGIFullName equal the pathInfo fragment which
232         * corresponds to the actual cgi script.
233         * 3) Thus, PATH_INFO = request.getPathInfo().substring(
234         * sCGIFullName.length())
235         *
236         * (see method findCGI, where the real work is done)
237         *
238         */

239
240         if (pathInfo == null ||
241             (pathInfo.substring(sCGIFullName.length()).length() <= 0)) {
242                 sPathInfoCGI = "";
243         } else {
244             sPathInfoCGI = pathInfo.substring(sCGIFullName.length());
245         }
246         envp.put("PATH_INFO", sPathInfoCGI);
247
248         /*-
249         * PATH_TRANSLATED must be determined after PATH_INFO (and the
250         * implied real cgi-script) has been taken into account.
251         *
252         * The following example demonstrates:
253         *
254         * servlet info = /servlet/cgigw/dir1/dir2/cgi1/trans1/trans2
255         * cgifullpath = /servlet/cgigw/dir1/dir2/cgi1
256         * path_info = /trans1/trans2
257         * webAppRootDir = servletContext.getRealPath("/")
258         *
259         * path_translated = servletContext.getRealPath("/trans1/trans2")
260         *
261         * That is, PATH_TRANSLATED = webAppRootDir + sPathInfoCGI
262         * (unless sPathInfoCGI is null or blank, then the CGI
263         * specification dictates that the PATH_TRANSLATED metavariable
264         * SHOULD NOT be defined.
265         *
266         */

267
268         if (sPathInfoCGI != null && !("".equals(sPathInfoCGI))) {
269             sPathTranslatedCGI = getContext().getRealPath(sPathInfoCGI);
270         } else {
271             sPathTranslatedCGI = null;
272         }
273         if (sPathTranslatedCGI == null || "".equals(sPathTranslatedCGI)) {
274             //NOOP
275
} else {
276             envp.put("PATH_TRANSLATED", nullsToBlanks(sPathTranslatedCGI));
277         }
278         envp.put("SCRIPT_NAME", nullsToBlanks(sCGIScriptName));
279         envp.put("QUERY_STRING", nullsToBlanks(req.getQueryString()));
280         envp.put("REMOTE_HOST", nullsToBlanks(req.getRemoteHost()));
281         envp.put("REMOTE_ADDR", nullsToBlanks(req.getRemoteAddr()));
282         envp.put("AUTH_TYPE", nullsToBlanks(req.getAuthType()));
283         envp.put("REMOTE_USER", nullsToBlanks(req.getRemoteUser()));
284         envp.put("REMOTE_IDENT", ""); //not necessary for full compliance
285
envp.put("CONTENT_TYPE", nullsToBlanks(req.getContentType()));
286
287         /* Note CGI spec says CONTENT_LENGTH must be NULL ("") or undefined
288         * if there is no content, so we cannot put 0 or -1 in as per the
289         * Servlet API spec.
290         */

291
292         int contentLength = req.getContentLength();
293         String JavaDoc sContentLength = (contentLength <= 0 ? "" : (
294             new Integer JavaDoc(contentLength)).toString());
295         envp.put("CONTENT_LENGTH", sContentLength);
296         Enumeration JavaDoc headers = req.getHeaderNames();
297         String JavaDoc header = null;
298         while (headers.hasMoreElements()) {
299             header = null;
300             header = ((String JavaDoc)headers.nextElement()).toUpperCase();
301             //REMIND: rewrite multiple headers as if received as single
302
//REMIND: change character set
303
//REMIND: I forgot what the previous REMIND means
304
if ("AUTHORIZATION".equalsIgnoreCase(header)
305                 || "PROXY_AUTHORIZATION".equalsIgnoreCase(header)) {
306                     //NOOP per CGI specification section 11.2
307
} else if ("HOST".equalsIgnoreCase(header)) {
308                 String JavaDoc host = req.getHeader(header);
309                 envp.put("HTTP_" + header.replace('-', '_'),
310                     host.substring(0, host.indexOf(":")));
311             } else {
312                 envp.put("HTTP_" + header.replace('-', '_'),
313                     req.getHeader(header));
314             }
315         }
316         command = sCGIFullPath;
317         workingDirectory = new File JavaDoc(command.substring(0,
318             command.lastIndexOf(File.separator)));
319         envp.put("X_TOMCAT_COMMAND_PATH", command); //for kicks
320
this.setEnvironment(envp);
321         return true;
322     }
323
324
325     /**
326      * Resolves core information about the cgi script. <p> Example URI:
327      * <PRE> /servlet/cgigateway/dir1/realCGIscript/pathinfo1 </PRE> <ul>
328      * <LI><b>path</b> = $CATALINA_HOME/mywebapp/dir1/realCGIscript
329      * <LI><b>scriptName</b> = /servlet/cgigateway/dir1/realCGIscript</LI>
330      * <LI><b>cgiName</b> = /dir1/realCGIscript
331      * <LI><b>name</b> = realCGIscript
332      * </ul>
333      * </p>
334      * <p>
335      * CGI search algorithm: search the real path below
336      * &lt;my-webapp-root&gt; and find the first non-directory in
337      * the getPathTranslated("/"), reading/searching from left-to-right.
338      * </p>
339      * <p>
340      * The CGI search path will start at
341      * webAppRootDir + File.separator + cgiPathPrefix (or webAppRootDir
342      * alone if cgiPathPrefix is null).
343      * </p>
344      * <p>
345      * cgiPathPrefix is usually set by the calling servlet to the servlet's
346      * cgiPathPrefix init parameter
347      * </p>
348      *
349      * @param pathInfo String from HttpServletRequest.getPathInfo()
350      * @param webAppRootDir String from context.getRealPath("/")
351      * @param contextPath String as from HttpServletRequest.getContextPath()
352      * @param servletPath String as from HttpServletRequest.getServletPath()
353      * @param cgiPathPrefix subdirectory of webAppRootDir below which the
354      * web app's CGIs may be stored; can be null.
355      * @return
356      * <ul> <li> <code>path</code> - full file-system path to valid cgi
357      * script, or null if no cgi was found
358      * <li> <code>scriptName</code> - CGI variable SCRIPT_NAME; the full
359      * URL path to valid cgi script or
360      * null if no cgi was found
361      * <li> <code>cgiName</code> - servlet pathInfo fragment
362      * corresponding to the cgi script
363      * itself, or null if not found
364      * <li> <code>name</code> - simple name (no directories) of
365      * the cgi script, or null if no cgi
366      * was found
367      * </ul>
368      * @author Martin Dengler [root@martindengler.com]
369      * @since Tomcat 4.0
370      */

371     protected String JavaDoc[] findCGI(String JavaDoc pathInfo, String JavaDoc webAppRootDir,
372         String JavaDoc contextPath, String JavaDoc servletPath, String JavaDoc cgiPathPrefix) {
373             String JavaDoc path = null;
374             String JavaDoc name = null;
375             String JavaDoc scriptname = null;
376             String JavaDoc cginame = null;
377             if ((webAppRootDir != null)
378                 && (webAppRootDir.lastIndexOf("/")
379                 == (webAppRootDir.length() - 1))) {
380                     //strip the trailing "/" from the webAppRootDir
381
webAppRootDir =
382                         webAppRootDir.substring(0,
383                         (webAppRootDir.length() - 1));
384             }
385             if (cgiPathPrefix != null) {
386                 webAppRootDir = webAppRootDir + File.separator
387                     + cgiPathPrefix;
388             }
389             if (debug >= 2) {
390                 log("findCGI: start = [" + webAppRootDir
391                     + "], pathInfo = [" + pathInfo + "] ");
392             }
393             File JavaDoc currentLocation = new File JavaDoc(webAppRootDir);
394             StringTokenizer JavaDoc dirWalker = new StringTokenizer JavaDoc(pathInfo, "/");
395             while (!currentLocation.isFile() && dirWalker.hasMoreElements()) {
396                 currentLocation = new
397                     File JavaDoc(currentLocation, (String JavaDoc) dirWalker.nextElement());
398                 if (debug >= 3) {
399                     log("findCGI: traversing to [" + currentLocation + "]");
400                 }
401             }
402             if (!currentLocation.isFile()) {
403                 return new String JavaDoc[] { null, null, null, null };
404             } else {
405                 if (debug >= 2) {
406                     log("findCGI: FOUND cgi at [" + currentLocation + "]");
407                 }
408                 path = currentLocation.getAbsolutePath();
409                 name = currentLocation.getName();
410                 cginame = currentLocation.getParent()
411                     .substring(webAppRootDir.length())
412                     + File.separator + name;
413                     if (".".equals(contextPath)) {
414                         scriptname = servletPath + cginame;
415                 } else {
416                     scriptname = contextPath + servletPath + cginame;
417                 }
418             }
419             if (debug >= 1) {
420                 log("findCGI calc: name=" + name + ", path=" + path
421                     + ", scriptname=" + scriptname + ", cginame=" + cginame);
422             }
423             return new String JavaDoc[] { path, scriptname, cginame, name };
424     }
425
426
427     /**
428      * Print important CGI environment information in an
429      * easy-to-read HTML table
430      * @return HTML string containing CGI environment info
431      */

432     public String JavaDoc toString() {
433         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
434         sb.append("<TABLE border=2>");
435         sb.append("<tr><th colspan=2 bgcolor=grey>");
436         sb.append("ProcessEnvironment Info</th></tr>");
437         sb.append("<tr><td>Debug Level</td><td>");
438         sb.append(debug);
439         sb.append("</td></tr>");
440         sb.append("<tr><td>Validity:</td><td>");
441         sb.append(isValid());
442         sb.append("</td></tr>");
443         if (isValid()) {
444             Enumeration JavaDoc envk = env.keys();
445             while (envk.hasMoreElements()) {
446                 String JavaDoc s = (String JavaDoc)envk.nextElement();
447                 sb.append("<tr><td>");
448                 sb.append(s);
449                 sb.append("</td><td>");
450                 sb.append(blanksToString((String JavaDoc)env.get(s),
451                     "[will be set to blank]"));
452                     sb.append("</td></tr>");
453             }
454         }
455         sb.append("<tr><td colspan=2><HR></td></tr>");
456         sb.append("<tr><td>Derived Command</td><td>");
457         sb.append(nullsToBlanks(command));
458         sb.append("</td></tr>");
459         sb.append("<tr><td>Working Directory</td><td>");
460         if (workingDirectory != null) {
461             sb.append(workingDirectory.toString());
462         }
463         sb.append("</td></tr>");
464         sb.append("<tr><td colspan=2>Query Params</td></tr>");
465         Enumeration JavaDoc paramk = queryParameters.keys();
466         while (paramk.hasMoreElements()) {
467             String JavaDoc s = paramk.nextElement().toString();
468             sb.append("<tr><td>");
469             sb.append(s);
470             sb.append("</td><td>");
471             sb.append(queryParameters.get(s).toString());
472             sb.append("</td></tr>");
473         }
474
475         sb.append("</TABLE><p>end.");
476         return sb.toString();
477     }
478
479
480     /**
481      * Gets process' derived query parameters
482      * @return process' query parameters
483      */

484     public Hashtable JavaDoc getParameters() {
485         return queryParameters;
486     }
487
488 }
489
Popular Tags