package org.gcube.portlets.user.gcubeloggedin.server; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.SessionManager; import org.gcube.common.homelibrary.home.HomeLibrary; import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException; import org.gcube.portal.custom.communitymanager.OrganizationsUtil; import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper; import org.gcube.portlets.user.gcubeloggedin.client.LoggedinService; import org.gcube.portlets.user.gcubeloggedin.shared.VObject; import org.gcube.portlets.user.gcubeloggedin.shared.VObject.UserBelongingClient; import org.gcube.portlets.user.gcubeloggedin.shared.VREClient; import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.UserManager; import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayUserManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.servlet.ImageServletTokenUtil; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.model.Group; import com.liferay.portal.model.Organization; import com.liferay.portal.service.GroupLocalServiceUtil; import com.liferay.portal.theme.ThemeDisplay; /** * The server side implementation of the RPC service. */ @SuppressWarnings("serial") public class LoggedinServiceImpl extends RemoteServiceServlet implements LoggedinService { private static final Logger _log = LoggerFactory.getLogger(LoggedinServiceImpl.class); private boolean isDeployed = true; /** * the current ASLSession * @return . */ private ASLSession getASLSession() { isDeployed = true; String sessionID = this.getThreadLocalRequest().getSession().getId(); String user = (String) this.getThreadLocalRequest().getSession().getAttribute(ScopeHelper.USERNAME_ATTRIBUTE); if (user == null) { _log.warn("LOGGEDIN PORTLET: USER IS NULL \n\n SESSION ID READ: " +sessionID ); user = "test.user"; _log.warn("session ID= *" + sessionID + "* user= *" + user + "*" ); isDeployed = false; } return SessionManager.getInstance().getASLSession(sessionID, user); } /** * return the current selected VRE */ public VObject getSelectedRE() { ASLSession aslSession = getASLSession(); if (! isDeployed) { return new VREClient("Test", "", "" + "Fishery and Aquaculture Resources Management (FARM) Virtual Organisation The FARM Virtual Organisation is the dynamic group of individuals and/or institutions defined around a set of sharing rules in which resource providers and consumers specify clearly and carefully just what is shared, who is allowed to share, and the conditions under which sharing occurs to serve the needs of the Fisheries and Aquaculture Resources Management. This VO is conceived to support various application scenarios arising in the FARM Community including the production of Fisheries and Aquaculture Country Profiles, the management of catch statistics including harmonisation, the dynamic generation of biodiversity maps and species distribution maps. This Virtual Organisation currently consists of:" + "", "", "", UserBelongingClient.BELONGING); } _log.trace("getting Selected Research Environment"); HttpSession session = this.getThreadLocalRequest().getSession(); ThemeDisplay themeDisplay = (ThemeDisplay) this.getThreadLocalRequest().getSession().getAttribute(WebKeys.THEME_DISPLAY); Organization currOrg = (Organization) session.getAttribute("CURR_RE_NAME"); /** * set the current ORG bean in session */ aslSession.setAttribute(ScopeHelper.CURR_ORG, currOrg); aslSession.setGroupModelInfos(currOrg.getName(), currOrg.getOrganizationId()); _log.trace("CURRENT ORG SET IN SESSION: " + currOrg.getName()); String name = currOrg.getName(); long logoId = currOrg.getLogoId(); String logoURL = themeDisplay.getPathImage()+"/organization_logo?img_id="+ logoId +"&t" + ImageServletTokenUtil.getToken(logoId); String desc = ""; //set the description for the vre if (currOrg.getComments() != null) desc = currOrg.getComments(); VREClient vre = new VREClient(name, "", desc, logoURL, "", UserBelongingClient.BELONGING); return vre; } /** * The Default Community is a community where all portal user belong to * @return the default community URL * @throws SystemException * @throws PortalException */ public String getDefaultCommunityURL() { //get the portles to look for from the property file Properties props = new Properties(); String comName = ""; try { String propertyfile = this.getServletContext().getRealPath("")+"/config/resources.properties"; File propsFile = new File(propertyfile); FileInputStream fis = new FileInputStream(propsFile); props.load( fis); comName = props.getProperty("defaultcommunity"); } //catch exception in case properties file does not exist catch(IOException e) { _log.error("/config/resources.properties not found, Returning \"Data e-Infrastructure gateway\" as default Community"); } String toReturn = ""; _log.trace("Default Community Read from config file: " + comName); Group myPlace = null; try { myPlace = GroupLocalServiceUtil.getGroup(OrganizationsUtil.getCompany().getCompanyId(), comName); if(myPlace.isCommunity()) { ThemeDisplay themeDisplay = (ThemeDisplay) this.getThreadLocalRequest().getSession().getAttribute(WebKeys.THEME_DISPLAY); toReturn = myPlace.getPathFriendlyURL(true, themeDisplay) + myPlace.getFriendlyURL(); } } catch (PortalException e) { e.printStackTrace(); } catch (SystemException e) { e.printStackTrace(); } toReturn = getPortalBasicUrl()+toReturn; _log.trace("RETURNING Default Community URL: " + toReturn); return toReturn; } /** * * @return the portal basic url, e.g. http://www.foo.com */ private String getPortalBasicUrl() { HttpServletRequest request = this.getThreadLocalRequest(); String toReturn = ""; //protocol String protocol = (request.isSecure()) ? "https://" : "http://" ; toReturn += protocol; //server name toReturn += request.getServerName(); //port toReturn += (request.getServerPort() == 80) ? "" : ":"+request.getServerPort() ; request.getQueryString(); _log.trace("\n getPortalBasicUrl: " +toReturn + "queryString: " + request.getQueryString()); return toReturn; } /** *@return the redirect url if everything goes ok, null otherwise */ @Override public String removeUserFromVRE() { String username = getASLSession().getUsername(); if (username.compareTo("test.user") == 0) return null; _log.debug("Going to remove user from the current Group: " + getCurrentGroupID() + ". Username is: " + username); UserManager userM = new LiferayUserManager(); try { userM.dismissUserFromGroup(getCurrentGroupID(), userM.getUserId(username)); removeUserFromHLGroup(username, getASLSession().getScope()); return getPortalBasicUrl(); } catch (Exception e) { e.printStackTrace(); return null; } } /** * Get the current group ID * * @return the current group ID or null if an exception is thrown * @throws Exception */ private String getCurrentGroupID(){ ASLSession session = getASLSession(); _log.debug("The current group NAME is --> " + session.getGroupName()); try { try { GroupManager groupM = new LiferayGroupManager(); return groupM.getGroupId(session.getGroupName()); } catch (UserManagementSystemException e) { throw new Exception(e.getMessage(), e.getCause()); } } catch (Exception e) { e.printStackTrace(); } return null; } private void removeUserFromHLGroup(String username, String group) { try { org.gcube.common.homelibrary.home.workspace.usermanager.UserManager um = HomeLibrary.getHomeManagerFactory().getUserManager(); um.removeUserFromGroup(group, username, getASLSession().getUsername()); } catch (InternalErrorException e) { _log.error("Failed to get the usermanager from HL. Could not add remove user from the HL group"); } catch (ItemNotFoundException e1) { } } }