Mule is frequently used for proxying Web Service requests and replies. It’s quite possible we want to have SOAP requests validated before they are proxied. For example, according to the service’s WSDL, a request such as the following is invalid because the arg1 element contains a string when it should be an integer:
Ideally, a SOAP fault is returned to the client in case of a validation error. Implementing this in Mule is simply done through configuration as seen below:
SOAP validation is switched on when the validationEnable attribute is set to true on the proxy-service element. However, enabling validation means setting some additional attributes so that the proxy-service knows what to validate the SOAP request against. One attribute is the wsdlLocation which points to a WSDL in the Java classpath. Other required attributes are service and namespace. The SOAP fault below is a result of sending the above SOAP request to the proxy with validation enabled:
Great, we have validation. Of course, being the complexaholics that we are, we would like to customise the SOAP fault sent back. Doing this requires some knowledge of Apache CXF’s API and DOM. The first step is to create an interceptor to catch SOAP faults:
The interceptor is extracting the fault from the CXF message. It then goes on to check what type of fault it is by retrieving the fault code. If the fault code is a validation error, then the interceptor uses DOM to include additional info in the fault. Easy peasy :).
The last step is to hook the interceptor into our proxy service as shown below.
Testing the service with the invalid SOAP request gives out the following SOAP fault:
To see this example in action go on our GitHub page.