Tuesday, February 21, 2017

How to write custom class mediator to activate/deactivate proxy services created in WSO2 ESB.

To activate/deactivate proxy services which are exposed as "http" without using ServiceAdmin, you could use a class mediator solution. We have used ProxyDeactivator and ProxyActivator class mediators to activate/deactivate proxy services.

Class mediator code for "ProxyDeactivator"


package org.wso2.custom;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class ProxyDeactivator extends AbstractMediator {

    private static final Log log = LogFactory.getLog((Class)ProxyDeactivator.class);

    public boolean mediate(MessageContext context) {
        log.info((Object)"[ProxyDeactivator] --------------Invocation START-------------");

        context.getConfiguration().getProxyService("testProxy").stop(context.getConfiguration());

        log.info((Object)"[ProxyDeactivator] --------------Invocation END-------------");
        return true;
    }
}


To test the above scenario, place the jar created with above configuration inside the <ESB_HOME>/repository/components/lib directory.

Create a proxy service (http) with the above ProxyDeactivator class mediator.


<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="testProxy"
       transports="http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="custom">
            <property name="LOG" value="---------inside the InSeq--------"/>
         </log>
         <class name="org.wso2.custom.ProxyDeactivator"/>
         <send>
            <endpoint>
               <address uri="http://www.google.com"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>                              


Invoke the proxy service with below curl command.

curl -v -i -H "Content-Type: application/soap+xml" -H "Accept: application/soap+xml" http://ubuntu-ThinkCentre-M83:8280/services/testProxy


As you could see below proxy service got deactivated after invoking the proxy.



Class mediator code for "ProxyActivator":


package org.wso2.custom;
package org.wso2.custom;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class ProxyActivator extends AbstractMediator {

    private static final Log log = LogFactory.getLog((Class)ProxyActivator.class);

    public boolean mediate(MessageContext context) {

        log.info((Object)"[ProxyActivator] --------------Invocation START-------------");

        context.getConfiguration().getProxyService("testProxy").start(context.getConfiguration());

        log.info((Object)"[ProxyActivator] --------------Invocation END-------------");
        return true;
    }
}


To test the above scenario, place the jar created with above configuration inside the <ESB_HOME>/repository/components/lib directory.

You need to use this inside a separate proxy service and class mediator will activate the proxy inside it


<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="testProxy1"
       transports="http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="custom">
            <property name="LOG" value="---------inside the InSeq--------"/>
         </log>
         <class name="org.wso2.custom.ProxyActivator"/>
         <send>
            <endpoint>
               <address uri="http://www.google.com"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>


Invoke the proxy service with below curl command.

curl -v -i -H "Content-Type: application/soap+xml" -H "Accept: application/soap+xml" http://ubuntu-ThinkCentre-M83:8280/services/testProxy1

As you could see below proxy service got activated after invoking the proxy.

No comments:

Post a Comment