MultipleHeaders.jws Sample

This topic inludes the source code for the MultipleHeaders.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 org.w3c.dom.Element;
008 import org.w3c.dom.Node;
009 import org.w3c.dom.NodeList;
010 
011 
012 public class multipleHeaders implements com.bea.jws.WebService
013 
014     /** @common:control */
015     private soapHeaders.multipleHeadersControl myServiceControl;
016 
017     static final long serialVersionUID = 1L;
018     
019     static int NUM_HEADERS = 3;             // Set up for 3 headers
020 
021     /**
022      * In this method, headers are created using HeaderDocument.Factory.Parse.
023      * The FirstChild is extracted into header, which is type Element. Each header 
024      * is put into an array that is passed to the JwsContext service method, 
025      * setOutputHeaders, which puts the headers in place.
026      
027      * The format of the Element header is:
028      * firstChild
029      *  localName   = "Header"
030      *  name        = "SOAP-ENV:Header"
031      *  namespaceURI= "http://schemas-xmlsoap.org/soap/envelope/"
032      *      firstChild
033      *          localName   = "content<header#>"
034      *          name        = "my:content<header#>"
035      *          namespaceURI= "http://my.com/uri/"
036      *              firstChild
037      *                  data    = "Header<header#>"
038      
039      * @common:operation
040      */
041     public void myMethod()
042     {     
043         String fromGetHeader[];
044         fromGetHeader = new String[5];
045         String fromGetThisProtocol;
046      /* 
047      * This method uses an XMLBean representation of the SOAP header and makes
048      * and instance of it using HeaderDocument.Factory.parse
049      */
050      HeaderDocument hd = null;
051      Element [] headerElement = new Element[NUM_HEADERS];       // Make room for up to 3 headers
052      for (int iHeader = 0; iHeader < NUM_HEADERS; iHeader++)    // Get the headers
053      {
054         try
055         {
056            hd = HeaderDocument.Factory.parse(
057                "<SOAP-ENV:Header xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" 
058                 "<my:content" + iHeader + 
059                 " xmlns:my=\"http://my.com/uri/\">Header " + iHeader + 
060                 "</my:content" + iHeader + ">" +
061                 "</SOAP-ENV:Header>");
062         }
063         catch (XmlException xe)
064         {
065             //should do something here.
066         }
067      /*
068      * Once there is a HeaderDocument we can obtain a Node using the newDomNode() method. 
069      * Using the getFirstChild() operation yields us the Header Node which is cast to an Element. 
070      * This Element is then passed to setOutputHeaders( Element[] ) on the service control.
071      */
072 
073         headerElement[iHeader(Elementhd.newDomNode().getFirstChild()// Get Node
074      }
075      myServiceControl.setOutputHeaders(headerElement);             // Set the headers
076   
077         fromGetHeader = myServiceControl.GetHeaders1();            // One way to ge the header names
078         fromGetHeader = myServiceControl.GetHeaders2();            // Another way to ge the header names
079     }
080     
081     
082     /**
083     * @common:context
084     */
085     JwsContext context;                                            // Create a context for the service
086    
087    /**
088     * The header can be obtained by using the JwsContext interface getInputHeaders(), which is the 
089     * compliment to setOutputHeaders() that was used in myMethod to add the header. The result is
090     * formated exactly the same way. Note that, while myMethod created multiple elements in
091     * headerElement[], the getInputHeaders method created a single array element. What you get 
092     * back with getInputHeaders is that single array element that contains all of the headers arranged
093     * in a tree of nodes.
094     
095     * You can extract the header names by traversing the tree that. To do this use the getFirstChild 
096     * and getNextSibling methods that are part of the Element interface.
097     *  
098     * @common:operation
099     */ 
100    
101     public String[] GetHeaders1()
102     {
103         String nodeNameValue[];
104         int nodeNumber = 0;
105         int numberOfHeaders;                                        // Number of header nodes
106         
107         Element[] myHeaders = context.getInputHeaders();            // Get all the headers
108         numberOfHeaders = myHeaders[0].getChildNodes().getLength()// Get the number of header nodes
109         nodeNameValue = new String[numberOfHeaders+1];              // Make enough room for headers
110                                                                     // plus the envelope name
111          
112         nodeNameValue[nodeNumber++= myHeaders[0].getNodeName();   // Get the envelope header 
113         
114         
115         Node node = myHeaders[0].getFirstChild();                   // Get inside the envelope
116 
117         for(nodeNumber =1; nodeNumber < numberOfHeaders+1; nodeNumber++)
118         {
119          nodeNameValue[nodeNumber= node.getNodeName();            // Node name for header
120          node = node.getNextSibling();                              // Get next header in tree 
121         }
122 
123         return nodeNameValue;                                       // Return header names
124   
125   
126   
127   /**
128    * This method gets just the header names, not the envelope name.
129    
130    * It's a somewhat simpler method that relys on getting a list of Child Nodes,
131    * where each child is one of the headers. The getChildNodes method also gives the number of
132    * nodes in the list. This way all you have to do is index into the list instead
133    * of keeping track of the child nodes and siblings.
134    
135    * To do this, use the getInputHeaders method to get the single array element with all the headers
136    * in it. Then use the Element interface method, getChildNodes to get a list of nodes in a NodeList
137    * type object. You then use the NodeList interface methods, getLength and item, to get the 
138    * number of nodes and header node, respectively. getNodeName then supplies the name of the node.
139    
140    *     
141    * @common:operation
142    */
143   
144     public String[] GetHeaders2()
145     {
146         String nodeNameValue[];
147         NodeList listOfHeaders;
148         int numberOfHeaders;                                        // Number of header nodes
149         
150         Element[] myHeaders = context.getInputHeaders();            // Get all the headers
151         numberOfHeaders = myHeaders[0].getChildNodes().getLength()// Get the number of header nodes
152         nodeNameValue = new String[numberOfHeaders];                // Make enough room for headers
153                                                                    
154         listOfHeaders = myHeaders[0].getChildNodes();               // Get a list of nodes
155         
156     /*
157     * Now just extract the name from each node using an index
158     * into the list. 
159     */
160     
161         for(int nodeNumber =0; nodeNumber < numberOfHeaders; nodeNumber++)
162         {
163          nodeNameValue[nodeNumber= listOfHeaders.item(nodeNumber).getNodeName()//Get the node name
164         }
165 
166         return nodeNameValue;                                       // Return header names
167   
168 
169