package examples.wlec.ejb.simpapp;

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

import weblogic.html.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.RemoteException;
import java.util.*;

/**
 * This example demonstrates how a WebLogic Servlet can connect to WebLogic Enterprise through
 * an EJBean which then invokes an operation on a CORBA object. This 
 * servlet can either be called directly from the browser or through 
 * the <font face="Courier New" size=-1>ejbSimpapp.html</font> file. 
 * <UL>
 * <LI>When the user submits the form, the appropriate method on the remote object is called.
 * <LI>This request goes through the ISL/ISH to the appropriate server in the WebLogic Enterprise domain.
 * <LI>The server converts the string and sends it back to the EJBean which
 * in turn sends it to the servlet.
 * <LI> The servlet returns the converted string to the client browser within some generated HTML.
 * </UL>
 *
 *
 * @author Copyright (c) 1999-2000 BEA Systems, Inc.  All rights reserved.
 */

public class ejbSimpappServlet extends HttpServlet {

  /**
  * A simple implementation of the service method, in
  * which we get the remote EJObject from the EJB Home and then 
  * invoke the <font face="Courier New" size=-1>toLower</font>
  * or <font face="Courier New" size=-1>toUpper</font> method (as requested by the user), 
  * construct a servlet page and print the user string in uppercase or lowercase.  
  * See the provided <font face="Courier New" size=-1>ejbSimpapp.html</font> for the HTML form used to submit
  * the data.
  */

  final String defaultCase = "UPPER";
  final String defaultString = "It Works";

  public void service(HttpServletRequest req, HttpServletResponse res) 
  throws IOException, ServletException
  {
      ConverterResult result;

    // Create a HTML page
    res.setContentType("text/html");
    ServletPage page = 
      new ServletPage("Weblogic Enterprise Connectivity - Simpapp EJB Sample");

    // Set the color of the page
    page.getBodyElement()
      .setAttribute(BodyElement.bgColor, HtmlColor.purple);
    page.getBodyElement()
      .setAttribute(BodyElement.textColor, HtmlColor.white);

    // Read the input data
    String mixed = (String) req.getParameter("InData");
    String changeCase = (String) req.getParameter("ChangeCase");
    String user = (String) req.getParameter("user");
    String password = (String) req.getParameter("password");

    if(user!=null && password!=null){
      page.getBody().
        addElement("Note: Using the user: " + user + "and password: " + password);
    } 
    else {
      page.getBody()
        .addElement("Note: No user and password credentials");
    }

    // If no string is passed, use the default string
    if ((mixed == null) || (mixed.length() <= 0)) {
      mixed = defaultString;
      page.getBody()
        .addElement(MarkupElement.Break)
        .addElement("Note: Using the default data: " + mixed);
    }

    // If no case is passed, convert to UPPERCASE
    if ((changeCase == null) || (changeCase.length() <= 0)) {
      changeCase = defaultCase;
      page.getBody()
        .addElement(MarkupElement.Break)
        .addElement("Note: Using the default action: " + changeCase);
    }

    try {

      // Create a converter object, which will later help us do conversion
      Context ctx = getInitialContext(user, password);
      ConverterHome converterH = (ConverterHome) ctx.lookup("simpapp.ConverterHome");

      Converter converter = converterH.create();

      try {
        if (changeCase.equals(defaultCase)) {
          // To Upper
          result = converter.toUpper(mixed);
        }
        else
        {
          // To lower
          result = converter.toLower(mixed);
        }  
        page.getBody()
          .addElement(new HeadingElement("Output String: " + result.outData, 4));
      }
      catch (ProcessingErrorException pe) {
        log("Processing Error: " + pe);
        return;
      } 
      catch (Exception e) {
        log(":::::::::::::: Error :::::::::::::::::");
        return;
      }

      if (converter != null) {
        try {
          // Remove converter
          converter.remove();
        }
        catch (Exception e) {
          log("Error removing converter ");
          return;
        }
      }
    }
    catch (Exception e) {
      log(":::::::::::::: Error :::::::::::::::::");
      return;
    }
  
    // Return the html to the client browser
    page.output(res.getOutputStream());
    // everything succeeded.
  }

  /**
  * Basic servlet info.
  */
  public String getServletInfo() {
    return "A simple servlet which acts a client for WebLogic Enterprise";
  }

  /**
  * Gets an initial context.
  *
  * @return                  Context
  * @exception               java.lang.Exception if there is
  *                          an error in getting a Context
  */
  static public Context getInitialContext(String user, String password) 
  throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY,
      "weblogic.jndi.WLInitialContextFactory");
    if (user != null) {
      p.put(Context.SECURITY_PRINCIPAL, user);
      if (password == null) 
        password = "";
      p.put(Context.SECURITY_CREDENTIALS, password);
    } 
    return new InitialContext(p);
  }
}
