merged also with LDAP Sync script

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-mail-servlet@117368 82a268e6-3cf1-43bd-a215-b396298e98cf
Feature/21689
Massimiliano Assante 9 years ago
parent 843fa4420c
commit 6e242f1f83

@ -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 = "<DIV>Check Notification Email Started ... </DIV>";
ScheduledExecutorService ldapScheduler = Executors.newScheduledThreadPool(1);
ldapScheduler.scheduleAtFixedRate(new LDAPSync(), 0, LDAP_MINUTES_DELAY, TimeUnit.MINUTES);
toReturn += "<DIV>LDAPSync SCRIPT Started ... </DIV>";
response.setContentType("text/html");
response.getWriter().write(toReturn);
}

@ -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<User> 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<User> 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<SearchResult> 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<SearchResult> 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<User> 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());
}
}

@ -1,6 +0,0 @@
LDAP_PROVIDER_URL=
LDAP_BASE_DN=
LDAP_SECURITY_PRINCIPAL=
LDAP_SECURITY_CREDENTIALS=
LDAP_FILTER=

@ -6,24 +6,15 @@
<display-name>Email Comments Reader Webapp</display-name>
<servlet>
<servlet-name>read-mail</servlet-name>
<servlet-class>org.gcube.portal.socialmail.MailReader</servlet-class>
</servlet>
<servlet>
<servlet-name>export-users</servlet-name>
<servlet-class>org.gcube.portal.ldapexport.LDAPExporter</servlet-class>
<servlet-name>start-peridic-tasks</servlet-name>
<servlet-class>org.gcube.portal.PortalSchedulerService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>read-mail</servlet-name>
<url-pattern>/read-mail</url-pattern>
<servlet-name>start-peridic-tasks</servlet-name>
<url-pattern>/start-peridic-tasks</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>export-users</servlet-name>
<url-pattern>/export-users</url-pattern>
</servlet-mapping>
</web-app>

Loading…
Cancel
Save