package examples.servlets;

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

import weblogic.html.*;

/**
 * This simple phonebook application is
 * built on top of a properties list. It uses
 * the text file "phonelist" also in this directory.
 *
 * @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 PhoneServlet extends HttpServlet { 
  private Properties phones;
  
  public void init(ServletConfig config) throws ServletException {
    int next;
    String name, number;
    
    super.init(config);
    
    log("PhoneServlet.initialize: enter");
    
    String fileName = getInitParameter("phonelist");
    if (fileName == null) {
      log("PhoneServlet.init: phonelist parameter not defined");
      return;
    }
    
    log("PhoneServlet.initialize: filename = " + fileName);
    
    phones = new Properties();
    FileInputStream fin;
    try {
      fin = new FileInputStream(fileName);
      phones.load(fin);
    }
    catch (IOException e) {
      log("Phone servlet file not found. " + e);
      phones = null;
      throw new ServletException("PhoneServlet failed to find phonelist file: " + fileName, e);
    }
  }

  /**
   * Implements the service method.  If the query is for a specific
   * person, this method just returns their number; otherwise, it dumps 
   * the whole extensions list. We use an htmlKona servlet page to display
   * the results.
   */
  public void service(HttpServletRequest req, HttpServletResponse res)
       throws IOException
  {
    res.setContentType("text/html");
    ServletPage sp = new ServletPage("Phonelist servlet");
    sp.getBody()
         .addElement(new FontElement()
                     .setFontColor("#db1260")
                     .setFontFace("Helvetica")
                     .setElement(new HeadingElement("Phonelist")));
    sp.getBodyElement().setAttribute(BodyElement.bgColor, "#ffffff");
    if (phones == null) {
      sp.getBody().addElement(new StringElement("Sorry, No phone list."));
    }
    else {
      // The req object contains the HttpRequest parameters
      // This servlet checks for a parameter called 'name'
      String name = req.getParameter("name");
      
      if ((name != null) && (!name.equals(""))) {
        // Try to return the phone number for the specified 'name' parameter
        String phone = (String) phones.get(name);
        if (phone == null) {
          phone = name + " was Not found";
        }
        else {
          phone = name + ": " + phone;
        }
        sp.getBody().addElement(new StringElement(phone));
      }
      else {
        // List the entire phone directory using the 
        // TableElement(Dictionary) constructor to auto
        // create a table of the contents of 'phones'.
        sp.getBody().addElement((new TableElement(phones)).setWidth(5));
      }
    }
    sp.output(res.getOutputStream());
  }

  /**
   * Simple information about the servlet.
   */
  public String getServletInfo() {
    return "This servlet is useful for looking up phone extensions.";
  }
}
