/*******************************************************************************
*                                                                              *
*  File:        Servlet_02.java                         Revision:  1.0         *
*                                                                              *
*  Purpose:     displaying a list of headers supplied 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_02 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");

      OutStream = Response.getWriter();
        insertHeader("Servlet_02 - Header Display");
          OutStream.println("          <h3>Header List</h3>");
          OutStream.println("          <p align=justify>");
          OutStream.println("            The following table contains an overview of all headers sent along with this request:");
          OutStream.println("          <p align=justify>");
          OutStream.println("            <table border=\"0\" cellspacing=\"0\" cellpadding=\"2\" width=100%>");
            oddTableRow = true;

            showHeader("(Request&nbsp;Method)",   Request.getMethod());
            showHeader("(Request&nbsp;URI)",      Request.getRequestURI());
            showHeader("(Request&nbsp;Protocol)", Request.getProtocol());

            Enumeration HeaderList = Request.getHeaderNames();
            while (HeaderList.hasMoreElements()) {
              String HeaderName  = (String) HeaderList.nextElement();
              String HeaderValue = Request.getHeader(HeaderName);

              showHeader(HeaderName, HeaderValue);
            };
          OutStream.println("            </table>");
        insertTrailer();
      OutStream.close();
    } catch (IOException Signal) {
      log(
        "caught IOException " + Signal.getClass().getName() + " while constructing " +
        "the response (reason: \"" + Signal.getMessage() + "\")"
      );
    };
  };

/*******************************************************************************
*                                                                              *
* 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>");
  };

/*******************************************************************************
*                                                                              *
* showHeader                    displays a given request header with its value *
*                                                                              *
*******************************************************************************/

  public void showHeader (String Name, String Value) {
    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.replace('\n','|') + "\"");
      };
    OutStream.println("</td>");
    OutStream.println("                <td align=right><img src=\"/PaperHole.gif\"></td>");
    OutStream.println("              </tr>");

    oddTableRow = !oddTableRow;
  };
};
