Switch Mediator¶
The Switch Mediator evaluates a Synapse expression and returns a result. This result is then matched against the regular expressions defined in each switch case, in the specified order. If a matching case is found, it is executed, and the remaining cases are not processed. If none of the case statements match the default case will be executed.
Syntax¶
<switch source="[expression]">
<case regex="string">
mediator+
</case>+
<default>
mediator+
</default>
</switch>
Configuration¶
The parameters available to configure the Switch mediator are as follows.
Parameter Name | Description |
---|---|
Expression | The Synapse expression to be evaluated which should return a string. |
Cases | This section displays the number of cases currently added to the switch mediator and option to add a new case.
|
Adding a new case¶
- To add a case, click on the Switch mediator to open the Edit Switch Mediator panel. Then click Add new case under the Cases section.
- It will open a form where a regular expression can be added in the Regex Pattern parameter. Once submitted, it will add an empty case under the switch mediator as a child.
- Click on the + mark under a case to add the mediator(s) you want to execute when this case matches the switching value.
Examples¶
In this example, the Variable mediator sets the variable named symbol
on the current message depending on the evaluation of the expression. The expression will get the symbol value and match it against the values MSFT
and IBM
. If the symbol value does not match either of these symbols, the default case will be executed.
<switch source="${payload.request.symbol}">
<case regex="IBM">
<!-- the variable mediator sets a variable on the *current* message -->
<variable name="symbol" type="STRING" value="Great stock - IBM"/>
</case>
<case regex="MSFT">
<variable name="symbol" type="STRING" value="Are you sure? - MSFT"/>
</case>
<default>
<!-- it is possible to assign the result of an expression as well -->
<variable name="symbol" type="STRING" expression="${'Normal Stock - ' + payload.request.symbol}"/>
</default>
</switch>