Friday, November 12, 2010

How to write a Axis2 Message Builder

Apache Axis2 is a open source SOAP/ Web services engine which consists of many extension points. Message Builders and Message Formatters are two ways we can extend the axis2 transport framework.In this post i’m going to explain the idea of Message builders and how we can implement a custom message builder and use it.

As i mentioned above Axis2 is a SOAP engine. So it only accept SOAP messages for processing. So then a problem comes that how we can use axis2 with non-soap invocations like REST. So to convert row non soap content coming from a transport to a SOAP content we can use Message builders.

This can really be useful when your in coming message have a non-xml format and you need to invoke a web service or process it with the axis2 engine with the given content information.

Its simple to Write your own Message builder.
First what you have to do is write a class implementing org.apache.axis2.builder.Builder interface in the Axis2 Kernal module.

The Builder method signature we need to implement look like this.



public OMElement processDocument(InputStream inputStream, String contentType,
MessageContext messageContext) throws AxisFault

As we see here we have three input parameters
  1. input stream : Stream of the raw payload
  2. content type : content type of the message that Builder is registered with.
  3. message context : axis2 message context


Here we can use input stream to create the SOAP info set from the coming message and its not a must to consume the input stream inside the builder.And we can use Axis2 message context to set any message context property information we get from the content or to get information from the message context that we need to build the message.

After we implement the Message builder we need to register it in the axis2 configuration (axis2.xml) with a content type. So messages that comes with that content type will be intercepted by the registered Message builder .

Example configuration in axis2.xml of that will look like this

< messageBuilder contentType="application/xml"
class="org.apache.axis2.builder.ApplicationXMLBuilder"/>

For a recent project i had to implement a custom Message builder to parse a Email coming in a plan text format and create a SOAP Info set out of it to processing.The Message builder solution become ideal for that problem since i had no control over the system that send the Mails.

No comments: