KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > servlet > servlets > CgiProcessor


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: CgiProcessor.java,v 1.9 2005/07/27 11:17:40 slobodan Exp $
23  */

24
25 package org.enhydra.servlet.servlets;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import javax.servlet.ServletOutputStream JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37
38 /**
39  * This Servlet is for the purpose of executing a CGI program that resides on
40  * the system. In order to use this servlet in the server please
41  * remember to specify the fully qualified classname
42  * org.enhydra.servlet.servlets.CGIServlet in the Classname field and specify
43  * the directory of the CGI to run in the DocRoot field.
44  *
45  * @version $Revision: 1.9 $
46  * @author Kent Henneuse
47  * @author Paul Morgan
48  */

49 class CgiProcessor {
50
51     /**
52      * Buffer size for writeing one stream to another.
53      */

54     private static final int buffer_size = 1024;
55
56
57     /**
58      * Html text to send clent for a file not found.
59      */

60     private final static String JavaDoc NOT_FOUND_MSG =
61         "<TITLE>Not Found</TITLE><H1>Not Found</H1> " +
62         "The requested object does not exist on this Enhydra Server. " +
63         "The link you followed is either outdated, inaccurate, " +
64         "or the server has been instructed not to let you have it.";
65
66
67     /**
68      *
69      */

70     public void processCgiRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc scriptPath, String JavaDoc pathInfo, String JavaDoc scriptName)
71     throws IOException JavaDoc {
72
73     ServletOutputStream JavaDoc out;
74     String JavaDoc strBuffer;
75     String JavaDoc name, value;
76     byte buffer[] = new byte[buffer_size];
77     int bytesRead;
78
79         // Execute the CGI & report on errors...
80
Process JavaDoc process = null;
81     Runtime JavaDoc runner = Runtime.getRuntime();
82         String JavaDoc envVars[] = getCgiEnv(request, scriptPath, pathInfo, scriptName);
83         File JavaDoc script = new File JavaDoc(scriptPath);
84         if (! (script.exists() && script.isFile())) {
85             // script doesn't exist
86
response.sendError(response.SC_NOT_FOUND, NOT_FOUND_MSG);
87             return;
88         }
89         process = runner.exec(scriptPath, envVars);
90
91         InputStreamReader JavaDoc tempIn
92             = new InputStreamReader JavaDoc(process.getInputStream());
93     BufferedReader JavaDoc in = new BufferedReader JavaDoc(tempIn);
94
95         strBuffer = in.readLine();
96         if (scriptName.toLowerCase().startsWith("nph-")) {
97             String JavaDoc code = strBuffer.substring(strBuffer.indexOf(" "));
98             response.setStatus(Integer.parseInt(code));
99             strBuffer = in.readLine();
100         }
101
102         // Set the response header fields...
103
while ((strBuffer != null) && (strBuffer.length() != 0)) {
104             int colon = strBuffer.indexOf(":");
105             if (colon != -1) {
106                 name = strBuffer.substring(0, colon);
107                 value = strBuffer.substring(colon + 1);
108         String JavaDoc lName = name.toLowerCase();
109         if (lName.equals("status")) {
110             String JavaDoc tValue = value.trim();
111             if (tValue.indexOf(" ") != -1) {
112                 String JavaDoc nValue = tValue.substring(0, tValue.indexOf(" "));
113                 response.setStatus(Integer.parseInt(nValue));
114             } else {
115                 response.setStatus(Integer.parseInt(tValue));
116             }
117         } else if (lName.equals("content-type")) {
118             String JavaDoc tValue = value.trim();
119             response.setContentType(tValue);
120         } else {
121             response.setHeader(name, value);
122         }
123             }
124             strBuffer = in.readLine();
125         }
126     
127         bytesRead = in.read();
128         // then write the data of the response
129
out = response.getOutputStream();
130     
131         while(bytesRead != -1) {
132             out.write(bytesRead);
133             bytesRead = in.read();
134         }
135         in.close();
136         tempIn.close();
137         out.flush();
138         out.close();
139     }
140
141     /**
142      *
143      */

