User login

How to send custom SOAP Fault messages in Axis2?

Story :

Project :

There are two ways one can achieve this.

  1. Using AxisFault
  2. Using Message Context

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

5
Average: 5 (1 vote)