Foren

RE: Session destroy event / How to access user in order to clean up?

thumbnail
Ay Kay, geändert vor 10 Jahren.

Session destroy event / How to access user in order to clean up?

Junior Member Beiträge: 52 Beitrittsdatum: 17.11.11 Neueste Beiträge
Hi there.

Pre-note: Feel reluctant to post, especially since the forum gets bombarded these days, but I simply cannot find a solution from the archives.

My case:
After a liferay user ends his / her session I need my portlet to clean up some data. Hence, I introduced a hook being triggered by a
[indent]servlet.session.destroy.events[/indent]
defined in docroot/WEB-INF/src/portal.properties. The class I mention in that file extends com.liferay.portal.kernel.events.SessionAction. Liferay IDE conveniently creates all this. The method public void run(HttpSession httpSession) gives me the HttpSession which we all know has nothing to do with the PortletSession. So all information on the user that just logged out is lost.

See, I have only one portlet, means one WAR, means one realm for attributes, scopes, etc.
Therefore I guess, I can sidestep all hints on the INet focusing on Session Sharing such as this, this and this since that technique described targets portlet to portlet communication. Right?

With my one and only portlet, I figured, it must be easy to access user information, etc. Wrong! I am facing the same probs these other poor guys do. So how can I access or transfer these data (user, etc.) to my hook? So far in my VAADIN portlet I tried:

			HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(request);
			HttpServletRequest originalHttpServletRequest = (HttpServletRequest)PortalUtil.getOriginalServletRequest(httpServletRequest);
			HttpSession httpSession = originalHttpServletRequest.getSession();
			httpSession.setAttribute("userId", this.currentUser.getUserId());


And in the hook's run method I tried to access it via:

		System.out.println("User ID: " + httpSession.getAttribute("userId"));


But it's null.

Really appreciate your professional input since we're totally stuck here.
Thanks for listening!
thumbnail
Ay Kay, geändert vor 10 Jahren.

RE: Session destroy event / How to access user in order to clean up?

Junior Member Beiträge: 52 Beitrittsdatum: 17.11.11 Neueste Beiträge
I found a solution inspired by Petr Vašek in this thread. He just puts the data in a singleton hashmap storage. Works like a charm, solved my problem.

Since I am very unsure about scopes here in my portlet, I played things save by storing values according to their HTTP session ID. I made sure that the portlet AND the hook do really address the same session ID when getting hold of HttpSession beforehand.

Here is the code.

PortletStorage.java
import java.util.HashMap;

public class PortletStorage
{
	private static PortletStorage						instance	= null;
	private HashMap<string, hashmap<string, object>&gt;	storage		= null;

	private PortletStorage()
	{
		storage = new HashMap<string, hashmap<string, object>&gt;();
	}

	public static PortletStorage getInstance()
	{
		if (instance == null)
			instance = new PortletStorage();

		return instance;
	}

	public void setValue(String sessionId, String key, Object value)
	{
		HashMap<string, object> sessionValueMap = storage.get(sessionId);

		boolean isNew = sessionValueMap == null;
		if (isNew)
			sessionValueMap = new HashMap<string, object>();

		sessionValueMap.put(key, value);
		if (isNew)
			storage.put(sessionId, sessionValueMap);
	}
	
	public Object getValue(String sessionId, String key)
	{
		HashMap<string, object> sessionValueMap = storage.get(sessionId);
		
		if (sessionValueMap == null)
			return null;
		
		return sessionValueMap.get(key);
	}

	public void clearSession(String sessionId)
	{
		storage.remove(sessionId);
	}
}
</string,></string,></string,></string,></string,>


in VAADIN portlet
			HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(this.request);
			HttpServletRequest originalHttpServletRequest = (HttpServletRequest)PortalUtil.getOriginalServletRequest(httpServletRequest);
			HttpSession httpSession = originalHttpServletRequest.getSession();
			PortletStorage portletStorage = PortletStorage.getInstance();
			portletStorage.setValue(httpSession.getId(), "userId", this.currentUser.getUserId());


in my hook
	public void run(HttpSession httpSession) throws ActionException
	{
		PortletStorage portletStorage = PortletStorage.getInstance();
		Long userId= (Long)portletStorage.getValue(httpSession.getId(), "userId");
		portletStorage.clearSession(httpSession.getId());
		System.out.println("User ID: " + userId);
		System.out.println("Elvis has left the building.");
	}