2

How can I get the current Liferay user in java:

I found this: How can I get the current user in Liferay? and this: Get the current user Liferay using a simple Java code

But anyone tells how to initialize a "request"

even this document http://www.liferay.com/documentation/liferay-portal/6.0/development/-/ai/understanding-the-two-phases-of-portlet-execution seems to be complicated

Community
  • 1
  • 1
Jhon
  • 87
  • 4
  • 13

4 Answers4

3

Let your Vaadin application implement com.vaadin.terminal.gwt.server.PortletRequestListener, then you can access the request:

public class MyApp extends Application implements PortletRequestListener {

    public void init() {
    …
    }


    @Override
    public void onRequestStart(PortletRequest request, PortletResponse response) {
        User user = PortalUtil.getUser(request);
        setUser(user);
    }

    @Override
    public void onRequestEnd(PortletRequest request, PortletResponse response) {

    }
}
Henri Kerola
  • 4,947
  • 1
  • 17
  • 19
  • 1
    in your code, user is local, thus not available outside of your method. you chould add a call to this.setUser(user), so that the data is available via a simple getUser later on... – PATRY Guillaume May 22 '12 at 08:11
1

the request object is available as a parameter to the action- or render-method as well as in jsps. You don't do initialization of it, it's just there (see the parameters for e.g. processAction(ActionRequest request, ActionResponse response), the same is there for the render-, event and resource-phase (albeit different with subtypes of requests/responses).

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • when I try this : ActionRequest request = null; String userId = request.getRemoteUser(); I get NullPointerException I need a code example please ! – Jhon May 21 '12 at 14:02
  • class YourPortlet extends GenericPortlet { void processAction(ActionRequest request, ActionResponse response) { User currentUser = PortalUtil.getUser(request); ... } } – Olaf Kock May 21 '12 at 15:03
  • when calling processAction function wich parameters should I pass as request and response ? because when declaring them as local variables it expects me to initialize variables – Jhon May 21 '12 at 15:25
  • The portal calls this when an action is triggered on your portal. You don't call that yourself. No offense, but it looks like you either need some more basic knowledge of portal development or we need more detailed information in your question about the problem at hand. – Olaf Kock May 21 '12 at 15:27
  • Yes I don't know anything about portal development. The fact is that I'm developping a liferay portlet based on vaadin which allows managing Jenkins jobs remotely and I need to get Liferay current user when application starts to use those parameters for jenkins authentication – Jhon May 21 '12 at 15:33
  • In Vaadin portlets things work a bit differently, see my answer. – Henri Kerola May 22 '12 at 20:08
  • Agreed, now that Vaadin is in the loop, there's no distinct Action/Render request. This was the missing link - I didn't have a clue what other explanation I could add until this clarification. Coming back from a few days of travelling luckily the problem seems to be solved. Thanks Henri for taking over. – Olaf Kock May 25 '12 at 08:05
1

I've been using Vaadin 6.8.0 and Liferay 6.1. Here is a little bit different code using Henri Kerola solution:

    @Override
public void onRequestStart(PortletRequest request, PortletResponse response) {
    try {
        this.setUser(request.getUserPrincipal().getName());
    } catch (Exception e) {
        this.setUser("anonymous");
    }
}

Where user property is a Vaadin Label. The try/catch is necessary because of getUserPrincipal nullPointer for anonymous users (guests from Liferay).

best regards,

WinstonQF
  • 11
  • 1
0

Have you try in your Vaadin Portlet (Application) to use the methode: getUser()?

Vaadin Portlet wraped a Liferay API with Vaadin specific framework. Hence the developing of Vaadin Portlet has this own specification, and the standard Liferay API are not simple useable here.

Mark
  • 17,887
  • 13
  • 66
  • 93