Siebel Web UI Dynamic Developer Kit Guide > Using Native Web Service Technology Stacks >

Examples of Setting and Getting SOAP Headers Using IBM WebSphere


To use Siebel session management and authentication SOAP headers in IBM WebSphere, you need to write custom code to set and get the SOAP headers. The following example demonstrates implementing Siebel SOAP headers using IBM WebSphere. The example has three components:

Client Context Handler Example

This example demonstrates including Java dependencies, declaring UsernameToken, PasswordText, and SessionType as strings, and setting and getting the SOAP headers.

package com.siebel.headers.handlers;

import javax.xml.namespace.QName;
import javax.xml.rpc.handler.*;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.*;
import java.util.Iterator;

public final class ClientContextHandler implements Handler {

        QName qn[] = null;
        public static String uname="username";
        public static String pword="password";
        public static String sessionType = "Stateless";
        //public static String sessionTypeLogout = "None";
        public static String SESSID;
        public static String ACTID;

        private static final String NS_URI
                = "http://siebel.com/webservices";
        private static final String SESSIONTYPE_ELEMENT
                = "SessionType";
        private static final String USERNAME_ELEMENT
                = "UsernameToken";
        private static final String PASSWORD_ELEMENT
                = "PasswordText";
        private static final QName SESSIONTYPE_HEADER
                = new QName(NS_URI, SESSIONTYPE_ELEMENT);
        private static final QName USERNAME_HEADER
                = new QName(NS_URI, USERNAME_ELEMENT);
        private static final QName PASSWORD_HEADER
                = new QName(NS_URI, PASSWORD_ELEMENT);

// Construct SOAP Header Elements Here ***

        public static QName[] HEADERS = new QName[]
{ SESSIONTYPE_HEADER,USERNAME_HEADER,PASSWORD_HEADER };

        public MySessionBean sessionId;
        public void init(HandlerInfo info) {
                qn = info.getHeaders();
                java.util.Map m = info.getHandlerConfig();
                uname = (String) m.get("username");
                pword = (String) m.get("password");
        }

        public boolean handleRequest(MessageContext msgContext) {
                        SOAPMessageContext smc = (SOAPMessageContext)msgContext;
                        SOAPMessage msg = smc.getMessage();
                        SOAPPart sp = msg.getSOAPPart();
                        try {
                                SOAPEnvelope se = sp.getEnvelope();
                                SOAPHeader header = se.getHeader();

                                // add SessionType

                                Name name1 = se.createName(qn[0].getLocalPart(),
                                        "s1", qn[0].getNamespaceURI());
                                SOAPHeaderElement headerElement1 = header.addHeaderElement(name1);
                                sessionId = (MySessionBean)msgContext.getProperty("SessionID");
                                if(sessionId.getValue()!=null)
                                {
                                        headerElement1.addTextNode(sessionType);
                                }
                                //if SessionId is null ie Login does not happen
                                        else
                                        {
        headerElement1.addTextNode("None");
                                        }

                                // add UsernameToken
                                Name name2 = se.createName(qn[1].getLocalPart(),
                                        "s2", qn[1].getNamespaceURI());
                                SOAPHeaderElement headerElement2 = header.addHeaderElement(name2);
                                headerElement2.addTextNode(uname);
                                // add PasswordText
                                Name name3 = se.createName(qn[2].getLocalPart(),
                                        "s3", qn[2].getNamespaceURI());
                                SOAPHeaderElement headerElement3 = header.addHeaderElement(name3);
                                headerElement3.addTextNode(pword);
                        } catch (SOAPException e) {
                                        e.printStackTrace();
                        }
                        return true;
                }

