Author: remy.maucherat(a)jboss.com
Date: 2008-09-30 16:32:07 -0400 (Tue, 30 Sep 2008)
New Revision: 799
Modified:
trunk/java/org/apache/catalina/servlets/CGIServlet.java
trunk/webapps/docs/changelog.xml
Log:
- Generics cleanup.
Modified: trunk/java/org/apache/catalina/servlets/CGIServlet.java
===================================================================
--- trunk/java/org/apache/catalina/servlets/CGIServlet.java 2008-09-30 15:56:04 UTC (rev
798)
+++ trunk/java/org/apache/catalina/servlets/CGIServlet.java 2008-09-30 20:32:07 UTC (rev
799)
@@ -340,9 +340,9 @@
// Document the properties from ServletRequest
out.println("<h1>ServletRequest Properties</h1>");
out.println("<ul>");
- Enumeration attrs = req.getAttributeNames();
+ Enumeration<String> attrs = req.getAttributeNames();
while (attrs.hasMoreElements()) {
- String attr = (String) attrs.nextElement();
+ String attr = attrs.nextElement();
out.println("<li><b>attribute</b> " + attr +
" = " +
req.getAttribute(attr));
}
@@ -352,14 +352,14 @@
req.getContentLength());
out.println("<li><b>contentType</b> = " +
req.getContentType());
- Enumeration locales = req.getLocales();
+ Enumeration<Locale> locales = req.getLocales();
while (locales.hasMoreElements()) {
- Locale locale = (Locale) locales.nextElement();
+ Locale locale = locales.nextElement();
out.println("<li><b>locale</b> = " + locale);
}
- Enumeration params = req.getParameterNames();
+ Enumeration<String> params = req.getParameterNames();
while (params.hasMoreElements()) {
- String param = (String) params.nextElement();
+ String param = params.nextElement();
String values[] = req.getParameterValues(param);
for (int i = 0; i < values.length; i++)
out.println("<li><b>parameter</b> " + param +
" = " +
@@ -386,9 +386,9 @@
for (int i = 0; i < cookies.length; i++)
out.println("<li><b>cookie</b> " +
cookies[i].getName() +" = " +cookies[i].getValue());
}
- Enumeration headers = req.getHeaderNames();
+ Enumeration<String> headers = req.getHeaderNames();
while (headers.hasMoreElements()) {
- String header = (String) headers.nextElement();
+ String header = headers.nextElement();
out.println("<li><b>header</b> " + header +
" = " +
req.getHeader(header));
}
@@ -423,7 +423,7 @@
out.println("<ul>");
attrs = req.getAttributeNames();
while (attrs.hasMoreElements()) {
- String attr = (String) attrs.nextElement();
+ String attr = attrs.nextElement();
out.println("<li><b>" + attr + "</b> =
" +
req.getAttribute(attr));
}
@@ -453,7 +453,7 @@
out.println("<ul>");
attrs = session.getAttributeNames();
while (attrs.hasMoreElements()) {
- String attr = (String) attrs.nextElement();
+ String attr = attrs.nextElement();
out.println("<li><b>" + attr + "</b> =
" +
session.getAttribute(attr));
}
@@ -475,7 +475,7 @@
out.println("<ul>");
params = getServletConfig().getInitParameterNames();
while (params.hasMoreElements()) {
- String param = (String) params.nextElement();
+ String param = params.nextElement();
String value = getServletConfig().getInitParameter(param);
out.println("<li><b>" + param + "</b> =
" + value);
}
@@ -501,7 +501,7 @@
out.println("<ul>");
params = getServletContext().getInitParameterNames();
while (params.hasMoreElements()) {
- String param = (String) params.nextElement();
+ String param = params.nextElement();
String value = getServletContext().getInitParameter(param);
out.println("<li><b>" + param + "</b> =
" + value);
}
@@ -513,7 +513,7 @@
out.println("<ul>");
attrs = getServletContext().getAttributeNames();
while (attrs.hasMoreElements()) {
- String attr = (String) attrs.nextElement();
+ String attr = attrs.nextElement();
out.println("<li><b>" + attr + "</b> =
" +
getServletContext().getAttribute(attr));
}
@@ -625,6 +625,7 @@
} //doGet
+
/** For future testing use only; does nothing right now */
public static void main(String[] args) {
System.out.println("$Header$");
@@ -664,7 +665,7 @@
private File tmpDir = null;
/** derived cgi environment */
- private Hashtable env = null;
+ private Hashtable<String, String> env = null;
/** cgi command to be invoked */
private String command = null;
@@ -933,7 +934,6 @@
// Add the CGI environment variables
String sPathInfoOrig = null;
- String sPathTranslatedOrig = null;
String sPathInfoCGI = null;
String sPathTranslatedCGI = null;
String sCGIFullPath = null;
@@ -946,10 +946,6 @@
sPathInfoOrig = this.pathInfo;
sPathInfoOrig = sPathInfoOrig == null ? "" : sPathInfoOrig;
- sPathTranslatedOrig = req.getPathTranslated();
- sPathTranslatedOrig =
- sPathTranslatedOrig == null ? "" : sPathTranslatedOrig;
-
if (webAppRootDir == null ) {
// The app has not been deployed in exploded form
webAppRootDir = tmpDir.toString();
@@ -983,7 +979,8 @@
envp.put("SERVER_PROTOCOL", nullsToBlanks(req.getProtocol()));
int port = req.getServerPort();
- Integer iPort = (port == 0 ? new Integer(-1) : new Integer(port));
+ Integer iPort =
+ (port == 0 ? Integer.valueOf(-1) : Integer.valueOf(port));
envp.put("SERVER_PORT", iPort.toString());
envp.put("REQUEST_METHOD", nullsToBlanks(req.getMethod()));
@@ -1032,8 +1029,6 @@
*/
if (sPathInfoCGI != null && !("".equals(sPathInfoCGI))) {
sPathTranslatedCGI = context.getRealPath(sPathInfoCGI);
- } else {
- sPathTranslatedCGI = null;
}
if (sPathTranslatedCGI == null || "".equals(sPathTranslatedCGI)) {
//NOOP
@@ -1065,15 +1060,15 @@
*/
int contentLength = req.getContentLength();
String sContentLength = (contentLength <= 0 ? "" :
- (new Integer(contentLength)).toString());
+ (Integer.valueOf(contentLength)).toString());
envp.put("CONTENT_LENGTH", sContentLength);
- Enumeration headers = req.getHeaderNames();
+ Enumeration<String> headers = req.getHeaderNames();
String header = null;
while (headers.hasMoreElements()) {
header = null;
- header = ((String) headers.nextElement()).toUpperCase();
+ header = headers.nextElement().toUpperCase();
//REMIND: rewrite multiple headers as if received as single
//REMIND: change character set
//REMIND: I forgot what the previous REMIND means
@@ -1145,10 +1140,14 @@
}
// create directories
- String dirPath = new String (destPath.toString().substring(
- 0,destPath.toString().lastIndexOf("/")));
+ String dirPath = destPath.toString().substring(
+ 0,destPath.toString().lastIndexOf("/"));
File dir = new File(dirPath);
- dir.mkdirs();
+ if (!dir.mkdirs() && debug >= 2) {
+ log("expandCGIScript: failed to create directories for '"
+
+ dir.getAbsolutePath() + "'");
+ return;
+ }
try {
synchronized (expandFileLock) {
@@ -1174,7 +1173,10 @@
} catch (IOException ioe) {
// delete in case file is corrupted
if (f.exists()) {
- f.delete();
+ if (!f.delete() && debug >= 2) {
+ log("expandCGIScript: failed to delete '" +
+ f.getAbsolutePath() + "'");
+ }
}
}
}
@@ -1205,13 +1207,13 @@
sb.append("</td></tr>");
if (isValid()) {
- Enumeration envk = env.keys();
+ Enumeration<String> envk = env.keys();
while (envk.hasMoreElements()) {
- String s = (String) envk.nextElement();
+ String s = envk.nextElement();
sb.append("<tr><td>");
sb.append(s);
sb.append("</td><td>");
- sb.append(blanksToString((String) env.get(s),
+ sb.append(blanksToString(env.get(s),
"[will be set to blank]"));
sb.append("</td></tr>");
}
@@ -1231,7 +1233,7 @@
sb.append("<tr><td>Command Line
Params</td><td>");
for (int i=0; i < cmdLineParameters.size(); i++) {
- String param = (String) cmdLineParameters.get(i);
+ String param = cmdLineParameters.get(i);
sb.append("<p>");
sb.append(param);
sb.append("</p>");
@@ -1275,7 +1277,7 @@
* @return CGI environment
*
*/
- protected Hashtable getEnvironment() {
+ protected Hashtable<String,String> getEnvironment() {
return env;
}
@@ -1287,7 +1289,7 @@
* @return CGI query parameters
*
*/
- protected ArrayList getParameters() {
+ protected ArrayList<String> getParameters() {
return cmdLineParameters;
}
@@ -1389,13 +1391,13 @@
private String command = null;
/** environment used when invoking the cgi script */
- private Hashtable env = null;
+ private Hashtable<String,String> env = null;
/** working directory used when invoking the cgi script */
private File wd = null;
/** command line parameters to be passed to the invoked script */
- private ArrayList params = null;
+ private ArrayList<String> params = null;
/** stdin to be passed to cgi script */
private InputStream stdin = null;
@@ -1423,8 +1425,8 @@
* @param params ArrayList with the script's query command line
* paramters as strings
*/
- protected CGIRunner(String command, Hashtable env, File wd,
- ArrayList params) {
+ protected CGIRunner(String command, Hashtable<String,String> env,
+ File wd, ArrayList<String> params) {
this.command = command;
this.env = env;
this.wd = wd;
@@ -1502,13 +1504,13 @@
* @exception NullPointerException if a hash key has a null value
*
*/
- protected String[] hashToStringArray(Hashtable h)
+ protected String[] hashToStringArray(Hashtable<String,?> h)
throws NullPointerException {
Vector<String> v = new Vector<String>();
- Enumeration e = h.keys();
+ Enumeration<String> e = h.keys();
while (e.hasMoreElements()) {
- String k = e.nextElement().toString();
- v.add(k + "=" + h.get(k));
+ String k = e.nextElement();
+ v.add(k + "=" + h.get(k).toString());
}
String[] strArr = new String[v.size()];
v.copyInto(strArr);
@@ -1596,6 +1598,7 @@
* with major modifications by Martin Dengler
*/
Runtime rt = null;
+ BufferedReader cgiHeaderReader = null;
InputStream cgiOutput = null;
BufferedReader commandsStdErr = null;
BufferedOutputStream commandsStdIn = null;
@@ -1615,7 +1618,7 @@
for (int i=0; i < params.size(); i++) {
cmdAndArgs.append(" ");
- String param = (String) params.get(i);
+ String param = params.get(i);
if (param.indexOf(" ") < 0) {
cmdAndArgs.append(param);
} else {
@@ -1635,7 +1638,7 @@
rt = Runtime.getRuntime();
proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd);
- String sContentLength = (String) env.get("CONTENT_LENGTH");
+ String sContentLength = env.get("CONTENT_LENGTH");
if(!"".equals(sContentLength)) {
commandsStdIn = new BufferedOutputStream(proc.getOutputStream());
@@ -1658,12 +1661,12 @@
new Thread() {
public void run () {
sendToLog(stdErrRdr) ;
- } ;
+ }
}.start() ;
InputStream cgiHeaderStream =
new HTTPHeaderInputStream(proc.getInputStream());
- BufferedReader cgiHeaderReader =
+ cgiHeaderReader =
new BufferedReader(new InputStreamReader(cgiHeaderStream));
while (isRunning) {
@@ -1726,12 +1729,21 @@
}
}
} //replacement for Process.waitFor()
+
}
catch (IOException e){
log ("Caught exception " + e);
throw e;
}
finally{
+ // Close the header reader
+ if (cgiHeaderReader != null) {
+ try {
+ cgiHeaderReader.close();
+ } catch (IOException ioe) {
+ log ("Exception closing header reader " + ioe);
+ }
+ }
// Close the output stream if used
if (cgiOutput != null) {
try {
@@ -1824,11 +1836,11 @@
rdr.close() ;
} catch (IOException ce) {
log("sendToLog error", ce) ;
- } ;
- } ;
+ }
+ }
if ( lineCount > 0 && debug > 2) {
log("runCGI: " + lineCount + " lines received on
stderr") ;
- } ;
+ }
}
} //class CGIRunner
@@ -1837,7 +1849,7 @@
* upto and including the two blank lines terminating the headers. It
* allows the content to be read using bytes or characters as appropriate.
*/
- protected class HTTPHeaderInputStream extends InputStream {
+ protected static class HTTPHeaderInputStream extends InputStream {
private static final int STATE_CHARACTER = 0;
private static final int STATE_FIRST_CR = 1;
private static final int STATE_FIRST_LF = 2;
Modified: trunk/webapps/docs/changelog.xml
===================================================================
--- trunk/webapps/docs/changelog.xml 2008-09-30 15:56:04 UTC (rev 798)
+++ trunk/webapps/docs/changelog.xml 2008-09-30 20:32:07 UTC (rev 799)
@@ -49,6 +49,9 @@
<fix>
<bug>45906</bug>: Another ETag improvement. Patch provided by Chris
Hubick. (remm)
</fix>
+ <fix>
+ CGI servlet generics cleanup. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">