Quadcap Embeddable Server

com/quadcap/http/servlets/cgi/CgiServlet.java

Go to the documentation of this file.
00001 package com.quadcap.http.servlets.cgi; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.File; 00042 import java.io.FileInputStream; 00043 import java.io.IOException; 00044 import java.io.OutputStream; 00045 00046 import java.util.Enumeration; 00047 import java.util.Hashtable; 00048 00049 import javax.servlet.ServletConfig; 00050 import javax.servlet.ServletException; 00051 00052 import javax.servlet.http.HttpServlet; 00053 import javax.servlet.http.HttpServletRequest; 00054 import javax.servlet.http.HttpServletResponse; 00055 00056 import com.quadcap.util.Debug; 00057 00058 /** 00059 * This servlet class implements a CGI interface. 00060 * <p> 00061 * 00062 * @author Stan Bailes 00063 */ 00064 00065 public class CgiServlet extends HttpServlet { 00066 Hashtable scripts = new Hashtable(); 00067 Hashtable extMap = new Hashtable(); 00068 String defaultInterp; 00069 00070 /** 00071 * Initialize this servlet 00072 * 00073 * @param config the servlet configuration object, containing my 00074 * initialization parameters 00075 * @exception ServletException if I can't initialize for some reason. 00076 */ 00077 public void init(ServletConfig config) throws ServletException { 00078 super.init(config); 00079 Enumeration e = config.getInitParameterNames(); 00080 defaultInterp = config.getInitParameter("defaultInterp"); 00081 while (e.hasMoreElements()) { 00082 String name = e.nextElement().toString(); 00083 if (name.startsWith(".")) { 00084 extMap.put(name.substring(1), config.getInitParameter(name)); 00085 } 00086 } 00087 } 00088 00089 /** 00090 * Handle an incoming request. 00091 * 00092 * @param req HttpServletRequest that encapsulates the request to 00093 * the servlet 00094 * @param res HttpServletResponse that encapsulates the response 00095 * from the servlet 00096 * 00097 * @exception IOException if detected when handling the request 00098 * @exception ServletException if the request could not be handled 00099 */ 00100 protected void service(HttpServletRequest req, HttpServletResponse res) 00101 throws ServletException, IOException 00102 { 00103 CgiScript file = getFileForRequest(req); 00104 if (file == null) { 00105 res.sendError(HttpServletResponse.SC_NOT_FOUND); 00106 } else { 00107 file.service(req, res); 00108 } 00109 } 00110 00111 /** 00112 * A cheesy implementation. XXX 00113 * 00114 * @param req the request for which we need to associate a file 00115 * @return the file name 00116 */ 00117 public String getFileNameForRequest(HttpServletRequest req) { 00118 String uri = req.getRequestURI(); 00119 int idx = uri.indexOf('?'); 00120 if (idx >= 0) uri = uri.substring(0, idx); 00121 return uri; 00122 } 00123 00124 /** 00125 * Return a file object which is suitable for processing the specified 00126 * request. 00127 * 00128 * @param req the http request to service. 00129 * @return the file, or null if the file doesn't exist. 00130 * 00131 * @exception ServletException may be thrown. 00132 */ 00133 public CgiScript getFileForRequest(HttpServletRequest req) 00134 throws ServletException 00135 { 00136 String name = getServletContext().getRealPath(req.getServletPath()); 00137 CgiScript script = null; 00138 00139 synchronized (scripts) { 00140 script = (CgiScript)scripts.get(name); 00141 00142 if (script == null) { 00143 int idx = name.lastIndexOf('.'); 00144 String ext = null; 00145 if (idx > 0) ext = name.substring(idx+1); 00146 String interp = null; 00147 if (ext != null) interp = (String)extMap.get(ext); 00148 if (interp == null) interp = defaultInterp; 00149 if (interp == null) { 00150 throw new ServletException("No interpreter for " + name); 00151 } 00152 00153 File f = new File(name); 00154 if (!f.exists()) return null; 00155 00156 script = new CgiScript(this, interp, f); 00157 scripts.put(name, script); 00158 return script; 00159 } 00160 } 00161 return script; 00162 } 00163 }