Foren

Session Attribute share from Portlet to Hook

thumbnail
Subhasis Roy, geändert vor 8 Jahren.

Session Attribute share from Portlet to Hook

Expert Beiträge: 275 Beitrittsdatum: 20.01.12 Neueste Beiträge
Hi,

I have set some attributes in Liferay Portlet and trying to get the same session object and the attribute inside hook. Its not happening.

Scenario explained below -

Portlet code:
HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(actionRequest);
HttpSession session = httpRequest.getSession();
session.setAttribute("userName", user.getScreenName());

Hook Code:
HttpSession session = request.getSession();
String myUserName = (String)session.getAttribute("userName");


But I am only getting NULL value as a result of userName attribute.

As per my understanding its creating a new session object inside Hook, instead of using the same session which I have created inside portlet.
Also I am not able to see the userName parameter in the session attributes list inside hook.

Also, I need to use this hook an portlet as 2 different war files in my case. Will the HttpSession work for two different war files?

Can any one please help me on this.
thumbnail
Olaf Kock, geändert vor 8 Jahren.

RE: Session Attribute share not working from Portlet to Hook

Liferay Legend Beiträge: 6403 Beitrittsdatum: 23.09.08 Neueste Beiträge
It depends where your hook code lives: A portal defines communication through the portlet session, not through the servlet session. If you implement a servlet in a hook, it will have its own session (check the servlet context name) that has nothing to do with the portal's session), thus you're seeing the expected behaviour here.
thumbnail
Subhasis Roy, geändert vor 8 Jahren.

RE: Session Attribute share from Portlet to Hook

Expert Beiträge: 275 Beitrittsdatum: 20.01.12 Neueste Beiträge
Olaf Kock:
It depends where your hook code lives: A portal defines communication through the portlet session, not through the servlet session. If you implement a servlet in a hook, it will have its own session (check the servlet context name) that has nothing to do with the portal's session), thus you're seeing the expected behaviour here.


So how can I resolve this issue.
Should I need to write the Hook project and Portlet project inside same war file?
And use PortletSession instead of HttpSession in both portlet and hook?
thumbnail
Subhasis Roy, geändert vor 8 Jahren.

[SOLVED] - RE: Session Attribute share from Portlet to Hook

Expert Beiträge: 275 Beitrittsdatum: 20.01.12 Neueste Beiträge
Hi Olaf,
Got it solved by using "LIFERAY_SHARED_" and using PortletSession.
I am using Liferay 6.2 SP10 in this case and portlet and hook are two differnet war files.

Portlet Code:
PortletSession session = actionRequest.getPortletSession();
session.setAttribute("LIFERAY_SHARED_userName", user.getScreenName(), PortletSession.APPLICATION_SCOPE);


Hook Code:
HttpSession session = httpRequest.getSession();
String userName = (String)session.getAttribute("LIFERAY_SHARED_userName");


Also added the <private-session-attributes>false</private-session-attributes> in the liferay-portlet.xml file

liferay-portlet.xml entry:
<portlet>
	<portlet-name>CustomLoginPortlet</portlet-name>
	<icon>/icon.png</icon>
	<private-session-attributes>false</private-session-attributes>
	<header-portlet-css>/css/main.css</header-portlet-css>
	<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
</portlet>


Thank you for your help.