Monday, September 21, 2015

[WSO2 ESB] How to read a value from xml file stored in a registry

You can do like this. Here we have below test.xml file stored in gov:/test.xml

<root>
<book>A Song of Ice and Fire</book>
<author>George R. R. Martin</author>
</root>
view raw books.xml hosted with ❤ by GitHub
Your synapse configuration should be like this. Here we use xapth to read the XML.

<property name="xmlFile" expression="get-property('registry','gov:/test.xml')" scope="default" type="OM"></property>
<log level="custom">
<property name="Book_Name" expression="$ctx:xmlFile//book"></property>
</log>
view raw reg_xpath.xml hosted with ❤ by GitHub
Your output log will look like this.

[2015-09-21 16:01:28,750]  INFO - LogMediator Book_Name = A Song of Ice and Fire 

Please comment below if you have any questions.

Related:
How to read a file from registry
How to read a registry property

Tuesday, September 8, 2015

[WSO2 ESB] How to URL Encode in WSO2 ESB

You can do this using either Script mediator or Class mediator. Here I'm discussing about how to use Script mediator.

In javascript, there are 2 ways to do URL encoding.

1) encodeURIComponent() - This should be used only to encode query parameters.

eg. name=John Conner

<script language="js" description="Query Parameter Encoding Script">
mc.setProperty("ENCODED_PARAMS", encodeURIComponent(mc.getProperty('QUERY_PARAMS')));
</scrpt>
2) encodeURI() - This is used to encode an entire URL.

eg. www.transformers.com/charactors?name=John Conner

<script language="js" description="URL Encoding Script">
mc.setProperty("ENCODED_URL", encodeURI(mc.getProperty('URL_TO_BE_ENCODED')));
</scrpt>
If you have any questions, please comment below.

Tuesday, September 1, 2015

[WSO2 ESB] How to read a registry property

Here is how to do it. This will load 'abc' property of collection "gov:/data/xml/collectionx", and store in 'regProperty' property.

<property name="regProperty" expression="get-property('registry', 'gov:/data/xml/collectionx@abc')"/>
Related:
How to read a file from registry
How to read a value from xml file stored in a registry

Tuesday, August 25, 2015

[WSO2 ESB] How to replace message body by a value of a property

Here is how to do it. This will replace message body by the value of "xmlfile" property.

<property name="xmlfile" expression="get-property('registry', 'gov:/xml/body.xml')" type="OM" />
<enrich>
<source clone="true" xpath="get-property('xmlfile')" />
<target type="body" />
</enrich>
view raw syn.xml hosted with ❤ by GitHub
Make sure you set type="OM" in property. Otherwise you will get below error.

"ERROR - EnrichMediator Invalid Object type to be inserted into message body"


Monday, August 24, 2015

[WSO2 ESB] How to read a file from registry and store in a property

Here is how to do it. This will load body.xml file from governance registry, and store in 'xmlfile' property.

<property name="xmlfile" expression="get-property('registry', 'gov:/xml/body.xml')" type="OM" />
view raw reg.xml hosted with ❤ by GitHub
Having 'registry' scope is the key here. Type is "OM" because it is an xml file.

Related:
How to read a file from registry

Tuesday, August 18, 2015

How to Split a JSON array in WSO2 ESB

Say you have this JSON array.

{
"users":[
{"name":"X", "age":"10"},
{"name":"Y", "age":"12"},
{"name":"Z","age":"15"}
]
}
view raw input.json hosted with ❤ by GitHub
Now you want to split users, and call some backend service for each user. Here is how to do. (Here I'm logging each user details instead of calling a backend service.)

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="JsonSplitterBlog"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<payloadFactory media-type="json">
<format>{"users":[{"name":"X", "age":"10"},{"name":"Y", "age":"12"},{"name":"Z","age":"15"}]}</format>
<args/>
</payloadFactory>
<iterate continueParent="true" expression="//users">
<target>
<sequence>
<log level="custom">
<property name="Name is" expression="//users/name"/>
<property name="Age is" expression="//users/age"/>
</log>
</sequence>
</target>
</iterate>
</inSequence>
</target>
<description/>
</proxy>
Output will look like this.

[2015-08-18 15:49:27,200]  INFO - LogMediator Name is = X, Age is = 10
[2015-08-18 15:49:27,204]  INFO - LogMediator Name is = Y, Age is = 12
[2015-08-18 15:49:27,205]  INFO - LogMediator Name is = Z, Age is = 15

Hope this will be helpful to someone.

References
https://denuwanthi.wordpress.com/2015/06/03/wso2-esbaccess-an-array-defined-in-property-mediator/