Skip to content

Commit

Permalink
Merge pull request #1166 from SanojPunchihewa/switch
Browse files Browse the repository at this point in the history
Update Switch mediator for 4.4.0 version
  • Loading branch information
DinithiDiaz authored Dec 12, 2024
2 parents 1e598b6 + 99cc11e commit 95c667b
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions en/docs/reference/mediators/switch-mediator.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Switch Mediator

The **Switch Mediator** is an XPath or JSONPath filter. The XPath or JSONPath expression is evaluated and returns a string. This string is matched against the regular expression 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 and a default case is specified, the default case will be executed.
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

```xml
<switch source="[XPath|json-eval(JSON Path)]">
<switch source="[expression]">
<case regex="string">
mediator+
</case>+
Expand All @@ -28,44 +28,40 @@ The parameters available to configure the Switch mediator are as follows.
</thead>
<tbody>
<tr class="odd">
<td><strong>Source XPath</strong></td>
<td>The source XPath or JSONPath to be evaluated. When specifying a JSONPath, use the format <code>json-eval(&lt;JSON_PATH&gt;)</code> , such as <code>json-eval(getQuote.request.symbol)</code> . If you use namespaces in the expression, click the <strong>edit icon</strong>, then click <strong>Add Namespace</strong> to map the namespace prefix to the correct URI.
<td><strong>Expression</strong></td>
<td>The Synapse expression to be evaluated which should return a string.</td>
</tr>
<tr class="even">
<td><strong>Number of cases</strong></td>
<td><p>This parameter displays the number of cases currently added to the Switch mediator configuration.</p>
<td><strong>Cases</strong></td>
<td><p>This section displays the number of cases currently added to the switch mediator and option to add a new case.</p>
<p><br />
</p></td>
</tr>
<tr class="odd">
<td><strong>Specify default case</strong></td>
<td>Adding a default case is optional. If it is specified, it will be executed if no matching case is identified.</td>
</tr>
</tbody>
</table>

## Switch-case mediator
### Adding a new case

1. To add a case, go to edit the switch and click **Add parameter** under **Case Branches**.
2. It will open a form where a regular expression can be added in the **Case Regex** parameter. By submitting, it will add an empty case under the switch mediator as a child.
3. Click on the `+` mark under a chase to add the mediator(s) you want to execute when this case matches the switching value.
1. 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.
2. 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.
3. Click on the <strong>+</strong> 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 [Property mediator]({{base_path}}/reference/mediators/property-mediator) sets the local property named `symbol` on the current message depending on the evaluation of the string. It will get the text of the symbol element and match it against the values `MSFT` and `IBM`. If the text does not match either of these symbols, the default case will be executed.
In this example, the [Variable mediator]({{base_path}}/reference/mediators/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.

```xml
<switch source="//m0:getQuote/m0:request/m0:symbol">
<switch source="${payload.request.symbol}">
<case regex="IBM">
<!-- the property mediator sets a local property on the *current* message -->
<property name="symbol" scope="default" type="STRING" value="Great stock - IBM"/>
<!-- the variable mediator sets a variable on the *current* message -->
<variable name="symbol" type="STRING" value="Great stock - IBM"/>
</case>
<case regex="MSFT">
<property name="symbol" scope="default" type="STRING" value="Are you sure? - MSFT"/>
<variable name="symbol" type="STRING" value="Are you sure? - MSFT"/>
</case>
<default>
<!-- it is possible to assign the result of an XPath or JSON Path expression as well -->
<property name="symbol" scope="default" type="STRING" expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"/>
<!-- 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>
```

0 comments on commit 95c667b

Please sign in to comment.