Search code examples
proxysequenceswitchingmediatorwso2-esb

Switching OutSequence in WSO2 ESB Proxy Service based oh SOAP response


I deployed a proxy service in WSO2 ESB for dataset retrieving from a SOAP WS, and i have an OutSequence based on a sequence calling a template.

I have to channel various WS response based on the different request routing them on different files written by the vfs transport.

Actual sequence is the following:

<sequence xmlns="http://ws.apache.org/ns/synapse" name="seq_prova_con_template">
   <call-template target="file">
      <with-param name="filename" value="IstatAllDataflow-template.xml"></with-param>
   </call-template>
</sequence>

I think about a switch-case mediator but i'd like to understand how "catch" the information for choosing the right case. In the example:

`<switch source="//m0:getQuote/m0:request/m0:symbol"     xmlns:m0="http://services.samples/xsd">
      <case regex="IBM">
           <!-- the property mediator sets a local property on the *current*     message -->
          <property name="symbol" value="Great stock - IBM"/>
      </case>
      <case regex="MSFT">
          <property name="symbol" value="Are you sure? - MSFT"/>
      </case>
      <default>
          <!-- it is possible to assign the result of an XPath or JSON Path     expression as well -->
      <property name="symbol"
            expression="fn:concat('Normal Stock - ', //m0:getQuote/m0:request/m0:symbol)"
            xmlns:m0="http://services.samples/xsd"/>
  </default>

`

i ask myself how to set the source param of the switch case, and i'd like to know if someone has already implemented a solution like this in order to use a single proxy service for differentiate various answer from the WS. My sequence is the following:

<sequence xmlns="http://ws.apache.org/ns/synapse" name="seq_template_switch">
   <switch xmlns:ns="http://org.apache.synapse/xsd" xmlns:m0="http://services.samples" source="??????">
      <case regex="QueryStructure">
         <call-template target="file">
            <with-param name="filename" value="IstatAllDataflow-template.xml"></with-param>
         </call-template>
      </case>
      <case regex="GetCompactData">
         <call-template target="file">
            <with-param name="filename" value="GetCompactData-template.xml"></with-param>
         </call-template>
      </case>
   </switch>
</sequence>

I need to catch the choice for the switch case from che method inside the request of my request message, in order to write a certain file when i ask for a kind of answer, and another file with a different name when i ask for another kind of answer.

[EDIT] Log file has this:

TID: [0] [ESB] [2015-09-18 10:33:09,125]  INFO {org.apache.synapse.mediators.builtin.LogMediator} -  To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:7cc540d3-2893-4b0e-8a24-ab4538236d45, Direction: response, Envelope: <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><QueryStructureResponse xmlns="http://ec.europa.eu/eurostat/sri/service/2.0"><QueryStructureResult><RegistryInterface xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message"><Header><ID>IT1001</ID><Test>true</Test><Name xml:lang="en">ISTAT_JD_237</Name><Prepared>2001-03-11T15:30:47+01:00</Prepared><Sender id="ISTAT"><Name xml:lang="en">Italian Statistical Institute</Name><Contact>

It would be useful making the choice of switch/case mediator about the tag <QueryStructureResponse>. Instead of this tag i could have<GetCompactData> , for example. I'd like to create a switch/case mediator that is driven by the presence of one of this two tags. This would be a good beginning for understanding how to use XPath location and using a single sequence for differentiating SOAP answers in different file by vfs transport.The choice of the file to write in would be taken by the kind of answer from the WS.


Solution

  • Below should work for you. It is checking if QueryStructureResponse exists in SOAP xml or not. If available then it will call IstatAllDataflow-template.xml template otherwise it will invoke GetCompactData-template.xml template.

    <switch source="boolean($body//*[local-name() = 'QueryStructureResponse'])">
            <case regex="true">
               <call-template target="file">
            <with-param name="filename" value="IstatAllDataflow-template.xml"></with-param>
         </call-template>
            </case>
            <case regex="false">
               <call-template target="file">
            <with-param name="filename" value="GetCompactData-template.xml"></with-param>
         </call-template>
            </case>
         </switch>
    

    Another solution using filter in WSO2 esb.

    <filter source="boolean($body//*[local-name() = 'QueryStructureResponse'])" regex="true">
       <then>
          <log>
             <property name="======================== TRUE =========================" value="true"/>
          </log>
          <call-template target="file">
             <with-param name="filename" value="IstatAllDataflow-template.xml"/>
          </call-template>
       </then>
       <else>
          <log>
             <property name="==================== FALSE =========================" value="false"/>
          </log>
          <call-template target="file">
             <with-param name="filename" value="GetCompactData-template.xml"/>
          </call-template>
       </else>
    </filter>