Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

You have not said clearly as to, where you are using the velocity template - in themes, web-content templates etc?

I would guess it would be the theme since you gave a link at the start which describes how to use them in themes, so the short answer is you can't get PortletSession and RenderRequest from the velocity templates but don't worry since you can get HttpSession and HttpServletRequest in the template files and use it as it is used in liferay's init.vm.

So the variable $request is for HttpRequest and is not for RenderRequest and hence $request.getSession() gives HttpSession and not PortletSession.

I think the reason you can't get portlet specific request and session in velocity may be because the templates are portlet agnostic and hence there is no means to know for which portlet are you asking the request.

Anyways but all the attributes or parameters that can be there in a portlet's request are there in the HttpServletRequest, so I guess you would need to change the method signatures to:

public interface MyTest{
    public String getUserCountry(HttpSession portletSession);
    public String getUserCountry(HttpServletRequest request);
}

and then use:

#set($result = $currentProfileUtil.getUserCountry($request.getSession()))
#set($result = $currentProfileUtil.getUserCountry($request))

I have tried this but you can try getting the RenderRequest from the HttpServletRequest:

RenderRequest renderRequest = (RenderRequest) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);

and response through:

RenderResponse renderResponse = (RenderResponse) request.getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);

Note: $request variable is also available in web-content templates (OP's comment)

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top