WSO2Con 2013 CFP Banner
WSO2Con 2013

How to send custom SOAP Fault messages in Axis2?

There are two ways one can achieve this.
  1. Using AxisFault
  2. Using Message Context
Date: Tue, 13th Jun, 2006
Level:
Reads: 23232
Discuss this article on Stack Overflow
Eran Chinthaka
Software Engineer
WSO2 Inc.
chinthaka's picture
Using AxisFault If you just want to send a simple custom SOAP Fault message, then create a new instance of AxisFault giving custom details and throw it out. Throw new AxisFault(new QName("http://test.org", "FaultCode", "test"), "FaultReason", new Exception("This is a test Exception")); Using Message Context The second method gives you enough flexibility to create highly customed SOAP Fault. How can this be done? Create SOAPFaultCode, SOAPFaultReason, SOAPFaultDetail, SOAPFaultNode, SOAPFaultRole instances and put them in to the message context. Throw new AxisFault(); Here is an example:
SOAPFactory soapFactory;
if (inMessageContext.isSOAP11()) {
soapFactory = OMAbstractFactory.getSOAP11Factory();
} else {
soapFactory = OMAbstractFactory.getSOAP12Factory();
}

soapFaultCode = soapFactory.createSOAPFaultCode();
SOAPFaultValue soapFaultValue = soapFactory.createSOAPFaultValue(soapFaultCode);
soapFaultValue.setText(new QName("http://test.org", "TestFault", "test"));

soapFaultReason = soapFactory.createSOAPFaultReason();
SOAPFaultText soapFaultText = soapFactory.createSOAPFaultText(soapFaultReason);
soapFaultText.setText("This is some FaultReason");

soapFaultDetail = soapFactory.createSOAPFaultDetail();
QName qName = new QName("http://someuri.org", "FaultException");
OMElement detail = soapFactory.createOMElement(qName, soapFaultDetail);
qName = new QName("http://someuri.org", "ExceptionMessage");
Throwable e = new Exception("This is a test Exception");
OMElement exception = soapFactory.createOMElement(qName, null);
exception.setText(e.getMessage());
detail.addChild(exception);

inMessageContext.setProperty(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME, soapFaultCode);
inMessageContext.setProperty(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME, soapFaultReason);
inMessageContext.setProperty(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME, soapFaultDetail);

Make sure, you have accessed to inMessageContext, using message context injection. If you do not know how to do it, here is an explanation. Axis2 code base contains a test case and a sample service testing Axis2 fault handling within a service.

Applies To:

Apache Axis2/Java versions post 1.0

mona's picture

Can you provide an example code???

I am new to web service. I have written webservice which when I am trying to call from java axis2 client, I get axis fault exceptions for all exceptions occured at service side but in case I call from .net client, an internal error html page is returned to client instead of SOAP Fault message. Can you please help me out in this and give an example service/client side code for the above tutorial. thanks in advance .... Mona