Foren

Liferay session variables

thumbnail
Francois Damiens, geändert vor 12 Jahren.

Liferay session variables

Junior Member Beiträge: 27 Beitrittsdatum: 04.08.11 Neueste Beiträge
Hi,

I would like to put a new variable for a session in Liferay, how to do that ? The variable has to be reachable from every portlet during the session.

Thanks.
thumbnail
Leo Pratlong, geändert vor 12 Jahren.

RE: Liferay session variables

Expert Beiträge: 363 Beitrittsdatum: 06.07.10 Neueste Beiträge
Use PortletSession:


final PortletSession psession = request.getPortletSession();
psession.setAttribute(key, value, PortletSession.APPLICATION_SCOPE);
//

// READ
psession.getAttribute(key, PortletSession.APPLICATION_SCOPE);



Use a prefix "LIFERAY_SHARED_" for your key, so your variable will be available for all the portlets (those which share their session with the other and those which not).
If you need to get the resource from the portal, use HttpSession and not PortletSession anymore.

Edit: fix some typing errors.
thumbnail
Samir Gami, geändert vor 12 Jahren.

RE: Liferay session variables

Regular Member Beiträge: 162 Beitrittsdatum: 04.02.11 Neueste Beiträge
Agree with Leo....

Also you can define your own prefix,

 #
    # Portlets that have been configured to use private request attributes in
    # liferay-portlet.xml may still want to share some request attributes. This
    # property allows you to configure which request attributes will be shared.
    # Set a comma delimited list of attribute names that will be shared when the
    # attribute name starts with one of the specified attribute names. For
    # example, if you set the value to "hello_,world_", then all attribute names
    # that start with "hello_" or "world_" will be shared.
    #
    request.shared.attributes=LIFERAY_SHARED_
thumbnail
Francois Damiens, geändert vor 12 Jahren.

RE: Liferay session variables

Junior Member Beiträge: 27 Beitrittsdatum: 04.08.11 Neueste Beiträge
Leo Pratlong:


Use a prefix "LIFERAY_SHARED_" for your key, so your variable will be available for all the portlets (those which share their session with the other and those which not).
If you need to get the resource from the portal, use HttpSession and not PortletSession anymore.


Ok thanks, actually it could look like this :

String myVar = "123456789";
final PortletSession psession = request.getPortletSession();
psession.setAttribute("LIFERAY_SHARED_PASS", myVar, PortletSession.APPLICATION_SCOPE);

// READ
psession.getAttribute("LIFERAY_SHARED_PASS", PortletSession.APPLICATION_SCOPE);


Would it be ok ?
thumbnail
Leo Pratlong, geändert vor 12 Jahren.

RE: Liferay session variables

Expert Beiträge: 363 Beitrittsdatum: 06.07.10 Neueste Beiträge
Yes, that looks OK.
Don't forget that, when you read, you use PortletSession when you are in portlet and HttpSession when you are in portal.
PortletSession exists for the PortletContainer. It's a kind of suremcapsulation of the HttpSession.

Maybe, change "PASS" by a more complex keyword. I'm not sure it's not ever used by Liferay variables.
thumbnail
Francois Damiens, geändert vor 12 Jahren.

RE: Liferay session variables

Junior Member Beiträge: 27 Beitrittsdatum: 04.08.11 Neueste Beiträge
Leo Pratlong:
Yes, that looks OK.
Don't forget that, when you read, you use PortletSession when you are in portlet and HttpSession when you are in portal.

Maybe, change "PASS" by a more complex keyword. I'm not sure it's not ever used by Liferay variables.


Ok, but in fact, I met a new problem. I want to "write" the session variable in a class called in the authentification pipeline, so I don't have any :

request.getPortletSession();


Is there another way ??
thumbnail
Leo Pratlong, geändert vor 12 Jahren.

RE: Liferay session variables

Expert Beiträge: 363 Beitrittsdatum: 06.07.10 Neueste Beiträge
Do you have the HttpSession ? (request.getSession() for example) ? If you are in the portal, you can find the variables from PortletSession in the HttpSession.
If you don't have it (in a service for example), find a way to propagate the value from the request entry (an action for example) to your authentication service, by redefining the method prototype.
thumbnail
Francois Damiens, geändert vor 12 Jahren.

