Tag: esb

Wso2 ESB/APIM add query parameter to endpoint

Lets say there is a http endpoint http://localhost:8080/restapi defined in a api and want to add query parameter in sequence level before invoking the endpoint.

i.e   http://localhost:8080/restapi?id=2&name=admin&role=admin

By using Property “REST_URL_POSTFIX” this can be achieved as follow.


<?xml version="1.0" encoding="UTF-8"?>
<sequence
 xmlns="http://ws.apache.org/ns/synapse" name="admin--test:v1--In">
 <log level="full">
 <property name="IN_MESSAGE" value="IN_MESSAGE"/>
 </log>
 <property name="id" value="2"/>
 <property name="name" value="admin"/>
 <property name="role" value="admin"/>
 <property name="REST_URL_POSTFIX" expression="fn:concat('?id=',$ctx:id,'&amp;name=',$ctx:name,'&amp;role=',$ctx:role)" scope="axis2"/>
 <endpoint
 xmlns="http://ws.apache.org/ns/synapse" name="HTTPEndpoint">
 <http uri-template="http://localhost:8080/restapi" method="GET"></http>
 </endpoint>
</sequence>

Advertisements

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
name=”ClassMediator”
transports=”https,http”
statistics=”disable”
trace=”disable”
startOnLoad=”true”>
<target>
<inSequence>
<class name=”org.wso2.carbon.mediator.CustomMediator”/>
<log level=”custom”>
<property name=”customlog” expression=”get-property(‘message’)”/>
</log>
</inSequence>
</target>
<description/>
</proxy>

Download the source of example here

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