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-09-07 18:46:26 +02:00
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
|
|
|
import java.io.OutputStreamWriter;
|
2017-11-29 16:29:01 +01:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2017-09-07 18:46:26 +02:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
***REMOVED***
|
|
|
|
import java.net.URLConnection;
|
2017-07-05 17:50:42 +02:00
|
|
|
import java.net.URLEncoder;
|
2017-07-06 14:54:04 +02:00
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|
2017-07-05 17:50:42 +02:00
|
|
|
|
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-05 17:50:42 +02:00
|
|
|
***REMOVED***
|
2017-07-06 14:54:04 +02:00
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
***REMOVED***
|
2017-09-07 18:46:26 +02:00
|
|
|
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
2017-07-05 17:50:42 +02:00
|
|
|
***REMOVED***
|
2017-11-29 16:29:01 +01:00
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.util.exception.EMailException;
|
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;
|
2017-07-06 14:54:04 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
public class SendMail {
|
2017-07-05 17:50:42 +02:00
|
|
|
|
|
|
|
public SendMail() {
|
|
|
|
***REMOVED*** TODO Auto-generated constructor stub
|
|
|
|
***REMOVED***
|
|
|
|
|
2017-11-29 16:29:01 +01:00
|
|
|
public void sendNotification(String subject, String body) throws EMailException {
|
2017-07-06 17:34:45 +02:00
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
***REMOVED***AnalysisLogger.getLogger().debug("Emailing System->Starting request of email in scope " + ScopeProvider.instance.get());
|
2017-07-06 17:34:45 +02:00
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
***REMOVED***String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
|
2017-07-06 17:34:45 +02:00
|
|
|
|
2017-11-29 16:29:01 +01:00
|
|
|
|
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
String serviceAddress = this.getSocialService();
|
|
|
|
|
|
|
|
|
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:***REMOVED***", "https:***REMOVED***").replace(":80", "");
|
2017-07-05 17:50:42 +02:00
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
***REMOVED***AnalysisLogger.getLogger().debug("Emailing System->Request url is going to be " + requestForMessage);
|
2017-07-05 17:50:42 +02:00
|
|
|
|
|
|
|
***REMOVED*** put the sender, the recipients, subject and body of the mail here
|
2017-11-29 16:29:01 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
subject = URLEncoder.encode(subject, "UTF-8");
|
|
|
|
body = URLEncoder.encode(body, "UTF-8");
|
|
|
|
***REMOVED***
|
|
|
|
catch (UnsupportedEncodingException e)
|
|
|
|
{
|
|
|
|
throw new EMailException(e);
|
|
|
|
***REMOVED***
|
|
|
|
|
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-09-07 18:46:26 +02:00
|
|
|
String response = this.sendPostRequest(requestForMessage, requestParameters);
|
|
|
|
***REMOVED***AnalysisLogger.getLogger().debug("Emailing System->Emailing response OK ");
|
|
|
|
|
2017-11-29 16:29:01 +01:00
|
|
|
if (response == null) throw new EMailException();
|
|
|
|
|
2017-07-05 17:50:42 +02:00
|
|
|
***REMOVED***
|
|
|
|
|
2017-07-06 17:34:45 +02:00
|
|
|
***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***
|
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();
|
|
|
|
***REMOVED***
|
|
|
|
|
2017-07-07 14:52:56 +02:00
|
|
|
***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***
|
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
public String getSocialService() {
|
|
|
|
SimpleQuery query = queryFor(GCoreEndpoint.class);
|
|
|
|
query.addCondition("$resource/Profile/ServiceName/text() eq 'SocialNetworking'");
|
|
|
|
DiscoveryClient<GCoreEndpoint> client = clientFor(GCoreEndpoint.class);
|
|
|
|
List<GCoreEndpoint> resources = client.submit(query);
|
|
|
|
String a = resources.get(0).profile().endpointMap().get("jersey-servlet").uri().toString();
|
|
|
|
return a;
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String sendPostRequest(String endpoint, String requestParameters) {
|
|
|
|
|
|
|
|
***REMOVED*** Build parameter string
|
|
|
|
String data = requestParameters;
|
|
|
|
try {
|
|
|
|
|
|
|
|
***REMOVED*** Send the request
|
|
|
|
URL url = new URL(endpoint);
|
|
|
|
URLConnection conn = url.openConnection();
|
|
|
|
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
|
|
|
|
|
|
|
|
***REMOVED*** write parameters
|
|
|
|
writer.write(data);
|
|
|
|
writer.flush();
|
|
|
|
|
|
|
|
***REMOVED*** Get the response
|
|
|
|
StringBuffer answer = new StringBuffer();
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
answer.append(line);
|
|
|
|
***REMOVED***
|
|
|
|
writer.close();
|
|
|
|
reader.close();
|
|
|
|
|
|
|
|
***REMOVED*** Output the response
|
|
|
|
return answer.toString();
|
|
|
|
|
|
|
|
***REMOVED*** catch (MalformedURLException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
***REMOVED*** catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
***REMOVED***
|
|
|
|
return null;
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-07 14:52:56 +02:00
|
|
|
|
|
|
|
public String getAdminRoles() throws Exception{
|
|
|
|
|
|
|
|
***REMOVED*** discover social gcore endpoint
|
|
|
|
|
|
|
|
***REMOVED***GcoreEndpointReader ep = new GcoreEndpointReader(ScopeProvider.instance.get());
|
|
|
|
|
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
String serviceAddress = this.getSocialService();
|
|
|
|
***REMOVED***String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
|
2017-07-07 14:52:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
***REMOVED***String serviceAddress = ep.getResourceEntyName();***REMOVED***"https:***REMOVED***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());
|
|
|
|
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
2017-07-06 14:54:04 +02:00
|
|
|
|
2017-11-29 16:29:01 +01:00
|
|
|
public String getAdmins(){
|
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]);
|
|
|
|
***REMOVED***
|
|
|
|
s.add(this.username(SecurityTokenProvider.instance.get()));
|
|
|
|
***REMOVED***
|
2017-07-06 17:34:45 +02:00
|
|
|
return s.toString().replace("[", "").replace("]", "");
|
2017-07-25 14:17:52 +02:00
|
|
|
***REMOVED***
|
2017-11-29 16:29:01 +01:00
|
|
|
catch(Exception a){return "ciro.formisano, lucio.lelii, roberto.cirillo, gianpaolo.coro, giancarlo.panichi, scarponi"; ***REMOVED***
|
2017-07-25 14:17:52 +02:00
|
|
|
|
|
|
|
***REMOVED***
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-24 16:50:38 +02:00
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
***REMOVED*** public String getRootToken() throws Exception {
|
2017-07-24 16:50:38 +02:00
|
|
|
***REMOVED***
|
2017-09-07 18:46:26 +02:00
|
|
|
***REMOVED*** ***REMOVED***ApplicationContext ctx = ContextProvider.get(); ***REMOVED*** get this info from
|
|
|
|
***REMOVED*** ***REMOVED*** SmartGears
|
|
|
|
***REMOVED*** ***REMOVED***System.out.println(ctx.container().configuration().infrastructure());
|
|
|
|
***REMOVED*** String a = "";
|
|
|
|
***REMOVED*** SimpleQuery query2 = queryFor(ServiceEndpoint.class);
|
|
|
|
***REMOVED*** query2.addCondition("$resource/Profile/Name/text() eq 'SAIService'").setResult("$resource");
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED*** DiscoveryClient<ServiceEndpoint> client2 = clientFor(ServiceEndpoint.class);
|
|
|
|
***REMOVED*** List<ServiceEndpoint> df = client2.submit(query2);
|
|
|
|
***REMOVED***
|
|
|
|
***REMOVED*** for (ServiceEndpoint b : df) {
|
|
|
|
***REMOVED*** a = StringEncrypter.getEncrypter().decrypt(b.profile().accessPoints().iterator().next().password());
|
|
|
|
***REMOVED*** ***REMOVED***
|
|
|
|
***REMOVED*** return a;
|
|
|
|
***REMOVED******REMOVED***
|
2017-07-24 16:50:38 +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
|
|
|
***REMOVED***ScopeProvider.instance.set("/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab");
|
|
|
|
***REMOVED***SecurityTokenProvider.instance.set("3a23bfa4-4dfe-44fc-988f-194b91071dd2-843339462");
|
2017-07-06 17:34:45 +02:00
|
|
|
|
|
|
|
***REMOVED***ScopeProvider.instance.set("/gcube/devNext");
|
2017-07-07 14:52:56 +02:00
|
|
|
***REMOVED***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
|
|
|
***REMOVED***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
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
***REMOVED***ScopeProvider.instance.set("/gcube/devNext/NextNext");
|
|
|
|
***REMOVED***SecurityTokenProvider.instance.set("708e7eb8-11a7-4e9a-816b-c9ed7e7e99fe-98187548");
|
|
|
|
***REMOVED***System.out.println(sm.getRootToken());
|
2017-07-24 16:50:38 +02:00
|
|
|
***REMOVED***sm.getGenericResourceByName("");
|
|
|
|
|
2017-09-07 18:46:26 +02:00
|
|
|
ScopeProvider.instance.set("/gcube/preprod/preVRE");
|
|
|
|
SecurityTokenProvider.instance.set("2eceaf27-0e22-4dbe-8075-e09eff199bf9-98187548");
|
|
|
|
|
|
|
|
|
2017-07-24 16:50:38 +02:00
|
|
|
***REMOVED***sm.sendNotification(nh.getFailedSubject(), nh.getFailedBody("test failed"));
|
2017-07-06 17:34:45 +02:00
|
|
|
***REMOVED*** sm.username(SecurityTokenProvider.instance.get());
|
2017-07-07 14:52:56 +02:00
|
|
|
***REMOVED***sm.retrieveAdminRole();
|
|
|
|
***REMOVED***sm.getAdminRoles();
|
2017-09-07 18:46:26 +02:00
|
|
|
System.out.println(sm.getAdmins());
|
2017-07-07 14:52:56 +02:00
|
|
|
***REMOVED***System.out.println(sm.getAdmins());
|
2017-09-07 18:46:26 +02:00
|
|
|
***REMOVED***sm.sendNotification("test", "test");
|
|
|
|
***REMOVED***System.out.println(sm.getSocialService());
|
2017-07-05 17:50:42 +02:00
|
|
|
***REMOVED***
|
|
|
|
***REMOVED***
|