Create Servlet Filter in Liferay 7.2

 


Servlet filter provides a filter layer between the client and the servlet. When the client invokes the request, at that time servlet filter will perform the pre-process steps to filter the request before completing the request, also it will perform the post-processing steps before sending it to the client.

You can apply the filter on:

  • Logging
  • Auditing
  • Transaction management
  • Security

1. Create a module project:

Steps to create a Liferay module:
- Go to Liferay workspace project → modules → new.
- Select other → Liferay → Liferay Module Project and Click on "Next".
- Enter the project name "ThemerayServletFilter"
- Select “Project Template Name” as “war-hook” and click on "Next".



- Enter a Package name "com.themeray.servlet.filter", component Class Name "ThemerayServletFilter" and click on the “Finish”. 



The necessary file structure will be created as below.


2. Create a servlet filter class:

 Create a new Class "ServletFilter" with implemented "Filter" Class
  package com.themeray.servlet.filter;
  import com.liferay.portal.kernel.util.WebKeys;
  import java.io.IOException;
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  
  public class ServletFilter implements Filter {

      @Override
      public void init(FilterConfig filterConfig) throws ServletException {
          System.out.println("Called ServletFilter.init(" + filterConfig + ") where hello="
                  + filterConfig.getInitParameter("hello"));

      }

      @Override
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 
              FilterChain filterChain)throws IOException, ServletException {

          String uri = (String) servletRequest.getAttribute(WebKeys.INVOKER_FILTER_URI);

          System.out.println("Called ServletFilter.doFilter(" + servletRequest + ", " 
                  + servletResponse + ", "+ filterChain + ") for URI " + uri);

          filterChain.doFilter(servletRequest, servletResponse);

      }

      @Override
      public void destroy() {
      }

  }
  

Filter interface provides the following life cycle methods:

  • init(): It is used to initialize the filter.
  • doFilter(): It is used to perform filtering tasks.
  • destroy(): Clean up the filter’s unneeded resources.


3. Edit Liferay-hook.xml:

Add the mapping in liferay-hook.xml file:
  
      <?xml version="1.0"?>
  <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 7.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_7_2_0.dtd">

  <hook>
      <portal-properties>portal.properties</portal-properties>
      <servlet-filter>
          <servlet-filter-name>ServletFilter</servlet-filter-name>
          <servlet-filter-impl>com.themeray.servlet.filter.ServletFilter</servlet-filter-impl>
          <init-param>
              <param-name>hello</param-name>
              <param-value>world</param-value>
          </init-param>
      </servlet-filter>
      <servlet-filter-mapping>
      <servlet-filter-name>ServletFilter</servlet-filter-name>
          <url-pattern>/group/*</url-pattern>
          <url-pattern>/user/*</url-pattern>
          <url-pattern>/web/*</url-pattern>
          <url-pattern>*.jsp</url-pattern>
          <dispatcher>REQUEST</dispatcher>
          <dispatcher>FORWARD</dispatcher>
      </servlet-filter-mapping>
  </hook>
  

4. Deploy the module:

When you deploy the module you can see in the logs:




0 comments: