• Portlet Filters In Liferay 7 DXP

    Portlet Filters In Liferay 7 DXP

    Portlet Filter is used to intercept and manipulate the request and response before it is delivered to the portlet at given life cycle such as action, render and resource

    Environment
    Liferay IDE 3.1.2 GA3
    Liferay CE Portal Tomcat 7 GA4
    JDK 8
    MySql 5.7

    What is Portlet Filter?

    Portlet Filter is used to intercept and manipulate the request and response before it is delivered to the portlet at given life cycle such as action, render and resource

    Types Of Filters For A Liferay Portlet

    Action Filter
    Render Filter
    Resource Filter
    Base Filter

    Which Filter To Use?

    Action Filter : To Intercept only The Action Requests
    Render Filter : To Intercept only The Render Requests
    Resource Filter : To Intercept only The Resource Requests
    Base Filter : To Intercept all the http request comes to your portlet.

    How to Implement Portlet Filter In liferay 7 DXP?


    Step 1 : Create A Module Project for Portlet
    If you want to know how to create a portlet module project in Liferay 7 DXP, please refer my earlier blog, Below is my Portlet Controller Class.


    Step 2 : Create A Portlet Filter Component Class
    You can create Portlet Filter Component Class Using Eclipse, by right clicking on you portlet module project and select select "New Component Class" and Select "Portlet Filter" Component Template, I prefer to create a class manually for now, I Am creating a Class named "MyRenderFilter"

    Step 3 : Component Declaration
    Declare the Class as an OSGI Component Using the Below Code Snippet

    import org.osgi.service.component.annotations.Component;
    import javax.portlet.filter.PortletFilter;
    @Component(immediate = true, 
     property = {
      "javax.portlet.name="+MyPortletKeys.My, 
     },
     service = PortletFilter.class
    )
    
    Note : Make sure you give correct portlet name and service must be PortletFilter.class

    Step 4 : Extend The RenderFilter
    import javax.portlet.filter.RenderFilter;
    public class MyRenderFilter implements RenderFilter{}
    

    Step 5 : Implement Unimplemented Methods of the Parent Class RenderFilter
    public void init();
    public void destroy();
    public void doFilter();
    

    Complete MyRenderFilter.java Code

    Each time when you Hit a Render URL doFilter() method the MyRenderFilter will be triggered.

    Portlet Filter For Action Request


    Similarly You can create the filter for all Action Request By extending ActionFilter Class, as shown below


    Portlet Filter For Resource Request


    For all Resource request you can extend your class with ResourceFilter Class, as shown below

    Servlet Filter In Liferay 7 DXP

    This Filter Intercepts all the requests comes to the portlet, whether it is action, render, resource and it also can be HttpServletRequest , if you want filter all the Http Servlet Request comes to your portlet please use this filter, your filter component just have to extend "BaseFilter" Class, refer the below code

    Note : This BaseFilter Will be Triggered Before all the other filters such as ActionFilter, RenderFilter and ResourceFilter.
    I have created a portlet which can easily make you understand flow of Portlet Filters and Portlet Requests In detail, please download the portlet and deploy to test, it can give more insight into the portlet Filters In Liferay, when you add this portlet to the page init() and doFilter() method will be triggered.


    Source Code


  • 3 comments:

    1. I have made changes on mine portlet to run filters but nothing is working except Servlet Filter,
      plz help me out related to filters.

      Thanks

      ReplyDelete
    2. Please give more details about your issue,which portlet filter you are using action,render or resource, download the source code and refer it... its a tested example make sure all the configurations done properly

      ReplyDelete
    3. In hopes that this might get answered...Is it possible to use the BaseFilter & Servlet Filter implementation to create a module that acts as a Filter for all other modules deployed at the portal? If yes, how should I go about it?

      Does the Servlet Filter actually have the ability to monitor all requests comming to the portal?

      ReplyDelete