Showing posts with label Axiom. Show all posts
Showing posts with label Axiom. Show all posts

Tuesday, October 7, 2008

Axis2 - Adding custom SOAP Headers to a request using ServiceClient

The Axis2 ServiceClient API provides three main ways of adding SOAP Headers to a request. Lets take a look at how this could be done,

  • Add a simple header which contains a String to the request


serviceClient.addStringHeader(new QName("http://wso2.org", "ThirdHeader"), "This is a sample Header");

This is the simplest method of adding a header. But this method restricts you to simple String headers.

  • Creating a header as a OMElement and adding it to the request


OMElement firstHeader = AXIOMUtil.stringToOM(
"This is a sample Header");
serviceClient.addHeader(firstHeader);

Users are expected to create the complete header themselves using Axiom's OMElement APIs.

  • Creating a header as a SOAPHeaderBlock and adding it to the request


OMNamespace omNamespace =
OMAbstractFactory.getOMFactory().createOMNamespace("http://wso2.com", "ws");
SOAPHeaderBlock secondHeader = OMAbstractFactory.getSOAP12Factory().createSOAPHeaderBlock("SecondHeader", omNamespace);
secondHeader.addChild(AXIOMUtil.stringToOM("This is a sample Header"));
//secondHeader.setMustUnderstand(true);
serviceClient.addHeader(secondHeader);

Users are expected to create the header using Axiom's SOAP APIs. Users could use the API to set various options on the header such as the MustUnderstand Attribute.

Monday, October 6, 2008

Axiom - How to create a OMElement from a String

If someone is using Axis2's ServiceClient to send a request to a service they will have to deal with OMElements. That is because ServiceClient accepts the payload as a OMElement. Using Axiom API's to create this OMElement could be time consuming and may take several lines of code. Alternatively users could create this OMElement from a String as follows,

OMElement payload = AXIOMUtil.stringToOM("Hi! This is a sample Request.");

This utility method that comes with Axiom takes in a String as the argument and returns an OMElement.