2017-07-05 17:50:42 +02:00
|
|
|
package org.gcube.dataanalysis.dataminer.poolmanager.util;
|
|
|
|
|
2017-07-06 14:54:04 +02:00
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
2017-07-24 16:50:38 +02:00
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
2017-07-06 14:54:04 +02:00
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
import java.net.URLEncoder;
|
2017-07-06 14:54:04 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-07-05 17:50:42 +02:00
|
|
|
|
|
|
|
import org.apache.activemq.transport.stomp.Stomp.Headers.Send;
|
2017-07-07 14:52:56 +02:00
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
2017-07-06 14:54:04 +02:00
|
|
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
2017-07-05 17:50:42 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2017-07-24 16:50:38 +02:00
|
|
|
import org.gcube.common.encryption.StringEncrypter;
|
|
|
|
import org.gcube.common.resources.gcore.GenericResource;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
2017-07-05 17:50:42 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.contentmanagement.graphtools.utils.HttpRequest;
|
|
|
|
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
|
|
|
|
import org.gcube.dataanalysis.ecoengine.interfaces.StandardLocalInfraAlgorithm;
|
|
|
|
import org.gcube.dataanalysis.executor.util.InfraRetrieval;
|
2017-07-24 16:50:38 +02:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.smartgears.ContextProvider;
|
|
|
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
2017-07-06 14:54:04 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
|
|
|
|
public class SendMail extends StandardLocalInfraAlgorithm {
|
|
|
|
|
|
|
|
public SendMail() {
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
}
|
|
|
|
|
2017-07-06 17:34:45 +02:00
|
|
|
public void sendNotification(String subject, String body) throws Exception {
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger()
|
|
|
|
.debug("Emailing System->Starting request of email in scope " + ScopeProvider.instance.get());
|
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
|
2017-07-06 17:34:45 +02:00
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
if (!serviceAddress.endsWith("/"))
|
2017-07-06 17:34:45 +02:00
|
|
|
serviceAddress = serviceAddress + "/";
|
|
|
|
|
|
|
|
String requestForMessage = serviceAddress + "messages/writeMessageToUsers" + "?gcube-token="
|
|
|
|
+ SecurityTokenProvider.instance.get();
|
|
|
|
requestForMessage = requestForMessage.replace("http://", "https://").replace(":80", "");
|
2017-07-05 17:50:42 +02:00
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug("Emailing System->Request url is going to be " + requestForMessage);
|
|
|
|
|
|
|
|
// put the sender, the recipients, subject and body of the mail here
|
2017-07-06 17:34:45 +02:00
|
|
|
subject = URLEncoder.encode(subject, "UTF-8");
|
|
|
|
body = URLEncoder.encode(body, "UTF-8");
|
2017-07-07 14:52:56 +02:00
|
|
|
String requestParameters = "&sender=dataminer&recipients=" + this.getAdmins() + "&subject=" + subject + "&body="
|
2017-07-06 17:34:45 +02:00
|
|
|
+ body;
|
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
String response = HttpRequest.sendPostRequest(requestForMessage, requestParameters);
|
|
|
|
AnalysisLogger.getLogger().debug("Emailing System->Emailing response OK ");
|
2017-07-06 17:34:45 +02:00
|
|
|
|
|
|
|
if (response == null) {
|
2017-07-05 17:50:42 +02:00
|
|
|
Exception e = new Exception("Error in email sending response");
|
|
|
|
throw e;
|
|
|
|
}
|
2017-07-06 17:34:45 +02:00
|
|
|
}
|
2017-07-05 17:50:42 +02:00
|
|
|
|
2017-07-06 17:34:45 +02:00
|
|
|
// public void notifySubmitter(String a, String b) throws Exception {
|
|
|
|
// NotificationHelper nh = new NotificationHelper();
|
|
|
|
// super.sendNotification(nh.getSubject(),
|
|
|
|
// nh.getBody());
|
|
|
|
// }
|
2017-07-05 17:50:42 +02:00
|
|
|
|
2017-07-06 17:34:45 +02:00
|
|
|
public String username(String token) throws ObjectNotFound, Exception {
|
2017-07-06 14:54:04 +02:00
|
|
|
AuthorizationEntry entry = authorizationService().get(token);
|
|
|
|
System.out.println(entry.getClientInfo().getId());
|
|
|
|
return entry.getClientInfo().getId();
|
|
|
|
}
|
|
|
|
|
2017-07-07 14:52:56 +02:00
|
|
|
// public String retrieveAdminRole() throws Exception {
|
|
|
|
// String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
|
|
|
|
//
|
|
|
|
// if (!serviceAddress.endsWith("/"))
|
|
|
|
// serviceAddress = serviceAddress + "/";
|
|
|
|
//
|
|
|
|
// String requestForMessage = serviceAddress + "2/users/get-usernames-by-global-role";
|
|
|
|
// requestForMessage = requestForMessage.replace("https://", "http://").replace(":80", "");
|
|
|
|
// String requestParameters = "&role-name=Administrator" + "&gcube-token=" + SecurityTokenProvider.instance.get();
|
|
|
|
//
|
|
|
|
// String response = HttpRequest.sendGetRequest(requestForMessage, requestParameters);
|
|
|
|
// System.out.println(response.toString());
|
|
|
|
//
|
|
|
|
// if (response == null) {
|
|
|
|
// Exception e = new Exception("Error in querying the recipient");
|
|
|
|
// throw e;
|
|
|
|
// }
|
|
|
|
// return response;
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
public String getAdminRoles() throws Exception{
|
|
|
|
|
|
|
|
// discover social gcore endpoint
|
|
|
|
|
|
|
|
//GcoreEndpointReader ep = new GcoreEndpointReader(ScopeProvider.instance.get());
|
|
|
|
|
|
|
|
|
2017-07-06 14:54:04 +02:00
|
|
|
String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
|
2017-07-07 14:52:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
//String serviceAddress = ep.getResourceEntyName();//"https://socialnetworking1.d4science.org/social-networking-library-ws/rest/";
|
|
|
|
serviceAddress = serviceAddress.endsWith("/") ? serviceAddress : serviceAddress + "/";
|
2017-07-24 17:47:47 +02:00
|
|
|
serviceAddress+= "2/users/get-usernames-by-role?";
|
2017-07-13 18:13:18 +02:00
|
|
|
serviceAddress+= "role-name=DataMiner-Manager" + "&gcube-token=" + SecurityTokenProvider.instance.get();
|
2017-07-07 14:52:56 +02:00
|
|
|
|
|
|
|
System.out.println("Url is " + serviceAddress);
|
|
|
|
|
|
|
|
CloseableHttpClient client = HttpClientBuilder.create().build();
|
|
|
|
HttpGet getReq = new HttpGet(serviceAddress);
|
|
|
|
getReq.setHeader("accept", "application/json");
|
|
|
|
getReq.setHeader("content-type", "application/json");
|
|
|
|
System.out.println(EntityUtils.toString(client.execute(getReq).getEntity()));
|
|
|
|
|
|
|
|
return EntityUtils.toString(client.execute(getReq).getEntity());
|
|
|
|
|
2017-07-06 14:54:04 +02:00
|
|
|
}
|
2017-07-07 14:52:56 +02:00
|
|
|
|
|
|
|
|
2017-07-06 14:54:04 +02:00
|
|
|
|
|
|
|
public String getAdmins() throws Exception {
|
2017-07-25 14:17:52 +02:00
|
|
|
try{
|
2017-07-06 14:54:04 +02:00
|
|
|
List<String> s = new LinkedList<String>();
|
2017-07-07 14:52:56 +02:00
|
|
|
JSONObject obj = new JSONObject(this.getAdminRoles());
|
2017-07-06 14:54:04 +02:00
|
|
|
JSONArray data = obj.getJSONArray("result");
|
|
|
|
if (data != null) {
|
|
|
|
String[] names = new String[data.length()];
|
|
|
|
for (int i = 0; i < data.length(); i++) {
|
|
|
|
names[i] = data.getString(i);
|
|
|
|
s.add(names[i]);
|
|
|
|
}
|
|
|
|
s.add(this.username(SecurityTokenProvider.instance.get()));
|
|
|
|
}
|
2017-07-06 17:34:45 +02:00
|
|
|
return s.toString().replace("[", "").replace("]", "");
|
2017-07-25 14:17:52 +02:00
|
|
|
}
|
|
|
|
catch(Exception a){return "ngalante, lucio.lelii, roberto.cirillo, gianpaolo.coro, giancarlo.panichi, scarponi"; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-24 16:50:38 +02:00
|
|
|
|
|
|
|
public String getRootToken() throws Exception {
|
|
|
|
|
|
|
|
//ApplicationContext ctx = ContextProvider.get(); // get this info from
|
|
|
|
// SmartGears
|
|
|
|
//System.out.println(ctx.container().configuration().infrastructure());
|
|
|
|
String a = "";
|
|
|
|
SimpleQuery query2 = queryFor(ServiceEndpoint.class);
|
|
|
|
query2.addCondition("$resource/Profile/Name/text() eq 'SAIService'").setResult("$resource");
|
|
|
|
|
|
|
|
DiscoveryClient<ServiceEndpoint> client2 = clientFor(ServiceEndpoint.class);
|
|
|
|
List<ServiceEndpoint> df = client2.submit(query2);
|
|
|
|
|
|
|
|
for (ServiceEndpoint b : df) {
|
|
|
|
a = StringEncrypter.getEncrypter().decrypt(b.profile().accessPoints().iterator().next().password());
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init() throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
2017-07-06 17:34:45 +02:00
|
|
|
}
|
2017-07-05 17:50:42 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void process() throws Exception {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
2017-07-06 17:34:45 +02:00
|
|
|
}
|
2017-07-05 17:50:42 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setInputParameters() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
2017-07-06 17:34:45 +02:00
|
|
|
}
|
2017-07-05 17:50:42 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void shutdown() {
|
|
|
|
// TODO Auto-generated method stub
|
2017-07-06 17:34:45 +02:00
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
}
|
2017-07-06 17:34:45 +02:00
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
public static void main(String[] args) throws Exception {
|
2017-07-24 16:50:38 +02:00
|
|
|
//ScopeProvider.instance.set("/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab");
|
|
|
|
//SecurityTokenProvider.instance.set("3a23bfa4-4dfe-44fc-988f-194b91071dd2-843339462");
|
2017-07-06 17:34:45 +02:00
|
|
|
|
|
|
|
//ScopeProvider.instance.set("/gcube/devNext");
|
2017-07-07 14:52:56 +02:00
|
|
|
//SecurityTokenProvider.instance.set("aa6eec71-fe07-43ab-bd1c-f03df293e430-98187548");
|
2017-07-06 17:34:45 +02:00
|
|
|
|
2017-07-24 16:50:38 +02:00
|
|
|
//NotificationHelper nh = new NotificationHelper();
|
2017-07-06 17:34:45 +02:00
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
SendMail sm = new SendMail();
|
2017-07-24 16:50:38 +02:00
|
|
|
|
|
|
|
ScopeProvider.instance.set("/gcube/devNext/NextNext");
|
|
|
|
System.out.println(sm.getRootToken());
|
|
|
|
//sm.getGenericResourceByName("");
|
|
|
|
|
|
|
|
//sm.sendNotification(nh.getFailedSubject(), nh.getFailedBody("test failed"));
|
2017-07-06 17:34:45 +02:00
|
|
|
// sm.username(SecurityTokenProvider.instance.get());
|
2017-07-07 14:52:56 +02:00
|
|
|
//sm.retrieveAdminRole();
|
|
|
|
//sm.getAdminRoles();
|
|
|
|
//System.out.println(sm.getAdmins());
|
2017-07-06 17:34:45 +02:00
|
|
|
//System.out.println(sm.getAdmins());
|
|
|
|
// sm.sendNotification("test", "test");
|
2017-07-05 17:50:42 +02:00
|
|
|
}
|
|
|
|
}
|