Applying Global filter to all the portlets running in WPS

The WebSphere Application Server makes use of OSGi model for tying different pieces of application server together. One advantage of this approach is that even you can use the extension points to extend functionality of websphere application server

Before few days i was trying to build a Portlet Filter that can be applied to existing WebSphere Portlet Filter and Jerome suggested that i should use portlet global filter, that way i don't have to touch the existing portlet. So i tried that and it worked, to me it seems that Global Filter can be one of the most powerful tool in the portlet developers toolbox

I built this sample HelloGlobalFilter while i was trying to learn how to use Global Filter. This filter is very simple only thing that it does is calculate time spent in render() method of every portlet and prints that time along with WindowId, you can easily change it to create generic performance monitoring filter. You can download sample code from here

This is how my HelloGlobalFilter.java looks like

package com.webspherenotes.portlet.filter;

import java.io.IOException;

import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.filter.FilterChain;
import javax.portlet.filter.FilterConfig;
import javax.portlet.filter.RenderFilter;

public class HelloGlobalFilter implements RenderFilter{
public void destroy() {
System.out.println("Inside HelloGlobalFilter.destroy()");

}
public void init(FilterConfig arg0) throws PortletException {
System.out.println("Inside HelloGlobalFilter.init()");
}

public void doFilter(RenderRequest request, RenderResponse response,
FilterChain filterChain)
throws IOException, PortletException {
System.out.println("Entering HelloGlobalFilter.doFilter()");
long beginTime = System.currentTimeMillis();
filterChain.doFilter(request, response);
System.out.println("Window ID " + request.getWindowID() +" Time to execute render() "
+ (System.currentTimeMillis() - beginTime));
System.out.println("Exiting HelloGlobalFilter.doFilter()");

}

}


Developing HelloGlobalFilter is like developing any other standard compliant Portlet Filter. In this case i am implementing RenderFilter interface and then inside the doFitler() method i am calculating time spent by the portlet in executing. Once the HelloGlobalFilter is developed i had to copy it in the shared library so that it can be accessed by portal across application.

Last step is to create plugin.xml file like this

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin id="com.webspherenotes.hello.filter" name="WS_Server"
provider-name="IBM" version="2.0.0">
<extension point="com.ibm.ws.portletcontainer.portlet-filter-config">
<portlet-filter-config
class-name="com.webspherenotes.portlet.filter.HelloGlobalFilter" order="100">
<description> Hello Global Filter, order = 101 </description>
<lifecycle>RENDER_PHASE</lifecycle>
</portlet-filter-config>
</extension>
</plugin>


In this case i am using com.ibm.ws.portletcontainer.portlet-filter-config extension point for injecting my custom HelloGlobalFilter. I want to apply the filter only to the RENDER_PHASE and that's the reason for value of lifecycle element. Copy the plugin.xml file in the root of the HelloGlobalFilter.

2 comments:

  1. Please also consult the official documentation on global portlet filters in WebSphere, especially the "avoid trouble" section: http://publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=%2Fcom.ibm.websphere.nd.doc%2Finfo%2Fae%2Fae%2Fcport_portlet_filters.html

    ReplyDelete