Hello all,
I'm currently developing a framework using AXIS2 to send custome messages.
In case of an exception (abnormal behaviour) we have to send Faults - so
far it is quite usual.
I have done some research and found only one working way to set the proper
fault parameters. It's something like this:
---
SOAP 1.1:
SOAP11Factory soap11Factory = new SOAP11Factory();
SOAPFaultCode code = soap11Factory.createSOAPFaultCode();
code.setText(...);
msgContext.setProperty(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME, code);
SOAPFaultReason reason = soap11Factory.createSOAPFaultReason();
reason.setText(...)
msgContext.setProperty(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME,
reason);
SOAPFaultDetail details = new SOAP11FaultDetailImpl(new SOAP11Factory());
// set details...
msgContext.setProperty(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME,
details);
SOAP 1.2:
SOAP12Factory soap12Factory = new SOAP12Factory();
SOAPFaultCode soapFaultCode = soap12Factory.createSOAPFaultCode();
SOAPFaultValue soapFaultCodeValue = soap12Factory.createSOAPFaultValue
(soapFaultCode);
soapFaultCodeValue.setText(new QName(SOAP12Constants.
SOAP_ENVELOPE_NAMESPACE_URI, ..., SOAP12Constants.
SOAP_DEFAULT_NAMESPACE_PREFIX));
soapFaultCode.setValue(soapFaultCodeValue);
SOAPFaultSubCode soapfaultsubcode = soap12Factory.createSOAPFaultSubCode();
SOAPFaultValue soapFaultSubCodeValue = soap12Factory.createSOAPFaultValue
(soapfaultsubcode);
soapFaultSubCodeValue.setText(new QName(SOAP12Constants.
SOAP_ENVELOPE_NAMESPACE_URI, ...), SOAP12Constants.
SOAP_DEFAULT_NAMESPACE_PREFIX));
soapfaultsubcode.setValue(soapFaultSubCodeValue);
soapFaultCode.setSubCode(soapfaultsubcode);
msgContext.setProperty(SOAP12Constants.SOAP_FAULT_CODE_LOCAL_NAME,
soapFaultCode);
SOAPFaultReason soapFaultReason = soap12Factory.createSOAPFaultReason();
SOAPFaultText soapFaultText = soap12Factory.createSOAPFaultText
(soapFaultReason);
soapFaultText.setText(...);
msgContext.setProperty(SOAP12Constants.SOAP_FAULT_REASON_LOCAL_NAME,
soapFaultReason);
SOAPFaultDetail soapFaultDetail = soap12Factory.createSOAPFaultDetail();
//set fault detail
msgContext.setProperty(SOAP12Constants.SOAP_FAULT_DETAIL_LOCAL_NAME,
soapFaultDetail);
---
I think, the way to create a Fault by setting 3 MessageContext parameters
is kind of... not optimal... :-) Especially if we know what we want to
invoke:
---
MessageContext msgContext = ...
Exception e = ...
OutputStream out = ...
AxisFault fault = new AxisFault("");
invokeAxisFaultFlow(msgContext, e, fault, out);
---
If we create an AxisFault and we set all needed parameters then it will be
forgotten. Only the MessageContext parameters will be considered.
I think, the better way to do the job is something like this:
---
SOAP 1.1:
SOAP11Factory soap11Factory = new SOAP11Factory();
SOAPFault soapFault = soap11Factory.createSOAPFault();
SOAPFaultCode soapFaultCode = soap11Factory.createSOAPFaultCode(soapFault);
soapFaultCode.setText(...);
soapFault.setCode(soapFaultCode);
SOAPFaultReason soapFaultReason = soap11Factory.createSOAPFaultReason
(soapFault);
soapFaultReason.setText(...);
soapFault.setReason(soapFaultReason);
SOAPFaultDetail soapFaultDetail = soap11Factory.createSOAPFaultDetail
(soapFault);
OMElement codeElement = soap11Factory.createOMElement(new QName(...));
codeElement.setText(...);
OMElement stringElement = soap11Factory.createOMElement(new QName(...));
stringElement.setText(...);
soapFaultDetail.addChild(codeElement);
soapFaultDetail.addChild(stringElement);
soapFault.setDetail(soapFaultDetail);
AxisFault fault = new AxisFault(soapFault);
SOAP 1.2:
SOAP12Factory soap12Factory = new SOAP12Factory();
SOAPFault soapFault = soap12Factory.createSOAPFault();
SOAPFaultCode soapFaultCode = soap12Factory.createSOAPFaultCode(soapFault);
SOAPFaultValue soapFaultCodeValue = soap12Factory.createSOAPFaultValue
(soapFaultCode);
soapFaultCodeValue.setText(new QName(SOAP12Constants.
SOAP_ENVELOPE_NAMESPACE_URI, ..., SOAP12Constants.
SOAP_DEFAULT_NAMESPACE_PREFIX));
SOAPFaultSubCode soapFaultSubCode = soap12Factory.createSOAPFaultSubCode
(soapFaultCode);
SOAPFaultValue soapFaultSubCodeValue = soap12Factory.createSOAPFaultValue
(soapFaultSubCode);
soapFaultSubCodeValue.setText(new QName(SOAP12Constants.
SOAP_ENVELOPE_NAMESPACE_URI, ..., SOAP12Constants.
SOAP_DEFAULT_NAMESPACE_PREFIX));
soapFaultCode.setSubCode(soapFaultSubCode);
soapFault.setCode(soapFaultCode);
//fault reason
SOAPFaultReason soapFaultReason = soap12Factory.createSOAPFaultReason
(soapFault);
SOAPFaultText soapFaultText = soap12Factory.createSOAPFaultText
(soapFaultReason);
soapFaultText.setText(...);
//fault detail
SOAPFaultDetail soapFaultDetail = soap12Factory.createSOAPFaultDetail
(soapFault);
OMElement codeElement = soap12Factory.createOMElement(new QName(...));
codeElement.setText(...);
OMElement stringElement = soap12Factory.createOMElement(new QName(...));
stringElement.setText(...);
soapFaultDetail.addChild(codeElement);
soapFaultDetail.addChild(stringElement);
soapFault.setDetail(soapFaultDetail);
AxisFault fault = new AxisFault(soapFault);
---
And then we could pass the so created AxisFault to the "
invokeAxisFaultFlow..." method.
This provides a better testability to our code. We can create faults, test
them and so on, without considering what happens in the
"invokeAxisFaultFlow" method.
By the way - i've read in the mailing list, that only one child element can
be set into the Details element of an Fault. This is not correct (or maybe
not anymore...) We have always set two child elements...
Some additional information: We use Axis2 1.5.1.
Greetings,
Marcin Markiewicz
----------------------------------------------------------------------------------------------------------------------------------------------
Fiducia IT AG
Fiduciastraße 20
76227 Karlsruhe
Sitz der Gesellschaft: Karlsruhe
AG Mannheim HRB 100059
Vorsitzender des Aufsichtsrats: Gregor Scheller
Vorsitzender des Vorstands: Michael Krings
Stellv. Vorsitzender des Vorstands: Klaus-Peter Bruns
Vorstand: Jens-Olaf Bartels, Hans-Peter Straberger
Umsatzsteuer-ID.Nr. DE143582320, http://www.fiducia.de
----------------------------------------------------------------------------------------------------------------------------------------------