                public boolean handleResponse(MessageContext msgContext)
                {
                        SOAPMessageContext smc = (SOAPMessageContext)msgContext;
                        SOAPMessage msg = smc.getMessage();
                        SOAPPart sp = msg.getSOAPPart();
                        try {

                                SOAPEnvelope se = sp.getEnvelope();|
                                SOAPHeader header = se.getHeader();

                                Iterator it = header.getChildElements();
                                int level=0;
                                while(it.hasNext())
                                {
                                        SOAPHeaderElement he = (SOAPHeaderElement) it.next();
                                        Name name1 = he.getElementName();
                                        Iterator it2 = he.getChildElements();
                                        level++;
                                        if (it2.hasNext())
                                        {
                                                if (level<=1)
                                                {
                                                        Text t = (Text) it2.next();
                                                        sessionId = (MySessionBean)msgContext.getProperty("SessionID");
                                                        sessionId.SetValue(t.getValue());
                                                }
                                                else
                                                {
                                                        Text t = (Text) it2.next();
                                                }

                                        }
                                }
                        } catch (SOAPException e) {
                                e.printStackTrace();

                        }

                        return true;

                }

                public QName[] getHeaders() {
                        return qn;
                }

                public void destroy() {}

                public boolean handleFault(MessageContext mc) { SOAPMessageContext messageContext = (SOAPMessageContext) mc;
                        return true; }
}

MySessionBean Example

This example demonstrates storing the Session ID in the MySessionBean object.

package com.siebel.headers.handlers;

public class MySessionBean {

String value = "default";

    public String getValue()

    {

        return value;

    }

    public void SetValue(String strSessionID)
{
        value = strSessionID;

    }
}

JSP Page Example

The following JSP example demonstrates how the HandlerInfo object passes username, password, and session ID into the handler.

<HTML>
<HEAD>
<%@ page language="java"
    contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"
    session="true"
%>

<%@ page import="com.siebel.www.*" %>
<%@ page import="com.siebel.headers.handlers.*" %>
<%@ page import="javax.xml.rpc.handler.*" %>
<%@ page import="javax.xml.namespace.QName" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.URL" %>

<%

    String uname = request.getParameter("username");
    String pword = request.getParameter("password");
    String sessid;

%>

<HEAD>

<TITLE>Siebel Custom Headers</TITLE>
</HEAD>
<BODY>

<FORM METHOD="Get" >
    <TABLE BORDER="0" CELLPADDING="4" CELLSPACING="0" BGCOLOR="#eeeeee">
        <TR BGCOLOR="#ccccff">
            <TD COLSPAN="2" WIDTH="100%">
                <B><FONT FACE="Arial">Sign In</FONT></B>
            </TD>
        </TR>
        <TR>
            <TD ALIGN="right">
                <FONT FACE="Arial" SIZE="-1 ">User ID:</FONT>
            </TD>
            <TD>
                <INPUT NAME="username" SIZE="12" MAXLENGTH="32">
            </TD>
        </TR>
        <TR>
            <TD ALIGN="right">
                <FONT FACE="Arial" SIZE="-1">Password:</FONT>
            </TD>
            <TD>
                <INPUT NAME="password" SIZE="12" TYPE="password" MAXLENGTH="32">
            </TD>
        </TR>
        <TR>
            <TD>
                <INPUT NAME="action" SIZE="12" TYPE="hidden" value="login">
            </TD>
        </TR>
        <TR>
            <TD>
                &nbsp;
            </TD>
            <TD>
                <FONT FACE="Arial" SIZE="-1"><INPUT TYPE="submit" VALUE="Sign in" NAME="Invoke"></FONT>
            </TD>
        </TR>
        <TR>
            <TD>
                <INPUT NAME="action" SIZE="12" TYPE="hidden" value="logout">
            </TD>
        </TR>
        <TR>
            <TD>
                &nbsp;
            </TD>
            <TD>
                <FONT FACE="Arial" SIZE="-1"><INPUT TYPE="submit" VALUE="Logout" NAME="InvokeLogout"></FONT>
            </TD>
        </TR>
    </TABLE>
</FORM>

<%
    if (request.getParameter("Invoke")!= null)
    {
        uname = request.getParameter("username");
        pword = request.getParameter("password");
        sessid = InvokeWS(uname, pword);

        out.println("Your session ID is: " + sessid);
    }

%>

