home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jsdk / SnoopSer.jav < prev    next >
Encoding:
Text File  |  1997-08-17  |  5.4 KB  |  183 lines

  1. /*
  2.  * @(#)SnoopServlet.java    1.16 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. import java.io.*;
  23. import java.util.*;
  24.  
  25. import javax.servlet.*;
  26. import javax.servlet.http.*;
  27.  
  28. import sun.security.x509.*;
  29.  
  30.  
  31. /**
  32.  * Snoop servlet. This servlet simply echos back the request line and
  33.  * headers that were sent by the client, plus any HTTPS information
  34.  * which is accessible.
  35.  *
  36.  * @version     1.16, 05/22/97
  37.  * @author     David Connelly
  38.  */
  39. public
  40. class SnoopServlet extends HttpServlet {
  41.  
  42.     public void doGet (HttpServletRequest req, HttpServletResponse res)
  43.     throws ServletException, IOException
  44.     {
  45.     res.setContentType("text/html");
  46.     ServletOutputStream out = res.getOutputStream();
  47.     out.println("<html>");
  48.     out.println("<head><title>Snoop Servlet</title></head>");
  49.     out.println("<body>");
  50.  
  51.     out.println("<h1>Requested URL:</h1>");
  52.     out.println("<pre>");
  53.     out.println (HttpUtils.getRequestURL (req).toString ());
  54.     out.println("</pre>");
  55.  
  56.     Enumeration enum = getServletConfig().getInitParameterNames();
  57.         if (enum != null) {
  58.             boolean first = true;
  59.         while (enum.hasMoreElements()) {
  60.         if (first) {
  61.                 out.println("<h1>Init Parameters</h1>");
  62.                 out.println("<pre>");
  63.             first = false;
  64.                 }
  65.         String param = (String) enum.nextElement();
  66.                 out.println(" "+param+": "+getInitParameter(param));
  67.         }
  68.         out.println("</pre>");
  69.         }
  70.  
  71.     out.println("<h1>Request information:</h1>");
  72.     out.println("<pre>");
  73.     print(out, "Request method", req.getMethod());
  74.     print(out, "Request URI", req.getRequestURI());
  75.     print(out, "Request protocol", req.getProtocol());
  76.     print(out, "Servlet path", req.getServletPath());
  77.     print(out, "Path info", req.getPathInfo());
  78.     print(out, "Path translated", req.getPathTranslated());
  79.     print(out, "Query string", req.getQueryString());
  80.     print(out, "Content length", req.getContentLength());
  81.     print(out, "Content type", req.getContentType());
  82.     print(out, "Server name", req.getServerName());
  83.     print(out, "Server port", req.getServerPort());
  84.     print(out, "Remote user", req.getRemoteUser());
  85.     print(out, "Remote address", req.getRemoteAddr());
  86.     print(out, "Remote host", req.getRemoteHost());
  87.     print(out, "Authorization scheme", req.getAuthType());
  88.     out.println("</pre>");
  89.     
  90.     Enumeration e = req.getHeaderNames();
  91.     if (e.hasMoreElements()) {
  92.         out.println("<h1>Request headers:</h1>");
  93.         out.println("<pre>");
  94.         while (e.hasMoreElements()) {
  95.         String name = (String)e.nextElement();
  96.         out.println(" " + name + ": " + req.getHeader(name));
  97.         }
  98.         out.println("</pre>");
  99.     }
  100.  
  101.     e = req.getParameterNames();
  102.     if (e.hasMoreElements()) {
  103.         out.println("<h1>Servlet parameters (Single Value style):</h1>");
  104.         out.println("<pre>");
  105.         while (e.hasMoreElements()) {
  106.         String name = (String)e.nextElement();
  107.         out.println(" " + name + " = " + req.getParameter(name));
  108.         }
  109.         out.println("</pre>");
  110.     }
  111.  
  112.     e = req.getParameterNames();
  113.     if (e.hasMoreElements()) {
  114.         out.println("<h1>Servlet parameters (Multiple Value style):</h1>");
  115.         out.println("<pre>");
  116.         while (e.hasMoreElements()) {
  117.         String name = (String)e.nextElement();
  118.         String vals[] = (String []) req.getParameterValues(name);
  119.         if (vals != null) {
  120.             out.print("<b> " + name + " = </b>"); 
  121.             out.println(vals[0]);
  122.             for (int i = 1; i<vals.length; i++)
  123.             out.println("           " + vals[i]);
  124.         }
  125.         out.println("<p>");
  126.         }
  127.         out.println("</pre>");
  128.     }
  129.  
  130.     String    cipherSuite = (String)
  131.         req.getAttribute ("javax.net.ssl.cipher_suite");
  132.  
  133.     if (cipherSuite != null) {
  134.         X509Cert    certChain [] = (X509Cert [])
  135.         req.getAttribute ("javax.net.ssl.peer_certificates");
  136.         
  137.         out.println ("<h1>HTTPS Information:</h1>");
  138.         out.println("<pre>");
  139.  
  140.         out.println ("Cipher Suite:  " + cipherSuite);
  141.  
  142.         if (certChain != null) {
  143.         for (int i = 0; i < certChain.length; i++) {
  144.             out.println ("client cert chain [" + i + "] = "
  145.             + certChain [i].toString ());
  146.         }
  147.         }
  148.  
  149.         // javax.net.ssl.session --> ssl.Session object
  150.         // ... has above data plus creation and last used dates
  151.  
  152.         out.println("</pre>");
  153.     }
  154.  
  155.     
  156.     out.println("</body></html>");
  157.     }
  158.  
  159.     private void print(ServletOutputStream out, String name, String value)
  160.     throws IOException
  161.     {
  162.     out.print(" " + name + ": ");
  163.     out.println(value == null ? "<none>" : value);
  164.     }
  165.  
  166.     private void print(ServletOutputStream out, String name, int value)
  167.     throws IOException
  168.     {
  169.     out.print(" " + name + ": ");
  170.     if (value == -1) {
  171.         out.println("<none>");
  172.     } else {
  173.         out.println(value);
  174.     }
  175.     }
  176.  
  177.     private static final String UNKNOWN = "<unknown>";
  178.  
  179.     public String getServletInfo() {
  180.     return "A servlet that shows the request headers sent by the client";
  181.     }
  182. }
  183.