Thursday, December 24, 2015

Differentiating the Admin/API/Proxy/Main Sequence service calls in WSO2 ESB at axis2 level

This article shows how to differentiate the Admin/API/ProxyMain sequence service calls using a axis2 handler. Here only giving a sample class to handle this requirement. To get more information on how to create module and handler refer [1], [2].
package com.custom.axis.loggers;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.SynapseConstants;

/*
 * This class differentiate's the Proxy Service / API / Admin Service Calls.
 *
 */

public class CustomLoggingHandler extends AbstractHandler {

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

    public InvocationResponse invoke(MessageContext msgContext)
            throws AxisFault {

        log.debug("----------------------CustomLoggingHandler Invoked----------------------");

        AxisService axisService = msgContext.getAxisService();

        if (isProxyService(msgContext, axisService)) { // For Proxy Services checking the ServiceType Parameter
            if (log.isDebugEnabled()) {
                log.debug("Is a proxy service.");
            }
        } else { // For APIs/Main Sequence and Admin Services
            if (axisService.getAxisServiceGroup().getServiceGroupName()
                    .equals(SynapseConstants.SYNAPSE_SERVICE_NAME)) { //For APIs and main sequence
                if (log.isDebugEnabled()) {
                    log.debug("Is a API.");
                }
            } else { // For Admin Services
                if (log.isDebugEnabled()) {
                    log.debug("Is a Admin Service.");
                }
            }
        }

        return InvocationResponse.CONTINUE;
    }

    // Method to check whether request is for Proxy Service.
    public static boolean isProxyService(MessageContext messageContext,
            AxisService axisService) {

        if (axisService != null) {
            Parameter val = axisService.getParameter("serviceType");
            if (val != null
                    && val.getValue().toString().equalsIgnoreCase("Proxy")) {
                if (log.isDebugEnabled()) {
                    log.debug("Parameter Value Service Type: "
                            + val.getValue().toString());
                }
                return true;
            }
        }
        return false;
    }


[1] http://ajanthane.blogspot.com/2015/12/creating-axis2-module-for-custom.html
[2] http://ajanthane.blogspot.com/2015/10/creating-custom-axis2-handler.html

No comments:

Post a Comment