a peek into my MIND

September 10, 2008

Throwing custom exception types in Axis 2

Filed under: Java — Bharat Kondeti @ 7:47 pm

After doing some search we realized that axis 2 does not throw custom exceptions in a straight forward way, it will always throw an AxisFault, unless detail is set explicitly with the exception we want to throw.

Following is an example soap fault message where detail is set to throw ServiceFault. So when ever server throws a ServiceFault, client will catch the exception as ServiceFault rather than AxisFault.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>100</faultcode>
<faultstring>Something</faultstring>
<detail>
<dtaService:serviceFault xmlns:dtaService="xxx" xmlns:types="yyy">
<types:messages>
<types:message>
<types:code>XXX</types:code>
<types:text>XXXXXX</types:text>
</types:message>
</types:messages>
</dtaService:serviceFault>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>

detail in AxisFault is a OMElement, so the custom exception has to be converted into OMElement. This can be done using classes and utilities provide by axis2. Following is an example code to create AxisFault with detail set to it.

ServiceFault serviceFault = new ServiceFault("Some Fault");
XMLStreamReader reader = BeanUtil.getPullParser(serviceFault);
StreamWrapper parser = new StreamWrapper(reader);
StAXOMBuilder stAXOMBuilder =OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), parser);
OMElement element = stAXOMBuilder.getDocumentElement();
AxisFault af = new AxisFault(serviceFault.getMessage());
af.setDetail(element);

Advertisements

Hello world!

Filed under: screams — Bharat Kondeti @ 1:40 pm

Finally I signed up for a blog. Don’t know why it took me so long to realize that Its hard to maintain your own blog and keep up with all the updates and plug-ins. I am tired of seeing my blog down because of my stupidity and laziness. may be I will write more from now on….

Blog at WordPress.com.