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

156 lines
5.5 KiB
Java

package org.gcube.dataanalysis.dataminer.poolmanager.util;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.net.URLEncoder;
import java.util.LinkedList;
***REMOVED***
import org.apache.activemq.transport.stomp.Stomp.Headers.Send;
***REMOVED***
import org.gcube.common.authorization.library.AuthorizationEntry;
***REMOVED***
***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.json.JSONArray;
import org.json.JSONObject;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
public class SendMail extends StandardLocalInfraAlgorithm ***REMOVED***
public SendMail() ***REMOVED***
***REMOVED*** TODO Auto-generated constructor stub
***REMOVED***
public void sendNotification(String subject, String body) throws Exception ***REMOVED***
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()*/this.username(SecurityTokenProvider.instance.get()) + "&subject=" + subject + "&body="
+ body;
String response = HttpRequest.sendPostRequest(requestForMessage, requestParameters);
AnalysisLogger.getLogger().debug("Emailing System->Emailing response OK ");
if (response == null) ***REMOVED***
Exception e = new Exception("Error in email sending response");
throw e;
***REMOVED***
***REMOVED***
***REMOVED*** public void notifySubmitter(String a, String b) throws Exception ***REMOVED***
***REMOVED*** NotificationHelper nh = new NotificationHelper();
***REMOVED*** super.sendNotification(nh.getSubject(),
***REMOVED*** nh.getBody());
***REMOVED*** ***REMOVED***
public String username(String token) throws ObjectNotFound, Exception ***REMOVED***
AuthorizationEntry entry = authorizationService().get(token);
System.out.println(entry.getClientInfo().getId());
return entry.getClientInfo().getId();
***REMOVED***
public String retrieveAdminRole() throws Exception ***REMOVED***
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:***REMOVED***", "http:***REMOVED***").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) ***REMOVED***
Exception e = new Exception("Error in querying the recipient");
throw e;
***REMOVED***
return response;
***REMOVED***
public String getAdmins() throws Exception ***REMOVED***
List<String> s = new LinkedList<String>();
JSONObject obj = new JSONObject(this.retrieveAdminRole());
JSONArray data = obj.getJSONArray("result");
if (data != null) ***REMOVED***
String[] names = new String[data.length()];
for (int i = 0; i < data.length(); i++) ***REMOVED***
names[i] = data.getString(i);
s.add(names[i]);
***REMOVED***
s.add(this.username(SecurityTokenProvider.instance.get()));
***REMOVED***
return s.toString().replace("[", "").replace("]", "");
***REMOVED***
***REMOVED***
public String getDescription() ***REMOVED***
***REMOVED***
***REMOVED***
***REMOVED***
***REMOVED***
public void init() throws Exception ***REMOVED***
***REMOVED***
***REMOVED***
***REMOVED***
protected void process() throws Exception ***REMOVED***
***REMOVED***
***REMOVED***
***REMOVED***
protected void setInputParameters() ***REMOVED***
***REMOVED***
***REMOVED***
***REMOVED***
public void shutdown() ***REMOVED***
***REMOVED***
***REMOVED***
public static void main(String[] args) throws Exception ***REMOVED***
ScopeProvider.instance.set("/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab");
SecurityTokenProvider.instance.set("***REMOVED***");
***REMOVED***ScopeProvider.instance.set("/gcube/devNext");
***REMOVED***SecurityTokenProvider.instance.set("***REMOVED***");
NotificationHelper nh = new NotificationHelper();
SendMail sm = new SendMail();
sm.sendNotification(nh.getFailedSubject(), nh.getFailedBody("test failed"));
***REMOVED*** sm.username(SecurityTokenProvider.instance.get());
***REMOVED*** sm.retrieveAdminRole();
***REMOVED***System.out.println(sm.getAdmins());
***REMOVED*** sm.sendNotification("test", "test");
***REMOVED***
***REMOVED***