Overriding Events: Working with PreActions and PostActions in EXT

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.

Blogs
Would like to add some points here:

1. You mentioned "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"

I think this sends out message that these properties should be added to main portal-ext.properties under [LIFERAY_HOME] or use the one with EXT, however all of these event properties can be added using any portal properties hook.

2. You mentioned "Now we have to add the following classes in Ext:"

This also can be avoided as use of EXT requires server restart. However these custom event classes can be added in the same hook & those classes can be mentioned/extended in the hook's portal properties file.

Cheers & happy blogging emoticon
Hi Jay, firstly thanks for reading.
As the title of the article says, I was focusing evants customization particularly using Ext plugin. I was thinking to write a dedicated article for customisation of language properties and portal properties using hooks.
As far as "addition of properties in portal-ext.properties" sentance is concerned, I suppose that those properties exist in portal.properties in portal jar, not in portal-ext.properties (which is custom made) and both are physically different files and the file portal-ext.properties is a custom file which may not include those entries.
Extremely sorry for any ambiguity.
Hi Jay, firstly thanks for reading.
As the title of the article says, I was focusing evants customization particularly using Ext plugin. I was thinking to write a dedicated article for customisation of language properties and portal properties using hooks.
As far as "addition of properties in portal-ext.properties" sentance is concerned, I suppose that those properties exist in portal.properties in portal jar, not in portal-ext.properties (which is custom made) and both are physically different files and the file portal-ext.properties is a custom file which may not include those entries.
Extremely sorry for any ambiguity.
Hi Jay, firstly thanks for reading.
As the title of the article says, I was focusing evants customization particularly using Ext plugin. I was thinking to write a dedicated article for customisation of language properties and portal properties using hooks.
As far as "addition/updation of properties in portal-ext.properties" sentance is concerned, I suppose that those properties exist in portal.properties in portal jar, not in portal-ext.properties (which is custom made) and both are physically different files and the file portal-ext.properties is a custom file which may not include those entries.
Extremely sorry for any ambiguity.
No problem at all Apoorva, I got it that you wanted to explain this event customization piece in "EXT". But since EXT use is NOT preferred & the complete scenario you explained above including portal properties for event & Extended Action classes can be created in hook, the bottom line is that it should not send out wrong message to new Liferay user that this can be done only in "EXT".

Cheers emoticon
Very thoughtful Jay. I'll mention this also in post. Thanks.
Hi Apoorva

I'm trying to implement same with hook, but it's not working for me.

here is my class:

public class LogoutPreAction extends Action {

@Override
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
System.out.println("Logout Action...................");
}
}

liferay-hook.xml:

<hook>
<portal-properties>portal.properties</portal-properties>
</hook>

portal.properties:
logout.events.post=com.liferay.examples.event.LogoutPreAction

print lin statement not getting printed, all i'm seeing in logs is

16:43:00,049 INFO [tomcat-http--12][PortalLogoutAction:20] Inside PortalLogoutAction...
16:43:00,052 INFO [tomcat-http--12][PortalLogoutAction:38] End PortalLogoutAction

Please let me know what's missing here.
I am just curious.. Yes, We can write Pre and Post actions using Hooks..And if we want to take pain we can use Ext as mentioned in this article.
But can we do it using LIFERAY_HOME\portal-ext.properties and placing these Action classes in server common lib library? Can liferay load the classes and execute them?

Hi, Is it possible to stop the logout action in the pre-logout event? In the security area they tell me that the url "/ c / portal / logout" is insecure and the solution I am giving is to add a parameter to the url and I only have to close the session if you have that parameter. Example: / c / portal / logout? p_auth = aasdsadas. Any help to stop the logout in this event please.