Portlet Filters in Liferay 7

Liferay 7 comes with a lot of feature and each feature is worth to learn.  Portlet Filter is one of them. However, it is possible to write portlet filter in earlier versions but in Liferay 7 it has certain benefits.

As compared to earlier version Portlet Filter can be deployed as service in OSGI. There is no need to write filter in the same portlet plugin that is the ultimate benefit of Liferay 7 modular architecture, it gives us the possibility to apply filter on OOTB portlets with ease. This is beneficial where It can be applied and removed independently from the portlet without redeploying the portlet as the contracts between osgi services are loosely coupled.

 

In earlier version It is hard to apply filters on OOTB portlets. I think, it is not possible without EXT plugin. Taking advantage of OSGI in Liferay 7, Portlet Filter on OOTB portlet can be applied as a service and can be removed without need of the Portal downtime.

 

There can be following Portlet Filters.

a) Render Filter: -Render Filter mainly implements the doFilter(RenderRequest request, RenderResponse response, FilterChain chain) method of the the javax.portlet.filter.RenderFilter class.

b) Resource Filter: - Resource Filter implements the doFilter(ResourceRequest request, ResourceResponse response, FilterChain chain) method of the the javax.portlet.filter.ResourceFilter class.

c) Action Filter: - Action Filter mainly implements the doFilter(ActionRequest request, ActionResponse response, FilterChain chain) method of the the javax.portlet.filter.ActionFilter class.

d) Event Filter: - Event Filter mainly implements the doFilter(EventRequest request, EventResponse response, FilterChain chain) method of the the javax.portlet.filter.EventFilter class.

 

To define PortletFilter first step there is need to declare following properties that will identify this service as PortletFilter and on which portlet service it is applied. Suppose we want to apply filter on Login Portlet. It can be done just by writing an independent module that has following component class.

@Component(

                immediate = true,

                property = {

        "javax.portlet.name= com_liferay_login_web_portlet_LoginPortlet"

    },

    service = PortletFilter.class

)

public class MyLoginPortletFilter implements RenderFilter {

 

                @Override

                public void destroy() {

                }

 

                @Override

                public void doFilter(

                                                RenderRequest request, RenderResponse response, FilterChain chain)

                                throws IOException, PortletException {

 

                                System.out.println("Before filter action");

                                chain.doFilter(request, response);

                                System.out.println("After filter action");

                }

 

                @Override

                public void init(FilterConfig filterConfig) throws PortletException {

                }

}