/*******************************************************************************
*                                                                              *
*  File:        Servlet_01.java                         Revision:  1.0         *
*                                                                              *
*  Purpose:     reading parameters sent along with a request                   *
*                                                                              *
*  Creation:    08.09.2000                     Last Modification:  09.09.2000  *
*                                                                              *
*  Platform:    IBM-compatible PC running Windows 98SE                         *
*                                                                              *
*  Environment: Java 1.3, Gefion Software LiteWebServer 2.2.1                  *
*                                                                              *
*  Author:      Andreas Rozek           Phone:  ++49 (711) 6770682             *
*               Kirschblütenweg 15      Fax:    -                              *
*             D-70569 Stuttgart         EMail:  Andreas.Rozek@T-Online.De      *
*               Germany                                                        *
*                                                                              *
*  URL:         http://Home.T-Online.De/home/Andreas.Rozek                     *
*                                                                              *
*  Copyright:   this software is published under the "GNU General Public Li-   *
*               cense" (see "http://www.gnu.org/copyleft/gpl.html" for addi-   *
*               tional information)                                            *
*                                                                              *
*  Comments:    the paths in this servlet assume that the LiteWebServer is     *
*               still in its default configuration                             *
*                                                                              *
*               Warning: for the sake of simplicity, this servlet is not able  *
*               to handle several requests concurrently                        *
*                                                                              *
*******************************************************************************/

import java.io.*;                            // Java file handling and stream IO
import java.util.*;                  // Java utility classes and data structures
import javax.servlet.*;                        // servlet implementation classes
import javax.servlet.http.*;                             // servlet HTTP support

public class Servlet_01 extends HttpServlet {

/*******************************************************************************
*                                                                              *
*                         Non-Public Instance Variables                        *
*                                                                              *
*******************************************************************************/

  PrintWriter OutStream;         // is used to construct this servlet's response
  boolean     oddTableRow;                       // a simple table row "counter"

/*******************************************************************************
*                                                                              *
*                           Public Instance Methods                            *
*                                                                              *
*******************************************************************************/

/*******************************************************************************
*                                                                              *
* doGet                                                  handles a GET request *
*                                                                              *
*******************************************************************************/

  public void doGet (HttpServletRequest Request, HttpServletResponse Response)
  throws ServletException, IOException {
    try {
      Response.setContentType("text/html");

    /**** send HTML document with input form in case of missing parameters ****/

      Enumeration NameList = Request.getParameterNames();
      if (!NameList.hasMoreElements()) {
  System.out.println("Servlet_01: processing request without parameters");
        try {
          BufferedReader InputStream = new BufferedReader(new FileReader("../HTML/Servlet_01.html"));
            ArrayList LineList = new ArrayList();
              String Line = InputStream.readLine();
              while (Line != null) {
                LineList.add(Line);
                Line = InputStream.readLine();
              };
            int LineCount = LineList.size();

            OutStream = Response.getWriter();
              for (int i = 0; i < LineCount; i++) {
                OutStream.println((String) LineList.get(i));
              };
            OutStream.close();
          InputStream.close();
        } catch (IOException Signal) {
          OutStream = Response.getWriter();
            insertHeader("Servlet_01 - Error Report");
              OutStream.println("<h3><font color=\"red\">IOException</font></h3>");
              OutStream.println("An error occured while reading file \"../HTML/Servlet_01.html\"<br>");
              OutStream.print  ("Reason: ");
                String Message = Signal.getMessage();
                if (Message == null) {
                  OutStream.print("(unknown)");
                } else {
                  OutStream.print("\"" + Message + "\"");
                };
              OutStream.println("<br>");
            insertTrailer();
          OutStream.close();
        };
System.out.println("Servlet_01: finished processing the request");

        return;
      };

    /**** process all given parameters and show their contents ****/

System.out.println("Servlet_01: processing parameterized request");
      OutStream = Response.getWriter();
        insertHeader("Servlet_01 - Form Input Display");
          OutStream.println("          <h3>Parameter List</h3>");
          OutStream.println("          <p align=justify>");
          OutStream.println("            The following table contains an overview of all request parameters:");
          OutStream.println("          <p align=justify>");
          OutStream.println("            <table border=\"0\" cellspacing=\"0\" cellpadding=\"2\" width=100%>");
            oddTableRow = true;

            while (NameList.hasMoreElements()) {
              String   ParameterName = (String) NameList.nextElement();
              String[] ValueList = Request.getParameterValues(ParameterName);

              if (ValueList.length == 0) {
                showParameter(ParameterName, null);
              } else {
                showParameter(ParameterName, ValueList[0]);
                for (int i = 1; i < ValueList.length; i++) {
                  showParameter(null, ValueList[i]);
                };
              };
            };
          OutStream.println("            </table>");
        insertTrailer();
      OutStream.close();
    } catch (IOException Signal) {
      log(
        "caught IOException " + Signal.getClass().getName() + " while constructing " +
        "the response (reason: \"" + Signal.getMessage() + "\")"
      );
    };
System.out.println("Servlet_01: finished processing the request");
  };

/*******************************************************************************
*                                                                              *
* doPost                                                handles a POST request *
*                                                                              *
*******************************************************************************/

