Friday, December 25, 2015

Sending POST Requests to WSO2 ESB Proxy Service without Body

This article shows how to send a Request without body to a ESB proxy service. When we send a post request to ESB without body and any content aware mediators in the way then the Send Timeout error will be thrown in the ESB carbon log. This can be reproduced using a log mediator in the synapse configuration. To overcome this issue need to set the below property in synapse configuration.

<property name="FORCE_POST_PUT_NOBODY" scope="axis2" type="BOOLEAN" value="true"/> 

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

Creating Axis2 Module for Custom Logging and Integrating to WSO2 ESB

This article is about creating Axis2 module and Integrating it with WSO2 ESB.

1) Create a Maven Project using eclipse. And add the below to the dependencies in pom.xml.Below is the pom.xml created for this sample.

    4.0.0

    CustomAxis2ModuleProject
    CustomAxis2ModuleProject
    0.0.1-SNAPSHOT
    jar

    CustomAxis2ModuleProject
    http://maven.apache.org

    
        UTF-8
    

    
        
            junit
            junit
            3.8.1
            test
        
        
            org.apache.axis2.wso2
            axis2
            1.6.1.wso2v14
        
        
            org.apache.synapse
            synapse-core
            2.1.3-wso2v11
        
        
            commons-logging
            commons-logging
            1.1.1
        
    
    
        
            
                true
                daily
                ignore
            
            wso2-nexus
            http://maven.wso2.org/nexus/content/groups/wso2-public/
        
    
    
        
            
                true
                daily
                ignore
            
            wso2-nexus
            http://maven.wso2.org/nexus/content/groups/wso2-public/
        
    


2) Create the Axis2 Module Implementation.This can be done by creating a CustomLoggingModule class by implementing the Module class like below.
package com.custom.axis.loggers;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.modules.Module;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;

public class CustomLoggingModule implements Module {

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

    public void applyPolicy(Policy arg0, AxisDescription arg1) throws AxisFault {
        // TODO Auto-generated method stub

    }

    public boolean canSupportAssertion(Assertion arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    public void engageNotify(AxisDescription arg0) throws AxisFault {
        // TODO Auto-generated method stub

    }

    public void init(ConfigurationContext arg0, AxisModule arg1)
            throws AxisFault {
        if (log.isDebugEnabled()) {
            log.debug("Custom Logging Module Init Method Invoked");
        }
    }

    public void shutdown(ConfigurationContext arg0) throws AxisFault {
        // TODO Auto-generated method stub

    }

}


3) Create the Handlers. This can be done by creating a CustomLoggingHandler class by extending AbstractHandler class.
package com.custom.axis.loggers;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
 * Custom Axis2 Handler for custom logging.
 *
 */

public class CustomLoggingHandler extends AbstractHandler {

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

    public InvocationResponse invoke(MessageContext msgContext)
            throws AxisFault {

        // Add your custom logging logic here

        return InvocationResponse.CONTINUE;
    }

}



4) Create a module.xml in src/main/resources/META-INF. The sample module.xml is below. Here only invoking the handler in the InFlow.

Note: Here due to auto formatting in the editor the <inFlow> is converted to <inflow>. Use <inFlow> when you try out the sample. 

    
        
            
        
     
 

5) The maven project structure will look like below.
 6) Build the project using Maven build.

7) Copy the jar created to  ESB_HOME/repository/deployment/server/axis2modules/ and rename with .mar extension.
8) Modify the axis2.xml to add the phase to the InFlow.
 9) To assign the module globally to all the service requests add the module to axis2.xml as below.
10) Now the custom module is ready to use.

References
[1] http://wso2.com/library/777/ 
[2] https://axis.apache.org/axis2/java/core/docs/modules.html
[3] https://axis.apache.org/axis2/java/core/docs/Axis2ArchitectureGuide.html#requirements