Sitemap

WSO2 Class Mediators: Convert Payloads

Change an Attribute Value of a Payload using Class Mediator

Class Mediators used for user-specific custom developments when there are no built-in mediators to solve the requirements.

Class Mediators enables and provides an extended platform to develop custom mediation logic in the form of Java classes and to apply them in various sequences. In this technical writing, I will be navigating you all through on a “how-to” develop a simple custom class mediator to read, manipulate and convert the payloads to meet your custom requirements.

To simply put, we will be developing a custom Class Mediator to read and change the value of the name attribute of a given payload from “medium” to “Athiththan”. Given below are the actual payload and the expected converted payload formats …

-------------------------------------// request payload || the original{
"post": "WSO2 Class Mediator: Convert Payload",
"dev": {
"name": "medium"
}
}
-------------------------------------// mediated payload || the converted{
"post": "WSO2 Class Mediator: Convert Payload",
"dev" : {
"name": "Athiththan"
}
}
-------------------------------------

Developing the Project

We’ll create a maven project to design and develop our custom class mediator to convert the payloads. You can either follow …

  • The given pom.xml file to replace and build the maven project
  • Clone, build, install and use the maven archetype designed to generate boilerplate for WSO2 Class Mediators

POM.XML

Create a maven quick-start project and paste the following dependencies, repositories and build tags to generate and develop custom class mediators.

<!-- change the packaging to bundle (from jar) to support OSGi --><packaging>jar</packaging><dependencies>
<!-- apache synapse and related dependencies -->
<dependency>
<groupId>org.apache.synapse</groupId>
<artifactId>synapse-core</artifactId>
<version>2.1.7-wso2v60</version>
</dependency>
<dependency>
<groupId>org.apache.axis2.wso2</groupId
<artifactId>axis2</artifactId
<version>1.6.1.wso2v20</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>wso2-nexus</id
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy
<checksumPolicy>ignore</checksumPolicy>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>wso2-nexus</id
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<releases>
<enabled>true</enabled
<updatePolicy>daily</updatePolicy
<checksumPolicy>ignore</checksumPolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId
<artifactId>maven-compiler-plugin</artifactId
<version>2.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- build plugin for OSGi Bundle support -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId
<version>2.3.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>
${groupId}
</Bundle-SymbolicName>
<Bundle-Name>${groupId}</Bundle-Name
<Export-Package>
${groupId}.*,
</Export-Package>
<Import-Package>
*; resolution:=optional
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>

Build using Maven Archetypes

Clone or download the following repository to your machine and run the following command to build and install the maven archetype locally in your machine.

mvn clean install

The above command will download all necessary dependencies and installs the defined maven archetype locally in your machine to generate boilerplate to kick-start the implementations of custom Class Mediators.

Get Athiththan Kathirgamasegaran’s stories in your inbox

Join Medium for free to get updates from this writer.

Use the following command to generate a maven project using the archetype, and don’t forget to change the command by adding your Group ID and Artifact ID in related fields.

mvn archetype:generate  -DarchetypeGroupId=com.athiththan.wso2     \
-DarchetypeArtifactId=wso2-mediators \
-DarchetypeVersion=1.0.0 \
-DgroupId=<Your Group ID> \
-DartifactId=<Your Artifact ID>

Developing the Class Mediator

We can develop a Class Mediator by implementing the Mediator class or by extending the Abstract Mediator class. But, in this writing, I will be only considering the implementation of extending the org.apache.synapse.mediators.AbstractMediator class.

Our mediation logic must reside inside the mediate() method to get executed.

Given below is a code snippet of the custom Class Mediator designed to convert the name attribute from medium to Athiththan.

Packaging & Running

Run the following maven command from the root folder of your project to build and package our class mediator implementation …

mvn clean package

As a result, Maven will build and package our implementation in a JAR (Java Archive) extension format artifact, which we can find it inside the target folder of our project.

For demonstration purposes,

I will be mocking and using the Token Endpoint of the WSO2 API Manager (2.6.0). I will be referring to the installation path of the WSO2 API Manager as <APIM_HOME> and our built JAR artifact as <CM_IMPL_JAR>.

  1. Copy the <CM_IMPL_JAR> from the /target folder and paste it inside the <APIM_HOME>/repository/components/lib/
  2. Navigate to <APIM_HOME>/repository/deployment/server/synapse-configs/default/api/ and open and edit the inSequence tag of the_TokenAPI_.xml file as follows.
<inSequence>
<!-- <property name="uri.var.portnum" expression="get-property('keyManager.port')"/>
<property name="uri.var.hostname" expression="get-property('keyManager.hostname')"/>
<send>
<endpoint>
<http uri-template="https://{uri.var.hostname}:{uri.var.portnum}/oauth2/token">
<timeout>
<duration>60000</duration
<responseAction>fault</responseAction>
</timeout>
</http>
</endpoint>
</send> -->
<!-- convert dev.name to athiththan -->
<class name="<YOUR PACKAGE NAME>.<CLASS NAME>" />
<respond />
</inSequence>

Replace <YOUR PACKAGE NAME>.<CLASS NAME> with the actual package name and your Java class name.

For example …

If you have implemented your class mediator implementation in a Java class named CustomClassMediator inside the com.sample.demos package, then it comes as com.sample.demos.CustomClassMediator resulting <class name="com.sample.demos.CustomClassMediator" />.

After deploying our <CM_IMPL_JAR> and editing the _TokenAPI_.xml file, start the WSO2 API Manager instance. Use the following cURL command to execute the Token Endpoint of our WSO2 API Manager instance by replacing the <Your Host> tag with respective host value …

curl -k --header "Content-Type: application/json" --request POST --data '{"post":"WSO2 Class Mediator: Convert Payload","dev":{"name":"medium"}}' https://<YOUR HOST>:8243/token

After successful execution, we will get the response as follows

{"post":"WSO2 Class Mediator: Convert Payload","dev":{"name":"Athiththan"}}

Voila !!! Now you have successfully developed a custom class mediator to mediate and convert an attribute value of a given request payload. You can checkout the full implementation here …

Don’t do Class Mediators if you are not in control or doesn’t have frequent usages and less user-specific. :-?

Happy mediating … :)

Written by Athiththan Kathirgamasegaran

@athiththan11 | GH:athiththan11 | Site Reliability Engineer@WSO2