Sunday, May 26, 2013

Adding Root Element for Aggregate Response (Alternative)

Following code segment shows the, possible alternative way of adding root element to the response when Iterator/Aggregate in use, there are tons of alternatives but this just works for you...


<proxy xmlns="http://ws.apache.org/ns/synapse" name="IteratorProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence>
         <enrich>
            <source type="body" clone="true"/>
            <target type="property" property="ORINAL_SOAP"/>
         </enrich>
         <payloadFactory media-type="xml">
            <format>
               <test/>
            </format>
         </payloadFactory>
         <enrich>
            <source type="body" clone="true"/>
            <target type="property" property="RESPONSE_WRAPPER"/>
         </enrich>
         <enrich>
            <source type="property" clone="true" property="ORINAL_SOAP"/>
            <target type="body"/>
         </enrich>
         <iterate xmlns:xsd="http://services.samples/xsd" xmlns:ser="http://services.samples" id="iteratorID" expression="//ser:Requests/ser:getQuote">
            <target>
               <sequence>
                  <send>
                     <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                     </endpoint>
                  </send>
               </sequence>
            </target>
         </iterate>
      </inSequence>
      <outSequence>
         <enrich>
            <source type="body" clone="true"/>
            <target action="child" xpath="$ctx:RESPONSE_WRAPPER"/>
         </enrich>
         <aggregate id="iteratorID">
            <completeCondition>
               <messageCount min="-1" max="-1"/>
            </completeCondition>
            <onComplete expression="$body/child::*">
               <log level="custom">
                  <property name="MESSAGE###########################3" expression="$ctx:RESPONSE_WRAPPER"/>
               </log>
               <payloadFactory media-type="xml">
                  <format>
                     <dummy/>
                  </format>
               </payloadFactory>
               <enrich>
                  <source clone="true" xpath="$ctx:RESPONSE_WRAPPER"/>
                  <target type="body"/>
               </enrich>
               <send/>
            </onComplete>
         </aggregate>
      </outSequence>
   </target>
   <description></description>
</proxy>
                             


the ultimate response will be looked as,


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <test xmlns="http://ws.apache.org/ns/synapse">
         <ns:getQuoteResponse xmlns:ns="http://services.samples">
            <ns:return xsi:type="ax21:GetQuoteResponse" xmlns:ax21="http://services.samples/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
               <ax21:change>4.2866157514113805</ax21:change>
               <ax21:earnings>12.584682581151846</ax21:earnings>
               <ax21:high>75.5962918071053</ax21:high>
               <ax21:last>72.60406241158645</ax21:last>
               <ax21:lastTradeTimestamp>Sun May 26 21:25:05 IST 2013</ax21:lastTradeTimestamp>
               <ax21:low>75.8658711254791</ax21:low>
               <ax21:marketCap>-3467898.59685205</ax21:marketCap>
               <ax21:name>IBM Company</ax21:name>
               <ax21:open>-71.56274026362532</ax21:open>
               <ax21:peRatio>23.075895025476</ax21:peRatio>
               <ax21:percentageChange>-5.90419120121126</ax21:percentageChange>
               <ax21:prevClose>-72.60292909436893</ax21:prevClose>
               <ax21:symbol>IBM</ax21:symbol>
               <ax21:volume>15608</ax21:volume>
            </ns:return>
         </ns:getQuoteResponse>
         <ns:getQuoteResponse xmlns:ns="http://services.samples">
            <ns:return xsi:type="ax21:GetQuoteResponse" xmlns:ax21="http://services.samples/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
               <ax21:change>-2.750820910784016</ax21:change>
               <ax21:earnings>-9.787293133205745</ax21:earnings>
               <ax21:high>-75.16825065031121</ax21:high>
               <ax21:last>75.80190597722125</ax21:last>
               <ax21:lastTradeTimestamp>Sun May 26 21:25:05 IST 2013</ax21:lastTradeTimestamp>
               <ax21:low>-74.79485577495237</ax21:low>
               <ax21:marketCap>-5189872.124349184</ax21:marketCap>
               <ax21:name>DLL Company</ax21:name>
               <ax21:open>-74.65259440061676</ax21:open>
               <ax21:peRatio>24.229158211448674</ax21:peRatio>
               <ax21:percentageChange>3.6744576753831906</ax21:percentageChange>
               <ax21:prevClose>-74.86331736008216</ax21:prevClose>
               <ax21:symbol>DLL</ax21:symbol>
               <ax21:volume>16903</ax21:volume>
            </ns:return>
         </ns:getQuoteResponse>
      </test>
   </soapenv:Body>
</soapenv:Envelope>



                               

2 comments:

  1. Excellent blog Dushan! Just what I was looking for, keep up the great work!

    ReplyDelete
  2. Following is an alternative way of doing this.

    <aggregate>
    <completeCondition>
    <messageCount min="-1" max="-1"/>
    </completeCondition>
    <onComplete xmlns:ns="http://org.apache.synapse/xsd"
    xmlns:sb="http://ws.starbucks.com"
    expression="//sb:addOrderResponse">
    <property name="BODY" expression="$body/child::*"/>
    <payloadFactory>
    <format>
    <rootElement>$1</rootElement>
    </format>
    <args>
    <arg expression="$ctx:BODY"/>
    </args>
    </payloadFactory>
    </onComplete>
    </aggregate>


    In ESB 4.7.0, a new configuration option for aggregate mediator has introduced to acheive this.
    You can define the root element in a property of type "OM" in the mediadation follow.

    To refer to it, we have a new configuration option in the aggregate mediator called "enclosingElementProperty".


    New aggregate mediator configuration looks like follows.

    <aggregate>
    <completeCondition>
    <messageCount min="-1" max="-1"/>
    </completeCondition>
    <onComplete xmlns:ns="http://org.apache.synapse/xsd"
    xmlns:sb="http://ws.starbucks.com"
    expression="//sb:addOrderResponse" enclosingElementProperty="ENCLOSING_ELEMENT_PROPERTY_NAME_HERE">

    </onComplete>
    </aggregate>

    ReplyDelete