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 | ||||
---|---|---|---|---|
|
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, which you can extend by adding custom mediation sequences. You create a custom mediation sequences either manually or using WSO2 tooling support (i.e., WSO2 Developer Studio), and then engage it per API or globally to all APIs.
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: 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 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. - In the multi-tenant mode, save the XML file in the tenant's synapse sequence folder. For example, if tenant id is 1, then save it in
<API_Gateway>/repository/tenants/1/synapse-configs/default/sequences
folder.
In the naming pattern, the <DIRECTION>
can be In
or Out
. When it is In
, the extension is triggered on the in-flow (request path) . Similarly, when the direction of the sequence and when it 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 | ||||
---|---|---|---|---|
| ||||
<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 | ||
---|---|---|
| ||
<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 |
To test the code, do the following:
...
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 in the API Manager 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
...
The above sequence prints a log message on the console whenever the API is invoked directory.