Search code examples
liferayliferay-6liferay-velocity

Cannot pass PortletSession to CustomUtil from Portlet Velocity templates


I have checked on Using custom services or liferay services in liferay themes (velocity templates)?

and Custom Methods are working fine as they are. But I have some specific requirement.

Please let me know if you know how to pass PortletSession object or RenderRequest to Custom method.

E.g.

I have tried to create the following interface:

package com.myTool;


import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;

public interface MyTest{
  public String getUserCountry(PortletSession portletSession);
  public String getUserCountry(RenderRequest request);
}

In class that implements interface I have the following methods:

public String getUserCountry(PortletSession portletSession) {
        try {
            myUtil.setPortletSession(portletSession);
            return "Success";
        }
        catch (Exception e) {
            e.printStackTrace();
            return "exception-portletSession";
        }

    };

    public String getUserCountry(RenderRequest request) {
        try {
            myUtil.setPortletSession(request.getPortletSession());
            return "Success request";
        }
        catch (Exception e) {
            e.printStackTrace();
            return "exception-renderRequest";
        }

    };

I have tried the following ways with no luck:

1)

#set($currentProfileUtil = $utilLocator.findUtil("myportlets-1.0-SNAPSHOT", "com.myTool.MyTest"))
#set($result = $currentProfileUtil.getUserCountry($request.getAttribute("javax.portlet.response")))
$result

2)

#set($currentProfileUtil = $utilLocator.findUtil("myportlets-1.0-SNAPSHOT", "com.myTool.MyTest"))
#set($result = $currentProfileUtil.getUserCountry($request.get("portlet-session")))
$result

3)

#set($currentProfileUtil = $utilLocator.findUtil("myportlets-1.0-SNAPSHOT", "com.myTool.MyTest"))
#set($result = $currentProfileUtil.getUserCountry($request.getSession()))
$result

4)

#set($currentProfileUtil = $utilLocator.findUtil("myportlets-1.0-SNAPSHOT", "com.myTool.MyTest"))
#set($result = $currentProfileUtil.getUserCountry($request.getSession()))
$result

P.S. Please do not say "why do you need this?" - just provide any ideas on how to send PortletSession or any object from which PortletSession can be retrieved. Thanks


Solution

  • Answer was provided on Liferay Forum (this is not PortletSession but HttpSession, but that was OK for my case):

    http://www.liferay.com/web/raymond.auge/blog/-/blogs/6051129 (comment from Ray Augé dated 11/30/12 9:54 AM)

    In my case (from original question), solution was the following:

    #set($serviceContext = $portal.getClass().forName("com.liferay.portal.service.ServiceContextThreadLocal").getServiceContext())
    #set($currentProfileUtil = $utilLocator.findUtil("myportlets-1.0-SNAPSHOT", "com.myTool.MyTest"))
    #set($result = $currentProfileUtil.getUserCountry($serviceContext.getSession()))
    $result
    

    Hope that will help other people as well