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

我的未来我决定 提交于 2019-12-01 14:19:17

I solved my question using a different approach. It is in line with the answer given 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 mapping xml into a property. This property is converted to XML in the javascript code and then using LOOK_UP the correct endpoint is retrieved.

Hope this helps someone else.

Regards, nidkil

I have got a similar situation. The problem seems to be that you cannot perform xpath expressions over a property content.

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

Hope this helps you.

Yes this is a limitation. Actually, this should be a functionality of the enrich mediator. Will create a feature request to get this fixed for the next ESB release.

For now you can do this with:

  • Preserve current soap payload : using enrich
  • Replace current body with the content of the property - enrich
  • Evaluate xpath against the current body and extract the required content. (Use that as the EP)- property
  • Restore the message body with preserved payload - enrich

I know... it's a hack :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!