This commit is contained in:
Nunzio Andrea Galante 2017-07-07 12:52:56 +00:00
parent b8b07462ec
commit 9247b7019d
2 changed files with 66 additions and 26 deletions

View File

@ -41,6 +41,11 @@
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>
<dependency> <dependency>

View File

@ -7,6 +7,10 @@ import java.net.URLEncoder;
***REMOVED*** ***REMOVED***
import org.apache.activemq.transport.stomp.Stomp.Headers.Send; 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*** ***REMOVED***
import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.AuthorizationEntry;
***REMOVED*** ***REMOVED***
@ -44,7 +48,7 @@ public class SendMail extends StandardLocalInfraAlgorithm {
***REMOVED*** put the sender, the recipients, subject and body of the mail here ***REMOVED*** put the sender, the recipients, subject and body of the mail here
subject = URLEncoder.encode(subject, "UTF-8"); subject = URLEncoder.encode(subject, "UTF-8");
body = URLEncoder.encode(body, "UTF-8"); body = URLEncoder.encode(body, "UTF-8");
String requestParameters = "&sender=dataminer&recipients=" + this.getAdmins()/*this.username(SecurityTokenProvider.instance.get())*/ + "&subject=" + subject + "&body=" String requestParameters = "&sender=dataminer&recipients=" + this.getAdmins() + "&subject=" + subject + "&body="
+ body; + body;
String response = HttpRequest.sendPostRequest(requestForMessage, requestParameters); String response = HttpRequest.sendPostRequest(requestForMessage, requestParameters);
@ -68,30 +72,60 @@ public class SendMail extends StandardLocalInfraAlgorithm {
return entry.getClientInfo().getId(); return entry.getClientInfo().getId();
***REMOVED*** ***REMOVED***
public String retrieveAdminRole() throws Exception { ***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()); String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
if (!serviceAddress.endsWith("/"))
serviceAddress = serviceAddress + "/";
String requestForMessage = serviceAddress + "2/users/get-usernames-by-global-role"; ***REMOVED***String serviceAddress = ep.getResourceEntyName();***REMOVED***"https:***REMOVED***socialnetworking1.d4science.org/social-networking-library-ws/rest/";
requestForMessage = requestForMessage.replace("https:***REMOVED***", "http:***REMOVED***").replace(":80", ""); serviceAddress = serviceAddress.endsWith("/") ? serviceAddress : serviceAddress + "/";
String requestParameters = "role-name=Administrator" + "&gcube-token=" + SecurityTokenProvider.instance.get(); serviceAddress+= "2/users/get-usernames-by-global-role?";
serviceAddress+= "role-name=Administrator" + "&gcube-token=" + SecurityTokenProvider.instance.get();
String response = HttpRequest.sendGetRequest(requestForMessage, requestParameters); System.out.println("Url is " + serviceAddress);
System.out.println(response.toString());
if (response == null) { CloseableHttpClient client = HttpClientBuilder.create().build();
Exception e = new Exception("Error in querying the recipient"); HttpGet getReq = new HttpGet(serviceAddress);
throw e; getReq.setHeader("accept", "application/json");
***REMOVED*** getReq.setHeader("content-type", "application/json");
return response; System.out.println(EntityUtils.toString(client.execute(getReq).getEntity()));
return EntityUtils.toString(client.execute(getReq).getEntity());
***REMOVED*** ***REMOVED***
public String getAdmins() throws Exception { public String getAdmins() throws Exception {
List<String> s = new LinkedList<String>(); List<String> s = new LinkedList<String>();
JSONObject obj = new JSONObject(this.retrieveAdminRole()); JSONObject obj = new JSONObject(this.getAdminRoles());
JSONArray data = obj.getJSONArray("result"); JSONArray data = obj.getJSONArray("result");
if (data != null) { if (data != null) {
String[] names = new String[data.length()]; String[] names = new String[data.length()];
@ -101,7 +135,6 @@ public class SendMail extends StandardLocalInfraAlgorithm {
***REMOVED*** ***REMOVED***
s.add(this.username(SecurityTokenProvider.instance.get())); s.add(this.username(SecurityTokenProvider.instance.get()));
***REMOVED*** ***REMOVED***
return s.toString().replace("[", "").replace("]", ""); return s.toString().replace("[", "").replace("]", "");
***REMOVED*** ***REMOVED***
@ -140,14 +173,16 @@ public class SendMail extends StandardLocalInfraAlgorithm {
SecurityTokenProvider.instance.set("3a23bfa4-4dfe-44fc-988f-194b91071dd2-843339462"); SecurityTokenProvider.instance.set("3a23bfa4-4dfe-44fc-988f-194b91071dd2-843339462");
***REMOVED***ScopeProvider.instance.set("/gcube/devNext"); ***REMOVED***ScopeProvider.instance.set("/gcube/devNext");
***REMOVED***SecurityTokenProvider.instance.set("708e7eb8-11a7-4e9a-816b-c9ed7e7e99fe-98187548"); ***REMOVED***SecurityTokenProvider.instance.set("aa6eec71-fe07-43ab-bd1c-f03df293e430-98187548");
NotificationHelper nh = new NotificationHelper(); NotificationHelper nh = new NotificationHelper();
SendMail sm = new SendMail(); SendMail sm = new SendMail();
sm.sendNotification(nh.getFailedSubject(), nh.getFailedBody("test failed")); sm.sendNotification(nh.getFailedSubject(), nh.getFailedBody("test failed"));
***REMOVED*** sm.username(SecurityTokenProvider.instance.get()); ***REMOVED*** sm.username(SecurityTokenProvider.instance.get());
***REMOVED*** sm.retrieveAdminRole(); ***REMOVED***sm.retrieveAdminRole();
***REMOVED***sm.getAdminRoles();
***REMOVED***System.out.println(sm.getAdmins());
***REMOVED***System.out.println(sm.getAdmins()); ***REMOVED***System.out.println(sm.getAdmins());
***REMOVED*** sm.sendNotification("test", "test"); ***REMOVED*** sm.sendNotification("test", "test");
***REMOVED*** ***REMOVED***