  public void doPost (HttpServletRequest Request, HttpServletResponse Response)
  throws ServletException, IOException {
    doGet(Request, Response);
  };

/*******************************************************************************
*                                                                              *
* getLastModified                 yields the timestamp of the requested object *
*                                                                              *
*******************************************************************************/

  public long getLastModified (HttpServletRequest Request) {
    return -1L;
  };

/*******************************************************************************
*                                                                              *
* insertHeader                       inserts a page header into the "Response" *
*                                                                              *
*******************************************************************************/

  public void insertHeader (String Title) {
    OutStream.println("<!DocType HTML public \"-//W3C//DTD HTML 4.01//EN\"");
    OutStream.println(" \"http://www.w3.org/tr/html4/loose.dtd\">");
    OutStream.println("<html>");
    OutStream.println("  <head>");
    OutStream.println("    <title>" + Title + "</title>");
    OutStream.println("  </head>");
    OutStream.println();
    OutStream.println("  <body background=\"/Paper.jpg\">");
    OutStream.println("    <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=600>");
    OutStream.println("      <tr valign=\"bottom\">");
    OutStream.println("        <td align=left><B>" + Title + "</B></td>");
    OutStream.println("        <td align=right></td>");
    OutStream.println("      </tr>");
    OutStream.println();
    OutStream.println("      <tr valign=top>");
    OutStream.println("        <td height=10 colspan=2><img src=\"/Line_600.gif\" width=\"600\" height=\"2\"></td>");
    OutStream.println("      </tr>");
    OutStream.println("    </table>");

    OutStream.println("    <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=600>");
    OutStream.println("      <tr valign=\"top\">");
    OutStream.println("        <td align=left>");
  };

/*******************************************************************************
*                                                                              *
* insertTrailer                     inserts a page trailer into the "Response" *
*                                                                              *
*******************************************************************************/

  public void insertTrailer () {
    OutStream.println("        </td>");
    OutStream.println("      </tr>");
    OutStream.println("    </table>");

    OutStream.println("    <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=600>");
    OutStream.println("      <tr valign=\"bottom\">");
    OutStream.println("        <td height=10 colspan=2><img src=\"/Line_600.gif\" width=\"600\" height=\"2\"></td>");
    OutStream.println("      </tr>");
    OutStream.println();
    OutStream.println("      <tr valign=top>");
    OutStream.println("        <td align=left><I><A href=\"mailto:Andreas.Rozek@T-Online.De\">Andreas Rozek</A></I></td>");
    OutStream.println("        <td align=right></td>");
    OutStream.println("      </tr>");
    OutStream.println("    </table>");
    OutStream.println("  </body>");
    OutStream.println("</html>");
  };

/*******************************************************************************
*                                                                              *
* showParameter                      displays a given parameter with its value *
*                                                                              *
*******************************************************************************/

  public void showParameter (String Name, String Value) {

  /**** beware of values with multiple lines ****/

    if (Value != null) {
      if ((Value.indexOf('\r') >= 0) || (Value.indexOf('\n') >= 0)) {
        StringTokenizer Parser = new StringTokenizer(Value, "\r\n");
        switch (Parser.countTokens()) {
          case 0:  Value = "";
                   break;
          case 1:  Value = Parser.nextToken();
                   break;
          default: showParameter(Name, Parser.nextToken());
                   while (Parser.hasMoreTokens()) {
                     showParameter(null, Parser.nextToken());
                   };
                   return;
        };
      };
    };

  /**** now handle missing values or those without CR or LF ****/

    OutStream.println("              <tr valign=top bgcolor=" + (oddTableRow ? "#CCFFCC" : "white") + ">");
    OutStream.println("                <td align=left><img src=\"/PaperHole.gif\"></td>");
      if (Name == null) {
        OutStream.println("                <td></td>");
      } else {
        OutStream.println("                <td align=right><b><tt>" + Name + "</tt></b>:&nbsp;</td>");
      };
    OutStream.print  ("                <td align=left>");
      if (Value == null) {
        OutStream.print("<i>(not available)</i>");
      } else {
        OutStream.print("\"" + Value + "\"");
      };
    OutStream.println("</td>");
    OutStream.println("                <td align=right><img src=\"/PaperHole.gif\"></td>");
    OutStream.println("              </tr>");

    oddTableRow = !oddTableRow;
  };
};

