package org.gcube.portal.events; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.liferay.portal.kernel.events.Action; import com.liferay.portal.kernel.events.ActionException; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.struts.LastPath; import com.liferay.portal.kernel.util.PropsKeys; import com.liferay.portal.kernel.util.PropsUtil; import com.liferay.portal.kernel.util.StringBundler; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.model.Group; import com.liferay.portal.model.User; import com.liferay.portal.model.VirtualHost; import com.liferay.portal.service.GroupLocalServiceUtil; import com.liferay.portal.service.LayoutSetLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.service.VirtualHostLocalServiceUtil; import com.liferay.portal.util.PortalUtil; public class LandingPageAction extends Action { private static final Logger _log = LoggerFactory.getLogger(LandingPageAction.class); public static final String GUEST_GROUP_FRIENDLY_URL = "/guest"; public static final String PRIVATE_GROUP_SERVLET_MAPPING = PropsUtil.get(PropsKeys.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING); public static final String PORTAL_CONTEXT = PortalUtil.getPathContext(); @Override public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException { _log.info("LandingPageAction is ON"); HttpSession session = request.getSession(); String path = ""; try { path = getCustomLandingPage(request); } catch (Exception e) { e.printStackTrace(); } session.setAttribute(WebKeys.LAST_PATH, new LastPath(StringPool.BLANK, path)); } /** * Returns custom landing page path after user login * * @param request * @return * @throws PortalException * @throws SystemException */ private String getCustomLandingPage(final HttpServletRequest request) throws PortalException, SystemException { String customLandingPagePath =""; customLandingPagePath = getLandingPagePath(request); _log.info("Private Site LandingPage Path = " + customLandingPagePath); return customLandingPagePath; } public static String getLandingPagePath(final HttpServletRequest request) throws PortalException, SystemException { String sitePath = StringPool.BLANK; User currentUser = PortalUtil.getUser(request); String currentVirtualHost = request.getServerName(); _log.debug("currentHost is " + currentVirtualHost); Group site = null; List vHosts = VirtualHostLocalServiceUtil.getVirtualHosts(0, VirtualHostLocalServiceUtil.getVirtualHostsCount()); for (VirtualHost virtualHost : vHosts) { _log.debug("Found " + virtualHost.getHostname()); if (virtualHost.getHostname().compareTo("localhost") != 0 && virtualHost.getLayoutSetId() != 0 && virtualHost.getHostname().compareTo(currentVirtualHost) == 0) { long layoutSetId = virtualHost.getLayoutSetId(); site = LayoutSetLocalServiceUtil.getLayoutSet(layoutSetId).getGroup(); _log.debug("Found match! Your site is " + site.getName()); List userSites = getSites(currentUser.getUserId()); boolean isRegistered = false; for (Group group : userSites) { if (group.getGroupId() == site.getGroupId()) { isRegistered = true; _log.debug("user " + currentUser.getFullName() + " is registered to " + site.getName() + ". redirecting ..."); break; } } if (! isRegistered) _log.debug("But user " + currentUser.getFullName() + " is not registered to " + site.getName() + ". going to register ..."); registerUserToSite(currentUser, site); break; } } if (site.getPrivateLayoutsPageCount() > 0) { sitePath = getGroupFriendlyURL(request, site); } else { _log.debug(site.getName() + " site doesn't have any private page. Default landing page will be used"); } return sitePath; } /** * this method is used to register the user to the group if does not belong to it yet * IMPORTANT: it does not add the user to the Site's private pages if the Site Membership type is different from Private * @param user * @param site * @throws SystemException */ private static void registerUserToSite(User user, Group site) throws SystemException { UserLocalServiceUtil.addGroupUser(site.getGroupId(), user.getUserId()); _log.debug("User " + user.getScreenName() +" registered to " + site.getName()); } public static List getSites(final long userId) throws PortalException, SystemException { List sites = new ArrayList(); for (Group group : GroupLocalServiceUtil.getUserGroups(userId)) { if (group.isRegularSite() && !GUEST_GROUP_FRIENDLY_URL.equalsIgnoreCase(group.getFriendlyURL())) { sites.add(group); } } return sites; } /** * @param request * @param currentGroup * @param isPrivate * @param isUser * @return * @throws PortalException * @throws SystemException */ public static String getGroupFriendlyURL(final HttpServletRequest request, final Group currentGroup) throws PortalException, SystemException { String friendlyURL = PRIVATE_GROUP_SERVLET_MAPPING; StringBundler sb = new StringBundler(); sb.append(PORTAL_CONTEXT); sb.append(friendlyURL); sb.append(currentGroup.getFriendlyURL()); return sb.toString(); } }