package examples.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import weblogic.utils.*;
import weblogic.html.*;
import weblogic.common.*;
import weblogic.event.common.*;

/**
 * This example demonstrates sending an event to the WebLogic Server
 * containing all of the available header information of a request
 * to the servlet. It uses an
 * EventMessage to send information to WebLogic about the hits on
 * this page; whomever has registered an interest in the event
 * "HTTPWATCH" receives notification of the event, along with the
 * collected information.
 * <p>
 * To run this example, compile it and register the servlet with the
 * initArg "imagefile" set to a local image file on your WebLogic Server
 * host. Then start WebLogic. At a command-line prompt, start the
 * WebLogic utility, eventsnoop, and register an interest in the
 * page-related events with the following command:
 * <pre> $ <b>java utils.eventsnoop t3://localhost:7001 HTTPWATCH</b></pre>
 * where <i>t3://localhost:7001</i> corresponds to the host and port
 * of your running WebLogic Server. Then access the page. You'll see
 * the resulting events in the command window. 
 * <p>
 * WebLogic also has a builtin service that allows you to register an
 * interest in HTTP events and have them logged in common log format
 * to an HTTP log. For more info, read the WebLogic Administrators
 * Guide document, <a href=../../admindocs/http.html>Setting
 * up WebLogic as an HTTP server</a>.
 *
 * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class PageEvent extends HttpServlet { 

  /**
   * Implements the service method.
   */
  public void service(HttpServletRequest req, HttpServletResponse res)
       throws IOException 
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    
    out.println("<html><head><title>Page Event Servlet</title></head>");
    
    try {
      
      // Set up a ParamSet to hold information about the
      // hit on this page.
      ParamSet eventParameters = new ParamSet();
      eventParameters.setParam("Request method",   req.getMethod());
      eventParameters.setParam("Request protocol", req.getProtocol());
      eventParameters.setParam("Servlet path",     req.getServletPath());
      eventParameters.setParam("Path info",        req.getPathInfo());
      eventParameters.setParam("Path translated",  req.getPathTranslated());
      eventParameters.setParam("Query string",     req.getQueryString());
      eventParameters.setParam("Content length",   req.getContentLength());
      eventParameters.setParam("Content type",     req.getContentType());
      eventParameters.setParam("Server name",      req.getServerName());
      eventParameters.setParam("Server port",      req.getServerPort());
      eventParameters.setParam("Remote user",      req.getRemoteUser());
      eventParameters.setParam("Remote address",   req.getRemoteAddr());
      eventParameters.setParam("Remote host",      req.getRemoteHost());
      
      
      // Capture all other header names, and store in eventParameters
      String name;
      Enumeration names = req.getHeaderNames();
      while (names.hasMoreElements()) {
        name = (String)names.nextElement();
        eventParameters.setParam(name, req.getHeader(name));
      }
      // Create and submit an event message to the HTTPWATCH event
      // including the eventParameters captured from the HttpServletRequest
      EventMessageDef ev = T3Services.getT3Services().events().
           getEventMessage("HTTPWATCH", eventParameters);
      ev.submit();
      out.println("<body><h2>You have paged HTTPWATCH!</h2></body></html>");
    }
    catch (Exception e) {
      out.println("<body><h2>An exception occured:"+e+"</h2></body></html>");
      e.printStackTrace();
    }
  }
  
  public String getServletInfo() {
    return("This servlet sends an HTTPWATCH event containing request information when hit");
  }
}


