WSO2 API Manager for message mediation

Even though the API Manager’s primary focus is to manage and expose API’s to different API consumers, it a common scenario to have the API Manager to perform some level of message mediation for both message requests and responses. The gateway component of the WSO2 API Manager is essentially a lightweight ESB that can perform almost all mediation activities that can be done by a regular ESB.

Newer releases of WSO2 API Manager provides the ability to attach mediation sequence to the request, response or the fault sequence of an API. The sequence gets executed when a request is made or a response is received to a request. Let’s look at how to add a message mediation to an API.

  1. Firstly you need to create message mediation sequence. Lets create the following sequence that would add a header to an out-going message. Save this sequence as setHeader.xml`
<sequence xmlns="http://ws.apache.org/ns/synapse"  name="SetHeader">
<header name="TempHeader" value="Test Header Value" action=set/>
</sequence>
  1. Log-in to the management console of the API Manager. You can access the management console via the following URL https://{IP}:9443/carbon You will find multiple options in the left hand navigation bar, select the ‘Browse’ icon in the Resources tab as shown below to access the internal registry.

Resources1

  1. Once inside the registry, navigate into ‘/_system/governance/apimgt/customsequences/in/’ folder in the registry and add the newly created setHeader.xml. Similarly if you have any sequence that needs to be added to the out sequence or the fault sequence you can do so by going into each of these folders under ‘customsequences’ folder. You can upload a sequence by selecting ‘Add Resource’ button and upload this file as shown below.

Resources2

  1. Once this is done, you can go to the publisher of the API Manager, and create a new API. Enter the required information in ‘Design’ and ‘Implement’ tabs and move to the ‘Manage’ tab. In the ‘Manage’ tab tick on the ‘message mediation policies’ and select the ‘SetHeader’ from the inflow dropdown as shown below. Once this is done Save & Publish the API to the API Store.

Mediation1

  1. You can now invoke the API from the API Store. You will see that the new header called ‘TempHeader’ is added to the message request before the backend API is invoked.
Advertisements

How to access API definitions from the WSO2 API Manager

WSO2 API Manager provides an intuitive UI that can be used to add and configure API’s that are exposed via the API Manager. However not all the capabilities of an API can be manipulated from the API Publisher’s UI. Certain tweaks require the access to the API definitions directly. There are two possible ways of doing this.

1. Via the Management console of the API Manager

Access the Management console of the API Manager from the following URL and log-in using the admin credentials.

https://{IP}:{Port}/carbon/admin/login.jsp

Once inside the management console, you can see the source view icon on the left hand side navigation bar as indicated below. Navigate to the source view page.

Here you would find all the API definitions, you can change the API definitions as required.

2. Directly accessing the API definition from the file system of the API Gateway.

API definition is stored in the file-system of the API Gateway, you can access this from the following folder path.

{API-Manager_Home}/repository/deployment/server/synapse-configs/default/api

Each API is represented by a xml file. You can change the definition by changing the contents of the file. The changes would be hot deployed to the API Gateway.

Please be mindful of the changes you make as it affect the exposed API’s. API’s are defined in Apache Synapse hence you would need some level of knowledge on Apache Synapse to manipulate these files.