Friday, June 17, 2016

How to manipulate WSO2 ESB Registry resources using class mediator - ESB 4.9.0

In some scenarios  we may require to add registry resources (Gov or Conf) remotely without using WSO2 management console or CApps.

 (Note - WSO2 strongly recommend to use Carbon Applications to deploy the artifacts to the carbon products)

I have tested custom class mediator to create registry artifacts and  I was able to successfully manipulate them.

I used  Registry API for the Synapse, to create this mediator. 

Please find the class mediator and the proxy configuration on this scenario.

Class mediator source code
New resources are created at "conf:/repository/new/" location.

 package com.wso2.test;  
 import java.util.Map;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.core.axis2.Axis2MessageContext;  
 import org.apache.synapse.mediators.AbstractMediator;  
 public class TemplateManupulator extends AbstractMediator {  
      String action;  
      String resourceName;  
      String resourcePath;  
      public boolean mediate(MessageContext messageContext) {  
           resourcePath = "conf:/repository/new/";  
           org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)  
                     .getAxis2MessageContext();  
           Object headers = axis2MessageContext  
                     .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);  
           if (headers != null && headers instanceof Map) {  
                Map headersMap = (Map) headers;  
                action = headersMap.get("Action").toString();  
                resourceName = headersMap.get("Path").toString();  
           }  
           if (!action.equals(null) && !action.equals("")  
                     && !resourceName.equals(null) && !resourceName.equals("")) {  
                if (action.equals("save")) {  
                     messageContext.getConfiguration().getRegistry()  
                               .newResource(resourcePath+resourceName, false);  
                     messageContext  
                               .getConfiguration()  
                               .getRegistry()  
                               .updateResource(  
                                         resourcePath+resourceName,  
                                         messageContext.getProperty("myProperty")  
                                                   .toString().getBytes());                 
                } else if (action.equals("delete")) {  
                     messageContext.getConfiguration().getRegistry()  
                               .delete(resourcePath+resourceName);  
                } else if (action.equals("update")) {  
                     messageContext  
                               .getConfiguration()  
                               .getRegistry()  
                               .updateResource(  
                                         resourcePath+resourceName,  
                                         messageContext.getProperty("myProperty")  
                                                   .toString().getBytes());  
                } else {  
                     System.out.println("Correct header parameters not recieved");  
                }  
           }  
           return true;  
      }  
 }  

Proxy Configuration

 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
     name="resourceHandleProxy"  
     transports="https,http"  
     statistics="disable"  
     trace="disable"  
     startOnLoad="true">  
   <target>  
    <inSequence>  
      <log level="full"/>  
      <property name="myProperty"  
           expression="$body/child::*"  
           scope="default"  
           type="STRING"/>  
      <class name="com.wso2.test.TemplateManupulator"/>  
      <drop/>  
    </inSequence>  
   </target>  
   <description/>  
 </proxy>    

After deploying the proxy service with the class mediator we can invoke it using following kind SOAP request.

 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">  
   <soap:Header/>  
   <soap:Body>  
   <name>hello world </name>  
  </soap:Body>  
 </soap:Envelope>  

We required to send the resource name and action as HTTP headers in order to create registry resource. (Supporting actions are save, delete and update)

 Action: save  
 Path: hello.xml  

After  sending the request, log into the ESB management console and go to the "conf:/repository/new/" location using the registry browser. You will see a hello.xml in there.

This is a sample which I have tested and you can change the code and proxy configuration according to your requirement, As a suggestion, you can use VFS transport to get the file inside proxy service and class mediator to create the registry resource.



No comments :

Post a Comment