diff --git a/src/main/java/org/gcube/portal/socialmail/MailReader.java b/src/main/java/org/gcube/portal/PortalSchedulerService.java similarity index 76% rename from src/main/java/org/gcube/portal/socialmail/MailReader.java rename to src/main/java/org/gcube/portal/PortalSchedulerService.java index 880ebc8..2f4dccd 100644 --- a/src/main/java/org/gcube/portal/socialmail/MailReader.java +++ b/src/main/java/org/gcube/portal/PortalSchedulerService.java @@ -1,4 +1,4 @@ -package org.gcube.portal.socialmail; +package org.gcube.portal; import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; @@ -21,6 +21,8 @@ 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; +import org.gcube.portal.ldapexport.LDAPSync; +import org.gcube.portal.socialmail.PeriodicTask; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.slf4j.Logger; @@ -28,12 +30,11 @@ import org.slf4j.LoggerFactory; @SuppressWarnings("serial") -public class MailReader extends HttpServlet { +public class PortalSchedulerService extends HttpServlet { - private static final Logger _log = LoggerFactory.getLogger(MailReader.class); - private static final int MINUTES_DELAY = 1; - - private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + private static final Logger _log = LoggerFactory.getLogger(PortalSchedulerService.class); + private static final int POP3_MINUTES_DELAY = 1; + private static final int LDAP_MINUTES_DELAY = 1; private static DatabookStore store; @@ -79,12 +80,17 @@ public class MailReader extends HttpServlet { } } - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - scheduler.scheduleAtFixedRate(new PeriodicTask(store, portalName, host, user, password), 0, MINUTES_DELAY, TimeUnit.MINUTES); - String toReturn = "Check Notification Email Started ... "; + ScheduledExecutorService pop3Scheduler = Executors.newScheduledThreadPool(1); + pop3Scheduler.scheduleAtFixedRate(new PeriodicTask(store, portalName, host, user, password), 0, POP3_MINUTES_DELAY, TimeUnit.MINUTES); + String toReturn = "
Check Notification Email Started ...
"; + + ScheduledExecutorService ldapScheduler = Executors.newScheduledThreadPool(1); + ldapScheduler.scheduleAtFixedRate(new LDAPSync(), 0, LDAP_MINUTES_DELAY, TimeUnit.MINUTES); + + toReturn += "
LDAPSync SCRIPT Started ...
"; + response.setContentType("text/html"); response.getWriter().write(toReturn); } diff --git a/src/main/java/org/gcube/portal/ldapexport/LDAPExporter.java b/src/main/java/org/gcube/portal/ldapexport/LDAPSync.java similarity index 66% rename from src/main/java/org/gcube/portal/ldapexport/LDAPExporter.java rename to src/main/java/org/gcube/portal/ldapexport/LDAPSync.java index a159ce1..60c1890 100644 --- a/src/main/java/org/gcube/portal/ldapexport/LDAPExporter.java +++ b/src/main/java/org/gcube/portal/ldapexport/LDAPSync.java @@ -1,9 +1,5 @@ package org.gcube.portal.ldapexport; -import java.io.IOException; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; import java.util.List; import java.util.Properties; import java.util.ResourceBundle; @@ -17,13 +13,8 @@ import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; -import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import org.gcube.common.portal.PortalContext; import org.gcube.portal.custom.communitymanager.OrganizationsUtil; @@ -32,35 +23,23 @@ import org.slf4j.LoggerFactory; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; -import com.liferay.portal.kernel.util.PropsUtil; import com.liferay.portal.model.Organization; import com.liferay.portal.model.User; -import com.liferay.portal.service.GroupLocalServiceUtil; import com.liferay.portal.service.OrganizationLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil; -@SuppressWarnings("serial") -public class LDAPExporter extends HttpServlet { +public class LDAPSync implements Runnable { + private static final Logger _log = LoggerFactory.getLogger(LDAPSync.class); - private static final Logger _log = LoggerFactory.getLogger(LDAPExporter.class); + private ResourceBundle rb; - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - sync(); + public LDAPSync() { + _log.debug("LDAPSync()"); } - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} - - private List getAllLiferayUsers() throws SystemException, PortalException { - String infraName = PortalContext.getConfiguration().getInfrastructureName(); - _log.debug("Reading users belonging to: /" + infraName); - - Organization rootInfra = OrganizationLocalServiceUtil.getOrganization(OrganizationsUtil.getCompany().getCompanyId(), infraName); - return UserLocalServiceUtil.getOrganizationUsers(rootInfra.getOrganizationId()); - } - - - public void sync() { - ResourceBundle rb = ResourceBundle.getBundle("org.gcube.portal.ldapexport.settings"); + @Override + public void run() { + ResourceBundle rb = ResourceBundle.getBundle("org.gcube.portal.settings"); _log.debug("Reading Portal Users ..."); List users = null; try { @@ -71,7 +50,6 @@ public class LDAPExporter extends HttpServlet { } _log.debug("Initializing LDAP exporter ..."); - String baseDN = rb.getString("LDAP_BASE_DN"); String ldapUrl = rb.getString("LDAP_PROVIDER_URL"); String ldapPrincipal = rb.getString("LDAP_SECURITY_PRINCIPAL"); String ldapPwd = rb.getString("LDAP_SECURITY_CREDENTIALS"); @@ -85,52 +63,27 @@ public class LDAPExporter extends HttpServlet { try { DirContext ctx = new InitialDirContext(env); - _log.debug("Initiating LDAP Sync ..."); for (User user : users) { updateUserInLDAP(user.getScreenName(), user.getFirstName(), user.getLastName(), user.getEmailAddress(), "{SHA}"+user.getPassword(), ctx, filter); _log.debug("Updated " + user.getScreenName()); } - //updateUserInLDAP("testino.testone", "Testa", "LastName", "email@email.com", "SECRET", ctx); - _log.debug("LDAP Sync done ... reading LDAP users now .."); - SearchControls ctls = new SearchControls(); - ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); - - NamingEnumeration answer = ctx.search(baseDN, filter, ctls); - - while (answer.hasMoreElements()) { - - SearchResult a = answer.nextElement(); - Attributes result = a.getAttributes(); - - if (result == null){ - System.out.println("Attributes not present"); - }else{ - Attribute attr = result.get("uid"); - if (attr != null){ - for (NamingEnumeration vals = attr.getAll(); vals.hasMoreElements(); System.out.println(vals.nextElement().toString())); - } - - attr = result.get("userPassword"); - - if (attr != null){ - for (NamingEnumeration vals = attr.getAll(); vals.hasMoreElements(); ) { - _log.debug("sha pwd: " + new String((byte[])vals.nextElement())); - } - } - } - - } } catch (NamingException e) { _log.error("Something went Wrong during LDAP Sync"); e.printStackTrace(); } - _log.debug("LDAP Sync Completed"); + _log.info("LDAP Sync Completed OK!"); + } - private static boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) throws NamingException { + private String getSubContext(String username) { + return "uid="+username+",ou=People,o=Liferay,ou=Organizations,dc=d4science,dc=org"; + } + + + private boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) throws NamingException { SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration answer = ctx.search(getSubContext(username), filter, ctls); @@ -146,7 +99,7 @@ public class LDAPExporter extends HttpServlet { * @param ctx * @throws NamingException */ - private static void updateUserInLDAP(String username, String name, String lastName, String email, String passwd, DirContext ctx, String filter) throws NamingException { + private void updateUserInLDAP(String username, String name, String lastName, String email, String passwd, DirContext ctx, String filter) throws NamingException { Attributes attributes=new BasicAttributes(); Attribute objectClass=new BasicAttribute("objectClass"); objectClass.add("inetOrgPerson"); @@ -177,7 +130,11 @@ public class LDAPExporter extends HttpServlet { } } - private static String getSubContext(String username) { - return "uid="+username+",ou=People,o=Liferay,ou=Organizations,dc=d4science,dc=org"; + private List getAllLiferayUsers() throws SystemException, PortalException { + String infraName = PortalContext.getConfiguration().getInfrastructureName(); + _log.debug("Reading users belonging to: /" + infraName); + + Organization rootInfra = OrganizationLocalServiceUtil.getOrganization(OrganizationsUtil.getCompany().getCompanyId(), infraName); + return UserLocalServiceUtil.getOrganizationUsers(rootInfra.getOrganizationId()); } } diff --git a/src/main/java/org/gcube/portal/ldapexport/settings.properties b/src/main/java/org/gcube/portal/ldapexport/settings.properties deleted file mode 100644 index e780d27..0000000 --- a/src/main/java/org/gcube/portal/ldapexport/settings.properties +++ /dev/null @@ -1,6 +0,0 @@ - -LDAP_PROVIDER_URL= -LDAP_BASE_DN= -LDAP_SECURITY_PRINCIPAL= -LDAP_SECURITY_CREDENTIALS= -LDAP_FILTER= diff --git a/src/main/java/org/gcube/portal/settings.properties b/src/main/java/org/gcube/portal/settings.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index b8c14c6..d897a0e 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -6,24 +6,15 @@ Email Comments Reader Webapp - read-mail - org.gcube.portal.socialmail.MailReader - - - - export-users - org.gcube.portal.ldapexport.LDAPExporter + start-peridic-tasks + org.gcube.portal.PortalSchedulerService - read-mail - /read-mail + start-peridic-tasks + /start-peridic-tasks - - export-users - /export-users -