Search code examples
wso2esbpayload

How to combine two queries in wso2 esb


I am new with wso2 and probably my problem is very easy, but I've been fighting this for a week. The problem is that I have to combine two payloads in ESB 4.9.0.

I've tried enriching, making new payloadFactory with parameters from previous queries, but it all didn't make it in the way I want. The problem is that I have to get student data from DS and generate password from API (based on student id). All has to be combined in one payload and forwarded to xslt transformation. From DS I'm getting for example this:

<student>
  <try>
    <st_id>123456</st_id>
    <name>Michael</name>
    <surname>Smith</surname>
    <email>[email protected]</email>
    <ssn>123456789</ssn>
    <faculty name="IT">
      <field>Programming</field>
      <f_code>IT-1234-19</f_code>
    </faculty>
  </try>
</student>

Sometimes student can do only one faculty on one field of study, but some of them do two different faculties:

<student>
  <try>
    <st_id>121234</st_id>
    <name>John</name>
    <surname>Doe</surname>
    <email>[email protected]</email>
    <ssn>764896536</ssn>
    <faculty name="Management">
      <field>Production engineering</field>
      <f_code>MN-1234-19</f_code>
    </faculty>
  </try>
  <try>
    <st_id>121234</st_id>
    <name>John</name>
    <surname>Doe</surname>
    <email>[email protected]</email>
    <ssn>764896536</ssn>
    <faculty name="IT">
      <field>Electronics</field>
      <f_code>IT-4321-19</f_code>
    </faculty>
  </try>
</student>

API gives me that:

{"HASH":"{SSHA}PTFOuvF/20MrSGbTkQTkeBUC8A/0mfKF"}

Expected result: To add a HASH anywhere inside the student node f ex:

<student>
  <try>
    <st_id>123456</st_id>
    <name>Michael</name>
    <surname>Smith</surname>
    <email>[email protected]</email>
    <ssn>123456789</ssn>
    <faculty name="IT">
      <field>Programming</field>
      <f_code>IT-1234-19</f_code>
    </faculty>
  </try>
  <hash>{SSHA}PTFOuvF/20MrSGbTkQTkeBUC8A/0mfKF</hash>
</student>

My code is:

  <inSequence>

  <property expression="json-eval($.Numb)" name="Numb" scope="default" type="STRING"/>
  <payloadFactory media-type="xml">
    <format>
      <v1:getStudentRequest xmlns:v1="http://localhost/contract/ldap/v1">
        <v1:Numb>$1</v1:Numb>
      </v1:getStudentRequest>
    </format>
    <args>
      <arg evaluator="xml" expression="get-property('uri.var.Numb')"/>
    </args>
  </payloadFactory>
  <header name="Action" scope="default" value="urn:getStudentRequest"/>
  <call>
    <endpoint>
      <address uri="http://localhost:9769/services/LdapDS.SOAP11Endpoint" format="soap11"/>
    </endpoint>
  </call>
  <enrich>
    <source type="body" clone="true"/>
    <target type="property" property="Student"/>
  </enrich> 

  <property name="DISABLE_CHUNKING" value="true" scope="axis2"/>
  <call xmlns:v1="http://localhost/contract/ldap/v1">    
    <endpoint xmlns:v1="http://localhost/contract/ldap/v1"> 
      <http uri-template="http://localhost:39080/ldap/passgen/{uri.var.Numb}" method="GET" format="pox"></http>
    </endpoint>
  </call>
  <property evaluator="json" name="password" scope="default" expression="json-eval($.HASH)"/>
  <filter source="get-property('password')" regex="^$">
    <then>
      <log level="custom">
        <property expression="get-property('Numb')" name="Numb"/>
      </log>
    </then>
    <else>
      <property evaluator="json" name="pass" scope="default" expression="json-eval($.HASH)"/>
    </else>
  </filter>
  <!--
  <payloadFactory media-type="xml">
          <format>
            <m0:password xmlns:m0="http://localhost/contract/ldap/v1">$1</m0:password>
          </format>
          <args>
            <arg evaluator="xml" expression="get-property('pass')"/>
          </args>
  </payloadFactory>
  -->
  <enrich>
    <source xpath="$body" clone="true"/>
    <target action="child" type="custom" xpath="$Student//student/try"/>
  </enrich> 

  <enrich>
    <source type="property" clone="true" property="Student"/>
    <target action="replace" type="body"/>
  </enrich> 


  <xslt key="ldapXSL"/>

  <respond/>
</inSequence>

As a result I got just only get a HASH :(


Solution

  • The following contains a sample mediation that meets your requirements. Please refer to the following and develop your mediation. Analyzing the provided configurations it seems there are misconfigurations in the synapse artifacts (ex:-xpath="$Student//student/try") but the logic you have implemented is correct.

    <?xml version="1.0" encoding="UTF-8"?>
    <proxy xmlns="http://ws.apache.org/ns/synapse"
           name="testProxy"
           startOnLoad="true"
           statistics="disable"
           trace="disable"
           transports="http,https">
       <target>
          <inSequence>
             <log level="full"/>
             <call>
                <endpoint>
                   <http uri-template="http://www.mocky.io/v2/5dc6d3963800001c35cdec56"/>
                </endpoint>
             </call>
             <enrich>
                <source clone="true" type="body"/>
                <target property="property1" type="property"/>
             </enrich>
             <call>
                <endpoint>
                   <http uri-template="http://www.mocky.io/v2/5dc6d4723800004a00cdec59"/>
                </endpoint>
             </call>
             <property name="messageType" scope="axis2" value="application/xml"/>
             <enrich>
                <source clone="true" xpath="$body//HASH"/>
                <target action="child" xpath="$ctx:property1"/>
             </enrich>
             <enrich>
                <source clone="true" property="property1" type="property"/>
                <target type="body"/>
             </enrich>
             <log level="custom">
                <property expression="$ctx:property1" name="**-----**"/>
             </log>
             <respond/>
          </inSequence>
       </target>
       <description/>
    </proxy>
    

    The above proxy consists of the mock backends responses similar to that you have mentioned. Hope this helps