Sending implicit SOAP headers with JAX-WS

You can enable an existing Java™ API for XML-Based Web Services (JAX-WS) web services client to send values in implicit SOAP headers. By modifying your client code to send implicit SOAP headers, you can send specific information within an outgoing web service request.

Before you begin

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

An implicit SOAP header is a SOAP header that fits one of the following descriptions:
  • A message part that is declared as a SOAP header in the binding in the Web Services Description Language (WSDL) file, but the message definition is not referenced by a portType element within a WSDL file.
  • An element that is not contained in the WSDL file.

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

Using JAX-WS, there is no restriction on types of headers that you can manipulate.

About this task

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

Procedure

  1. Create a java.util.HashMap<QName, List<String>> 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 a List<String> object, and each String is the XML text of the entire SOAP header element. By using List<String> object, you can add multiple SOAP header elements that each have the same QName object.
  3. Set the HashMap object as a property on the request context of the Dispatch or Proxy object.
    The property name is com.ibm.wsspi.websvcs.Constants.JAXWS_OUTBOUND_SOAP_HEADERS. The value of the property is the HashMap.
  4. Issue the remote method calls using the Dispatch or Proxy object.
    The headers within the HashMap object are sent in the outgoing message.
    A WebServiceException 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 List<String> object.
    • The String representing an SOAP header is not a compliant XML message.
    • The HashMap contains a key that represents a SOAP header that is declared protected by the owning component.

Results

You have a JAX-WS web services client that is configured to send implicit SOAP headers.

Example

The following programming example illustrates how to set two request SOAP headers and retrieve one response SOAP header within a JAX-WS web services request and response context:
1  //Create the hashmaps for the outbound soap headers
2  Map<QName, List<String>> outboundHeaders=new HashMap<QName, List<String>>(); 
3
4  //Add "AtmUuid1" and "AtmUuid2" to the outbound map
5  List<String> list1 = new ArrayList<String>();
6  list1.add("<AtmUuid1 xmlns=\"com.rotbank.security\"><uuid>ROTB-0A01254385FCA09</uuid></AtmUuid1>");
7  List<String> list2 = new ArrayList<String>();
8  list2.add("<AtmUuid2 xmlns=\"com.rotbank.security\"><uuid>ROTB-0A01254385FCA09</uuid></AtmUuid2>"
9  outboundHeaders.put(new QName("com.rotbank.security", "AtmUuid1"), list1);
10 outboundHeaders.put(new QName("com.rotbank.security", "AtmUuid2"), list2);
11 // Set the outbound map on the request context
12 dispatch.getRequestContext().put("jaxws.binding.soap.headers.outbound");
13 // Invoke the remote operation
14 dispatch.invoke(parm1);
15 // Get the inbound header map from the response context
16 Map<QName,List<String>> inboundMap = dispatch.getResponseContext().get("jaxws.binding.soap.headers.outbound");
17 List<String> serverUuidList = inboundMap.get(new QName("com.rotbank.security","ServerUuid"));
18 String text = serverUiidList.get(0);
19 //Note: text now equals a XML object that contains a SOAP header:
21//"<y:ServerUuid xmlns:y=\"com.rotbank.security\"><:uuid>ROTB-0A03519322FSA01
22  </y:uuid></y:ServerUuid.");

On line 2, create the outbound SOAP header map.

On lines 5-10, the AtmUuid1 and AtmUuid2 headers elements are added to the outbound map.

On line 12, the outbound map is set on the request context, which causes the AtmUuid1 and AtmUuid2 headers to be added to the request message when the operation is invoked.

On line 14, invoke the remote operation.

On line 15, obtain the outbound header map.

On lines 17-18, the ServerUuid header is retrieved from the response Map. The Map accesses the SOAP header from the response message.