Monday, March 19, 2012

Adding SOAP Header element


Example for adding the SOAP Header element
For adding Version element in the SOAP header. Then final header will look like this.


<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<SOAP-ENV:Header>
  <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
  <Version xmlns="http://stack-over-flow.blogcpot.com/schema">1.1</Version>
  </SOAP-ENV:Header><SOAP-ENV:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-615">
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


When you are formatting your request at client side then you can add the following code for creation of header element.
soapMessage.getSoapHeader().addHeaderElement(new QName(" http://stack-over-flow.blogcpot.com /schema","Version")).setText("1.1");

It will add the SOAP header as shown in the example above


When you sent the request at server side if you want to access the header element from the request you can add interceptor like SoapEndpointInterceptor


add the following code in your customize interceptor 


import java.util.regex.Matcher;
import java.util.regex.Pattern;

private static final Pattern versionNamePattern = Pattern.compile("<Version xmlns=\" http://stack-over-flow.blogcpot.com/schema\">(.*)</Version>");
matcher = versionNamePattern.matcher(soapMessageText);
String version = (matcher.find())?matcher.group(1):null;







No comments:

Post a Comment