Welcome

Dealing with SOAP Headers in Axis

After a long period, I just returned back to playing with Axis to develop web services. Axis 1.4 is a framework to create SOAP processing clients and servers. One of the issues I encountered during my web service development is dealing with SOAP headers.

You can use SOAP header elements to send application specific information (e.g. authentication, context, time etc.) about SOAP messages. Although they are optional, there is an attribute called as mustUnderstand in SOAPHeaderElement. Once it is set as true, actor who receives SOAP message must process it correctly.

You must deal with this attribute or SOAP Header in order to successfully process your SOAP message, otherwise you will get a SOAP fault. There are several alternatives for dealing with this. You can either set this flag to false again, or remove SOAPHeaderElement from message envelope inside your custom Axis Handlers. For example;

public class SoapHeaderConsumerHandler extends BasicHandler {
	public void invoke(MessageContext messageContext) throws AxisFault {
		try {
			SOAPHeader soapHeader = (SOAPHeader) messageContext.getMessage().getSOAPHeader();
			Iterator itr = soapHeader.examineAllHeaderElements();
			while (itr.hasNext()) {
				SOAPHeaderElement element = (SOAPHeaderElement) itr.next();
				element.setMustUnderstand(false)
			}
		} catch (SOAPException e) {
			throw new RuntimeException(e);
		}
	}
}

The other alternative is to extract them from message envelope. Axis SOAPHeader provides to extract all header elements or to extract ones which belong to a specific actor.

SOAPHeader soapHeader = (SOAPHeader) messageContext. getMessage().getSOAPHeader();
Iterator itr = soapHeader.extractAllHeaderElements();

When you call extract method, you will get an iterator to iterate over SOAPHeaderElements which are removed from message envelope.

You can configure your custom handler in Axis at different levels. They can be configured globally, or be made specific to several services. It is also possible to invoke them just for requests or responses or both. Axis uses a web service deployment desciptor (wsdd) to keep all those configuration information. More info about Handlers are coming on next post…

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.