2015-07-20 19:15:16 +02:00
package org.gcube.portal ;
2015-07-08 18:47:36 +02:00
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor ;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
2016-08-30 18:14:40 +02:00
import java.io.File ;
import java.io.FileInputStream ;
2015-07-08 18:47:36 +02:00
import java.io.IOException ;
import java.util.List ;
2016-08-30 18:14:40 +02:00
import java.util.Properties ;
2015-07-08 18:47:36 +02:00
import java.util.concurrent.Executors ;
import java.util.concurrent.ScheduledExecutorService ;
import java.util.concurrent.TimeUnit ;
import javax.servlet.ServletException ;
import javax.servlet.http.HttpServlet ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import org.gcube.common.encryption.StringEncrypter ;
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl ;
import org.gcube.portal.databook.server.DatabookStore ;
2016-05-19 00:12:38 +02:00
import org.gcube.portal.socialmail.EmailPopAccount ;
2015-07-20 19:15:16 +02:00
import org.gcube.portal.socialmail.PeriodicTask ;
2015-07-08 18:47:36 +02:00
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
2016-05-19 00:12:38 +02:00
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault ;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys ;
2015-07-08 18:47:36 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2016-05-19 00:12:38 +02:00
import com.liferay.portal.kernel.exception.PortalException ;
import com.liferay.portal.kernel.exception.SystemException ;
import com.liferay.portal.model.Group ;
import com.liferay.portal.model.VirtualHost ;
import com.liferay.portal.service.LayoutSetLocalServiceUtil ;
import com.liferay.portal.service.VirtualHostLocalServiceUtil ;
2015-07-08 18:47:36 +02:00
@SuppressWarnings ( " serial " )
2015-07-20 19:15:16 +02:00
public class PortalSchedulerService extends HttpServlet {
2015-07-08 18:47:36 +02:00
2015-07-20 19:15:16 +02:00
private static final Logger _log = LoggerFactory . getLogger ( PortalSchedulerService . class ) ;
2017-03-15 10:04:26 +01:00
private static final String POP3_POLLING_MINUTES_INTERVAL = " pop3pollinginminutes " ;
2015-09-23 12:22:49 +02:00
private static final String POP3_SERVER_NAME = " Pop3MailServer " ;
2016-05-19 00:12:38 +02:00
2017-03-15 10:04:26 +01:00
2015-07-08 18:47:36 +02:00
private static DatabookStore store ;
2016-05-19 00:12:38 +02:00
2015-07-08 18:47:36 +02:00
public void init ( ) {
store = new DBCassandraAstyanaxImpl ( ) ;
2016-05-19 00:12:38 +02:00
}
2016-08-30 18:14:40 +02:00
2016-05-19 00:12:38 +02:00
protected void doGet ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
Group site ;
EmailPopAccount popAccount = null ;
try {
site = getSiteFromServletRequest ( request ) ;
popAccount = getPopAccountData ( site ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2016-08-30 18:14:40 +02:00
String toReturn = " <DIV>Check Notification Email Starting ... </DIV> " ;
2017-03-15 10:04:26 +01:00
int pollingInterval = getPollingInterval ( ) ;
ScheduledExecutorService pop3Scheduler = Executors . newScheduledThreadPool ( 1 ) ;
pop3Scheduler . scheduleAtFixedRate ( new PeriodicTask ( store , popAccount , request ) , 0 , pollingInterval , TimeUnit . MINUTES ) ;
String portalName = " unknown " ;
try {
popAccount . getPortalName ( ) ;
}
catch ( NullPointerException e ) {
_log . warn ( " Could not read popAccount data portal name " , e ) ;
}
_log . info ( " EmailParser stared for " + portalName + " , pollingInterval (in minutes)= " + pollingInterval ) ;
boolean keepPolling = true ;
while ( keepPolling ) {
try {
int newPolling = getPollingInterval ( ) ;
if ( newPolling < = 0 ) {
pop3Scheduler . shutdown ( ) ;
_log . info ( " EmailParser stopped for " + popAccount . getPortalName ( ) + " , found value less than 1 in gcube-data.properties file under $CATALINA_HOME/conf " ) ;
keepPolling = false ;
}
else if ( newPolling ! = pollingInterval ) {
pollingInterval = newPolling ;
pop3Scheduler . shutdown ( ) ;
_log . debug ( " Current thread EmailParser stopped, starting new one with different polling rate ... -> " + pollingInterval ) ;
pop3Scheduler = Executors . newScheduledThreadPool ( 1 ) ;
pop3Scheduler . scheduleAtFixedRate ( new PeriodicTask ( store , popAccount , request ) , 0 , pollingInterval , TimeUnit . MINUTES ) ;
_log . debug ( " EmailParser restarts in " + pollingInterval + " minutes, to change this polling delay edit gcube-data.properties file under $CATALINA_HOME/conf " ) ;
}
Thread . sleep ( pollingInterval * 1000 * 60 ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2016-08-31 18:05:42 +02:00
toReturn = " <DIV>Check Notification Email Started ... </DIV> " ;
2016-05-19 00:12:38 +02:00
2017-03-15 10:04:26 +01:00
// Thread likesThread = new Thread(new PeriodicTask(store, popAccount, request));
// likesThread.start();
2016-05-19 00:12:38 +02:00
response . setContentType ( " text/html " ) ;
response . getWriter ( ) . write ( toReturn ) ;
}
2017-03-15 10:04:26 +01:00
/ * *
* read the time interval in minutes from a property file and returns it
* /
public int getPollingInterval ( ) {
//get the portles to look for from the property file
Properties props = new Properties ( ) ;
int toReturn = 7 ;
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 ) ;
toReturn = Integer . parseInt ( props . getProperty ( POP3_POLLING_MINUTES_INTERVAL ) ) ;
}
//catch exception in case properties file does not exist
catch ( IOException e ) {
_log . error ( " gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default interval in minutes = " + toReturn ) ;
return toReturn ;
}
_log . debug ( " Returning poling interval in minutes: " + toReturn ) ;
return toReturn ;
}
/ * *
*
* @return $CATALINA_HOME
* /
private static String getCatalinaHome ( ) {
return ( System . getenv ( " CATALINA_HOME " ) . endsWith ( " / " ) ? System . getenv ( " CATALINA_HOME " ) : System . getenv ( " CATALINA_HOME " ) + " / " ) ;
}
2016-05-19 00:12:38 +02:00
protected void doPost ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { }
2016-08-30 18:14:40 +02:00
2016-05-19 00:12:38 +02:00
/ * *
*
* @param request
* @return the current Group instance based on the request
* @throws PortalException
* @throws SystemException
* /
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 ) {
_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 ( ) ) ;
return site ;
}
}
return null ;
}
private EmailPopAccount getPopAccountData ( Group site ) throws GroupRetrievalFault {
2016-08-30 18:14:40 +02:00
2016-05-19 00:12:38 +02:00
_log . debug ( " Found site for vhost, name " + site . getName ( ) + " reading custom field: " + CustomAttributeKeys . GATEWAY_SITE_NAME ) ;
String gatewayName = ( String ) new LiferayGroupManager ( ) . readCustomAttr ( site . getGroupId ( ) , CustomAttributeKeys . GATEWAY_SITE_NAME . getKeyName ( ) ) ;
//set the scope for the query
String curScope = ScopeProvider . instance . get ( ) ;
2015-07-08 18:47:36 +02:00
PortalContext context = PortalContext . getConfiguration ( ) ;
2016-05-19 00:12:38 +02:00
String scope2Set = " / " + context . getInfrastructureName ( ) ;
ScopeProvider . instance . set ( scope2Set ) ;
EmailPopAccount toReturn = new EmailPopAccount ( ) ;
toReturn . setPortalName ( gatewayName ) ;
2015-07-08 18:47:36 +02:00
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
query . addCondition ( " $resource/Profile/Category/text() eq 'Portal' " ) ;
2016-05-19 00:12:38 +02:00
query . addCondition ( " $resource/Profile/Name/text() eq ' " + gatewayName + " ' " ) ;
2015-07-08 18:47:36 +02:00
DiscoveryClient < ServiceEndpoint > client = clientFor ( ServiceEndpoint . class ) ;
List < ServiceEndpoint > list = client . submit ( query ) ;
if ( list = = null | | list . isEmpty ( ) ) {
2016-05-19 00:12:38 +02:00
_log . error ( " Could not find any Service endpoint registred in the infrastructure for this gateway: " + gatewayName ) ;
return null ;
2015-07-08 18:47:36 +02:00
}
else if ( list . size ( ) > 1 ) {
2016-05-19 00:12:38 +02:00
_log . warn ( " Found more than one Service endpoint registred in the infrastructure for this gateway: " + gatewayName ) ;
return null ;
2015-07-08 18:47:36 +02:00
}
else {
for ( ServiceEndpoint res : list ) {
2016-05-19 00:12:38 +02:00
org . gcube . common . resources . gcore . utils . Group < AccessPoint > apGroup = res . profile ( ) . accessPoints ( ) ;
2015-09-23 12:22:49 +02:00
AccessPoint [ ] accessPoints = ( AccessPoint [ ] ) apGroup . toArray ( new AccessPoint [ apGroup . size ( ) ] ) ;
for ( int i = 0 ; i < accessPoints . length ; i + + ) {
if ( accessPoints [ i ] . name ( ) . compareTo ( POP3_SERVER_NAME ) = = 0 ) {
_log . info ( " Found credentials for " + POP3_SERVER_NAME ) ;
AccessPoint found = accessPoints [ i ] ;
2016-05-19 00:12:38 +02:00
toReturn . setPop3Server ( found . address ( ) ) ;
toReturn . setPop3user ( found . username ( ) ) ;
2015-09-23 12:22:49 +02:00
String encrPassword = found . password ( ) ;
try {
2016-05-19 00:12:38 +02:00
toReturn . setPop3password ( StringEncrypter . getEncrypter ( ) . decrypt ( encrPassword ) ) ;
2015-09-23 12:22:49 +02:00
} catch ( Exception e ) {
_log . error ( " Something went wrong while decrypting password for " + POP3_SERVER_NAME ) ;
e . printStackTrace ( ) ;
}
}
2015-07-08 18:47:36 +02:00
}
}
}
2016-05-19 00:12:38 +02:00
//set the previous scope
ScopeProvider . instance . set ( curScope ) ;
return toReturn ;
2015-07-08 18:47:36 +02:00
}
2016-05-19 00:12:38 +02:00
2017-03-15 10:04:26 +01:00
2015-07-08 18:47:36 +02:00
}