Customizing faults in WSM custom steps
Usually, WSM custom steps reflecting your own policy can lead to faults (as per the request not being compliant with the implemented policy). In those cases a custom SOAP fault will make sense in order to provide the invoker with details of the reason for the failure. The following code shows up how to generate such custom SOAP faults when creating WSM custom steps.
package ....;
import com.cfluent.pipelineengine.container.MessageContext;
import com.cfluent.policysteps.sdk.*;
import javax.xml.soap.*
public class CustomStep1 extends AbstractStep {
private static final String CUSTOM_FAULT_NS = "http://my.custom.fault/ns";
private static final String CUSTOM_DETAIL_NS = "http://my.custom.detail/ns";
private static final String CUSTOM_DETAIL_PREFIX = "cd";
//setters and getters
.....
public IResult execute(IMessageContext msgContext) throws Fault {
IResult result = new Result();
MessageContext msgCtxt = (MessageContext) msgContext;
//evaluate error condition (true for testing)
boolean myFaultCondicionA = true;
if (myFaultCondicionA) {
throw createCustomFaultA(msgCtxt, "detailValue1", "detailValue2");
}
return result;
}
private Fault createCustomFaultA(MessageContext msgCtxt, String value1, String value2) {
return createFault(msgCtxt, "customCodeA", "error A description", new String[][] {{"CustomDetailA1", value1}, {"CustomDetailA2", value2}});
}
private Fault createFault(MessageContext msgCtxt, String faultCodeQualifier, String faultString, String[][] values) {
try {
SOAPFactory factory = SOAPFactoryImpl.newInstance();
SOAPFault sf = msgCtxt.getRequest().getAxisMessage().getSOAPPart().getEnvelope().getBody().addFault();
Detail detail = sf.addDetail();
for (int i = 0; i < values.length; i++) {
DetailEntry de = detail.addDetailEntry(factory.createName(values[i][0] , CUSTOM_DETAIL_PREFIX, CUSTOM_DETAIL_NS));
de.addTextNode(values[i][1]);
}
return new Fault(CUSTOM_FAULT_NS, faultCodeQualifier, faultString, null, detail);
} catch (SOAPException soape) {
return new Fault(soape);
}
}
}
The result obtained because of the execution of the former code looks like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode xmlns:p="http://my.custom.fault/ns">p:Client.customCodeA</faultcode>
<faultstring>error A description</faultstring>
<detail>
<cd:CustomDetailA1 xmlns:cd="http://my.custom.detail/ns">detailValue1</cd:CustomDetailA1>
<cd:CustomDetailA2 xmlns:cd="http://my.custom.detail/ns">detailValue2</cd:CustomDetailA2>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>