You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
social-mail-servlet/src/main/java/org/gcube/portal/socialmail/MailReader.java

102 lines
3.6 KiB
Java

package org.gcube.portal.socialmail;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.IOException;
import java.util.List;
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;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SuppressWarnings("serial")
public class MailReader 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 DatabookStore store;
private static String portalName;
private static String host;
private static String user;
private static String password;
public void init() {
store = new DBCassandraAstyanaxImpl();
portalName = PortalContext.getPortalInstanceName();
PortalContext context = PortalContext.getConfiguration();
String scope = "/" + context.getInfrastructureName();
ScopeProvider.instance.set(scope);
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq 'Portal'");
query.addCondition("$resource/Profile/Name/text() eq '" + portalName + "'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> list = client.submit(query);
if (list == null || list.isEmpty()) {
_log.error("Could not find any Service endpoint registred in the infrastructure for this portal: " + portalName);
}
else if (list.size() > 1) {
_log.warn("Found more than one Service endpoint registred in the infrastructure for this portal: " + portalName);
}
else {
for (ServiceEndpoint res : list) {
AccessPoint found = res.profile().accessPoints().iterator().next();
host = found.address();
user = found.username();
String encrPassword = found.password();
try {
password = StringEncrypter.getEncrypter().decrypt( encrPassword);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
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 ... ";
response.setContentType("text/html");
response.getWriter().write(toReturn);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
public static void main(String[] args) {
new MailReader().init();
System.out.println("Scheduling periodic task ... ");
//System.out.println(host + user + password);
scheduler.scheduleAtFixedRate(new PeriodicTask(store, portalName, host, user, password), 0, MINUTES_DELAY, TimeUnit.MINUTES);
}
}