Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The API Gateway has a default mediation flow for each API invocation. You can do additional custom mediation for the messages in the API Gateway by extending its mediation flow using a synapse mediation sequence. You can create a sequence in the following ways: 

Table of Contents
maxLevel3
minLevel3

Using the tooling support to create a sequence

The recommended way to create an API is to a tool like WSO2 Developer Studio. You can then engage the sequence it to the API using the API Publisher. This tutorial explains the steps: Engage a Custom Mediation Flow.

Manually creating a sequence

You can manually create mediation extensions globally or per API. The difference between a global extension and a per-API extension is simply in the name given to the sequence that you use to create it. 

Tip

Tip: You do not have to follow this naming convention if you plan to engage the sequence/s to the API/s through the API Publisher.

Creating global extensions

Given below is the naming pattern of a global extension sequence.

WSO2AM--Ext--<DIRECTION>

The <DIRECTION> can be In or Out. To change the default fault sequence, you can either modify the default sequence or write a custom fault sequence and engage it to APIs through the API Publisher. When the direction of the sequence is In, the extension is triggered on the in-flow (request path). Similarly, when the direction of the sequence is Out, the extension is triggered on the out-flow (response path). Shown below is an example synapse configuration of a global extension sequence.

Code Block
languagehtml/xml
titleGlobal Extension Sequence Example
<sequence xmlns="http://ws.apache.org/ns/synapse" name="WSO2AM--Ext--In">
    <log level="custom">
        <property name="TRACE" value="Global Mediation Extension"/>
    </log>
</sequence>

To test the code, copy it to an XML file (e.g., global_ext.xml) and save the file in the <APIM_HOME>/repository/deployment/server/synapse-configs/default/sequences directory. The above sequence prints a log message on the console on every API invocation.

Creating per-API extensions

Given below is the naming pattern of a per-API extension sequence.

<API_NAME>:v<VERSION>--<DIRECTION>

Shown below is an example synapse configuration of a per-API extension sequence. It is created for an API named admin--TwitterSearch with version 1.0.0.

Code Block
titleAPI Extension Sequence Example
<sequence xmlns="http://ws.apache.org/ns/synapse" name="admin--TwitterSearch:v1.0.0--In">
    <log level="custom">
        <property name="TRACE" value="API Mediation Extension"/>
    </log>
</sequence>
Tip

The tenant username must be given as <username>-AT-<domain> in the configuration. For example, if the tenant username is testuser and the domain is wso2.com, then the name attribute in the above configuration must be testuser-AT-wso2.com--TwitterSearch:v1.0.0–In. The @ sign must be given as AT.

To test the code, do the following:

...

, which you can extend by adding custom mediation sequences. You create a custom mediation sequence either manually or using WSO2 tooling support (i.e., WSO2 Developer Studio), and then engage it per API or globally to all APIs of a specific tenant.  

Note

Please note that following mediators are not usable within custom sequences since they are not supported by API Gateway custom medications.

  • Call mediator in non-blocking mode
  • Send mediator

Creating per-API extensions

The recommended way to engage a mediation extension sequence per API is to upload an XML file through the registry and then engage it using the API Publisher. The following tutorial shows how to do this: Change the Default Mediation Flow of API Requests.

Alternatively, you can name the mediation XML file in the pattern <API_NAME>:v<VERSION>--<DIRECTION> and save it directly in the following location:

  • In the single-tenant mode, save the XML file in the <APIM_HOME>/repository/deployment/server/synapse-configs/default/sequences directory directory.
  • In the the multi-tenant mode, copy save the XML file to in the tenant's synapse sequence folder. For example,if  if tenant id is 1, then copy save it to in <API_Gateway>/repository/tenants/1/synapse-configs/default/sequences folder folder

The above sequence prints a log message on the console whenever the API is invokedIn the naming pattern, the <DIRECTION> can be In or Out. When it is In, the extension is triggered on the in-flow (request path) and when it is Out, the extension is triggered on the out-flow (response path). To change the default fault sequence, you can either modify the default sequence or write a custom fault sequence and engage it to APIs through the API Publisher. 

Creating global extensions

You can also engage mediation extension sequences to all APIs of a specific tenant at once. To do that, simply create the XML with the naming pattern WSO2AM--Ext--<DIRECTION> and save it in the <APIM_HOME>/repository/deployment/server/synapse-configs/default/sequences directory.