How to Use Custom Servlets, Filters, and Listeners

Quantum Fabric lets you further customize your Quantum Fabric apps by adding custom servlets, filters and listeners. This topic provides examples and describes how to add these to your Quantum Fabric apps.

How to Add a Custom Servlet

You can add custom servlets with custom logic, and publish it by adding it to Quantum Fabric application and it works as per the Java Servlet Specification.

For example, you can

  • Create a servlet with the desired URL pattern where you can then write your custom code.
  • Use custom servlets to integrate other third-party servlets.

The following sample code shows a simple custom servlet that redirects a request to the provided URL.

 

How to Add a Custom Filter

You can add custom filters to add custom logic alongside the existing filters already provided by Quantum Fabric.

The following steps describe how to add security checks to a request.

  1. Create a filter class following the Java Servlet Specification, and then annotate it with the Quantum @IntegrationCustomFilter annotation.
  2. Write the logic in the doFilter() method, and then publish the custom JAR by adding it as a transitive dependency to your Quantum Fabric App.
  3. Configure filterOrder between the custom filters for your custom filter. Refer to the example below. Quantum recommends that you start the filterOrder from the number 101 as 1 – 100 are reserved for Quantum.
  4. Configure a custom filter to a request pattern
    or

    Configure a custom filter to one of the following Quantum-provided servlets
    • MWServlet - Used for integration and orchestration services.

    • AppServices - Used for object services runtime request.

    • AppMetadataServices – Used for object services metadata request.

  5. Request pattern can be given to match for a particular service URL pattern.

How to Add a Custom Listener

You can also use custom listeners to add custom logic for application events, such as application start-up, or session create and destroy.

NOTE: To create a custom session listener, use the @IntegrationHttpSessionListener annotation.

The following code sample shows a SimpleSessionListener class which implements anHttpSessionListener that is invoked every time the session is created or destroyed.

How to Create a Custom Servlet Context Listener

Servlet Context Listener is used to listen to startup and shutdown event of context. Every method in listener interface takes Event object as input.

To create a custom servlet context listener, use @IntegrationServletContextListener annotation.

The following sample for Custom ServletContextListener is invoked on application life cycle events.

  • contextInitialized - When the application boots up or during startup.

  • contextDestroyed - When the application is stopped or during shutdown.

 package com.kony.customlistener;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
import org.apache.log4j.Logger;
 
import com.konylabs.middleware.servlet.listeners.IntegrationServletContextListener;
 
@IntegrationServletContextListener
public class MWServletContextListener implements ServletContextListener {
 
  private static final Logger LOGGER = Logger.getLogger(MWServletContextListener.class);
 
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    LOGGER.error("Initializing custom resources");
    CustomResourceInitializer.initialize();
  }
 
  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    LOGGER.error("Destroying custom resources");
    CustomResourceInitializer.destroy();
  }
 
}

Follow these steps to publish a custom ServletContextListener -

  1. Create a servlet context listener with @IntegrationServletContextListener annotation.

  2. Build a jar file for the class.

  3. Upload the jar file as a dependency to a service in Quantum Fabric Console.

  4. Publish the app.

NOTE: As the ServletContextListener is an application life cycle, the events will be invoked only once during startup and shutdown. After publish, the server must restart to invoke the contextInitialized event.