KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > cgi > CgiServlet


1 package com.quadcap.http.servlets.cgi;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.FileInputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.OutputStream JavaDoc;
45
46 import java.util.Enumeration JavaDoc;
47 import java.util.Hashtable JavaDoc;
48
49 import javax.servlet.ServletConfig JavaDoc;
50 import javax.servlet.ServletException JavaDoc;
51
52 import javax.servlet.http.HttpServlet JavaDoc;
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55
56 import com.quadcap.util.Debug;
57
58 /**
59  * This servlet class implements a CGI interface.
60  * <p>
61  *
62  * @author Stan Bailes
63  */

64
65 public class CgiServlet extends HttpServlet JavaDoc {
66     Hashtable JavaDoc scripts = new Hashtable JavaDoc();
67     Hashtable JavaDoc extMap = new Hashtable JavaDoc();
68     String JavaDoc defaultInterp;
69
70     /**
71      * Initialize this servlet
72      *
73      * @param config the servlet configuration object, containing my
74      * initialization parameters
75      * @exception ServletException if I can't initialize for some reason.
76      */

77     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
78         super.init(config);
79     Enumeration JavaDoc e = config.getInitParameterNames();
80     defaultInterp = config.getInitParameter("defaultInterp");
81     while (e.hasMoreElements()) {
82         String JavaDoc name = e.nextElement().toString();
83         if (name.startsWith(".")) {
84         extMap.put(name.substring(1), config.getInitParameter(name));
85         }
86     }
87     }
88     
89     /**
90      * Handle an incoming request.
91      *
92      * @param req HttpServletRequest that encapsulates the request to
93      * the servlet
94      * @param res HttpServletResponse that encapsulates the response
95      * from the servlet
96      *
97      * @exception IOException if detected when handling the request
98      * @exception ServletException if the request could not be handled
99      */

100     protected void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
101         throws ServletException JavaDoc, IOException JavaDoc
102     {
103         CgiScript file = getFileForRequest(req);
104         if (file == null) {
105             res.sendError(HttpServletResponse.SC_NOT_FOUND);
106         } else {
107             file.service(req, res);
108         }
109     }
110
111     /**
112      * A cheesy implementation. XXX
113      *
114      * @param req the request for which we need to associate a file
115      * @return the file name
116      */

117     public String JavaDoc getFileNameForRequest(HttpServletRequest JavaDoc req) {
118     String JavaDoc uri = req.getRequestURI();
119     int idx = uri.indexOf('?');
120     if (idx >= 0) uri = uri.substring(0, idx);
121     return uri;
122     }
123     
124     /**
125      * Return a file object which is suitable for processing the specified
126      * request.
127      *
128      * @param req the http request to service.
129      * @return the file, or null if the file doesn't exist.
130      *
131      * @exception ServletException may be thrown.
132      */

133     public CgiScript getFileForRequest(HttpServletRequest JavaDoc req)
134         throws ServletException JavaDoc
135     {
136     String JavaDoc name = getServletContext().getRealPath(req.getServletPath());
137     CgiScript script = null;
138
139     synchronized (scripts) {
140         script = (CgiScript)scripts.get(name);
141
142         if (script == null) {
143         int idx = name.lastIndexOf('.');
144         String JavaDoc ext = null;
145         if (idx > 0) ext = name.substring(idx+1);
146         String JavaDoc interp = null;
147         if (ext != null) interp = (String JavaDoc)extMap.get(ext);
148         if (interp == null) interp = defaultInterp;
149         if (interp == null) {
150             throw new ServletException JavaDoc("No interpreter for " + name);
151         }
152
153         File JavaDoc f = new File JavaDoc(name);
154         if (!f.exists()) return null;
155
156         script = new CgiScript(this, interp, f);
157         scripts.put(name, script);
158         return script;
159         }
160     }
161         return script;
162     }
163 }
164
Popular Tags