[WSO2 ESB] Change local name of an element of the payload

There was a requirement to change the local name of an element in the payload using a parameter in the incoming payload. For example

user sends a following payload to the ESB

<people>
   <action>submit</action>
   <person>
      <name>asanka</name>
   </person>
   <person>
      <name>nuwan</name>
   </person>
   <person>
      <name>bandara</name>
   </person>
</people>

Requirement was, value in

  <action>submit</action> 

to be the root element of the response message like below.

<submit>
<status>SUCCESS</status>
</submit>

Guess what !!! .. it is just following piece of code.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="createPayload">
   
     <property name="RESPONSE_ROOT_TAG" expression="//action/text()" type="STRING"/>
         
        <script language="js">
            var rootElementName=mc.getProperty('RESPONSE_ROOT_TAG');
            mc.getEnvelope().getBody().getFirstElement().setLocalName(rootElementName);
        </script>    
</sequence>

hope this can save someone’s time 🙂 🙂

Advertisements

Wrap Aggregated Messages in a root element in one go !!

Hi.. this problem wasted a lot of time of mine 😦 So I am writing this blog , hope this will save someone’s time . I think you already know about the Aggregate Mediator. It is used to aggregate / collect responses resulted from Clone mediator or Iterate Mediator . I am not going to explain the usage of the Aggregate Mediator here. I hope to explain the behind the scene stuffs of the aggregate mediator in a future post. For the time being , lets just continue with this particular problem.

Suppose there are two back ends that sends following responses.

<userInfo>
   <id>1</id>
   <name>Asanka</name>
</userInfo>
<accountInfo>
   <userId>1</userId>
   <accNo>99239930923</accNo>
   <branchNo>221</branchNo>
</accountInfo>

And the expected response at the client is

<Information>
   <userInfo>
      <id>1</id>
      <name>Asanka</name>
   </userInfo>
   <accountInfo>
      <userId>1</userId>
      <accNo>99239930923</accNo>
      <branchNo>221</branchNo>
   </accountInfo>
</Information>

.Lets get started …

First a clone mediator should be used to send the request to multiple back ends. i am not going to specify back ends. But it looks like below.

<clone id="sa">
   <target>
      <endpoint>
         <address uri="URL_BackEnd1" />
      </endpoint>
   </target>
   <target>
      <endpoint>
         <address uri="URL_BackEnd2" />
      </endpoint>
   </target>
</clone>

Now lets have a look at how the outsequence should look like. Here are the things we have to do in  the outsequnce.

1.Create OM type property to accumulate responses in the Aggregate Mediator.

2.Introducing the Aggregate Mediator to collect responses from back ends.

3.Send them back to the client.

<outSequence>
   <property name="info" scope="default">
      <ns:Information xmlns:ns="www.asankatechtalks.com" />
   </property>
   <aggregate id="sa">
      <completeCondition />
      <onComplete expression="$body/*[1]" enclosingElementProperty="info">
         <send />
      </onComplete>
   </aggregate>
</outSequence>

This is to aggregate the responses inside it. This property is specified inside Aggregate mediator as attribute “enclosingElementProperty” of the <onComplete>.

   <aggregate id="sa">

Attribute “id” specifies the particular Clone mediator that is associated with the Aggregate mediator.

<completeCondition />

This represents the condition to Aggregate mediator to stop the aggregation of responses. Here I have not set this. Since clone has 3 targets, complete condition will be the moment it receives 3 responses.

      <onComplete expression="$body/*[1]" enclosingElementProperty="info">
         <send />
      </onComplete>

here the “expression” is the aggregate expression.This specifies which part of the response should be aggregated.In my case I have given it as the first child of the response body of the each response resulted from the clone mediator.

Then comes the main actor of the film :). enclosingElementProperty specifies the OM type property we defined at the beginning of the out sequence. So that property is added to the payload body and aggregated messages goes in to the particular tag created in this property.

 

So you will get the response as explained in the blog post.