Sunday, September 4, 2016

How to set a static response or property in class mediator in WSO2 ESB

WSO2 ESB provides us luxury to create a custom functionality in java as class mediator. In class mediator we can write our desired functionality in java class and that can be called from the ESB synapse file. We can pass some property from ESB file to class mediator and in a same way we can set some property in java class and that can be read from synapse file.

Here is an example in which I need to reply a static response to customer. However this can be done from many ways but I am choosing class mediator just to show how this can be done from class mediator.


 I want below static response to be shown in as actual response of the service. I have put this “StaticEmployeeDetails.xml” file in “XX\wso2esb-4.0.3\repository\conf\stub\StubXml\ StaticEmployeeDetails.xml” location.

Static XML Code:

<EmployeeDetails xmlns="http://shriwithjava.blogspot.co.nz/">
   <EmployeeData>
      <EmployeeId>201</EmployeeId>
      <EmployeeName>Shri</EmployeeName>
      <EmployeeAddress>Auckland</EmployeeAddress>
      <Department>IT</Department>
   </EmployeeData>
   <EmployeeData>
      <EmployeeId>202</EmployeeId>
      <EmployeeName>Bhajan</EmployeeName>
      <EmployeeAddress>Wellington</EmployeeAddress>
      <Department>IT</Department>
   </EmployeeData>
</EmployeeDetails>



Here is the Class mediator code which will be called from the wso2 Synapse file.

Class Mediator Code:

package org.wso2.custom;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class CustomMediator extends AbstractMediator {

                private static String Response = "StaticResponse";
                private static String ResponseXml = "";
                private static String FileName = "";
                private static String FilePath = "F:\\Shri Kant\\WSO2\\wso2esb-4.0.3\\repository\\conf\\stub\\StubXml\\";
                public boolean mediate(MessageContext context) {
                                // TODO Implement your mediation logic here
                                //FileName=context.getProperty("").toString();
                                FileName=(String)context.getProperty("FileName");
                                BufferedReader br = null;
                                try {

                                                String sCurrentLine;

                                                br = new BufferedReader(new FileReader(FilePath+FileName+".xml"));

                                                while ((sCurrentLine = br.readLine()) != null) {
                                                                ResponseXml+=               sCurrentLine;
                                                }
                                                context.setProperty(Response, ResponseXml);
                                               
                                                System.out.println(context.getSoapAction()+" : This is the SOAP Action");
                                                System.out.println(context.getConfiguration()+" : This is the getConfiguration");
                                                System.out.println(context.getMessageID()+" : This is the getMessageID");
                                                System.out.println(context.getTracingState()+" : This is the getTracingState");
                                                System.out.println(context.getWSAAction()+" : This is the getWSAAction");
                                                System.out.println(context.getWSAMessageID()+" : This is the getWSAMessageID");
                                                System.out.println(context.getClass()+" : This is the getClass");
                                                System.out.println(context.getContextEntries()+" : This is the getContextEntries");
                                                System.out.println(context.getEnvelope()+" : This is the getEnvelope");
                                                System.out.println(context.getEnvironment()+" : This is the getEnvironment");
                                                System.out.println(context.getFaultSequence()+" : This is the getFaultSequence");
                                                System.out.println(context.getFaultStack()+" : This is the getFaultStack");
                                                System.out.println(context.getPropertyKeySet()+" : This is the getPropertyKeySet");
                                                System.out.println(context.getServiceLog()+" : This is the getServiceLog");
                                                System.out.println(context.isDoingGET()+" : This is the isDoingGET");
                                                System.out.println(context.isDoingMTOM()+" : This is the isDoingMTOM");
                                                System.out.println(context.isDoingPOX()+" : This is the isDoingPOX");
                                                System.out.println(context.isDoingSWA()+" : This is the isDoingSWA");
                                                System.out.println(context.isFaultResponse()+" : This is the isFaultResponse");
                                                System.out.println(context.isSOAP11()+" : This is the isSOAP11");
                                                System.out.println(context.isResponse()+" : This is the isResponse");
                                               
                                } catch (IOException e) {
                                                e.printStackTrace();
                                } finally {
                                                Response="";
                                                try {
                                                                if (br != null){
                                                                                br.close();
                                                                               
                                                                }
                                                } catch (IOException ex) {
                                                                ex.printStackTrace();
                                                }
                                }
                               
                               
                                return true;
                }
}




  • ·         Different property has been used in above code just to show that what all we can set from class mediator and use it in our synapse file.
  • ·         Create ja jar file of this above code and place it in the “X\ wso2esb-4.0.3\repository\components\lib\CustomMediator.jar”
  • ·         Now we need to provide the reference of this class from the synapse.xml file

I am creating a service “MyCustomProxy”. You can see that I have used “<class name="org.wso2.custom.CustomMediator"/>” code to call the class mediator. I have also used the javascript function which will change the response from string to xml. “<property name="StaticResponse" action="set" expression="get-property('StaticResponse')"/>” is used to get response from the class mediator which will be read from the static xml file.

Synpse.xml:

<localEntry key="StaticServiceScript" src="file:repository/conf/stub/StaticTransformResponse.js"/>
               
<proxy name="MyCustomProxy" transports="http" startOnLoad="true" statistics="enable" trace="disable">
                <target inSequence="MyCustom_IN" outSequence="MyCustom_OUT" faultSequence="CommonFaultHandler"/>
                <publishWSDL key="MyCustom_wsdl"/>
</proxy>
<localEntry key="MyCustom_wsdl" src="file:repository/conf/employee/OverseaseEmployee.wsdl"/>
<sequence name="MyCustom_IN">
                <property name="FileName" value="StaticEmployeeDetails" action="set"/>     
                <class name="org.wso2.custom.CustomMediator"/>
                <property name="StaticResponse" action="set" expression="get-property('StaticResponse')"/>
                <script language="js" key="StaticServiceScript" function="sendStaticResponse"/>
                <property name="RESPONSE" value="true"/>
                <header name="To" action="remove"/>
                <send/>
                <drop/>
</sequence>    




Java Script Code:
I am using a java script code which will be called from synapse.xml file. This will create a string text to xml content.

StaticTransformResponse.js


<x><![CDATA[  

                function sendStaticResponse(mc) {
                var StaticResponse = new XML(mc.getProperty("StaticResponse"));
                                                mc.setPayloadXML(StaticResponse);
               
                  }
]]></x>




Just restart the server to reflect the changes and hit the “MyCustomProxy” service and you will get the desired response. If you want to change the static response just change the content of the “StaticEmployeeDetails.xml” file located inside of “wso2esb-4.0.3\repository\conf\stub\StubXml” and restart the server.


Above image shows all the parameter what we have set in out java class. Same things we can get it in synapse.xml file as per our requirement.

No comments:

Post a Comment