com.apipulse.bastion.modules.foundational.ScriptableTransformer.java Source code

Java tutorial

Introduction

Here is the source code for com.apipulse.bastion.modules.foundational.ScriptableTransformer.java

Source

/*
 * Bastion Data Ingestor
 * Copyright (c) 2016 API Fortress Inc.
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.apipulse.bastion.modules.foundational;

import com.apipulse.bastion.StageConfig;
import com.apipulse.bastion.actors.messages.StepMessage;
import com.apipulse.bastion.stages.AbstractTransformer;
import com.apipulse.bastion.stages.IScriptable;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.io.IOUtils;

import java.io.FileReader;

/**
 * Transformer that can be scripted. A simple script can either be in the chain configuration
 * ("script" field) or refer to script located in the etc/scripts directory ("scriptId" field).
 * The scripts must be written in Groovy.
 */
public class ScriptableTransformer extends AbstractTransformer implements IScriptable {

    /**
     * The script id. In this case, it should match the name of a file located in the etc/scripts
     * directory
     */
    String scriptId;
    /**
     * The body of a script
     */
    final String script;
    /**
     * The groovy shell
     */
    final GroovyShell shell;

    public ScriptableTransformer(int index, StageConfig config) {
        super(index, config);
        scriptId = config.getString("scriptId");
        script = config.getString("script");
        if (scriptId == null && script != null)
            scriptId = Base64.encodeBase64String(Md5Crypt.md5Crypt(script.getBytes()).getBytes());
        shell = new GroovyShell();
    }

    @Override
    public Object transform(StepMessage message) throws Exception {
        Script script = loadScript(scriptId);
        Binding binding = new Binding();
        binding.setVariable("message", message);
        script.setBinding(binding);
        script.run();
        return message.getData();
    }

    @Override
    public void stop() {

    }

    @Override
    public Script loadScript(String id) {
        String footprint = "b_" + id.replaceAll("\\.", "_");
        try {
            Class theClass = shell.getClassLoader().loadClass(footprint);
            return (Script) theClass.newInstance();
        } catch (Exception e) {
        }
        try {
            String data;
            if (script == null)
                data = IOUtils.toString(new FileReader("etc/scripts/" + id));
            else
                data = script;
            Class theClass = shell.getClassLoader().parseClass(data, footprint);
            return (Script) theClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}