144     private String JavaDoc[] getCgiEnv(HttpServletRequest JavaDoc request,
145                                String JavaDoc scriptPath,
146                                String JavaDoc pathInfo,
147                                String JavaDoc scriptName) {
148
149     Vector JavaDoc envList = new Vector JavaDoc();
150         String JavaDoc strBuffer = null;
151
152         // Get all of the Environment Variables that can not be gotten from
153
// the servlet request
154

155         // FIXME: The following code is commented out currently as
156
// it may not be required. Have not found a CGI to really test
157
// this functionality
158
/*
159          process = runner.exec("env");
160          tempIn = new InputStreamReader(process.getInputStream());
161      in = new BufferedReader(tempIn);
162
163          strBuffer = in.readLine();
164          while(strBuffer != null) {
165          envList.addElement(strBuffer);
166          strBuffer = in.readLine();
167          }
168         */

169
170         // Add the request variables to the list to be passed
171
strBuffer = request.getServerName();
172         if (strBuffer != null) {
173             envList.addElement("SERVER_NAME=" + strBuffer);
174         }
175
176         strBuffer = request.getProtocol();
177         if (strBuffer != null) {
178             envList.addElement("SERVER_PROTOCOL=" + strBuffer);
179         }
180
181         envList.addElement("SERVER_PORT=" + request.getServerPort());
182         
183         strBuffer = request.getMethod();
184         if (strBuffer != null) {
185             envList.addElement("REQUEST_METHOD=" + strBuffer);
186         }
187
188         if (pathInfo != null && pathInfo.length() != 0) {
189             envList.addElement("PATH_INFO=" + pathInfo);
190         }
191
192         strBuffer = request.getPathTranslated();
193         if (strBuffer != null) {
194             envList.addElement("PATH_TRANSLATED=" + strBuffer);
195             envList.addElement("SCRIPT_FILENAME=" + strBuffer);
196         }
197     
198         if (scriptPath != null || scriptPath.equals("")) {
199             envList.addElement("SCRIPT_NAME=" + request.getServletPath()
200                                + scriptName);
201         }
202
203         strBuffer = request.getRealPath("/");
204         if (strBuffer != null) {
205             envList.addElement("DOCUMENT_ROOT=" + strBuffer);
206         }
207
208         strBuffer = request.getQueryString();
209         if(strBuffer != null) {
210             envList.addElement("QUERY_STRING=" + strBuffer);
211         }
212
213         strBuffer = request.getRemoteHost();
214         if (strBuffer != null) {
215             envList.addElement("REMOTE_HOST=" + strBuffer);
216         }
217
218         strBuffer = request.getRemoteAddr();
219         if (strBuffer != null) {
220             envList.addElement("REMOTE_ADDR=" + strBuffer);
221         }
222
223         strBuffer = request.getAuthType();
224         if (strBuffer != null) {
225             envList.addElement("AUTH_TYPE=" + strBuffer);
226         }
227
228         strBuffer = request.getRemoteUser();
229         if (strBuffer != null) {
230             envList.addElement("REMOTE_USER=" + strBuffer);
231         }
232
233         if (request.getContentLength() != -1) {
234             envList.addElement("CONTENT_LENGTH="
235                                + request.getContentLength());
236         }
237
238         strBuffer = request.getContentType();
239         if (strBuffer != null) {
240             envList.addElement("CONTENT_TYPE=" + strBuffer);
241         }
242
243         strBuffer = request.getHeader("Accept");
244         if (strBuffer != null) {
245             envList.addElement("HTTP_ACCEPT=" + strBuffer);
246         }
247
248         strBuffer = request.getHeader("User-Agent");
249         if (strBuffer != null) {
250             envList.addElement("HTTP_USER_AGENT=" + strBuffer);
251         }
252
253         envList.addElement("SERVER_SOFTWARE=Enhydra6.5");
254         
255         // Create the needed array of environment variables
256
String JavaDoc envVars[] = new String JavaDoc[envList.size()];
257         envList.copyInto(envVars);
258         return envVars;
259     }
260
261     /**
262      *
263      */

264     public static void cgiError(HttpServletResponse JavaDoc response)
265         throws IOException JavaDoc {
266         
267         ServletOutputStream JavaDoc out = response.getOutputStream();
268         response.setContentType("text/html");
269
270         out.println("<HTML>");
271         out.println("<HEAD>");
272         out.println("<TITLE>CGIServlet Error</TITLE>\n</HEAD>");
273         out.println("<BODY>");
274         out.println("<H1>CGIServlet Error</H1>");
275         out.println("A CGI program to execute was not specified or could not be found.");
276         out.println("</BODY>\n</HTML>");
277         out.flush();
278         out.close();
279     }
280
281 }
282
Popular Tags