Adding unknown number of properties with WSO2 Enterprise Integrator

This article describes how to add properties to the message context from the data defined in a local entry of WSO2 Enterprise Integrator. This approach is useful in scenarios where we want to have unknown number of properties, and when we do not know the property name before hand.

Here, I have used a xml type local entry to keep the name-value pairs, and a class mediator to process the key-value pairs defined in the local entry.

The property related key value pairs are defined as a local entry in the xml format.

key-pairs local entry

<key-pairs xmlns="http://ws.apache.org/ns/synapse">
<property>
<name>abc</name>
<value>123</value>
</property>
<property>
<name>def</name>
<value>456</value>
</property>
</key-pairs>

At the class mediator we read the local entry, extract each key-value pair and add them to the message context.

package org.wso2.carbon.custom;
import org.apache.axiom.om.OMElement;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
import java.util.Iterator;
public class AddPropertiesMediator extends AbstractMediator {
public boolean mediate(MessageContext messageContext) {
System.out.println("Processing key-value pairs !!");
 //get local entry content
OMElement keyPairs = (OMElement) messageContext.getConfiguration().getEntry("key-pairs");
//iterate each key-value pair and add it to message context
Iterator children = keyPairs.getChildren();
if (null != children) {
while (children.hasNext()) {
addProperty(messageContext, children.next());
}
}
return true;
}
private void addProperty(MessageContext messageContext, Object object) {
String key = "";
String value = "";
if (object instanceof OMElement) {
Iterator children = ((OMElement) object).getChildren();
if (null != children) {
while (children.hasNext()) {
Object child = children.next();
if (child instanceof OMElement) {
if (((OMElement) child).getLocalName().equals("name")) {
key = ((OMElement) child).getText();
} else if (((OMElement) child).getLocalName().equals("value")) {
value = ((OMElement) child).getText();
}
}
}
}
messageContext.setProperty(key, value);
}
}
}

We can create a proxy service, and define the created class mediator to add the properties to the synapse context. 
<class name=”org.wso2.carbon.custom.AddPropertiesMediator”/>

Please refer [1] to create and apply a custom class mediator to WSO2 Enterprise Integrator.
Then, we can log the values of each property to verify the properties that are being added to the message context.

<proxy name="TestProxy" startOnLoad="true" trace="disable" transports="http https">
<description/>
<target>
<inSequence>
<class name="org.wso2.carbon.custom.AddPropertiesMediator"/>
<log level="custom">
<property expression="get-property('abc')" name="ABC"/>
<property expression="get-property('def')" name="DEF"/>
</log>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
</target>
</proxy>

[1] — https://docs.wso2.com/display/EI610/Class+Mediator