Overriding Events: Working with PreActions and PostActions in EXT

Community Blogs 16 de abril de 2013 Por Apoorva Prakash

Liferay provides us facility to execute some code before and after of some specific events, namely Login, Logout and Service. Login and Logout are self-explanatory, Service is the event that takes place before or after every Action. For instance, If we have CustomServicePreAction and CustomLoginPreAction, in that case CustomServicePreAction will be called first then CustomLoginPreAction and then the LoginAction class will be invoked.

To achieve this, we need to make some entry in the portal-ext.properties, however some values are already present in portal.properties file, we have to update those properties, as following:

servlet.service.events.pre=com.liferay.portal.events.ServicePreAction, com.liferay.portal.events.CustomServicePreAction servlet.service.events.post=com.liferay.portal.events.ServicePostAction, com.liferay.portal.events.CustomServicePostAction

login.events.pre=com.liferay.portal.events.LoginPreAction, com.liferay.portal.events.CustomLoginPreAction login.events.post=com.liferay.portal.events.LoginPostAction, com.liferay.portal.events.DefaultLandingPageAction, com.liferay.portal.events.CustomLoginPostAction

logout.events.pre=com.liferay.portal.events.LogoutPreAction, com.liferay.portal.events.CustomLogoutPreAction logout.events.post=com.liferay.portal.events.LogoutPostAction, com.liferay.portal.events.DefaultLogoutPageAction, com.liferay.portal.events.SiteMinderLogoutAction, com.liferay.portal.events.CustomLogoutPostAction
 
So the custom classes we need to create are listed below:
  1. com.liferay.portal.events.CustomServicePreAction, 
  2. com.liferay.portal.events.CustomServicePostAction, 
  3. com.liferay.portal.events.CustomLoginPreAction, 
  4. com.liferay.portal.events.CustomLoginPostAction,
  5. com.liferay.portal.events.CustomLogoutPreAction, and 
  6. com.liferay.portal.events.CustomLogoutPostAction.

 

Now we have to add the following classes in Ext:

1. CustomServicePreAction

package com.liferay.portal.events; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.events.Action;
/**
* @author apoorva.prakash
**/

 public class CustomServicePreAction extends Action { 
    public void run(HttpServletRequest request, HttpServletResponse response) 
     throws ActionException {    
       System.out.println("this is custom service preaction"); 
    } 
}
 
2. CustomServicePostAction
package com.liferay.portal.events;  

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import com.liferay.portal.kernel.events.Action;  

/**
* @author apoorva.prakash 
**/

 public class CustomServicePostAction extends Action { 
     public void run(HttpServletRequest request, HttpServletResponse response) 
       throws ActionException {  
        System.out.println("this is custom service post action");  
     }
}
 
3. CustomLoginPreAction
package com.liferay.portal.events;

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import com.liferay.portal.kernel.events.Action; 
import com.liferay.portal.kernel.events.ActionException;  

/**
* @author apoorva.prakash 
**/

public class CustomLoginPreAction extends Action { 
    public void run(HttpServletRequest request, HttpServletResponse response) 
        throws ActionException { 
         System.out.println("this is CustomLoginPreAction..."); 
    } 
}
 
 
4. CustomLoginPostAction
package com.liferay.portal.events; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import com.liferay.portal.kernel.events.Action; 
import com.liferay.portal.kernel.events.ActionException; 
import com.liferay.portal.kernel.log.Log; 
import com.liferay.portal.kernel.log.LogFactoryUtil; 
/** 
* @author apoorva.prakash 
**/

public class CustomLoginPostAction extends Action { 
    public void run(HttpServletRequest request, HttpServletResponse response) 
        throws ActionException { 
    System.out.println("this is CustomLoginPostAction..."); 
   } 
}
 
5. CustomLogoutPreAction
package com.liferay.portal.events;

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import com.liferay.portal.kernel.events.Action; 
import com.liferay.portal.kernel.events.ActionException; 
/** 
* @author apoorva.prakash 
* */

public class CustomLogoutPreAction extends Action { 
   public void run(HttpServletRequest request, HttpServletResponse response) 
      throws ActionException { 
     System.out.println("this is CustomLogoutPreAction");  
   } 
}
 
6. CustomLogoutPostAction
package com.liferay.portal.events;  

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import com.liferay.portal.kernel.events.Action; 
import com.liferay.portal.kernel.events.ActionException; 
 /** 
* @author apoorva.prakash 
**/

public class CustomLogoutPostAction extends Action { 
   public void run(HttpServletRequest request, HttpServletResponse response) 
            throws ActionException { 
       System.out.println("this is CustomLogoutPostAction"); 
   } 
}

And now, run your code. It will work. :)

This article focuses implementation of events customization purely with EXT-plugins, however the same can be achieved with the help of hooks also.

Mostrando 1 resultado.