Search code examples
wso2esbwso2-esb

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


I am trying to route a message based on information in a XML snippet stored as a local entry (key=mapping_id_ep_v1.xml). The id used to look-up the correct endpoint and it is part of the message body.

This is the XML snippet used to map id's to 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 retrieve the id from the body to look-up the endpoint using the following statement:

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

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

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

I log the 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 haven't been able to figure out how to retrieve the correct endpoint from the MAPPING property. Can anyone help out?


Solution

  • 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