dataminer-pool-manager/src/main/java/org/gcube/dataanalysis/dataminer/poolmanager/util/SendMail.java

230 lines
8.5 KiB
Java

package org.gcube.dataanalysis.dataminer.poolmanager.util;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.net.URLEncoder;
***REMOVED***
***REMOVED***
import org.apache.activemq.transport.stomp.Stomp.Headers.Send;
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;
***REMOVED***
import org.gcube.common.authorization.library.AuthorizationEntry;
***REMOVED***
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.ServiceEndpoint;
***REMOVED***
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;
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;
import org.json.JSONArray;
import org.json.JSONObject;
public class SendMail extends StandardLocalInfraAlgorithm {
public SendMail() {
***REMOVED*** TODO Auto-generated constructor stub
***REMOVED***
public void sendNotification(String subject, String body) throws Exception {
AnalysisLogger.getLogger()
.debug("Emailing System->Starting request of email in scope " + ScopeProvider.instance.get());
String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
if (!serviceAddress.endsWith("/"))
serviceAddress = serviceAddress + "/";
String requestForMessage = serviceAddress + "messages/writeMessageToUsers" + "?gcube-token="
+ SecurityTokenProvider.instance.get();
requestForMessage = requestForMessage.replace("http:***REMOVED***", "https:***REMOVED***").replace(":80", "");
AnalysisLogger.getLogger().debug("Emailing System->Request url is going to be " + requestForMessage);
***REMOVED*** put the sender, the recipients, subject and body of the mail here
subject = URLEncoder.encode(subject, "UTF-8");
body = URLEncoder.encode(body, "UTF-8");
String requestParameters = "&sender=dataminer&recipients=" + this.getAdmins() + "&subject=" + subject + "&body="
+ body;
String response = HttpRequest.sendPostRequest(requestForMessage, requestParameters);
AnalysisLogger.getLogger().debug("Emailing System->Emailing response OK ");
if (response == null) {
Exception e = new Exception("Error in email sending response");
throw e;
***REMOVED***
***REMOVED***
***REMOVED*** public void notifySubmitter(String a, String b) throws Exception {
***REMOVED*** NotificationHelper nh = new NotificationHelper();
***REMOVED*** super.sendNotification(nh.getSubject(),
***REMOVED*** nh.getBody());
***REMOVED*** ***REMOVED***
public String username(String token) throws ObjectNotFound, Exception {
AuthorizationEntry entry = authorizationService().get(token);
System.out.println(entry.getClientInfo().getId());
return entry.getClientInfo().getId();
***REMOVED***
***REMOVED*** public String retrieveAdminRole() throws Exception {
***REMOVED*** String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
***REMOVED***
***REMOVED*** if (!serviceAddress.endsWith("/"))
***REMOVED*** serviceAddress = serviceAddress + "/";
***REMOVED***
***REMOVED*** String requestForMessage = serviceAddress + "2/users/get-usernames-by-global-role";
***REMOVED*** requestForMessage = requestForMessage.replace("https:***REMOVED***", "http:***REMOVED***").replace(":80", "");
***REMOVED*** String requestParameters = "&role-name=Administrator" + "&gcube-token=" + SecurityTokenProvider.instance.get();
***REMOVED***
***REMOVED*** String response = HttpRequest.sendGetRequest(requestForMessage, requestParameters);
***REMOVED*** System.out.println(response.toString());
***REMOVED***
***REMOVED*** if (response == null) {
***REMOVED*** Exception e = new Exception("Error in querying the recipient");
***REMOVED*** throw e;
***REMOVED*** ***REMOVED***
***REMOVED*** return response;
***REMOVED***
***REMOVED******REMOVED***
public String getAdminRoles() throws Exception{
***REMOVED*** discover social gcore endpoint
***REMOVED***GcoreEndpointReader ep = new GcoreEndpointReader(ScopeProvider.instance.get());
String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
***REMOVED***String serviceAddress = ep.getResourceEntyName();***REMOVED***"https:***REMOVED***socialnetworking1.d4science.org/social-networking-library-ws/rest/";
serviceAddress = serviceAddress.endsWith("/") ? serviceAddress : serviceAddress + "/";
serviceAddress+= "2/users/get-usernames-by-role?";
serviceAddress+= "role-name=DataMiner-Manager" + "&gcube-token=" + SecurityTokenProvider.instance.get();
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());
***REMOVED***
public String getAdmins() throws Exception {
List<String> s = new LinkedList<String>();
JSONObject obj = new JSONObject(this.getAdminRoles());
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]);
***REMOVED***
s.add(this.username(SecurityTokenProvider.instance.get()));
***REMOVED***
return s.toString().replace("[", "").replace("]", "");
***REMOVED***
public String getRootToken() throws Exception {
***REMOVED***ApplicationContext ctx = ContextProvider.get(); ***REMOVED*** get this info from
***REMOVED*** SmartGears
***REMOVED***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());
***REMOVED***
return a;
***REMOVED***
@Override
public String getDescription() {
***REMOVED*** TODO Auto-generated method stub
return null;
***REMOVED***
@Override
public void init() throws Exception {
***REMOVED*** TODO Auto-generated method stub
***REMOVED***
@Override
protected void process() throws Exception {
***REMOVED*** TODO Auto-generated method stub
***REMOVED***
@Override
protected void setInputParameters() {
***REMOVED*** TODO Auto-generated method stub
***REMOVED***
@Override
public void shutdown() {
***REMOVED*** TODO Auto-generated method stub
***REMOVED***
public static void main(String[] args) throws Exception {
***REMOVED***ScopeProvider.instance.set("/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab");
***REMOVED***SecurityTokenProvider.instance.set("3a23bfa4-4dfe-44fc-988f-194b91071dd2-843339462");
***REMOVED***ScopeProvider.instance.set("/gcube/devNext");
***REMOVED***SecurityTokenProvider.instance.set("aa6eec71-fe07-43ab-bd1c-f03df293e430-98187548");
***REMOVED***NotificationHelper nh = new NotificationHelper();
SendMail sm = new SendMail();
ScopeProvider.instance.set("/gcube/devNext/NextNext");
System.out.println(sm.getRootToken());
***REMOVED***sm.getGenericResourceByName("");
***REMOVED***sm.sendNotification(nh.getFailedSubject(), nh.getFailedBody("test failed"));
***REMOVED*** sm.username(SecurityTokenProvider.instance.get());
***REMOVED***sm.retrieveAdminRole();
***REMOVED***sm.getAdminRoles();
***REMOVED***System.out.println(sm.getAdmins());
***REMOVED***System.out.println(sm.getAdmins());
***REMOVED*** sm.sendNotification("test", "test");
***REMOVED***
***REMOVED***