Portlet filters

Since the release of JSR 286: Portlet Specification 2.0, it is possible to intercept and manipulate the request and response before they are delivered to a portlet in a given phase. Using Portlet filters you can block the rendering of a portlet if a specific condition occurs. Also, you can use portlet filters to decorate a request and a response within a wrapper to modify the behavior of the portlet.

Portlet filter usage

To use the portlet filter feature, you must first complete the following actions:
  • Implement one or more of the following interfaces of the javax.portlet.filter package:
    • RenderFilter
    • ActionFilter
    • ResourceFilter
    • EventFilter
    You must implement the appropriate filter based on the method or phase of the portlet that you want to filter.
  • Register the filter within the portlet.xml file for the portlets in your web application.
The following sample code illustrates a portlet filter to screen the processAction method of a portlet:
package my.pkg;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.filter.ActionFilter;
import javax.portlet.filter.FilterChain;
import javax.portlet.filter.FilterConfig;

public class MyPortletFilter implements ActionFilter {
     public void init(FilterConfig config) throws PortletException {
          String myInitParameter = config.getInitParameter("myInitParameter");
          // ...
     }

     public void doFilter(ActionRequest request, ActionResponse response,
               FilterChain chain) throws IOException, PortletException {
          preProcess(request, response);
          chain.doFilter(request, response);
          postProcess (request, response);
     }

     private void preProcess(ActionRequest request, ActionResponse response) {
          //For example, create a javax.portlet.filter.PortletRequestWrapper here
     }

     public void destroy() {
          // free resources
     }
}
The following sample code illustrates how you can declare the previous portlet filter in the portlet.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" 
version="2.0" id="demo_app_id">
     <portlet >
          <portlet-name>MyPortlet1</portlet-name>
          <!-- [...] -->
     </portlet>

     <portlet >
          <portlet-name>MyPortlet2</portlet-name>
          <!-- [...] -->
     </portlet>

     <filter>
          <filter-name>PortletFilter</filter-name>
          <filter-class>my.pkg .MyPortletFilter</filter-class>
          <lifecycle>ACTION_PHASE</lifecycle>
          <init-param>
               <name>myInitParameter</name>
               <value>myValue</value>
          </init-param>
     </filter>

     <filter-mapping>
          <filter-name>PortletFilter</filter-name>
          <portlet-name>MyPortlet1</portlet-name>
          <portlet-name>MyPortlet2</portlet-name>
     </filter-mapping>

</portlet-app>
If you implement the RenderFilter interface, for example, add the <lifecycle>RENDER_PHASE</lifecycle> code to the filter section. This addition is analogous to the other filter interfaces. The following values are valid for the <lifecycle> parameter:
  • RESOURCE_PHASE
  • RENDER_PHASE
  • EVENT_PHASE
  • ACTION_PHASE



Related concepts
Portlets
Portlet container
Related tasks
Converting portlet fragments to an HTML document
Related information
JSR 286 Portlet Specification
Concept topic Concept topic    

Terms and conditions for information centers | Feedback

Last updatedLast updated: Feb 5, 2014 9:49:51 PM CST
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=compass&product=was-nd-mp&topic=cport_portlet_filters
File name: cport_portlet_filters.html