RE: Liferay session variables

Junior Member Beiträge: 27 Beitrittsdatum: 04.08.11 Neueste Beiträge
Leo Pratlong:
Do you have the HttpSession ? (request.getSession() for example) ? If you are in the portal, you can find the variables from PortletSession in the HttpSession.
If you don't have it (in a service for example), find a way to propagate the value from the request entry (an action for example) to your authentication service, by redefining the method prototype.


It would be diffocult to explain the whole problem but I'll try to sum up :

- in my authentification pipeline, I call my own class define in properties (auth.pipeline.pre=com.liferay.portal.security.auth.MyLoginAuth)
- in this class, i would like to add the session variables, but it's not a portlet so no "request.getPortletSession()".

How, in a simple Java class in the lib (btw, implementing the interface authenticator), can I set variables session ??
thumbnail
Leo Pratlong, geändert vor 12 Jahren.

RE: Liferay session variables

Expert Beiträge: 363 Beitrittsdatum: 06.07.10 Neueste Beiträge
A bad bad bad way should be to make a Singleton which contains a Map. As key, you put the user session id or the user login, and as value you put the variable you need to share.
But that's ugly.
It should be a better way to do that.
Don't you have any actionRequest, RenderRequest or HttpServletRequest object?
thumbnail
Sagar A Vyas, geändert vor 12 Jahren.

RE: Liferay session variables

Liferay Master Beiträge: 679 Beitrittsdatum: 17.04.09 Neueste Beiträge
Francois Damiens:
Hi,

I would like to put a new variable for a session in Liferay, how to do that ? The variable has to be reachable from every portlet during the session.

Thanks.



Hi,

Two thing need to be done

1) As mention by Leo
2) need set value in liferay -portlet.xml
<private-session-attributes>false</private-session-attributes>

Thanks,
Sagar Vyas
thumbnail
Leo Pratlong, geändert vor 12 Jahren.

RE: Liferay session variables

Expert Beiträge: 363 Beitrittsdatum: 06.07.10 Neueste Beiträge
Sagar A Vyas:

2) need set value in liferay -portlet.xml
<private-session-attributes>false</private-session-attributes>


Hi,
thanks for your help.
I think his problem is not relative to the session privacy. He does not have the request in his class. So, how to share resource with portlets? If the "entry point" of its class can not pass a request, he will not be able to get the PortletSession or the HttpSession.
The Singleton should be a solution, unless there is a way in Liferay to do otherwise.
thumbnail
Sagar A Vyas, geändert vor 12 Jahren.

RE: Liferay session variables

Liferay Master Beiträge: 679 Beitrittsdatum: 17.04.09 Neueste Beiträge
how to share resource with portlets? If the "entry point" of its class can not pass a request, he will not be able to get the PortletSession or the HttpSession.


Hi Leo,

Thanks for Reply,

Seems very interesting.

I don't think so its possible even if in servlet and jsp to get some parameter without any request object.

- in my authentification pipeline, I call my own class define in properties (auth.pipeline.pre=com.liferay.portal.security.auth.MyLoginAuth)
- in this class, i would like to add the session variables, but it's not a portlet so no "request.getPortletSession()".


Francois , You have to have some method which will pass request parameter to your class.

Sharing some global variable withing or application which will contains all required resource would be not thread safe at all.

Thanks,
Sagar Vyas
thumbnail
Francois Damiens, geändert vor 12 Jahren.

RE: Liferay session variables

Junior Member Beiträge: 27 Beitrittsdatum: 04.08.11 Neueste Beiträge
Leo Pratlong:

Hi,
thanks for your help.
I think his problem is not relative to the session privacy. He does not have the request in his class. So, how to share resource with portlets? If the "entry point" of its class can not pass a request, he will not be able to get the PortletSession or the HttpSession.
The Singleton should be a solution, unless there is a way in Liferay to do otherwise.


Yes, I only see this solution for the moment. Thanks.