Usage of Fault mediator in WSO2 ESB


This post explains, an usage of Fault mediator in wso2 ESB.

The sample scenario, refuses and sends an exception back to the client based on the Content-Type header property.

This example is tested in ESB 4.7.0

First validate the content-type - using filter mediator

If true, send back an exception to the client - using fault mediator

If false, continue the flow

Here is the sample proxy,

<proxy xmlns="http://ws.apache.org/ns/synapse"
name="CheckContentType"
transports="https http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<log level="custom">
<property name="_______Content-Type"
expression="get-property('transport','Content-Type')"/>
</log>
<filter source="get-property('transport','Content-Type')"
regex="application/xhtml\+xml">
<then>
<log>
<property name="Content-Type"
expression="get-property('transport','Content-Type')"/>
<property name="Decision"
value="Exception, due to unexpected Content-Type."/>
</log>
<makefault version="soap11">
<code xmlns:soap11Env="http://schemas.xmlsoap.org/soap/envelope/"
value="soap11Env:Client"/>
<reason value="Content-Type Error"/>
<role/>
<detail>Content-Type: application/xhtml+xml is not a valid content type.</detail>
</makefault>
<header name="To" scope="default" action="remove"/>
<send/>
</then>
<else>
<log>
<property name="Content-Type"
expression="get-property('transport','Content-Type')"/>
<property name="Decision" value="Continue the mediation flow..."/>
</log>
<send>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
</endpoint>
</send>
</else>
</filter>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<publishWSDL uri="http://localhost:9000/services/SimpleStockQuoteService?wsdl"/>
</proxy>