How to dynamically route a message to a WSO2 ESB based on an XML configuration file

I am trying to direct a message based on information in an XML fragment stored as a local record (key = mapping_id_ep_v1.xml). The identifier used to find the correct endpoint is part of the message body.

This is the XML fragment used to map the identifier to the endpoints:

<mappings> <mapping id="ep_1">http://localhost:8280/services/ep_1</mapping> <mapping id="ep_2">http://localhost:8280/services/ep_2</mapping> <mapping id="ep_3">http://localhost:8280/services/ep_3</mapping> <mappings> 

I extract the identifier from the body to find the endpoint using the following statement:

 <property name="LOOK-UP" expression="//controleFile/id" /> 

I can load the XML file into the properties file using the following entry in the sequence:

 <property name="MAPPING" expression="get-property('mapping_id_ep_v1.xml')" /> 

I register a property using the following statement:

 <log level="custom"> <property name="Look-up" expression="get-property('LOOK-UP')" /> <property name="Mapping" expression="get-property('MAPPING')" /> </log> 

So far so good. I was not able to figure out how to get the correct endpoint from the MAPPING property. Can anyone help?

+1
source share
3 answers

I solved my question using a different approach. This matches the answer given by bij fipries.

In the proxy, I added the following:

  <property name="MAPPING" expression="get-property('mapping_id_ep_v1')" /> <property name="LOOK_UP" expression="//controlFile/id" /> <log level="custom"> <property name="MAPPING" expression="get-property('MAPPING')" /> <property name="LOOK_UP" expression="get-property('LOOK_UP')" /> </log> <script language="js" key="testScript_2" function="getEndpointByID" /> <log level="custom"> <property name="EP" expression="get-property('EP')" /> </log> 

This is the contents of mapping_id_ep_v1:

 <mappings> <mapping id="ep_1">http://localhost:8280/services/ep_1</mapping> <mapping id="ep_2">http://localhost:8280/services/ep_2</mapping> <mapping id="ep_3">http://localhost:8280/services/ep_3</mapping> <mappings> 

This is the code in TestScript_2:

 <x> function getEndpointByID(mc) { var xml = new XML(mc.getProperty('MAPPING')); var look_up = new XML(mc.getProperty('LOOK_UP')); var ep = xml..mapping.(@id == look_up); mc.setProperty('EP', ep + ''); } </x> 

The proxy loads the xml mapping into the property. This property is converted into XML into javascript code, and then the correct endpoint is retrieved using LOOK_UP.

Hope this helps someone else.

Regards, nidkil

+2
source

I have the same situation. The problem is that you cannot execute xpath expressions on the contents of a property.

You can easily do what you need using a Javascript script after loading the xml into the property. Javascript + e4x offers an easy way to access the XML content of variables using xpath expressions.

Hope this helps you.

0
source

Yes, this is a limitation. In fact, it should be the functionality of an enriching intermediary. Will create a function request to get this hotfix for the next version of ESB.

Now you can do it with

  • Maintain current soap payload: using enrichment
  • Replace current body with property contents - enrich
  • Evaluate the xpath for the current body and extract the required content. (Use this as an EP) - Property
  • Recover message body with saved payload - enrich

I know ... this is a hack :)

0
source

All Articles