Liferay Portlet Filter Tutorial

In tutorial, we will see “how to create a filter in custom portlet”. Filters are new in JSR-286 and there are there types of filters.

  1. Render Filter   – Render Filter will be executed in Render Phase only, means render request urls
  2. Action Filter –  Action Filter will be executed for Action Phase requests only
  3. Resource Filter – Resource Filter will be executed for Resource Phase requests only
  4. EventFilter – Event Filter will be executed for Event Requests only
  5. All these filters are defined in javax.portlet.filter package and you can PortletRequestWrapper, ActionRequestWrapper,RenderRequestWrapper classes to override PortletRequest ActionRequest, RenderRequest classes

Step1:  Create Filter class by implementing RenderFilter,ActionFilter, ResourceFilter

public class RenderFilter implements javax.portlet.filter.RenderFilter {
  public void destroy() {
      System.out.println("execute destrou");
  }
  public void init(FilterConfig arg0) throws PortletException {
        System.out.println("execute inti config");
  }
  public void doFilter(RenderRequest arg0, RenderResponse arg1,
    FilterChain arg2) throws IOException, PortletException {
    
            // Your logic goes here

   }
 }

Step2: Add Filter entry in portlet.xml

<?xml version="1.0"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
            <portlet>
              <portlet-name>TestFilter</portlet-name>
              <display-name>Test Filter</display-name>
              <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet </portlet-class>
              <init-param>
                     <name>view-template</name>
                     <value>/html/test/view.jsp</value>
              </init-param>
              <expiration-cache>0</expiration-cache>
              <supports>
                     <mime-type>text/html</mime-type>
                     <portlet-mode>view</portlet-mode>
              </supports>
              <portlet-info>
                     <title>TestFilter</title>
                     <short-title>TestFilter</short-title>
                     <keywords></keywords>
              </portlet-info>
              <security-role-ref>
                     <role-name>administrator</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>guest</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>power-user</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>user</role-name>
              </security-role-ref>
       </portlet>
       <filter>
              <filter-name>RenderPhaseFilter</filter-name>
              <filter-class>com.filter.RenderFilter </filter-class>
              <lifecycle>RENDER_PHASE</lifecycle>
        </filter>
 <filter-mapping>
    <filter-name>RenderPhaseFilter</filter-name>
    <portlet-name>TestFilter</portlet-name>
 </filter-mapping>
</portlet-app>

Step3: Map the filter to Portlet

In this provide portlet name and filter name in the filter-mapping section

 

<filter-mapping>
  <filter-name>RenderPhaseFilter</filter-name>
   <portlet-name>TestFilter</portlet-name>
</filter-mapping>

Above portlet.xml contains all necessary configuration and you can cross verify with that. Hope this helps!!!!

Leave a Comment

Your email address will not be published. Required fields are marked *