Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Brian Scott Schupbach
Create filter that sets cookie to change theme (bring in style sheet)
June 12, 2013 11:23 AM
Answer

Brian Scott Schupbach

Rank: Expert

Posts: 331

Join Date: October 23, 2008

Recent Posts

From the research I've done, this is a little more complicated than I originally thought...

I have created a filter hook and would like to set a cookie based upon a session attribute set in an email gathering portlet. Once I am able to do that, I think bringing in the additional style sheet won't be an issue. The problem I'm having is reading the session attribute set in the portlet from my filter hook and also setting a cookie in the filter hook that the rest of my portlets can read. Has anyone had any luck sharing a session attribute with a filter hook?

The filter hook only has the ServletRequest object. I try to get the HttpServletRequest object like this:

HttpServletRequest request = (HttpServletRequest) servletRequest;

The portlet has the PortletRequest object and I'm getting the HttpServletRequest like this

HttpServletRequest req = PortalUtil.getHttpServletRequest(request); //where request is the PortletRequest
HttpSession session = req.getSession();
session.setAttribute("email", email);

The session object in the portlet is not the same as in the filter from what I can tell and I'm getting the session from the HttpServletRequest in both. I'm not sure where to go from here ..

Thanks in advance,

Brian
Milen Dyankov
RE: Create filter that sets cookie to change theme (bring in style sheet)
June 12, 2013 12:00 PM
Answer

Milen Dyankov

LIFERAY STAFF

Rank: Regular Member

Posts: 219

Join Date: October 30, 2012

Recent Posts

I'm not sure I understand what you are trying to do, but why don't you set the cookie you need from your portlet?
Brian Scott Schupbach
RE: Create filter that sets cookie to change theme (bring in style sheet)
June 12, 2013 12:06 PM
Answer

Brian Scott Schupbach

Rank: Expert

Posts: 331

Join Date: October 23, 2008

Recent Posts

I've tried that and the filter cannot read the cookie that the portlet sets. Maybe trying to correct that issue is the better way to solve the problem.

However, I've been reading some interesting articles from people who are suggesting setting an attribute in ThreadLocal

http://www.liferay.com/web/shuyang.zhou/blog/-/blogs/master-your-threadlocals

http://www.liferay.com/community/forums/-/message_boards/message/1294965
Brian Scott Schupbach
RE: Create filter that sets cookie to change theme (bring in style sheet)
June 12, 2013 12:23 PM
Answer

Brian Scott Schupbach

Rank: Expert

Posts: 331

Join Date: October 23, 2008

Recent Posts

After having trouble setting a cookie within the portlet, I've read about a lot of people who say the only possible way to set a cookie from a portlet is by sending back some javascript that sets the cookie on the client machine:

http://stackoverflow.com/questions/3749199/how-to-set-a-cookie-in-liferay-portlet

http://www.liferay.com/community/forums/-/message_boards/message/16889198
Brian Scott Schupbach
RE: Create filter that sets cookie to change theme (bring in style sheet)
June 12, 2013 1:58 PM
Answer

Brian Scott Schupbach

Rank: Expert

Posts: 331

Join Date: October 23, 2008

Recent Posts

Apparently you can set a cookie within a portlet by using renderResponse.setProperty. However, it appears as if there is a bug

http://issues.liferay.com/browse/LPS-25733