Plugin Tutorial: How to create an interceptor

An interceptor is a code that does something before and after a request. In this tutorial I will illustrate how to build a simple request profiler.

Create the plugin class

To create an interceptor plugin we need to extend the “HttpInterceptorPlugin” class. Two methods must be implemented: “preHandle” and “postHandle”. The first one executes before a request and the last executes after a request.

package mycompany;

import com.enonic.cms.api.plugin.HttpInterceptorPlugin;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpInterceptorExample
  extends HttpInterceptorPlugin
{
  public boolean preHandle(HttpServletRequest req, HttpServletResponse res)
  {
    req.setAttribute("startTime", System.currentTimeMillis());
    return true;
  }

  public void postHandle(HttpServletRequest req, HttpServletResponse res)
  {
    long startTime = (Long)req.getAttribute("startTime");
    long totalTime = System.currentTimeMillis() - startTime;
    System.out.println("Reqest [" + req.getRequestURL().toString() + 
      "] took [" + totalTime + "] ms");
  }
}

You see in the example that I return true in “preHandle”. This means that the request is allowed to be passed down the chain and eventually render some page. If false is returned, then the request is returned right away (acts as a controller).

All the code created should be packaged into a jar file. This jar file has no name restrictions, but in my example I will call it "interceptor-plugin.jar”.

Creating the configuration

Before you can deploy this plugin to the server, you have to create a deployment configuration for this plugin. The descriptor uses spring configuration and you can incject the plugin with all sorts of external beans.

The configuration file name must end with “-plugin.xml” to be recognized by the plugin loader. In this example I will call it "interceptor-plugin.xml”.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  <bean class="mycompany.HttpInterceptorExample">
    <property name="urlPattern" value="/site/[0-3]/slow/.*"/>
    <property name=”priority” value=”0”/>
  </bean>

</beans>

All http based plugins has two settings: “urlPattern” and “priority”. The “urlPattern” is a regular expression that is used to match which requests that should be intercepted. The “priority” is used if you have multiple interceptors and want a certain order.

Deploy the plugin

Now that you have "interceptor-plugin.jar” and "interceptor-plugin.xml” we can deploy it to the server. All files must be placed under $CMS_HOME/plugins directory. You can place any sub-directory into this directory, so all files under any sub-directory will be searched.

I place the files right into the root of $CMS_HOME/plugins directory. Restart the application and the plugins will be available. You can go to the front page of Vertical Site to check what kind of plugins where loaded.

If you need any additional jar files available, you just put it into the plugins directory to be loaded in Vertical Site.

Using the plugin

You can now test the plugin by entering one of the urls that matches the pattern you defined in the configuration.

Comments

If you want to comment on this article you need to be logged in.

Published in 2011

2010

2009

2008

2007