Sample 5: Creating SOAP Faults and Changing the Direction of Messages

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="myFaultHandler"> <makefault response="true"> <code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/> <reason expression="get-property('ERROR_MESSAGE')"/> </makefault> <send/> </sequence> <sequence name="main" onError="myFaultHandler"> <in> <switch xmlns:m0="http://services.samples" source="//m0:getQuote/m0:request/m0:symbol"> <case regex="MSFT"> <send> <endpoint> <address uri="http://bogus:9000/services/NonExistentStockQuoteService"/> </endpoint> </send> </case> <case regex="SUN"> <send> <endpoint> <address uri="http://localhost:9009/services/NonExistentStockQuoteService"/> </endpoint> </send> </case> </switch> <drop/> </in> <out> <send/> </out> </sequence> </definitions>

Objective

Demonstrating how makefault mediator can be used to construct custom SOAP faults and change the direction (in/out) of messages.

Pre-requisites

  • Start Synapse using the configuration numbered 5 (repository/conf/sample/synapse_sample_5.xml)
    Unix/Linux: sh synapse.sh -sample 5
    Windows: synapse.bat -sample 5

Executing the Client

When the MSFT stock quote is requested, an unknown host exception would be generated. A connection refused exception would be generated for the SUN stock request. These errors are captured and returned to the original client as a SOAP fault in this example.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT

returns,

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <faultcode>soapenv:Client</faultcode> <faultstring>java.net.UnknownHostException: bogus</faultstring> <detail /> </soapenv:Fault>

And

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

returns,

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <faultcode>soapenv:Client</faultcode> <faultstring>java.net.ConnectException: Connection refused</faultstring> <detail /> </soapenv:Fault>

Note that the response attribute is set to 'true' on the makefault mediator. This instructs the mediator to change the direction of messages to 'response' as messages are transformed into SOAP faults.

Back to Catalog