I have a custom SOAPHandler and the handleMessage() is as follows :
public boolean handleMessage(SOAPMessageContext context) {
// TODO Auto-generated method stub
/* Step-1: Extract credentials from the SOAP header */
Boolean isOutbound = (Boolean) context
.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
log.info("In TransportHandler.handleMessage(...) " + isOutbound);
if (!isOutbound) {
SOAPMessage soapMsg = context.getMessage();
try {
SOAPUtil soapUtil = new SOAPUtil();
Credentials credentials = soapUtil.retrieveCredentials(soapMsg);
/* Validate credentials against the directory */
SecurityUtil securityUtil = new SecurityUtil();
securityUtil.authenticateClient(credentials.getUserName(),
credentials.getPassword());
} catch (SecurityFault_Exception e) {
// TODO Auto-generated catch block
log.error(
"SecurityFault_Exception in TransportHandler.handleMessage(...)",
e);
SOAPFault securityFault = null;
try {
securityFault = soapMsg.getSOAPBody().addFault();
securityFault.setFaultString(e.getMessage());
throw new SOAPFaultException(securityFault);
} catch (SOAPException e1) {
// TODO Auto-generated catch block
log.error(
"SOAPException while handing SecurityFault_Exception in TransportHandler.handleMessage(...)",
e1);
}
} catch (RequestMessageFormatFault_Exception e) {
// TODO Auto-generated catch block
log.error(
"RequestMessageFormatFault_Exception in TransportHandler.handleMessage(...)",
e);
SOAPFault requestFormatFault = null;
try {
requestFormatFault = soapMsg.getSOAPBody().addFault();
requestFormatFault.setFaultString(e.getFaultInfo().getCustomMessage());
throw new SOAPFaultException(requestFormatFault);
} catch (SOAPException e1) {
// TODO Auto-generated catch block
log.error(
"SOAPException while handing RequestMessageFormatFault_Exception in TransportHandler.handleMessage(...)",
e1);
}
}
}
log.info("Returning from TransportHandler.handleMessage(...)");
return true;
}
For example, if someone doesn't add the security header, following is the response :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Server</faultcode>
<faultstring>Security header not present in the SOAP request</faultstring>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
Now in the handleMessage(...), I have wrapped the custom fault for bad header as shown :
catch (RequestMessageFormatFault_Exception e) {
// TODO Auto-generated catch block
log.error(
"RequestMessageFormatFault_Exception in TransportHandler.handleMessage(...)",
e);
SOAPFault requestFormatFault = null;
try {
requestFormatFault = soapMsg.getSOAPBody().addFault();
requestFormatFault.setFaultString(e.getFaultInfo().getCustomMessage());
throw new SOAPFaultException(requestFormatFault);
} catch (SOAPException e1) {
// TODO Auto-generated catch block
log.error(
"SOAPException while handing RequestMessageFormatFault_Exception in TransportHandler.handleMessage(...)",
e1);
}
}
As per our org. standards, I have the below faults declared in the wsdl(partially shown here) :
.
.
.
<message name="RequestMessageFormatFault">
<part name="RequestMessageFormatFault" element="sch:RequestMessageFormatFault" />
</message>
<message name="SecurityFault">
<part name="SecurityFault" element="sch:SecurityFault" />
</message>
.
.
.
<portType name="TransportInformationDelegate">
<operation name="GetTransportInformation">
.
.
<fault name="RequestMessageFormatFault" message="tns:RequestMessageFormatFault"/>
<fault name="SecurityFault" message="tns:SecurityFault"/>
</operation>
</portType>
<binding name="TransportInformationPortBinding" type="tns:TransportInformationDelegate">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="GetTransportInformation">
.
.
.
.
<fault name="RequestMessageFormatFault">
<soap:fault name="RequestMessageFormatFault" use="literal"/>
</fault>
<fault name="SecurityFault">
<soap:fault name="SecurityFault" use="literal"/>
</fault>
</operation>
How shall I ensure that the response has this fault ? I tried the SOAPFault.addDetail(...) in vain.