Sunday, April 10, 2016

[WSO2][ESB][Class Mediator]How to create an Empty OMElement Array?

Hi all,

Let's find out very interesting topic. While using store mediator in ESB, I just wanted to create an empty OMElement array.

If I clarify inside mediation within ESB, when it receive a message, it may convert into OMElements which will is used to next mediation process. Therefore, I can't send empty OMElement array from outside as it is an inside process of ESB (Just like sending SOAP or REST messages).

So we have to convert the message into OMElements within ESB.In my case, I have to add empty OMElement array to the message.

ESB is a marvellous middleware which will provide magical operations. Here it comes "Class Mediator". The Class mediator will retrieve a class and it may act as custom mediator.

So Class Mediator helped me to do my task.

Let's begin...

1) Write a java class which will set OMElement array to the message.

When writing java class which is going to deploy as a "Class Mediator", it must extend from "Abstract Mediator"

In here I used message context (org.apache.synapse.MessageContext) to add empty array.

As a property set new array without any elements.

Sample Configuration

 package org.wso2.test;  
 import java.util.ArrayList;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.mediators.AbstractMediator;  
 public class OMElementEmptyArray  
 extends AbstractMediator {  
   public boolean mediate(MessageContext context) {  
     context.setProperty("EMPTY_ARRAY", new ArrayList());  
     System.out.println("Routed through Class Mediator");  
     return true;  
   }  
 }  

2) Java class packaged as .jar and should be inside $ESB_HOME/repository/components/lib

3) ESB should be up and running.

4) Login to ESB and create an API which will add class mediator.

Sample Configuration:

 <api context="/SerializeProperty" name="StoreMediatorSerialize">  
     <resource methods="GET" protocol="http" url-mapping="/serializeOMArray">  
       <inSequence>  
         <class name="org.wso2.test.OMElementEmptyArray"/>  
       </inSequence>  
       <outSequence/>  
       <faultSequence/>  
     </resource>  
   </api>  

5) Invoke API.

Sample Invocation through REST Client:

GET      http://10.100.7.120:8280/SerializeProperty

Terminal will print follow log:

Routed through Class Mediator


Happy Coding !!!


No comments:

Post a Comment