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:

Create Scheduler in Liferay 7

 



The goal of this tutorial is to create a scheduler in Liferay 7. Results of tasks will be used in wide variety of applications - data annotation, multimedia, sentiment analysis and search engine result evaluation.

You need to create a Liferay module project in eclipse IDE.

1. Create scheduler class:

Create a new class in your module named EmailSchedulear and add the cron expression


	
    package com.themeray.emailschedulear.schedulear;
    
    import com.liferay.portal.kernel.messaging.BaseMessageListener;
    import com.liferay.portal.kernel.messaging.Message;

    import org.osgi.service.component.annotations.Component;

    @Component(
        immediate = true,
        property = {
            "cron.expression= 0 0/05 1/1 1/1 * ?"   // scheduler runs every 5 min.
        },
        service = EmailSchedulear.class
    )

    public class EmailSchedulear extends BaseMessageListener {

        @Override
        protected void doReceive(Message message) throws Exception {
            // TODO Auto-generated method stub

        }
    }
    

In this Class you can specify your cron expression in ‘cron.expression’ property. Cron expressions are based on the timezone of your server. More about corn expression here https://www.freeformatter.com/cron-expression-generator-quartz.html

2. Override the doReceive method in your scheduler class:

This method will be called periodically based on your cron expression. You can write your business logic here which you want to execute periodically. i.e. you can write email sending and newsletter sending code here.


@Override
protected void doReceive(Message message) throws Exception {
    log.info("Scheduled task executed...");
}

3. Register The scheduler:


@Activate
@Modified
protected void activate(Map<String, Object> properties) throws SchedulerException {
 
    try {
        String cronExpression = GetterUtil.getString(properties.get("cron.expression"), "cronExpression");
        log.info(" cronExpression: " + cronExpression);
 
        String listenerClass = getClass().getName();
        Trigger jobTrigger = TriggerFactoryUtil.createTrigger(listenerClass, listenerClass, new Date(), null, cronExpression);
 
        SchedulerEntryImpl schedulerEntryImpl = new SchedulerEntryImpl();
        schedulerEntryImpl.setEventListenerClass(listenerClass);
        schedulerEntryImpl.setTrigger(jobTrigger);
 
        SchedulerEngineHelperUtil.register(this, schedulerEntryImpl, DestinationNames.SCHEDULER_DISPATCH);
 
    } catch (Exception e) {
        log.error(e);
    }
}

4. Deactivate the scheduler:


@Deactivate
protected void deactive() {
    SchedulerEngineHelperUtil.unregister(this);
}


0 comments:

EU Cookie Agreement module for Liferay 7.3

 


This module allows the management of banners warning the user about profiling cookies on the website, or by any third part.
This banner will appear to the users at the first access to the page.

Download the module from Themeray: https://www.themeray.com/themes/eu-cookie-agreement





0 comments: