Using the Adapter SDK in Mashery Local with Third-Party Libraries

To use the Adapter SDK in Mashery Local with third-party libraries, follow the steps below:

Procedure

  1. Create a new Maven Project "Base64Adapter" in Eclipse, then add dependencies and class, as described in Using the Adapter SDK in Mashery Local with a Single Processor.
    The project layout should look like the following:

  2. Install the third-party library in Maven local repository, for example:
    mvn install:install-file -Dfile=org.apache.commons.codec_1.3.0.v201101211617.jar -DgroupId=org.apache.commons \
        -DartifactId=codec -Dversion=1.3.0.v201101211617 -Dpackaging=jar -DgeneratePom=true
  3. Add dependency on third-party library in the project:


  4. Use third-party library in the project, such as this example, which uses org.apache.commons.codec_1.3.0.v201101211617.jar:
    package com.example.masherylocal.Base64Adapter;
     
    import java.io.UnsupportedEncodingException;
     
    import org.apache.commons.codec.binary.Base64;
     
    import com.mashery.http.server.HTTPServerRequest;
    import com.mashery.http.server.HTTPServerResponse;
    import com.mashery.trafficmanager.event.listener.TrafficEventListener;
    import com.mashery.trafficmanager.event.model.TrafficEvent;
    import com.mashery.trafficmanager.event.model.TrafficEventCategory;
    import com.mashery.trafficmanager.event.model.TrafficEventType;
    import com.mashery.trafficmanager.event.processor.model.ProcessorEvent;
    import com.mashery.trafficmanager.model.core.APICall;
    import com.mashery.trafficmanager.model.core.ApplicationRequest;
    import com.mashery.trafficmanager.model.core.TrafficManagerResponse;
    import com.mashery.trafficmanager.processor.ProcessorBean;
     
    @ProcessorBean(enabled=true, name="com.example.masherylocal.Base64Adapter.Base64Processor", immediate=true)
    public class Base64Processor implements TrafficEventListener {
     
        public void handleEvent(TrafficEvent event) {
            TrafficEventType eventType = event.getType();
            if (eventType.getCategory() != TrafficEventCategory.PROCESSOR)
                return;
            ProcessorEvent processorEvent = (ProcessorEvent) event;
            APICall apiCall = processorEvent.getCallContext();
             
            if (eventType.getName().contentEquals("Pre-Process Event")) {
                ApplicationRequest appRequest = apiCall.getRequest();
                HTTPServerRequest httpRequest = appRequest.getHTTPRequest();
            } else if (eventType.getName().contentEquals("Post-Process Event")) {
                TrafficManagerResponse tmResp = apiCall.getResponse();
                HTTPServerResponse httpResp = tmResp.getHTTPResponse();
                try {
                    byte[] base64bytes = Base64.encodeBase64("Post-Process Event".getBytes("UTF-8"));
                    String base64str = new String(base64bytes, "UTF-8");
                    httpResp.getHeaders().add("Base64Adapter.Base64Processor_handleEvent", base64str);
                } catch (UnsupportedEncodingException e) {
                    // TODO Add logging
                }
            }
        }
    }
  5. Update build script to include Class-Path in MANIFEST.MF, as in this example Maven Project, this is done by adding org.apache.maven.plugins.maven-jar-plugin to pom.xml:
    <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.example.masherylocal</groupId>
      <artifactId>Base64Adapter</artifactId>
      <version>1.0</version>
      <packaging>jar</packaging>
     
      <name>Base64Adapter</name>
      <url>http://maven.apache.org</url>
     
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
       
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
              <archive>
                <index>true</index>
                <manifest>
                  <addClasspath>true</addClasspath>
                </manifest>
              </archive>
            </configuration>
          </plugin>
        </plugins>
      </build>
     
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.mashery</groupId>
            <artifactId>http</artifactId>
            <version>1.0.0.v20130130-0044</version>
        </dependency>
        <dependency>
            <groupId>com.mashery</groupId>
            <artifactId>trafficmanager</artifactId>
            <version>1.1.0.v20130214-0043</version>
        </dependency>
        <dependency>
            <groupId>com.mashery</groupId>
            <artifactId>util</artifactId>
            <version>1.0.0.v20130214-0015</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>codec</artifactId>
            <version>1.3.0.v201101211617</version>
        </dependency>
      </dependencies>
    </project>
    Here is an example of MANIFEST.MF:
    Manifest-Version: 1.0
    Built-By: beta
    Class-Path: http-1.0.0.v20130130-0044.jar trafficmanager-1.1.0.v201302
     14-0043.jar util-1.0.0.v20130214-0015.jar codec-1.3.0.v201101211617.j
     ar
    Created-By: Apache Maven 3.0.5
    Build-Jdk: 1.8.0_121
  6. Build the project in the folder, for example, /home/beta/work_diysdk/Base64Adapter/:
    mvn package
  7. Use a simple zip command to package the project in the folder, for example, /home/beta/work_diysdk/Base64Adapter/target/:
    cd /home/beta/work_diysdk/Base64Adapter/target/
    mkdir lib
    cp /home/beta/.m2/repository/org/apache/commons/codec/1.3.0.v201101211617/codec-1.3.0.v201101211617.jar lib/
    zip Base64Adapter-1.0.zip Base64Adapter-1.0.jar lib/codec-1.3.0.v201101211617.jar
    Here is an example of the zipped contents:

  8. Unload the zipped package using Mashery Local Cluster Manager.
  9. Apply the Custom Processor on the Endpoint using Mashery SaaS Control Center.