MySoap11Service.jws Sample

This topic inludes the source code for the MySoap11Service.jws Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/soapHeaders/

Sample Source Code


001 package soapHeaders; 
002 
003 import com.bea.control.JwsContext;
004 import com.bea.jws.Protocol;
005 import org.xmlsoap.schemas.soap.envelope.HeaderDocument;
006 import com.bea.xml.XmlException;
007 import javax.xml.soap.Node;
008 import org.w3c.dom.Element;
009 
010 
011 public class mySoap11Service implements com.bea.jws.WebService
012 
013     /** @common:control */
014     private soapHeaders.mySoap11ServiceControl myServiceControl;
015 
016     static final long serialVersionUID = 1L;
017 
018     /**
019      * In this method, a header is created using HeaderDocument.Factory.Parse.
020      * The FirstChild is extracted into header, which is type Element. The header
021      * is put into place using the JwsContext service method, setOutputHeaders.
022      
023      * The format of the Element header is:
024      * firstChild
025      *  localName   = "Header"
026      *  name        = "SOAP-ENV:Header"
027      *  namespaceURI= "http://schemas-xmlsoap.org/soap/envelope/"
028      *      firstChild
029      *          localName   = "content"
030      *          name        = "my:content"
031      *          namespaceURI= "http://my.com/uri/"
032      *              firstChild
033      *                  data    = "Context Text"
034      
035      * @common:operation
036      */
037     public void myMethod()
038     {     
039         String fromGetHeader[];
040         fromGetHeader = new String[5];
041         String fromGetThisProtocol;
042      /* 
043      * This method uses an XMLBean representation of the SOAP header and makes
044      * and instance of it using HeaderDocument.Factory.parse
045      */
046         HeaderDocument hd = null;
047         try
048         {
049            hd = HeaderDocument.Factory.parse(
050                "<SOAP-ENV:Header xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" 
051        "<my:content xmlns:my=\"http://my.com/uri/\">Content Text</my:content>" +
052                "</SOAP-ENV:Header>");
053         }
054         catch (XmlException xe)
055         {
056             //should do something here.
057         }
058      /*
059      * Once there is a HeaderDocument we can obtain a Node using the newDomNode() method. 
060      * Using the getFirstChild() operation yields us the Header Node which is cast to an Element. 
061      * This Element is then passed to setOutputHeaders( Element[] ) on the service control.
062      */
063 
064         Element header = (Elementhd.newDomNode().getFirstChild();
065         myServiceControl.setOutputHeaders(new Element[] { header });
066   
067         fromGetHeader = myServiceControl.GetHeader();               //  returns the header name
068         fromGetThisProtocol = myServiceControl.getThisProtocol();   //  returns the protocol name
069     }
070     
071     
072     /**
073     * @common:context
074     */
075     JwsContext context;                                            // Create a context for the service
076    
077    /**
078     * The header can be obtained by using the JwsContext interface getInputHeaders() is the 
079     * compliment to setOutputHeaders() used in myMethod to add the header and the result is
080     * formated exactly the same way. You can extract some of the header by using the methods 
081     * that come with the Element interface.
082     
083     *   getNodeName extracts the header name    "soapenv:Header"
084     *   getTagName extracts the tag name        "soapenv:Header"
085     *   
086     * Note that the header that is retrieved is that of the SOAP envelope. To get the rest
087     * of the header information you have to traverse the tree that is created. See the 
088     * multipleHeaders.jws.
089     
090     * @common:operation
091     */ 
092     public String[] GetHeader()
093     {
094         String nodeNameValue[];
095         int i;
096         String firstTag;
097         
098         
099         Element[] myHeaders = context.getInputHeaders();        // Get the header
100         nodeNameValue = new String[myHeaders.length];
101         
102         for(i=0; i<myHeaders.length; i++)
103         {     
104             nodeNameValue[i= myHeaders[i].getNodeName();      // Extract header name
105             firstTag = myHeaders[i].getTagName();               // Extract Tag name
106         }
107         return nodeNameValue;                                   // Return header name
108   
109 
110     /**
111     * getProtocol is in the JwsContext interface. It returns a type Protocol,
112     * which is made up of an ID and the text name of the protocol type. The Protocol
113     * class also contains the list of protocols that it refers to.
114     
115     * The Protocol class has a method by the name of getName that returns the
116     * name of the protocol. So, by using getName, you can tell if this is a
117     * SOAP protocol or some other type of protocol.
118     
119      * @common:operation
120      */
121     public String getThisProtocol()
122     {
123         Protocol myProtocol = context.getProtocol();            // Get the protocol info
124         
125         return myProtocol.getName();                            // Extract and return protocol type
126     }
127