SOAPBody body,
boolean ignoreDetailBlocks) throws WebServiceException {
// Get the factory and create the soapFault
SOAPFactory factory = (SOAPFactory)body.getOMFactory();
SOAPFault soapFault = factory.createSOAPFault(body);
OMNamespace ns = body.getNamespace();
// The SOAPFault structure is modeled after SOAP 1.2.
// Here is a sample comprehensive SOAP 1.2 fault which will help you understand the
// structure.
// <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
// xmlns:m="http://www.example.org/timeouts"
// xmlns:xml="http://www.w3.org/XML/1998/namespace">
// <env:Body>
// <env:Fault>
// <env:Code>
// <env:Value>env:Sender</env:Value>
// <env:Subcode>
// <env:Value>m:MessageTimeout</env:Value>
// </env:Subcode>
// </env:Code>
// <env:Reason>
// <env:Text xml:lang="en">Sender Timeout</env:Text>
// <env:Text xml:lang="de">Sender Timeout</env:Text>
// </env:Reason>
// <env:Node>http://my.example.org/Node</env:Node>
// <env:Role>http://my.example.org/Role</env:Role>
// <env:Detail>
// <m:MaxTime>P5M</m:MaxTime>
// </env:Detail>
// </env:Fault>
// </env:Body>
// </env:Envelope>
boolean isSoap11 = soapFault.getNamespace().getNamespaceURI()
.equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
// Set the primary Code Value
SOAPFaultCode soapCode = factory.createSOAPFaultCode(soapFault);
QName soapValueQName = xmlFault.getCode().toQName(ns.getNamespaceURI());
if (isSoap11) {
soapCode.setText(soapValueQName);
} else {
SOAPFaultValue soapValue = factory.createSOAPFaultValue(soapCode);
soapValue.setText(soapValueQName);
}
// Set the primary Reason Text
SOAPFaultReason soapReason = factory.createSOAPFaultReason(soapFault);
if (isSoap11) {
soapReason.setText(xmlFault.getReason().getText());
} else {
SOAPFaultText soapText = factory.createSOAPFaultText(soapReason);
soapText.setText(xmlFault.getReason().getText());
soapText.setLang(xmlFault.getReason().getLang());
}
// Set the Detail and contents of Detail
Block[] blocks = xmlFault.getDetailBlocks();
if (blocks != null && blocks.length > 0) {
SOAPFaultDetail detail = factory.createSOAPFaultDetail(soapFault);
if (!ignoreDetailBlocks) {
for (int i = 0; i < blocks.length; i++) {
// A Block implements OMDataSource. So create OMSourcedElements
// for each of the Blocks.
OMSourcedElement element =
factory.createOMElement(blocks[i], blocks[i].getQName());
detail.addChild(element);
}
}
}
// Now set all of the secondary fault information
// Set the SubCodes
QName[] subCodes = xmlFault.getSubCodes();
if (subCodes != null && subCodes.length > 0) {
OMElement curr = soapCode;
for (int i = 0; i < subCodes.length; i++) {
SOAPFaultSubCode subCode = (i == 0) ?
factory.createSOAPFaultSubCode((SOAPFaultCode)curr) :
factory.createSOAPFaultSubCode((SOAPFaultSubCode)curr);
SOAPFaultValue soapSubCodeValue = factory.createSOAPFaultValue(subCode);
soapSubCodeValue.setText(subCodes[i]);
curr = subCode;
}
}
// Set the secondary reasons and languages
XMLFaultReason reasons[] = xmlFault.getSecondaryReasons();
if (reasons != null && reasons.length > 0) {
for (int i = 0; i < reasons.length; i++) {
SOAPFaultText soapReasonText = factory.createSOAPFaultText(soapReason);
soapReasonText.setText(reasons[i].getText());
soapReasonText.setLang(reasons[i].getLang());
}
}
// Set the Role
if (xmlFault.getRole() != null) {
SOAPFaultRole soapRole = factory.createSOAPFaultRole();
soapRole.setText(xmlFault.getRole());
soapFault.setRole(soapRole);
}
// Set the Node
if (xmlFault.getNode() != null) {
SOAPFaultNode soapNode = factory.createSOAPFaultNode();
soapNode.setText(xmlFault.getNode());
soapFault.setNode(soapNode);
}
return soapFault;
}