Portlet Filter in Liferay 7.2
Liferay 7.2 - Portlet Filter Sample
Overview
This article describes how to create custom portlet filter in Liferay 7.2.
Note: Portet Filters are similar to Servlet Filters, but they are invoked in scope of portlet and, thus, use
javax.portlet.PortletRequest
/ javax.portlet.PortletResponse
(instead of javax.servlet.ServletRequest
/ javax.servlet.ServletResponse
).Why do you need it?
Portlet filter may be used in the following scenarios:
- for custom portlet: to simplify portet's and MVCCommand's code;
- for customization Liferay's OOTB portlets: customization portlets code may be tricky, and you may use portlet filters to inject custom functionality.
Portlet Filter creation
- Create
service
Liferay module for the filter class. - Define dependencies in
build.gradle
file:dependencies { compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "4.0.0" compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0" compileOnly group: "javax.portlet", name: "portlet-api", version: "3.0.0" compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1" compileOnly group: "jstl", name: "jstl", version: "1.2" compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.4.0" }
- Define OSGi metadata in
bnd.bnd
, sample:Bundle-Name: LifeDev AssetPublisher Configuration Portlet Filter Bundle-SymbolicName: com.lifedev.asset.publisher.filter Bundle-Version: 1.0.0
- Implement filter class, sample:
@Component( immediate = true, property = { "javax.portlet.name=com_liferay_portlet_configuration_web_portlet_PortletConfigurationPortlet", "service.ranking:Integer=101" }, service = PortletFilter.class ) public class AssetPublisherConfigurationActionFilter implements ActionFilter { @Override public void init(FilterConfig filterConfig) throws PortletException { _log.info("AssetPublisherConfigurationActionFilter => init."); } @Override public void doFilter(ActionRequest actionRequest, ActionResponse actionResponse, FilterChain filterChain) throws IOException, PortletException { try { _log.info("AssetPublisherConfigurationActionFilter => doFilter."); //todo: filter logic here } catch (Exception e) { _log.error(e.getMessage(), e); } finally { // Invoke the rest of the filters in the chain filterChain.doFilter(actionRequest, actionResponse); } } @Override public void destroy() { _log.info("AssetPublisherConfigurationActionFilter => destroy."); } private static final Log _log = LogFactoryUtil.getLog(AssetPublisherConfigurationActionFilter.class); }
- javax.portlet.name= - portlet name, for which filter is being created (
com_liferay_portlet_configuration_web_portlet_PortletConfigurationPortlet
in this example), - service.ranking ranking (priority) of filter's OSGi component.
Comments
Post a Comment