diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..faa9002 --- /dev/null +++ b/.classpath @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..fe501a7 --- /dev/null +++ b/.project @@ -0,0 +1,36 @@ + + + landing-page-library + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f9fe345 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..443e085 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..58912db --- /dev/null +++ b/.settings/org.eclipse.wst.common.component @@ -0,0 +1,5 @@ + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..1b22d70 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..0e51dd2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,64 @@ + + 4.0.0 + + maven-parent + org.gcube.tools + 1.0.0 + + + org.gcube.portal + landing-page-library + 1.0.0-SNAPSHOT + jar + + landing-page-library + + + UTF-8 + 6.2.5 + + + + + com.liferay.portal + portal-service + ${liferay.version} + provided + + + com.liferay.portal + util-java + ${liferay.version} + provided + + + javax.portlet + portlet-api + 2.0 + provided + + + javax.servlet + servlet-api + 2.4 + provided + + + javax.servlet.jsp + jsp-api + 2.0 + provided + + + org.slf4j + slf4j-log4j12 + 1.6.4 + + + org.slf4j + slf4j-api + 1.6.4 + + + diff --git a/src/main/java/org/gcube/portal/landingpage/LandingPageManager.java b/src/main/java/org/gcube/portal/landingpage/LandingPageManager.java new file mode 100644 index 0000000..08aa1a1 --- /dev/null +++ b/src/main/java/org/gcube/portal/landingpage/LandingPageManager.java @@ -0,0 +1,125 @@ +package org.gcube.portal.landingpage; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +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.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.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; + +/** + * + * @author Massimiliano Assante, CNR-ISTI + * + */ +public class LandingPageManager { + private static final Logger _log = LoggerFactory.getLogger(LandingPageManager.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(); + + public static String getLandingPagePath(final HttpServletRequest request) throws PortalException, SystemException { + User currentUser = PortalUtil.getUser(request); + return getLandingPagePath(request, currentUser); + } + /** + * + * @param request + * @param currentUser + * @return + * @throws PortalException + * @throws SystemException + */ + public static String getLandingPagePath(final HttpServletRequest request, User currentUser) throws PortalException, SystemException { + String sitePath = StringPool.BLANK; + + 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(); + } +}