This commit is contained in:
Massimiliano Assante 2016-07-21 13:26:46 +00:00
parent ae24cd0d7d
commit 1c9d5af09e
1 changed files with 108 additions and 3 deletions

View File

@ -128,8 +128,19 @@ public class PortalContext {
toReturn += (httpServletRequest.getServerPort() == 443) ? "" : ":"+httpServletRequest.getServerPort() ;
else
toReturn += (httpServletRequest.getServerPort() == 80) ? "" : ":"+httpServletRequest.getServerPort() ;
return toReturn;
}
/**
*
* @param serverName e.g. myportal.mydomain.org
* @param serverPort
* @param secure
* @return the gateway URL until the first slash, e.g. http(s)://mynode.d4science.org:8080, if the URL uses standard http(s) port like 80 or 443 the port is not returned.
*/
public String getGatewayURL(String serverName, int serverPort, boolean secure) {
return PortalUtil.getPortalURL(serverName, serverPort, secure);
}
/**
* @deprecated use getGatewayURL(HttpServletRequest httpServletRequest)
* @return the basic gateway url
@ -161,7 +172,29 @@ public class PortalContext {
try {
site = getSiteFromServletRequest(request);
if (site.getPrivateLayoutsPageCount() > 0) {
sitePath = getGroupFriendlyURL(request, site);
sitePath = getGroupFriendlyURL(site);
} else {
_log.debug(site.getName() + " site doesn't have any private page. Default landing page will be used");
}
}catch (Exception e) {
e.printStackTrace();
}
return sitePath;
}
/**
*
* @param serverName e.g. myportal.mydomain.org
* @return the landing page path of the current Site e.g. "/group/i-marine"
* @throws PortalException
* @throws SystemException
*/
public String getSiteLandingPagePath(final String serverName) {
String sitePath = StringPool.BLANK;
Group site;
try {
site = getSiteFromServerName(serverName);
if (site.getPrivateLayoutsPageCount() > 0) {
sitePath = getGroupFriendlyURL(site);
} else {
_log.debug(site.getName() + " site doesn't have any private page. Default landing page will be used");
}
@ -179,7 +212,29 @@ public class PortalContext {
*/
private Group getSiteFromServletRequest(final HttpServletRequest request) throws PortalException, SystemException {
String serverName = request.getServerName();
_log.debug("currentHost is " + serverName);
Group site = null;
List<VirtualHost> vHosts = VirtualHostLocalServiceUtil.getVirtualHosts(0, VirtualHostLocalServiceUtil.getVirtualHostsCount());
for (VirtualHost virtualHost : vHosts) {
if (virtualHost.getHostname().compareTo("localhost") != 0 &&
virtualHost.getLayoutSetId() != 0 &&
virtualHost.getHostname().compareTo(serverName) == 0) {
long layoutSetId = virtualHost.getLayoutSetId();
site = LayoutSetLocalServiceUtil.getLayoutSet(layoutSetId).getGroup();
return site;
}
}
_log.warn("serverName is " + serverName + " but i could not find any virtualHost associated to it");
return null;
}
/**
*
* @param serverName e.g. myportal.mydomain.org
* @return
* @throws PortalException
* @throws SystemException
*/
private Group getSiteFromServerName(final String serverName) throws PortalException, SystemException {
_log.debug("serverName passed is " + serverName);
Group site = null;
List<VirtualHost> vHosts = VirtualHostLocalServiceUtil.getVirtualHosts(0, VirtualHostLocalServiceUtil.getVirtualHostsCount());
for (VirtualHost virtualHost : vHosts) {
@ -204,12 +259,44 @@ public class PortalContext {
* @throws PortalException
* @throws SystemException
*/
@Deprecated
private static String getGroupFriendlyURL(HttpServletRequest request, final Group currentGroup) throws PortalException, SystemException {
String friendlyURL = GCubePortalConstants.PREFIX_GROUP_URL;
StringBundler sb = new StringBundler();
sb.append(friendlyURL).append(currentGroup.getFriendlyURL());
return sb.toString();
}
/**
* @param request
* @param currentGroup
* @param isPrivate
* @param isUser
* @return
* @throws PortalException
* @throws SystemException
*/
private static String getGroupFriendlyURL(final Group currentGroup) throws PortalException, SystemException {
String friendlyURL = GCubePortalConstants.PREFIX_GROUP_URL;
StringBundler sb = new StringBundler();
sb.append(friendlyURL).append(currentGroup.getFriendlyURL());
return sb.toString();
}
/**
*
* @param serverName e.g. myportal.mydomain.org
* @return the current Site Name based on the servername (e.g. i-marine.d4science.org)
*/
public String getGatewayName(final String serverName) {
String toReturn = DEFAULT_GATEWAY_NAME;
try {
Group currSite = getSiteFromServerName(serverName);
toReturn = currSite.getName();
} catch (Exception e) {
toReturn = DEFAULT_GATEWAY_NAME;
_log.error("Could not read Site Custom Attr: " + CustomAttributeKeys.GATEWAY_SITE_NAME.getKeyName() + ", returning default Gateway Name " + toReturn);
}
return toReturn;
}
/**
*
* @param request the HttpServletRequest instance of your servlet
@ -219,7 +306,7 @@ public class PortalContext {
String toReturn = DEFAULT_GATEWAY_NAME;
try {
Group currSite = getSiteFromServletRequest(request);
toReturn = (String) new LiferayGroupManager().readCustomAttr(currSite.getGroupId(), CustomAttributeKeys.GATEWAY_SITE_NAME.getKeyName());
toReturn = currSite.getName();
} catch (Exception e) {
toReturn = DEFAULT_GATEWAY_NAME;
_log.error("Could not read Site Custom Attr: " + CustomAttributeKeys.GATEWAY_SITE_NAME.getKeyName() + ", returning default Gateway Name " + toReturn);
@ -269,6 +356,24 @@ public class PortalContext {
}
return toReturn;
}
/**
*
* @param serverName e.g. myportal.mydomain.org
* @return the sender (from) email address for the current Site based on the request
*/
public String getSenderEmail(final String serverName) {
String toReturn = DEFAULT_GATEWAY_EMAIL;
try {
Group currSite = getSiteFromServerName(serverName);
toReturn = (String) new LiferayGroupManager().readCustomAttr(currSite.getGroupId(), CustomAttributeKeys.GATEWAY_SITE_EMAIL_SENDER.getKeyName());
} catch (Exception e) {
toReturn = DEFAULT_GATEWAY_EMAIL;
_log.error("Could not read Site Custom Attr: " + CustomAttributeKeys.GATEWAY_SITE_EMAIL_SENDER.getKeyName() + ", returning default Gateway Email Sender " + toReturn);
}
return toReturn;
}
/**
* read the sender (from) email address for notifications name from a property file and returns it
* @deprecated use getSenderEmail(HttpServletRequest request)