Sunday, February 7, 2016

Writing a Custom Handler for the WSO2 APIM

In various cases, we required to process incoming requests before going into the APIM mediation level (e.g - When we want to authenticate the user) and also we may need to process the response before sending it back to the client. Using Custom Handler we can achieve mentioned use-case.

You can get the details on Custom handlers by following the WSO2 official document.

In this post I will share a custom handler which I used.

Following is the Java class.

 package com.myOwn.test.handler;  
 import java.util.Map;  
 import org.apache.synapse.MessageContext;  
 import org.apache.synapse.core.axis2.Axis2MessageContext;  
 import org.apache.synapse.core.axis2.Axis2Sender;  
 import org.apache.synapse.rest.AbstractHandler; 
 
 public class newTestHandler extends AbstractHandler {  

      public boolean handleRequest(MessageContext messageContext) {  
           // Check whether there is Authorization header, if not available send 401 Status code.   
           org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)  
         .getAxis2MessageContext();  
     Object headers = axis2MessageContext  
         .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);  
     try {  
       if (headers != null && headers instanceof Map) {  
         Map headersMap = (Map) headers;  
         if (headersMap.get("Authorization") == null) {  
           headersMap.clear();  
           axis2MessageContext.setProperty("HTTP_SC", "401");  
           headersMap.put("WWW-Authenticate",  
               "Can not authorized without the Authoraization header");  
           axis2MessageContext.setProperty("NO_ENTITY_BODY",  
               new Boolean("true"));  
           messageContext.setProperty("RESPONSE", "true");  
           messageContext.setTo(null);  
           Axis2Sender.sendBack(messageContext);  
           return false;  
         } else {  
           return true;  
         }  
       }  
       return false;  
     } catch (Exception e) {  
       return false;  
     }  
      }  

      public boolean handleResponse(MessageContext messageContext) {  
           // get the message context and read custom header "name".  
           org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();  
            Object headers = axis2MessageContext.getProperty( org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);  
           Map headersMap = (Map) headers;  
           System.out.println(headersMap.get("name"));  
           return true;  
      }  
 }  


You can create a Maven project, and import the following POM file.

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.virtusa.test</groupId>  
  <artifactId>handler</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <packaging>jar</packaging>  
  <name>handler</name>  
  <url>http://maven.apache.org</url>  
  <properties>  
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>  
  <repositories>  
   <repository>  
     <id>wso2-nexus</id>  
     <name>WSO2 internal Repository</name>  
     <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>  
     <releases>  
       <enabled>true</enabled>  
       <updatePolicy>daily</updatePolicy>  
       <checksumPolicy>ignore</checksumPolicy>  
     </releases>  
   </repository>  
   </repositories>  
   <dependencies>  
   <dependency>  
       <groupId>org.apache.axis2.wso2</groupId>  
       <artifactId>axis2</artifactId>  
       <version>1.6.1.wso2v14</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-core</artifactId>  
       <version>2.1.3-wso2v11</version>  
     </dependency>  
     <dependency>  
       <groupId>commons-logging</groupId>  
       <artifactId>commons-logging</artifactId>  
       <version>1.1.1</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-extensions</artifactId>  
       <version>2.1.2-wso2v6</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-core</artifactId>  
       <version>2.1.2-wso2v6</version>  
       <exclusions>  
         <exclusion>  
           <groupId>org.apache.axis2</groupId>  
           <artifactId>axis2-codegen</artifactId>  
         </exclusion>  
       </exclusions>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-core</artifactId>  
       <version>2.1.0</version>  
     </dependency>  
     <dependency>  
       <groupId>org.wso2.carbon</groupId>  
       <artifactId>org.wso2.carbon.apimgt.impl</artifactId>  
       <version>1.2.3</version>  
     </dependency>  
     <dependency>  
       <groupId>org.apache.synapse</groupId>  
       <artifactId>synapse-core</artifactId>  
       <version>2.1.3-wso2v11</version>  
     </dependency>  
     <dependency>  
       <groupId>org.wso2.carbon.apimgt</groupId>  
       <artifactId>org.wso2.carbon.apimgt.gateway</artifactId>  
       <version>1.2.4</version>  
     </dependency>  
  </dependencies>  
 </project>