How to Write custom mediator for WSO2ESB

WSO2 ESB gives an extension points, where users can create their own processing logic in to the message path. One way is implementing a custom mediator  (class mediator) where it can be plugged with ESB sequences.

We can found lot of docs and blogs on writing a custom mediators. But I just like to summarize all very simplified manner..

1. You need to create a java project with Apache synpase core as dependency. If you are using Maven

<dependencies>
 <dependency>
 <groupId>org.apache.synapse</groupId>
 <artifactId>synapse-core</artifactId>
 <version>${synpase.core.version}</version>
 </dependency>
 </dependencies>

2. You can create a class by extending “AbstractMediator” and Implement your message processing logic in the mediate() method. Inside your class, you have full access to the “MessageContext” where you can exact values from message and update them.

public class JWTMediator extends AbstractMediator  {
   @Override
   public boolean mediate(MessageContext messageContext) {
   }
}

3. (Option)If you want to initialize some variables once (such as database connections). Then your mediator must implement the “ManagedLifecycle” interface where its provides a “init” method to initialize mediator. This “init” method would be called every time you update the synapse configuration

public class JWTMediator extends AbstractMediator implements ManagedLifecycle {
    @Override
    public boolean mediate(MessageContext messageContext) {
    }
    public void init(SynapseEnvironment synEnv) {
    }
}

4. (Optional) You can make your mediator more configurable by passing variables using synapse configuration. You can easily achieve it by adding setters and getters in your private variables

public class JWTMediator extends AbstractMediator implements ManagedLifecycle {
    private String requiredClaims;
    @Override
    public boolean mediate(MessageContext messageContext) {
    }
    public void init(SynapseEnvironment synEnv) {
    }
    public String getRequiredClaims() {
         return requiredClaims;
    }
    public void setRequiredClaims(String requiredClaims) {
        this.requiredClaims = requiredClaims;
    }
}

5. (Optional) If you need to access to OSGI services (such as user realm, registry service) that are available in carbon platform, You need to create this mediator as an OSGI bundle with SCR annotations.

6. You need to package your project in to Jar file or OSGI bundle.

7. You need to deploy the Jar file in to <ESB_HOME>/repository/components/lib directory. If it is an OSGI bundle, it must be deployed in to  <ESB_HOME>/repository/components/dropins directory.  If there are any external dependent libraries, you can put them it to  <ESB_HOME>/repository/components/lib directory

8. You can configure your custom mediator using synapse configuration.

Using UI,  you can configure this as class mediator

Synapse configuration would be as follows.

<class name="org.soasecurity.oauth.jwt.mediator.JWTMediator">
<property name="requiredClaims" value="http://localhost/claim"/>
</class>

9. Finally you can debug the code. You can start WSO2 ESB in debug mode as follows

sh wso2server.sh -debug 5005

 

Sample class mediator which is developed above,  can be found from here

 

Thanks for reading..!!!