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
public class ExampleInterceptorStore {
  private int pduCount;

  public ExampleInterceptorStore() {
    pduCount = 0;
  }

  public int getPduCount() {
    return pduCount;
  }

  public int increaseAndGetCount() {
    return ++pduCount;
  }

  public void increaseCount() {
    ++pduCount;
  }
}

Interceptor

Now we are going to create a simple Interceptor.

interceptor.ExampleInterceptor
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
public class ExampleInterceptorModule extends InterceptorModule
    implements Storable<ExampleInterceptorStore> {
  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
public class ExampleInterceptorModuleFactory extends InterceptorModuleFactory {
  public ExampleInterceptorModuleFactory(Extension extension) {
    super(extension);
  }

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

  @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));
Získejte registraci domén s tld .online, .space, .store, .tech zdarma!
Stačí si k jedné z těchto domén vybrat hosting Plus nebo Mega a registraci domény od nás dostanete za 0 Kč!
Objednat