+

Search Tips   |   Advanced Search

Sending implicit SOAP headers with JAX-RPC

We can enable an existing JAX-RPC Web services client to send values in implicit SOAP headers. By modifying the client code to send implicit SOAP headers, we can send specific information within an outgoing web service request.

To complete this task, we need a web services client that we can enable to send implicit SOAP headers.

An implicit SOAP header is a SOAP header that fits one of the following descriptions:

Handlers and service endpoints can manipulate implicit or explicit SOAP headers using the SOAP with Attachments API for Java (SAAJ) data model.

We cannot manipulate protected SOAP headers. A SOAP header that is declared protected by its owning component, for example, Web Services Security, is not accessible to client applications. An exception occurs if we try to manipulate protected SOAP headers.

The client application sets properties on the Stub or Call object to send and receive implicit SOAP headers.


Tasks

  1. Create a java.util.HashMap object.

  2. Add an entry to the HashMap object for each implicit SOAP header that the client wants to send. The HashMap entry key is the QName of the SOAP header. The HashMap entry value is either an SAAJ SOAPElement object or a String containing the XML text of the entire SOAP header element.

  3. Set the HashMap object as a property on the Stub or Call object. The property name is com.ibm.websphere.webservices.Constants.REQUEST_SOAP_HEADERS. The value of the property is the HashMap.
  4. Issue the remote method calls using the Stub or Call object. The headers within the HashMap object are sent in the outgoing message.

    A JAXRPCException error can occur if any of the following are true:

    • The HashMap object contains a key that is not a QName object or if the HashMap object contains a value that is not a String or a SOAPElement object.

    • The HashMap object contains a key that represents a SOAP header that is declared protected by the owning component.

We have a JAX-RPC web services client configured to send implicit SOAP headers.


Example

The following programming example illustrates how to send two request SOAP headers and receive one response SOAP header within a web services request and response:

1 //Create the request and response hashmaps.
2 HashMap requestHeaders=new HashMap();
3 HashMap responseHeaders=new HashMap();
4
5 //Add "AtmUuid1" and "AtmUuid2" to the request hashmap.
6 requestHeaders.put(new QName("com.rotbank.security", "AtmUuid1"),
7   "<AtmUuid1 xmlns=\"><uuid>ROTB-0A01254385FCA09</uuid></AtmUuid1>");
8 requestHeaders.put(new QName("com.rotbank.security", "AtmUuid2"),
9  ((IBMSOAPFactory)SOAPFactory.newInstance()).createElementFromXMLString(
10   "x:AtmUuid2 xmlns:x=\"com.rotbank.security\"><x:uuid>ROTB-0A01254385FCA09
     </x:uuid><x:AtmUuid2>"));
11
12 //Add "ServerUuid" to the response hashmap.
13 //If "responseHeaders" is empty, all the SOAP headers are
14 //extracted from the response message.
15 responseHeaders.put(new QName("com.rotbank.security","ServerUuid"), null);
16
17 //Set the properties on the Stub object.
18 stub.setProperty(Constants.REQUEST_SOAP_HEADERS.requestHeaders);
19 stub.setProperty(Constants.RESPONSE_SOAP_HEADERS.responseHeaders);
20 
21 //Call the operationon the Stub.
22 stub.foo(parm2, parm2);
23
24 //Retrieve "ServerUuid" from the response hashmap.
25 SOAPElement serverUuid =
26   (SOAPElement) responseHeaders.get(new QName("com.rotbank.security","ServerUuid"));
27
28 //Note: "serverUuid" now equals a SOAPElement object that represents the 
29 //following code:
30//"<y:ServerUuid xmlns:y=\"com.rotbank.security\"><:uuid>ROTB-0A03519322FSA01
   </y:uuid></y:ServerUuid.");

On lines 2-3, new HashMaps are created used for the request and response SOAP headers.

On lines 6-10, the AtmUuid1 and AtmUuid2 headers elements are added to the request HashMap.

On line 15, the ServerUuid header element name, along with a null value, is added to the response HashMap.

On line 18, the request HashMap is set as a property on the Stub object. This causes the AtmUuid1 and AtmUuid2 headers to be added to each request message associated with an operation that is invoked on the Stub object.

On line 19, the response HashMap is set as a property on the Stub object. This causes the ServerUuid header to be extracted from each response message associated with an operation that is invoked on the Stub object.

On line 22, the web service operation is invoked on the Stub object.

On lines 25-26, the ServerUuid header is retrieved from the response HashMap. The header was extracted from the response message and inserted into the HashMap by the web services engine.


Related:

  • SOAP with Attachments API for Java interface
  • Receiving implicit SOAP headers with JAX-RPC
  • Implement extensions to JAX-RPC web services clients