Siebel Portal Framework Guide > Delivering Content to External Web Applications > Connecting to the XML Web Interface >

Web Engine Interface Code Sample


The following code sample demonstrates invoking the Web Engine Interface business service.

///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003 Siebel Systems, Inc.,
// All rights reserved.
//
// FILE:     SWEBean.java
//     $Revision: $
//     $Date: $
//     $Author:$ of last update
//
// CREATOR:
//
// DESCRIPTION
//     This class encapsulates the initialization of SiebelDataBean and SWE
//     Web Service, and simplifies the communications between SWE and JSPs
//
//////////////////////////////////////////////////////////////////////////////

import com.siebel.data.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;

//----------------------------------------------------------------------
public class SWEBean
{
// SWE service name
static final String     SWE_SERVICE_NAME  =  "Web Engine Interface";

     // instance variables
     private SiebelDataBean  sdBean            = null;
     private SiebelService   swe               = null;
     private String          userName          = null;
     private String          password          = null;
     private String          connectString     = null;
     private String          serverPort        = null;
     private String          serverName        = null;

     private String          locale            = "enu";
     //-------------------------------------------------
     public SWEBean()
     {
     }

     //--------------------------------------------------
     // Entry point for testing
     public static void main(String[] args)
     {
          String testCmd = "<EXEC>" +
               "<CMD NAME=\"SWECmd\" VALUE=\"GotoPageTab\">" +
               "<ARG NAME=\"SWEScreen\">Opportunities Screen</ARG>" +
               "<ARG NAME=\"SWESetMarkup\">XML</ARG>" +
               "</CMD>" +
               "</EXEC>";

          try
          {
               String result;
               for(int i =0; i < args.length ; i++)
               {
                     System.out.println("Argument [" + i + "] : " + args[i]);
               }
               SWEBean sBean = new SWEBean();
               sBean.setConnectString(args[0]); // connect string is something like "siebel://KHLEE2:2320/siebel/SCCObjMgr_enu/KHLEE2"
               sBean.setUserName(args[1]); // testuser
               sBean.setPassword(args[2]); // db2
               sBean.setServerName(args[3]); // khlee2
               sBean.setServerPort(args[4]); // 2320
               if (args.length > 5)
               {
                    sBean.setLocale(args[5]);
               }

               // connect to OM
               sBean.init();

               //String testQ = "SWECmd=InvokeMethod&SWEReqrowId=1&SWEField=s_1_2_2_1&SWEMethod=Drilldown&SWERowId0=1-959&SWEView=Product+List+View&SWEApplet=Product+List+Applet&SWERowId=1-959&SWENeedContext=true&SWESetMarkup=XML";
               //result = sBean.makeXMLRequest("/callcenter_enu/start.swe", sBean.getUserName(), false, testQ);
               result = sBean.makeXMLRequest(testCmd, sBean.getUserName(), false, "");

               if (result != null)
               {
                    System.out.println("Result : " + result);
               }
               else
               {
                    System.out.println("Result : null");
               }
          }
          catch (Throwable t)
          {
               t.printStackTrace(System.err);
          }
     }
     //--------------------------------------------------
     public void init() throws Exception {
     init(userName, password, connectString);
     }

     //--------------------------------------------------
     protected void init(String uName, String passwd, String conn) throws Exception
     {
          if (uName != null && passwd != null && conn != null)
          {
               SiebelDataBean tBean = new SiebelDataBean();
               printInfo("Logging in to SiebelDataBean......");
               printInfo(" tBean.login(" + conn + ", " + uName + ",\n " +
               " " + passwd + ", " + getLocale() + ")");
               tBean.login(conn, uName, passwd, getLocale());
               printInfo("Finished login!");
               sdBean = tBean;

               initSWE();
          }
          else
          {
               throw new Exception("Invalid login parameters!");
          }
     }

     //--------------------------------------------------
     protected void initSWE() throws SiebelException
     {
          printInfo("Getting SWE Service......");
          swe = getService(SWE_SERVICE_NAME);
          printInfo("Finished getting SWE Service!");

          //pause();

          SiebelPropertySet pi = sdBean.newPropertySet();
          SiebelPropertySet po = sdBean.newPropertySet();

          /**
          the important property here is username because it tells
          the SWE that the user has logged in, which is different
          than telling swe who the user is, which we can do by setting
          the UserName in the args for a normal Request.
          **/
          printInfo("Initializing SWE......");
          pi.setProperty("A", getUserName()); // UserName
          pi.setProperty("B", "80"); // HTTPPort
          pi.setProperty("C", "443"); // HTTPSPort
          pi.setProperty("D", getServerName()); // ServerName
          pi.setProperty("G", "F"); // UseCookie
          // initialize the swe
          if (swe.invokeMethod("Start",pi,po))
          {
               printInfo("Done initializing SWE!");
               //     printPS(pi, "Input PropertySet");
               printPS(po, "Login Output PropertySet");
          }
          else
          {
               printInfo("Failed to initialize SWE!");
          }
     }