<%

    if (request.getParameter("InvokeLogout")!= null)
    {

        uname = request.getParameter("username");
        pword = request.getParameter("password");
        sessid = InvokeLogout(uname, pword);

        out.println("Your session ID is: " + sessid);
    }

%>

<%!

MySessionBean ctxValue;
        Port service;
        SessionAccessWSLocator locator;
        List handlerList ;
        HandlerRegistry hInfo;
        Map hConfig ;
public String InvokeWS(String username, String password)
{
    try{

        locator = new SessionAccessWSLocator();

        Iterator i = locator.getPorts();
        QName temp =(QName) i.next();

        //QName portQName = new QName("http://tempuri.org/", "WSHeaderSoap");
        //List handlerList = new ArrayList();
        handlerList = new ArrayList();
        HandlerRegistry hInfo = locator.getHandlerRegistry();
        hInfo = locator.getHandlerRegistry();

        hConfig = new HashMap();
        hConfig.put ("username", username);
        hConfig.put ("password", password);

        handlerList.add(new HandlerInfo(ClientContextHandler.class, hConfig, ClientContextHandler.HEADERS));
        hInfo.setHandlerChain(temp, handlerList);

        //WSHeaderSoapProxy wsheaderproxy = new WSHeaderSoapProxy();
        //WSHeaderSoap getConversationtemp = locator.getWSHeaderSoap();
        service = locator.getPort();

        ctxValue = new MySessionBean();
        ctxValue.SetValue("no session");
        //((WSHeaderSoapStub)getConversationtemp)._setProperty("SessionID",ctxValue);
        ((PortStub)service)._setProperty("SessionID", ctxValue);

        //getConversationtemp.doInvokeHeaders();
        service.sessionAccessPing(null);

        ctxValue = (MySessionBean)((PortStub)service)._getProperty("SessionID");
        //System.out.println("Session Bean is " + ctxValue.getValue());
        return (ctxValue.getValue());

    }
    catch(Exception e)
    {
        e.printStackTrace ();
        System.out.println (e.getMessage ());
        return (e.toString());
    }
}

%>

<%!

public String InvokeLogout(String username, String password)

{

    try{

        //SessionAccessWSLocator locator = new SessionAccessWSLocator();
        Iterator i = locator.getPorts();
        QName temp =(QName) i.next();
        //QName portQName = new QName("http://tempuri.org/", "WSHeaderSoap");
        //List handlerList = new ArrayList();
        handlerList = new ArrayList();
        //HandlerRegistry hInfo = locator.getHandlerRegistry();
        hInfo = locator.getHandlerRegistry();

        //Map hConfig = new HashMap();
        //hConfig.put ("username", username);
        //hConfig.put ("password", password);

        handlerList.add(new HandlerInfo(ClientContextHandler.class, hConfig, ClientContextHandler.HEADERS));
        hInfo.setHandlerChain(temp, handlerList);

        //WSHeaderSoapProxy wsheaderproxy = new WSHeaderSoapProxy();
        //WSHeaderSoap getConversationtemp = locator.getWSHeaderSoap();|
        service = locator.getPort();

        //MySessionBean ctxValue = new MySessionBean();
        ctxValue.SetValue(null);
        //((WSHeaderSoapStub)getConversationtemp)._setProperty("SessionID",ctxValue);
        //((PortStub)service)._setProperty("SessionID", ctxValue);
        ((PortStub)service)._setProperty("SessionID", ctxValue);

        //getConversationtemp.doInvokeHeaders();
        service.sessionAccessPing(null);

        ctxValue = (MySessionBean)((PortStub)service)._getProperty("SessionID");
        //System.out.println("Session Bean is " + ctxValue.getValue());
        return (ctxValue.getValue());

    }
    catch(Exception e)
    {
        e.printStackTrace ();
        System.out.println (e.getMessage ());
        return (e.toString());
    }
}

%>

</BODY>
</HTML>

Siebel Web UI Dynamic Developer Kit Guide Copyright © 2015, Oracle and/or its affiliates. All rights reserved. Legal Notices.