Get Data Vaadin portlet Liferay

Hello:
I opted to use the APIs to develop portlets in Vaadin Liferay.
I have the need to access user data such as your name, role, id, etc …
I searched different ways to get data from the portlet Liferay:
I use this code:

import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.vaadin.Application;
import com.vaadin.ui.
;

public class LoginApplication extends Application {
@Override
public void init() {
String userx=“”;
PortletRequest request=null;

	Map mapParam = (Map) request.getAttribute(PortletRequest.USER_INFO);
	String sId = mapParam.get("liferay.user.id").toString();
	Long testlong = Long.valueOf(sId);
	long lng = testlong.longValue();
	try {
	User user = UserLocalServiceUtil.getUser(lng);
	System.out.println("DOWIEW ------> usuario " + user.getFullName());
	[b]

userx = user.getFullName();
[/b]
} catch (PortalException ex) {
System.out.println(“WRONG”);
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

	Window mainWindow = new Window("Login Application");
    //Name of logged User
	Label label = new Label(userx);
	mainWindow.addComponent(label);
	setMainWindow(mainWindow);
}

}

I well I can access the user class methods using eclipse.
If I deploy the portlet I get in liferay: login is temporarily unavailable.

Catalina.out:

16:14:48,475 INFO [PortletHotDeployListener:371]
1 portlet for login is available for use
16:15:24,121 ERROR [PortletServlet:96]
javax.portlet.PortletException: java.lang.NullPointerException
javax.portlet.PortletException: java.lang.NullPointerException
at com.vaadin.terminal.gwt.server.AbstractApplicationPortlet.handleServiceException(AbstractApplicationPortlet.java:1334)
at com.vaadin.terminal.gwt.server.AbstractApplicationPortlet.handleRequest(AbstractApplicationPortlet.java:487)
at com.vaadin.terminal.gwt.server.AbstractApplicationPortlet.doDispatch(AbstractApplicationPortlet.java:715)
at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:101)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:64)
at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:92)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:628)
at com.liferay.portlet.InvokerPortletImpl.invokeRender(InvokerPortletImpl.java:713)
at com.liferay.portlet.InvokerPortletImpl.render(InvokerPortletImpl.java:424)
at org.apache.jsp.html.portal.render_005fportlet_jsp._jspService(Unknown Source)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377
[u]

[/u]
etc …
Caused by: java.lang.NullPointerException
at com.example.login.LoginApplication.init(LoginApplication.java:22)
at com.vaadin.Application.start(Application.java:549)
at com.vaadin.terminal.gwt.server.AbstractApplicationPortlet.startApplication(AbstractApplicationPortlet.java:752)
at com.vaadin.terminal.gwt.server.AbstractApplicationPortlet.handleRequest(AbstractApplicationPortlet.java:400)
… 191 more

Thanks

Hello,

at least one way to access the user data would be to implement the
com.vaadin.terminal.gwt.server.PortletRequestListener
interface and use the
PortletRequest
instance given as the parameter of the
onRequestStart
method to retrieve the current user.

In the code you provided, you don’t have an actual request instance and therefore you get the
NullPointerException
.

Hope this helps. I’ll be happy to provide more detailed instructions if needed.

  • Teemu

Hello Teemu:

Puffff, I’ve changed the code but I dont Know if it is ok :

package com.example.login;

import java.util.Map;
import java.util.logging.Level;

import javax.portlet.PortletRequest;

import org.mortbay.jetty.Request;

import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.vaadin.Application;
import com.vaadin.ui.
;
import com.vaadin.terminal.gwt.server.PortletRequestListener;

public class LoginApplication extends Application {
@Override
public void init() {

	Window mainWindow = new Window("Login Application");
    //Name of logged User

	[b]

Label label = new Label(TEXT LABEL);
[/b] //Need return here
mainWindow.addComponent(label);

	setMainWindow(mainWindow);
}

//Is this method executed correctly ???

void onRequestStart(javax.portlet.PortletRequest request,
javax.portlet.PortletResponse response){

Map mapParam = (Map) request.getAttribute(PortletRequest.USER_INFO);
    String sId = mapParam.get("liferay.user.id").toString();
    Long testlong = Long.valueOf(sId);
	long lng = testlong.longValue();
	try {
	User user = UserLocalServiceUtil.getUser(lng);
	System.out.println("DOWIEW ------> usuario " + user.getFullName());

	} catch (PortalException ex) {
	System.out.println("WRONG");
	} catch (SystemException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 

}
}

  1. I read in a Vaadin book that method onRequestStart is executed at start of init()
  2. I dont know if this code is ok, but there are not errors.
  3. I need return the user.getFullName() at the text label. How can I do???
  4. I would like documentation, examples about Vaadin liferay portlets.

Thanks Teemu !!!

I created a simple example application for you. I also tested it to work properly on Liferay Portal 5.2.3.

The
Application
class implements the
PortletRequestListener
and sets the Vaadin application user to the Liferay
User
instance inside the
onRequestStart
method. In order to get hold of the Liferay
User
instance, we use the
PortalUtil.getUser
static method.

In the
init
method of the application we add a
Label
which greets the current user or displays
“Hello anonymous Vaadin user”
if there is no Liferay user logged in.

Please note that you must have the
portlet.jar
,
portal-service.jar
and
portal-kernel.jar
libraries in your build path in order to compile the code.


package com.example.login;

import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;

import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.PortletRequestListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class LoginApplication extends Application implements
        PortletRequestListener {

    @Override
    public void init() {
        Window mainWindow = new Window("LoginApplication");

        Label label = new Label("Hello anonymous Vaadin user");
        if (getUser() != null) {
            // user has logged in
            label = new Label("Hello " + ((User) getUser()).getFullName());
        }
        mainWindow.addComponent(label);
        setMainWindow(mainWindow);
    }

    @Override
    public void onRequestStart(PortletRequest request, PortletResponse response) {
        if (getUser() == null) {
            try {
                User user = PortalUtil.getUser(request);
                setUser(user);
            } catch (PortalException e) {
                e.printStackTrace();
            } catch (SystemException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onRequestEnd(PortletRequest request, PortletResponse response) {
        // Nothing to do here currently, exists only to implement the
        // PortletRequestListener interface.
    }

}

Works perfectly !
I’m just practicing,

Thanks Treemu

hi there I am trying to make Vaadin Portlet where I am supposed to fetch current login user’s ID,Company Id and GroupId as I am supposed to created event in the Calendar any suggestion plz its urgent thanks in advance.

What is happening is that I am able to add event in the database but event is not shown in the Calendar

Please see this forum thread on the subject:

How to access the Liferay User Details inside the Vaadin Source Code?