     //--------------------------------------------------
     protected SiebelService getService(String sName) throws SiebelException
     {
          if (sName != null && sdBean != null)
          {
               SiebelService service = sdBean.getService(sName);
               return service;
          }
          return null;
          }

     //--------------------------------------------------
     public String makeXMLRequest(String xmlRequest,
                                  String remoteUser,
                                  boolean isSecure,
                                  String query) throws SiebelException
     {
          printInfo("Processing XMLRequest : " + xmlRequest);
          printInfo("Getting ExecPath......");
          //String execPath = getExecPath(xmlRequest);
          //printInfo("Done getting ExecPath : " + execPath);

          SiebelPropertySet pi = sdBean.newPropertySet();
          SiebelPropertySet po = sdBean.newPropertySet();

          pi.setProperty("J", "0");// Mode
          pi.setProperty("E", getServerName());     // ServerName
          pi.setProperty("F", getServerPort());     // ServerPort
          pi.setProperty("A", "POST");              // Method
          pi.setProperty("B", "");                  // RequestURI
          pi.setProperty("Q", "text/xml");          // RequestBodyType

          if (xmlRequest.equals(""))
          {
               pi.setProperty("S", "0");          // RequestBodyLength
               pi.setProperty("T", "0");          // RequestBodyLengthTotal
               pi.setProperty("U", "");           // RequestBodyData
          }
          else
          {
               xmlRequest = "<?xml version=\"1.0\"?> " + xmlRequest;
               String len = (new Integer(xmlRequest.length() * 2)).toString();
               pi.setProperty("S", len);          // RequestBodyLength
               pi.setProperty("T", len);          // RequestBodyLengthTotal
               pi.setProperty("U", xmlRequest);   // RequestBodyData
          }
          // -----------------------------------------------------
          pi.setProperty("C", query);                             // QueryString
          pi.setProperty("G", "");                                // ScriptName
          pi.setProperty("D", (isSecure ? "https" : "http"));     // Channel
          pi.setProperty("I", "F");                               // UseCookie
          pi.setProperty("K", getUserName());                     // RemoteUser
          // -----------------------------------------------------
          printPS(pi, "XMLRequest Input Property");
          printInfo("Making XMLRequest......");

          boolean result = swe.invokeMethod("Request", pi, po);

          if (result)
          {
               printInfo("Done XMLRequest!");
          }
          else
          {
               printInfo("XMLRequest failed!");
          }
          printPS(po, "XMLRequest output");
          String response = po.getProperty("RespData");
          return response;
     }

     //--------------------------------------------------
     // Accessor functions
     public void setUserName(String name)
     {
          userName = name;
     }

     //--------------------------------------------------
     public String getUserName()
     {
          return userName == null ? "" : userName;
     }

     //--------------------------------------------------
     public void setPassword(String passwd)
     {
          password = passwd;
     }

     //--------------------------------------------------
     public String getPassword()
     {
          return password == null ? "" : password;
     }

     //--------------------------------------------------
     public void setConnectString(String conn)
     {
          connectString = conn;
     }

     //--------------------------------------------------
     public String getConnectString()
     {
          return connectString == null ? "" : connectString;
     }

     //--------------------------------------------------
     public void setServerPort(String port)
     {
          serverPort = port;
     }

     //--------------------------------------------------
     public String getServerPort()
     {
          return serverPort == null ? "" : serverPort;
     }

     //--------------------------------------------------
     public void setServerName(String sName)
     {
          serverName = sName;
     }

     //--------------------------------------------------
     public String getServerName()
     {
          return serverName == null ? "" : serverName;
     }

          //--------------------------------------------------
     public void setLocale(String lang)
     {
         locale = lang;
     }

     //--------------------------------------------------
     public String getLocale()
     {
          return locale == null ? "enu" : locale;
     }

     //------------------------------------------------------------
     public void pause()
     {
          try
          {
               System.out.println("The batch command reader has paused.");
               System.out.print("Press Enter to resume.");
               BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
               String tmp = in.readLine();
          }
          catch (Exception ex)
          {
          }
     }

     //--------------------------------------------------
     protected void printInfo(String msg)
     {
          System.out.println(msg);
     }

     //--------------------------------------------------
     void printPS(SiebelPropertySet ps, String title)
     {
          // Testing
          printInfo("*************** " + title + " ***************");
          for (String name = ps.getFirstProperty(); name != null && !name.equals(""); name = ps.getNextProperty())
          {
          String value = ps.getProperty(name);
          printInfo(name + " = " + value);
          }
     }
}

Siebel Portal Framework Guide