Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Switch mediator for 4.4.0 version #1166

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, go to edit the switch mediator and click **Add new case** under **Cases** section.
2. It will open a form where a regular expression can be added in the **Regex Pattern** parameter. By submitting, it will add an empty case under the switch mediator as a child.
3. 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 [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>
```