MySoap12Service.jws Sample

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