2013-09-24 15:26:13 +02:00
package org.gcube.common.portal ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.IOException ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Properties ;
2016-03-23 18:33:58 +01:00
import javax.servlet.http.HttpServletRequest ;
2016-04-08 13:12:47 +02:00
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys ;
2013-09-24 15:26:13 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2016-03-24 12:19:28 +01:00
import com.liferay.portal.kernel.exception.PortalException ;
import com.liferay.portal.kernel.exception.SystemException ;
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.VirtualHost ;
2016-04-08 15:02:24 +02:00
import com.liferay.portal.service.CompanyLocalServiceUtil ;
2016-03-24 12:19:28 +01:00
import com.liferay.portal.service.LayoutSetLocalServiceUtil ;
import com.liferay.portal.service.VirtualHostLocalServiceUtil ;
2016-04-08 15:02:24 +02:00
import com.liferay.portal.util.PortalUtil ;
2016-03-24 12:19:28 +01:00
2013-09-24 15:26:13 +02:00
/ * *
2013-09-24 15:32:30 +02:00
* Clients can obtain the single instance of the { @link PortalContext } by invoking its static method { @link # getConfiguration ( ) } .
2013-09-24 15:26:13 +02:00
* The first invocation of the method triggers the initialisation of the instance .
*
* @author Massimiliano Assante ( ISTI - CNR )
*
* /
2013-09-24 15:32:30 +02:00
public class PortalContext {
private static final Logger _log = LoggerFactory . getLogger ( PortalContext . class ) ;
2016-03-24 17:27:37 +01:00
2016-04-08 13:12:47 +02:00
private static final String DEFAULT_INFRA_NAME = " gcube " ;
private static final String DEFAULT_VO_NAME = " devsec " ;
private static final String DEFAULT_GATEWAY_NAME = " D4science Gateway " ;
private static final String DEFAULT_GATEWAY_EMAIL = " do-not-reply@d4science.org " ;
2016-03-24 12:19:28 +01:00
2013-09-30 15:19:28 +02:00
private static PortalContext singleton = new PortalContext ( ) ;
2013-09-24 15:26:13 +02:00
private String infra ;
private String vos ;
2013-09-24 15:32:30 +02:00
private PortalContext ( ) {
2013-09-24 15:26:13 +02:00
initialize ( ) ;
}
/ * *
*
* @return the instance
* /
2013-09-24 15:32:30 +02:00
public synchronized static PortalContext getConfiguration ( ) {
return singleton = = null ? new PortalContext ( ) : singleton ;
2013-09-24 15:26:13 +02:00
}
2013-09-24 17:09:28 +02:00
2013-09-24 15:26:13 +02:00
private void initialize ( ) {
Properties props = new Properties ( ) ;
try {
2013-10-01 15:11:45 +02:00
String propertyfile = getCatalinaHome ( ) + File . separator + " conf " + File . separator + " infrastructure.properties " ;
2013-09-24 15:26:13 +02:00
File propsFile = new File ( propertyfile ) ;
FileInputStream fis = new FileInputStream ( propsFile ) ;
props . load ( fis ) ;
2016-03-17 15:19:05 +01:00
infra = props . getProperty ( GCubePortalConstants . INFRASTRUCTURE_NAME ) ;
vos = props . getProperty ( GCubePortalConstants . SCOPES ) ;
2013-09-24 15:26:13 +02:00
}
catch ( IOException e ) {
2016-04-08 13:12:47 +02:00
infra = DEFAULT_INFRA_NAME ;
vos = DEFAULT_VO_NAME ;
_log . error ( " infrastructure.properties file not found under $CATALINA_HOME/conf/ dir, setting default infrastructure Name " + infra + " and VO Name " + vos ) ;
2013-09-24 15:26:13 +02:00
}
2013-09-24 15:32:30 +02:00
_log . info ( " PortalContext configurator correctly initialized on " + infra ) ;
2013-09-24 15:26:13 +02:00
}
2013-09-24 17:09:28 +02:00
/ * *
*
* @return the infrastructure name in which your client runs
* /
2013-09-24 15:26:13 +02:00
public String getInfrastructureName ( ) {
return this . infra ;
}
2013-09-24 17:09:28 +02:00
/ * *
*
* @return the value of the scopes as it is in the property file ( a string with comma separated vales )
* /
public String getVOsAsString ( ) {
return this . vos ;
}
/ * *
*
* @return the value of the scopes
* /
2013-09-24 15:26:13 +02:00
public List < String > getVOs ( ) {
List < String > toReturn = new ArrayList < String > ( ) ;
if ( vos = = null | | vos . equals ( " " ) )
return toReturn ;
String [ ] split = vos . split ( " , " ) ;
for ( int i = 0 ; i < split . length ; i + + ) {
toReturn . add ( split [ i ] . trim ( ) ) ;
}
return toReturn ;
}
2015-07-08 12:09:20 +02:00
/ * *
2016-02-25 15:55:46 +01:00
*
* @deprecated use getConfiguration ( ) . getGatewayName ( ) method
* read the portal instance name from a property file and returns it
2015-07-08 12:09:20 +02:00
* /
2016-02-25 15:55:46 +01:00
@Deprecated
2015-07-08 12:09:20 +02:00
public static String getPortalInstanceName ( ) {
2016-02-25 15:55:46 +01:00
return getConfiguration ( ) . getGatewayName ( ) ;
}
2016-03-23 18:33:58 +01:00
/ * *
*
* @param httpServletRequest
* @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 ( HttpServletRequest httpServletRequest ) {
String serverName = httpServletRequest . getServerName ( ) ;
String toReturn = ( httpServletRequest . isSecure ( ) ) ? " https:// " : " http:// " ;
//server name
toReturn + = serverName ;
//port
if ( httpServletRequest . isSecure ( ) )
toReturn + = ( httpServletRequest . getServerPort ( ) = = 443 ) ? " " : " : " + httpServletRequest . getServerPort ( ) ;
else
toReturn + = ( httpServletRequest . getServerPort ( ) = = 80 ) ? " " : " : " + httpServletRequest . getServerPort ( ) ;
return toReturn ;
}
2016-04-08 15:02:24 +02:00
/ * *
* @deprecated use getGatewayURL ( HttpServletRequest httpServletRequest )
* @return the basic gateway url
* /
@Deprecated
public String getGatewayURL ( ) {
Long defaultCompanyId = PortalUtil . getDefaultCompanyId ( ) ;
try {
CompanyLocalServiceUtil . getCompany ( defaultCompanyId ) ;
return PortalUtil . getPortalURL ( CompanyLocalServiceUtil . getCompany ( defaultCompanyId ) . getVirtualHostname ( ) , 443 , true ) ;
} catch ( PortalException e ) {
e . printStackTrace ( ) ;
} catch ( SystemException e ) {
e . printStackTrace ( ) ;
}
return " " ;
}
2016-03-24 12:19:28 +01:00
/ * *
*
* @param request
* @return the landing page path of the current Site e . g . " /group/i-marine "
* @throws PortalException
* @throws SystemException
* /
public String getSiteLandingPagePath ( final HttpServletRequest request ) throws PortalException , SystemException {
String sitePath = StringPool . BLANK ;
2016-04-08 13:12:47 +02:00
Group site = getSiteFromServletRequest ( request ) ;
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 ;
}
/ * *
*
* @param request
* @return the current Group instance based on the request
* @throws PortalException
* @throws SystemException
* /
private Group getSiteFromServletRequest ( final HttpServletRequest request ) throws PortalException , SystemException {
2016-03-24 12:19:28 +01:00
String serverName = request . getServerName ( ) ;
_log . debug ( " currentHost is " + serverName ) ;
Group site = null ;
List < VirtualHost > 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 ( serverName ) = = 0 ) {
long layoutSetId = virtualHost . getLayoutSetId ( ) ;
site = LayoutSetLocalServiceUtil . getLayoutSet ( layoutSetId ) . getGroup ( ) ;
_log . debug ( " Found match! Your site is " + site . getName ( ) ) ;
2016-04-08 13:12:47 +02:00
return site ;
2016-03-24 12:19:28 +01:00
}
}
2016-04-08 13:12:47 +02:00
return null ;
2016-03-24 12:19:28 +01:00
}
/ * *
* @param request
* @param currentGroup
* @param isPrivate
* @param isUser
* @return
* @throws PortalException
* @throws SystemException
* /
2016-04-08 13:12:47 +02:00
private static String getGroupFriendlyURL ( HttpServletRequest request , final Group currentGroup ) throws PortalException , SystemException {
2016-03-24 17:27:37 +01:00
String friendlyURL = GCubePortalConstants . PREFIX_GROUP_URL ;
2016-03-24 12:19:28 +01:00
StringBundler sb = new StringBundler ( ) ;
2016-03-24 17:27:37 +01:00
sb . append ( friendlyURL ) . append ( currentGroup . getFriendlyURL ( ) ) ;
2016-03-24 12:19:28 +01:00
return sb . toString ( ) ;
}
2016-04-08 13:12:47 +02:00
/ * *
*
* @param request the HttpServletRequest instance of your servlet
* @return the current Site Name based on the request
* /
public String getGatewayName ( HttpServletRequest request ) {
String toReturn = DEFAULT_GATEWAY_NAME ;
try {
Group currSite = getSiteFromServletRequest ( request ) ;
toReturn = ( String ) new LiferayGroupManager ( ) . readCustomAttr ( currSite . getGroupId ( ) , CustomAttributeKeys . GATEWAY_SITE_NAME . getKeyName ( ) ) ;
} 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 ;
}
2016-02-25 15:55:46 +01:00
/ * *
* read the infrastructure gateway name from a property file and returns it
2016-04-08 13:12:47 +02:00
* @deprecated use getGatewayName ( HttpServletRequest request )
2016-02-25 15:55:46 +01:00
* /
2016-04-08 13:12:47 +02:00
@Deprecated
2016-02-25 15:55:46 +01:00
public String getGatewayName ( ) {
2015-07-08 12:09:20 +02:00
//get the portles to look for from the property file
Properties props = new Properties ( ) ;
String toReturn = " " ;
2015-06-22 15:24:39 +02:00
2015-07-08 12:09:20 +02:00
try {
String propertyfile = getCatalinaHome ( ) + File . separator + " conf " + File . separator + " gcube-data.properties " ;
File propsFile = new File ( propertyfile ) ;
FileInputStream fis = new FileInputStream ( propsFile ) ;
props . load ( fis ) ;
2016-03-17 15:19:05 +01:00
toReturn = props . getProperty ( GCubePortalConstants . GATEWAY_NAME ) ;
2015-07-08 12:09:20 +02:00
}
//catch exception in case properties file does not exist
catch ( IOException e ) {
2016-04-08 13:12:47 +02:00
toReturn = DEFAULT_GATEWAY_NAME ;
2015-07-08 12:09:20 +02:00
_log . error ( " gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default Portal Name " + toReturn ) ;
return toReturn ;
}
_log . debug ( " Returning Gateway Name: " + toReturn ) ;
return toReturn ;
}
2016-04-08 13:12:47 +02:00
/ * *
*
* @param request the HttpServletRequest instance of your servlet
* @return the sender ( from ) email address for the current Site based on the request
* /
public String getSenderEmail ( HttpServletRequest request ) {
String toReturn = DEFAULT_GATEWAY_EMAIL ;
try {
Group currSite = getSiteFromServletRequest ( request ) ;
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 ;
}
2015-06-22 15:24:39 +02:00
/ * *
2015-07-08 12:09:20 +02:00
* read the sender ( from ) email address for notifications name from a property file and returns it
2016-04-08 13:12:47 +02:00
* @deprecated use getSenderEmail ( HttpServletRequest request )
2015-06-22 15:24:39 +02:00
* /
2016-04-08 13:12:47 +02:00
@Deprecated
2015-06-22 15:48:38 +02:00
public String getSenderEmail ( ) {
2015-06-22 15:24:39 +02:00
//get the portles to look for from the property file
Properties props = new Properties ( ) ;
String toReturn = " " ;
try {
String propertyfile = getCatalinaHome ( ) + File . separator + " conf " + File . separator + " gcube-data.properties " ;
File propsFile = new File ( propertyfile ) ;
FileInputStream fis = new FileInputStream ( propsFile ) ;
props . load ( fis ) ;
2016-03-17 15:19:05 +01:00
toReturn = props . getProperty ( GCubePortalConstants . SENDER_EMAIL ) ;
2015-06-22 15:24:39 +02:00
}
//catch exception in case properties file does not exist
catch ( IOException e ) {
2016-04-08 13:12:47 +02:00
toReturn = DEFAULT_GATEWAY_EMAIL ;
2015-06-22 15:24:39 +02:00
_log . error ( " gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default Email " + toReturn ) ;
return toReturn ;
}
_log . debug ( " Returning SENDER_EMAIL: " + toReturn ) ;
return toReturn ;
}
2016-02-25 15:55:46 +01:00
/ * *
2016-03-14 14:33:53 +01:00
* use org . gcube . vomanagement . usermanagement . impl . LiferayUserManager # getAdmin method
2016-02-25 15:55:46 +01:00
* /
2016-03-14 14:33:53 +01:00
@Deprecated
2016-02-25 15:55:46 +01:00
public String getAdministratorUsername ( ) {
//get the portles to look for from the property file
Properties props = new Properties ( ) ;
String toReturn = " " ;
try {
String propertyfile = getCatalinaHome ( ) + File . separator + " conf " + File . separator + " gcube-data.properties " ;
File propsFile = new File ( propertyfile ) ;
FileInputStream fis = new FileInputStream ( propsFile ) ;
props . load ( fis ) ;
2016-03-17 15:19:05 +01:00
toReturn = props . getProperty ( GCubePortalConstants . ADMIN_USERNAME ) ;
2016-02-25 15:55:46 +01:00
}
//catch exception in case properties file does not exist
catch ( IOException e ) {
toReturn = " massimiliano.assante " ;
_log . error ( " gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default administrator " + toReturn ) ;
return toReturn ;
}
_log . debug ( " Returning Administrator username: " + toReturn ) ;
return toReturn ;
}
2015-06-22 15:24:39 +02:00
/ * *
2013-09-24 15:26:13 +02:00
*
* @return $CATALINA_HOME
* /
2013-10-01 15:11:45 +02:00
private static String getCatalinaHome ( ) {
return ( System . getenv ( " CATALINA_HOME " ) . endsWith ( " / " ) ? System . getenv ( " CATALINA_HOME " ) : System . getenv ( " CATALINA_HOME " ) + " / " ) ;
2013-09-24 15:26:13 +02:00
}
2016-03-23 18:33:58 +01:00
2013-09-24 15:26:13 +02:00
}