/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Copyright (c) Non, Inc. 1997 -- All Rights Reserved PROJECT: Servlet Library MODULE: Servlets FILE: PosterServlet.java AUTHOR: John D. Mitchell, Oct 5, 1997 REVISION HISTORY: Name Date Description ---- ---- ----------- JDM 97.10.05 Initial version. JDM 97.10.11 Added temp. directory clearing. DESCRIPTION: This servlet helps to deal with the problem that applets can't do a showDocument() call on a POST request. However, we can work around this by accepting the POST with the servlet, generating a temporary file containing the desired web page output, returning a magic string to the applet for encoding in the URL of a showDocument() call (which is a GET request), accepting the showDocument()'s request, decoding the magic string, and returning the previously generated content/file. Note that all of the calls to log() are commented out since JWS v1.1a seems to crash if log() is used inside of catch blocks. INSTALLATION: Compile this code and put it into the $JSERV_HOME/servlets/net/non/servlets/ directory. Add it to the known servlets list using the adminstration tool. Add a servlet alias for it of "/poster". %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ package net.non.servlets; // Standard Java core packages: import java.io.*; import java.util.*; // Java Servlet packages: import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.http.HttpUtils.*; public class PosterServlet extends HttpServlet { // The file/path name parts for the temporary files. protected String posterTempDir = "posters"; protected String posterTempExt = ".pst"; // Default value for limit of GET & POST information that we'll accept // so as to protect ourselves from a DOS attack. protected int postContentLimit = 100*1024; // 100KB limit. protected int getContentLimit = 1*1024; // 1KB limit. // Random number generator (helps generate temporary filenames. protected Random randomizer = null; // Override getServletInfo(). public String getServletInfo() { return ( "Copyright Non, Inc. 1997 -- " + "John D. Mitchell (john@non.net)\n\n" + "POST HTTP servlet." ); } public void init (ServletConfig sc) { // Create the randomizer. randomizer = new Random (); // Delete all of the (abandoned) generated files. clearTempDir(); } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Process the incoming information, generate page output, generate // response output. // Set the response type. response.setContentType ("text/plain"); // Check to limit Denial of Service attack. // Limit POST information to a reasonable size. if (postContentLimit < request.getContentLength()) { sendPostFailure (response, "Exceeded POST data length limit. Go away punk!"); /* // Log the error for the SysAdmin. log ("PosterServlet: POST content exceeded limit!"); */ return; } // Build the ouput filename. String fileName = (new Long (randomizer.nextLong())).toString(); File file = null; try { file = new File (posterTempDir + File.separator + fileName + posterTempExt); } catch (Exception e) { // Sigh. Something is really wrong since this is only supposed // to throw up if the path doesn't exist. sendPostFailure (response, "Unable to build output file path!"); /* // Log the error for the SysAdmin. log ("PosterServlet: Not allowed to access :" + file.getAbsolutePath() + ": Please check the permissions."); */ return; } // Open the output file. PrintWriter output = null; try { output = new PrintWriter ( new BufferedWriter ( new FileWriter (file))); } catch (IOException e) { // Hmm... Couldn't open the file. sendPostFailure (response, "Unable to open output file!"); /* // Log the error for the SysAdmin. log ("PosterServlet: Unable to open :" + file.getAbsolutePath() + ": for writing. Please check the permissions."); */ return; } // Spew out the appropriate header shme. output.println (""); output.print ("
");
// Read each line of input and spew it to the output file.
HttpUtils httpUtils = new HttpUtils();
try
{
while (null != (line = in.readLine()))
{
try
{
Hashtable data = httpUtils.parseQueryString (line);
// Rip through the list of entries and write out
// each pair to a single line of the output file.
String keyName = null;
Enumeration keys = data.keys();
// Should really also URLDecode the data but it's
// not provided as part of the standard Java
// classes and I don't want to add yet more code to
// this example.
while (keys.hasMoreElements())
{
String[] values = null;
keyName = (String) keys.nextElement();
output.print (keyName);
values = (String[]) data.get (keyName);
output.print (" =");
if (1 < values.length)
output.println ("");
for (int i = 0; i < values.length; i++)
{
output.print ("\t");
output.println (values[i]);
}
}
output.println();
}
catch (IllegalArgumentException e)
{
// Couldn't seem to parse the query string.
// Just dump the raw input string to the output.
output.print (line);
}
}
}
catch (IOException e)
{
// Must have been some sort of input error.
// Just note it and continue.
output.println ("\n"); output.println ("Unable to read entire POST contents!"); output.println ("
\n");
}
output.println (""); out.println ("Unable to read entire data file contents!"); out.println ("
"); out.println (reason); out.println ("