From ef73230461b1e795ce2ebc8440db5ee1dce86ea2 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Tue, 17 Jan 2017 16:24:44 +0000 Subject: [PATCH] added method PortalContext.setUserInSesssion for allowing custom servlet to read the current user, removed portal delegate git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@141609 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../gcube/common/portal/PortalContext.java | 49 +++++-------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/gcube/common/portal/PortalContext.java b/src/main/java/org/gcube/common/portal/PortalContext.java index 96892ab..8397c2e 100644 --- a/src/main/java/org/gcube/common/portal/PortalContext.java +++ b/src/main/java/org/gcube/common/portal/PortalContext.java @@ -9,9 +9,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; -import javax.portlet.PortletSession; import javax.portlet.RenderRequest; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; @@ -28,8 +28,6 @@ import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.liferay.portal.kernel.exception.PortalException; -import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.model.Group; import com.liferay.portal.model.VirtualHost; import com.liferay.portal.service.CompanyLocalServiceUtil; @@ -143,13 +141,12 @@ public class PortalContext { /** *

* Please note that this method works with AJAX calls only (i.e. XMLHttpRequest to exchange data with a server behind the scenes). - * If you use standard http servlet GET or POST to exchange data with a server, you must use Liferay's PortalDelegateServlet feature {@link com.liferay.portal.kernel.servlet.PortalDelegateServlet}. + * If you use standard http servlet GET or POST to exchange data with a server, you must call the methos PortalContext#setUserInSession in your portlet doView method. * - * For documentation on PortalDelegateServlet @see gCube WIKI PortalDelegateServlet page *

* * @param httpServletRequest the {@link HttpServletRequest} object - * @return the current user, or null if a current user could not be found + * @return the current user, or null if a current user could not be found or session expired * @see {@link GCubeUser} */ public GCubeUser getCurrentUser(HttpServletRequest httpServletRequest) { @@ -172,42 +169,18 @@ public class PortalContext { return toReturn; } else { //must be a custom servlet try { - userId = (long) httpServletRequest.getSession().getAttribute(USER_ID_ATTR_NAME); + HttpSession session = httpServletRequest.getSession(false); + userId = (long) session.getAttribute(USER_ID_ATTR_NAME); + _log.debug("read the current userid from the http session userId: " + userId + " sessionid="+session.getId()); return userManager.getUserById(userId); } catch (Exception e) { _log.error("Could not read the current userid from the http session, either session expired or user not logged in, exception: " + e.getMessage()); + return null; } } } return null; } - /** - *

- * Please note that this method works with Liferay's PortalDelegateServlet feature {@link com.liferay.portal.kernel.servlet.PortalDelegateServlet}. - * For documentation on PortalDelegateServlet @see gCube WIKI PortalDelegateServlet page - *

- * - * @param httpServletRequest the {@link HttpServletRequest} object - * @return the current user, or null if a current user could not be found - * @see {@link GCubeUser} - */ - public GCubeUser getCurrentUserPortalDelegateServlet(HttpServletRequest httpServletRequest) { - try { - if (PortalUtil.getUser(httpServletRequest) != null) { - long userId = PortalUtil.getUser(httpServletRequest).getUserId(); - return userManager.getUserById(userId); - } - else { - //the user is not logged in or session expired - _log.debug("Can't find the user, session expired or your not using "); - return null; - } - } catch (PortalException | SystemException | UserManagementSystemException | UserRetrievalFault e) { - _log.error("Could not read the current userid, either session expired or user not logged in, exception: " + e.getMessage()); - e.printStackTrace(); - } - return null; - } /** *

* This method sets the current userid in the session so that it is possible to get the current user in custom servlets of the same WAR. @@ -217,8 +190,12 @@ public class PortalContext { * @param request the portlet {@link RenderRequest} object */ public static void setUserInSession(RenderRequest request) { - long userid = Long.parseLong(request.getRemoteUser()); - request.getPortletSession().setAttribute(USER_ID_ATTR_NAME, userid, PortletSession.APPLICATION_SCOPE); + long userId = Long.parseLong(request.getRemoteUser()); + + HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(request); + HttpSession session = httpServletRequest.getSession(); + _log.debug("HttpSession#setUserInSession, set userId: " + userId + " sessionid="+session.getId()); + session.setAttribute(USER_ID_ATTR_NAME, userId); } /** * @param scopeGroupId the liferay groupid number (as String) of the VRE/VO