Tuesday, January 10, 2017

How to set template sequence parameters to registry stored XML?

Suppose we have the following XML template stored in the location of "gov:/metadata1.0.0.xml".

 <?xml version="1.0" encoding="UTF-8"?>  
 <MetaData>  
   <MessageVersion />  
   <Destinations>  
    <Destination>  
      <Branches />  
      <Brands />  
    </Destination>  
   </Destinations>  
 </MetaData>  

We have created the following sequence template and the call template which pass "version" parameter to invoke the template.

 <?xml version="1.0" encoding="UTF-8"?>  
 <template xmlns="http://ws.apache.org/ns/synapse" name="metadata-creator-v1">  
   <parameter name="version" />  
   <sequence>  
    <property expression="get-property('registry','gov:/metadata1.0.0.xml')" name="xmlTemplate" scope="default" type="OM" />  
    <log level="custom">  
      <property expression="$ctx:xmlTemplate//MessageVersion" name="****logging empty tag****" />  
    </log>  
    <enrich>  
      <source clone="true" type="custom" xpath="$func:version" />  
      <target action="replace" type="custom" xpath="$ctx:xmlTemplate//MessageVersion" />  
    </enrich>  
    <log level="custom">  
      <property expression="get-property('xmlTemplate')" name="****xmlTemplate***" />  
    </log>  
   </sequence>  
 </template>  


 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse" name="TestProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">  
   <target>  
    <inSequence>  
      <call-template target="metadata-creator-v1">  
       <with-param name="version" value="version 1.0.0" />  
      </call-template>  
    </inSequence>  
    <outSequence>  
      <send />  
    </outSequence>  
   </target>  
   <description />  
 </proxy>  

In the above sequence template, the xml stored in the registry can be read into the property as follows.


 <property expression="get-property('registry','gov:/metadata1.0.0.xml')" name="xmlTemplate" scope="default" type="OM" />  

After setting into the property, it's possible access the <MessageVersion> element of the xml by xpath "$ctx:xmlTemplate//MessageVersion".
The enrich mediator is used to set the value of the version coming as a parameter with the call template to set the text of <MessageVersion> element.

<enrich>  
      <source clone="true" type="custom" xpath="$func:version" />  
      <target action="replace" type="custom" xpath="$ctx:xmlTemplate//MessageVersion" />  
    </enrich>  

The output will be appeared as follows.

 <?xml version="1.0" encoding="UTF-8"?>  
 <MetaData>  
   <MessageVersion>version 1.0.0</MessageVersion>  
   <Destinations>  
    <Destination>  
      <Branches />  
      <Brands />  
    </Destination>  
   </Destinations>  
 </MetaData>  


No comments:

Post a Comment