Tag: classmediator

WSO2 ESB -Pass data from ClassMediator to Proxy

The Class Mediator creates an instance of a custom-specified class and sets it as a mediator. So you can  add your java logic in your Class Mediator.
Once You have written the Class Mediator create a jar out of it and place it in ESB_HOME/repository/components/lib folder. You can even create the Mediator as OSGI bundle.In that case place the bundle in ESB_HOME/repository/components/dropins/ folder

You can refer doc[1] to learn how to create CustomMediator(ClassMediator) and pass data from proxy to custom java code. But this post gives small idea  on how to pass data from custom java code (ClassMediator) to proxy .

How to pass data from java code to proxy ?

You can use setProperty method inside the mediator class to set the value for a property and in the proxy by calling getProperty() you can retrieve that value of that property.Thus you can pass data from the Java code to Proxy.
Please check the example given below.

1.Set the property value ( setProperty(“message”,”hello world!” ))

public boolean mediate(MessageContext mc) {

mc.setProperty(“message”,”hello world!” );

return true;

2. Sapmple proxy which read the property value ( get-property(‘message’) )

<?xml version=”1.0″ encoding=”UTF-8″?>
<proxy xmlns=”http://ws.apache.org/ns/synapse
<class name=”org.wso2.carbon.mediator.CustomMediator”/>
<log level=”custom”>
<property name=”customlog” expression=”get-property(‘message’)”/>

Download the source of example here

1. https://docs.wso2.com/display/ESB470/Sample+380%3A+Writing+your+own+Custom+Mediation+in+Java