Development Guide

Example - First Interceptor

Let's create our own interceptor that will count the number of PDUs that went through it.

In this example, we are going to use Storable interface.

Interceptor Store

First we are going to create a simple store for our PDU counter.

interceptor.ExampleInterceptorStore
/**
 * Store for persisting PDU count.
 */
public class ExampleInterceptorStore {
    private int pduCount;

    public ExampleInterceptorStore() {
        pduCount = 0;
    }

    /**
     * Increases the PDU count and returns it.
     */
    public int increaseAndGetCount() {
        return ++pduCount;
    }
}

Interceptor

Now we are going to create a simple Interceptor.

interceptor.ExampleInterceptor
/**
 * Example interceptor that logs number of intercepted PDUs.
 */
public class ExampleInterceptor extends Interceptor {
    private ExampleInterceptorStore store;

    public ExampleInterceptor(
            int id,
            InterceptorModule module,
            PetepHelper helper,
            ExampleInterceptorStore store) {
        super(id, module, helper);
        this.store = store;
    }

    @Override
    public boolean intercept(PDU pdu) {
        Logger.getGlobal().info(
                () -> "Example interceptor intercepted PDU number " + store.increaseAndGetCount() + "!"
        );

        return true;
    }

    @Override
    public boolean prepare() {
        Logger.getGlobal().info("Example interceptor prepared!");

        return true;
    }

    @Override
    public void stop() {
        Logger.getGlobal().info("Example interceptor stopped!");
    }
}

Interceptor Module

Ok, our interceptor logic is defined. Now we need to create module that will allow user to add our interceptor to the list of interceptors in PETEP.

interceptor.ExampleInterceptorModule
/**
 * Example interceptor module that creates interceptor for logging number of intercepted PDUs.
 */
public class ExampleInterceptorModule extends InterceptorModule implements Storable<ExampleInterceptorStore> {
    /**
     * Store for persisting the PDU counter
     */
    private ExampleInterceptorStore store;

    public ExampleInterceptorModule(
            InterceptorModuleFactory factory,
            String code,
            String name,
            String description,
            boolean enabled) {
        super(factory, code, name, description, enabled);
    }

    @Override
    public Interceptor createInterceptor(int id, PetepHelper helper) {
        if (store == null) {
            store = new ExampleInterceptorStore();
        }

        return new ExampleInterceptor(id, this, helper, store);
    }

    @Override
    public void loadStore(ExampleInterceptorStore store) {
        this.store = store;
    }

    @Override
    public ExampleInterceptorStore saveStore() {
        return store;
    }
}

Notice that our module implements Storable interface that allows it to store the data (our counter) in the project.

Interceptor Module Factory

Now we can create a factory that will create the modules.

interceptor.ExampleInterceptorModuleFactory
/**
 * Example interceptor module factory for creating example interceptor modules.
 */
public class ExampleInterceptorModuleFactory extends InterceptorModuleFactory {
    public ExampleInterceptorModuleFactory(Extension extension) {
        super(extension);
    }

    @Override
    public String getCode() {
        return "example-interceptor";
    }

    @Override
    public String getName() {
        return "Example interceptor";
    }

    @Override
    public InterceptorModule createModule(String code, String name, String description, boolean enabled) {
        return new ExampleInterceptorModule(this, code, name, description, enabled);
    }
}

And finally, we can register the factory to the PETEP in our PetepExtension.init() method by adding the following line to it.

petep.Extension: public void init(ExtensionHelper helper)
helper.registerInterceptorModuleFactory(new ExampleInterceptorModuleFactory(this));