ref 19081: DM Pool Manager uses a deprecated social networking api
https://support.d4science.org/issues/19081 Updated to new Social Networking API
This commit is contained in:
parent
bd981214c6
commit
9501585f09
|
@ -17,7 +17,11 @@
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
<classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
|
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
|
|
@ -2,4 +2,5 @@ eclipse.preferences.version=1
|
||||||
encoding//src/main/java=UTF-8
|
encoding//src/main/java=UTF-8
|
||||||
encoding//src/main/resources=UTF-8
|
encoding//src/main/resources=UTF-8
|
||||||
encoding//src/test/java=UTF-8
|
encoding//src/test/java=UTF-8
|
||||||
|
encoding//src/test/resources=UTF-8
|
||||||
encoding/<project>=UTF-8
|
encoding/<project>=UTF-8
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
||||||
<wb-module deploy-name="dataminer-pool-manager-2.6.0-SNAPSHOT">
|
<wb-module deploy-name="dataminer-pool-manager-2.7.0-SNAPSHOT">
|
||||||
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
|
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
|
||||||
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
|
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
|
||||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
|
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
<ReleaseNotes>
|
<ReleaseNotes>
|
||||||
|
<Changeset component="org.gcube.dataanalysis.dataminer-pool-manager.2-7-0" date="2020-04-16">
|
||||||
|
<Change>Updated to new Social Networking API [ticket #19081]</Change>
|
||||||
|
</Changeset>
|
||||||
<Changeset component="org.gcube.dataanalysis.dataminer-pool-manager.2-6-0" date="2019-12-11">
|
<Changeset component="org.gcube.dataanalysis.dataminer-pool-manager.2-6-0" date="2019-12-11">
|
||||||
<Change>Updated to Git and Jenkins</Change>
|
<Change>Updated to Git and Jenkins</Change>
|
||||||
<Change>Knime 4.1 added [ticket #18190]</Change>
|
<Change>Knime 4.1 added [ticket #18190]</Change>
|
||||||
|
|
2
pom.xml
2
pom.xml
|
@ -12,7 +12,7 @@
|
||||||
<groupId>org.gcube.dataanalysis</groupId>
|
<groupId>org.gcube.dataanalysis</groupId>
|
||||||
<artifactId>dataminer-pool-manager</artifactId>
|
<artifactId>dataminer-pool-manager</artifactId>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
<version>2.6.0</version>
|
<version>2.7.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<name>dataminer-pool-manager</name>
|
<name>dataminer-pool-manager</name>
|
||||||
<description>DataMiner Pool Manager is a service to support the integration of algorithms in D4Science Infrastructure</description>
|
<description>DataMiner Pool Manager is a service to support the integration of algorithms in D4Science Infrastructure</description>
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration;
|
package org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
@ -11,42 +13,34 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.tmatesoft.svn.core.SVNException;
|
import org.tmatesoft.svn.core.SVNException;
|
||||||
|
|
||||||
public class DMPMClientConfiguratorManager
|
public class DMPMClientConfiguratorManager {
|
||||||
{
|
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private Properties defaultAdmins;
|
private Properties defaultAdmins;
|
||||||
private String admins;
|
|
||||||
|
|
||||||
static DMPMClientConfiguratorManager instance;
|
static DMPMClientConfiguratorManager instance;
|
||||||
|
|
||||||
private ScopedCacheMap cacheMap;
|
private ScopedCacheMap cacheMap;
|
||||||
|
|
||||||
private DMPMClientConfiguratorManager ()
|
private DMPMClientConfiguratorManager() {
|
||||||
{
|
|
||||||
this.admins = null;
|
|
||||||
this.cacheMap = new ScopedCacheMap();
|
this.cacheMap = new ScopedCacheMap();
|
||||||
this.logger = LoggerFactory.getLogger(DMPMClientConfiguratorManager.class);
|
this.logger = LoggerFactory.getLogger(DMPMClientConfiguratorManager.class);
|
||||||
this.defaultAdmins = new Properties();
|
this.defaultAdmins = new Properties();
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
this.defaultAdmins.load(this.getClass().getResourceAsStream("/default.admins"));
|
this.defaultAdmins.load(this.getClass().getResourceAsStream("/default.admins"));
|
||||||
this.logger.debug("Default users successfully loaded");
|
this.logger.debug("Default users successfully loaded");
|
||||||
} catch (Exception e)
|
} catch (Exception e) {
|
||||||
{
|
|
||||||
this.logger.error("Unable to get default users", e);
|
this.logger.error("Unable to get default users", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ClientConfigurationCache getCurrentCache ()
|
private ClientConfigurationCache getCurrentCache() {
|
||||||
{
|
|
||||||
String currentScope = ScopeProvider.instance.get();
|
String currentScope = ScopeProvider.instance.get();
|
||||||
this.logger.debug("Current scope = " + currentScope);
|
this.logger.debug("Current scope = " + currentScope);
|
||||||
this.logger.debug("Getting current configuration cache");
|
this.logger.debug("Getting current configuration cache");
|
||||||
ClientConfigurationCache cache = this.cacheMap.get(currentScope);
|
ClientConfigurationCache cache = this.cacheMap.get(currentScope);
|
||||||
|
|
||||||
if (cache == null)
|
if (cache == null) {
|
||||||
{
|
|
||||||
this.logger.debug("Cache not created yet, creating...");
|
this.logger.debug("Cache not created yet, creating...");
|
||||||
cache = new ClientConfigurationCache();
|
cache = new ClientConfigurationCache();
|
||||||
this.cacheMap.put(currentScope, cache);
|
this.cacheMap.put(currentScope, cache);
|
||||||
|
@ -55,49 +49,42 @@ public class DMPMClientConfiguratorManager
|
||||||
|
|
||||||
return cache;
|
return cache;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DMPMClientConfiguratorManager getInstance() {
|
||||||
public static DMPMClientConfiguratorManager getInstance ()
|
if (instance == null)
|
||||||
{
|
instance = new DMPMClientConfiguratorManager();
|
||||||
if (instance == null) instance = new DMPMClientConfiguratorManager();
|
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Configuration getProductionConfiguration ()
|
public Configuration getProductionConfiguration() {
|
||||||
{
|
|
||||||
return new ConfigurationImpl(CONFIGURATIONS.PROD, getCurrentCache());
|
return new ConfigurationImpl(CONFIGURATIONS.PROD, getCurrentCache());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Configuration getStagingConfiguration ()
|
public Configuration getStagingConfiguration() {
|
||||||
{
|
|
||||||
return new ConfigurationImpl(CONFIGURATIONS.STAGE, getCurrentCache());
|
return new ConfigurationImpl(CONFIGURATIONS.STAGE, getCurrentCache());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDefaultAdmins ()
|
public List<String> getDefaultAdmins() {
|
||||||
{
|
List<String> admins = new ArrayList<String>();
|
||||||
if (this.admins == null && this.defaultAdmins.isEmpty()) this.admins= "ciro.formisano";
|
|
||||||
else if (admins == null)
|
if (defaultAdmins == null || defaultAdmins.isEmpty()) {
|
||||||
{
|
admins.add("statistical.manager");
|
||||||
|
} else {
|
||||||
Iterator<Object> keys = this.defaultAdmins.keySet().iterator();
|
Iterator<Object> keys = this.defaultAdmins.keySet().iterator();
|
||||||
StringBuilder response = new StringBuilder();
|
|
||||||
|
|
||||||
while (keys.hasNext())
|
while (keys.hasNext()) {
|
||||||
{
|
|
||||||
String key = (String) keys.next();
|
String key = (String) keys.next();
|
||||||
response.append(this.defaultAdmins.getProperty(key)).append(", ");
|
admins.add(defaultAdmins.getProperty(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.admins = response.substring(0, response.length()-2);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.debug("Default admins list "+this.admins);
|
|
||||||
return this.admins;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.debug("Default admins list: " + admins);
|
||||||
|
return admins;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException, SVNException {
|
public static void main(String[] args) throws IOException, SVNException {
|
||||||
DMPMClientConfiguratorManager a = new DMPMClientConfiguratorManager();
|
DMPMClientConfiguratorManager a = new DMPMClientConfiguratorManager();
|
||||||
|
|
|
@ -8,12 +8,10 @@ import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
@ -24,140 +22,134 @@ import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
|
||||||
import org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration.DMPMClientConfiguratorManager;
|
import org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration.DMPMClientConfiguratorManager;
|
||||||
import org.gcube.dataanalysis.dataminer.poolmanager.util.exception.EMailException;
|
import org.gcube.dataanalysis.dataminer.poolmanager.util.exception.EMailException;
|
||||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
public class SendMail {
|
public class SendMail {
|
||||||
|
|
||||||
private Logger logger;
|
private Logger logger = LoggerFactory.getLogger(SendMail.class);
|
||||||
private final String WRITE_MESSAGE_ADDRESS_PATH ="messages/writeMessageToUsers?gcube-token=",
|
private final String WRITE_MESSAGE_ADDRESS_PATH = "2/messages/write-message?gcube-token=",
|
||||||
USER_ROLES_ADDRESS_PATH = "2/users/get-usernames-by-role?role-name=DataMiner-Manager&gcube-token=",
|
USER_ROLES_ADDRESS_PATH = "2/users/get-usernames-by-role?role-name=DataMiner-Manager&gcube-token=",
|
||||||
ENCODING = "UTF-8",
|
|
||||||
SENDER_PARAMETER_FORMAT = "&sender=dataminer&recipients=%s&subject=%s&body=%s",
|
|
||||||
SOCIAL_SERVICE_QUERY_CONDITION = "$resource/Profile/ServiceName/text() eq 'SocialNetworking'",
|
SOCIAL_SERVICE_QUERY_CONDITION = "$resource/Profile/ServiceName/text() eq 'SocialNetworking'",
|
||||||
SOCIAL_SERVICE_URI="jersey-servlet",
|
SOCIAL_SERVICE_URI = "jersey-servlet", JSON_MIME_TYPE = "application/json";
|
||||||
JSON_MIME_TYPE = "application/json";
|
|
||||||
|
private String socialServiceAddress;
|
||||||
|
|
||||||
public SendMail() {
|
public SendMail() {
|
||||||
this.logger = LoggerFactory.getLogger(SendMail.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getRequestMessage (String addressPath)
|
public void sendNotification(String subject, String body) throws EMailException {
|
||||||
{
|
logger.debug("SendNotification");
|
||||||
String serviceAddress = this.getSocialService();
|
logger.debug("Notification Subject: " + subject);
|
||||||
StringBuilder requestMessageBuilder = new StringBuilder(serviceAddress);
|
logger.debug("Notification Body: " + body);
|
||||||
|
|
||||||
if (!serviceAddress.endsWith("/")) requestMessageBuilder.append('/');
|
retrieveSocialService();
|
||||||
|
|
||||||
requestMessageBuilder.append(addressPath).append(SecurityTokenProvider.instance.get());
|
|
||||||
String requestForMessage = requestMessageBuilder.toString();
|
|
||||||
this.logger.debug("Request "+requestForMessage);
|
|
||||||
return requestForMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendNotification(String subject, String body) throws EMailException
|
|
||||||
{
|
|
||||||
this.logger.debug("Sending mail notification for "+subject);
|
|
||||||
this.logger.debug("Body "+body);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//AnalysisLogger.getLogger().debug("Emailing System->Request url is going to be " + requestForMessage);
|
|
||||||
|
|
||||||
// put the sender, the recipients, subject and body of the mail here
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
subject = URLEncoder.encode(subject, ENCODING);
|
|
||||||
body = URLEncoder.encode(body, ENCODING);
|
|
||||||
}
|
|
||||||
catch (UnsupportedEncodingException e)
|
|
||||||
{
|
|
||||||
throw new EMailException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
String postBody = createPostBody(subject, body);
|
||||||
String requestForMessage = getRequestMessage(WRITE_MESSAGE_ADDRESS_PATH);
|
String requestForMessage = getRequestMessage(WRITE_MESSAGE_ADDRESS_PATH);
|
||||||
requestForMessage = requestForMessage.replace("http://", "https://").replace(":80", "");
|
|
||||||
|
|
||||||
String requestParameters = String.format(SENDER_PARAMETER_FORMAT, this.getAdmins(), subject , body);
|
sendPostRequest(requestForMessage, postBody);
|
||||||
|
|
||||||
|
|
||||||
String response = this.sendPostRequest(requestForMessage, requestParameters);
|
|
||||||
//AnalysisLogger.getLogger().debug("Emailing System->Emailing response OK ");
|
|
||||||
|
|
||||||
if (response == null) throw new EMailException();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public void notifySubmitter(String a, String b) throws Exception {
|
private String createPostBody(String subject, String body) throws EMailException {
|
||||||
// NotificationHelper nh = new NotificationHelper();
|
try {
|
||||||
// super.sendNotification(nh.getSubject(),
|
List<String> recipientsList = getRecipients();
|
||||||
// nh.getBody());
|
if (recipientsList == null || recipientsList.isEmpty()) {
|
||||||
// }
|
logger.error("Invalid recipient list: " + recipientsList);
|
||||||
|
throw new EMailException("Unable to send email notification. Invalid recipient list:" + recipientsList);
|
||||||
public String username(String token) throws ObjectNotFound, Exception {
|
|
||||||
AuthorizationEntry entry = authorizationService().get(token);
|
|
||||||
this.logger.debug(entry.getClientInfo().getId());
|
|
||||||
return entry.getClientInfo().getId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// public String retrieveAdminRole() throws Exception {
|
// {"subject": "subject-content", "body": "body-content",
|
||||||
// String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
|
// "recipients":[{"id":"userid"}]}
|
||||||
//
|
|
||||||
// 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 getSocialService() {
|
JSONObject data = new JSONObject();
|
||||||
|
data.put("subject", subject);
|
||||||
|
data.put("body", body);
|
||||||
|
|
||||||
|
JSONArray recipients = new JSONArray();
|
||||||
|
for (String recipient : recipientsList) {
|
||||||
|
JSONObject d = new JSONObject();
|
||||||
|
d.put("id", recipient);
|
||||||
|
recipients.put(d);
|
||||||
|
}
|
||||||
|
data.put("recipients", recipients);
|
||||||
|
|
||||||
|
logger.info("Post Body: " + data);
|
||||||
|
return data.toString();
|
||||||
|
|
||||||
|
} catch (EMailException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error("Error creating the notification body: " + e.getLocalizedMessage(), e);
|
||||||
|
throw new EMailException(e);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void retrieveSocialService() throws EMailException {
|
||||||
|
try {
|
||||||
SimpleQuery query = queryFor(GCoreEndpoint.class);
|
SimpleQuery query = queryFor(GCoreEndpoint.class);
|
||||||
query.addCondition(SOCIAL_SERVICE_QUERY_CONDITION);
|
query.addCondition(SOCIAL_SERVICE_QUERY_CONDITION);
|
||||||
DiscoveryClient<GCoreEndpoint> client = clientFor(GCoreEndpoint.class);
|
DiscoveryClient<GCoreEndpoint> client = clientFor(GCoreEndpoint.class);
|
||||||
List<GCoreEndpoint> resources = client.submit(query);
|
List<GCoreEndpoint> resources = client.submit(query);
|
||||||
String a = resources.get(0).profile().endpointMap().get(SOCIAL_SERVICE_URI).uri().toString();
|
socialServiceAddress = resources.get(0).profile().endpointMap().get(SOCIAL_SERVICE_URI).uri().toString();
|
||||||
return a;
|
logger.info("Retrieved Social Service Address: " + socialServiceAddress);
|
||||||
|
if (socialServiceAddress == null || socialServiceAddress.isEmpty()) {
|
||||||
|
throw new EMailException(
|
||||||
|
"Unable to send email notification. Invalid address in GCoreEndpoint resource on IS.");
|
||||||
|
}
|
||||||
|
} catch (EMailException e) {
|
||||||
|
logger.error(e.getLocalizedMessage(), e);
|
||||||
|
throw e;
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error(e.getLocalizedMessage(), e);
|
||||||
|
throw new EMailException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getRequestMessage(String addressPath) {
|
||||||
|
StringBuilder requestMessageBuilder = new StringBuilder(socialServiceAddress);
|
||||||
|
|
||||||
public String sendPostRequest(String endpoint, String requestParameters) {
|
if (!socialServiceAddress.endsWith("/"))
|
||||||
|
requestMessageBuilder.append('/');
|
||||||
|
|
||||||
this.logger.debug("Sending post request");
|
requestMessageBuilder.append(addressPath).append(SecurityTokenProvider.instance.get());
|
||||||
// Build parameter string
|
String requestForMessage = requestMessageBuilder.toString();
|
||||||
String data = requestParameters;
|
logger.debug("Request " + requestForMessage);
|
||||||
|
return requestForMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String username(String token) throws ObjectNotFound, Exception {
|
||||||
|
AuthorizationEntry entry = authorizationService().get(token);
|
||||||
|
logger.debug(entry.getClientInfo().getId());
|
||||||
|
return entry.getClientInfo().getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendPostRequest(String endpoint, String postBody) throws EMailException {
|
||||||
|
logger.info("Execute Post:" + endpoint);
|
||||||
|
logger.info("Post Body:" + postBody);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Send the request
|
// Send the request
|
||||||
URL url = new URL(endpoint);
|
URL url = new URL(endpoint);
|
||||||
URLConnection conn = url.openConnection();
|
URLConnection conn = url.openConnection();
|
||||||
|
conn.setRequestProperty("Accept", JSON_MIME_TYPE);
|
||||||
|
conn.setRequestProperty("Content-Type", JSON_MIME_TYPE);
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
|
|
||||||
|
|
||||||
// write parameters
|
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
|
||||||
writer.write(data);
|
writer.write(postBody);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
|
|
||||||
// Get the response
|
// Get the response
|
||||||
|
@ -170,125 +162,94 @@ public class SendMail {
|
||||||
writer.close();
|
writer.close();
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
this.logger.debug("Operation completed");
|
logger.debug("Operation completed");
|
||||||
String response = answer.toString();
|
String response = answer.toString();
|
||||||
this.logger.debug("Response "+response);
|
logger.info("Notification Response: " + response);
|
||||||
// Output the response
|
checkResponse(response);
|
||||||
return response;
|
|
||||||
|
|
||||||
} catch (MalformedURLException ex) {
|
} catch (EMailException e) {
|
||||||
this.logger.error("Invalid URL",ex);
|
throw e;
|
||||||
} catch (IOException ex) {
|
} catch (MalformedURLException e) {
|
||||||
|
logger.error("Invalid URL: " + e.getLocalizedMessage(), e);
|
||||||
this.logger.error("Error in the IO process",ex);
|
throw new EMailException(e);
|
||||||
}
|
} catch (IOException e) {
|
||||||
return null;
|
logger.error("Error in the IO process: " + e.getLocalizedMessage(), e);
|
||||||
|
throw new EMailException(e);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error("Error executing post:" + e.getLocalizedMessage(), e);
|
||||||
|
throw new EMailException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkResponse(String response) throws EMailException {
|
||||||
|
if (response == null) {
|
||||||
|
logger.error("Invalid notification response: " + response);
|
||||||
|
throw new EMailException();// TODO
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
JSONObject res = new JSONObject(response);
|
||||||
|
boolean success = res.getBoolean("success");
|
||||||
|
if (!success) {
|
||||||
|
String message = res.getString("message");
|
||||||
|
logger.error("Error in send email notification: " + message);
|
||||||
|
throw new EMailException("Error in send email notification: "+message);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (JSONException e) {
|
||||||
|
logger.error("Invalid notification response: " + response);
|
||||||
|
throw new EMailException(e);
|
||||||
|
}
|
||||||
|
|
||||||
public String getAdminRoles() throws Exception{
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// discover social gcore endpoint
|
private List<String> getRecipients() {
|
||||||
|
try {
|
||||||
|
List<String> recipients = new ArrayList<String>();
|
||||||
|
String dataMinerManagers = retrieveDataMinerManagers();
|
||||||
|
logger.debug("Retrieved DataMiner Managers: " + dataMinerManagers);
|
||||||
|
if (dataMinerManagers != null && !dataMinerManagers.isEmpty()) {
|
||||||
|
JSONObject obj = new JSONObject(dataMinerManagers);
|
||||||
|
JSONArray data = obj.getJSONArray("result");
|
||||||
|
if (data != null) {
|
||||||
|
for (int i = 0; i < data.length(); i++) {
|
||||||
|
recipients.add(data.getString(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.info("Use the default admins how workaround ");
|
||||||
|
List<String> defaultManagers = DMPMClientConfiguratorManager.getInstance().getDefaultAdmins();
|
||||||
|
recipients.addAll(defaultManagers);
|
||||||
|
}
|
||||||
|
recipients.add(this.username(SecurityTokenProvider.instance.get()));
|
||||||
|
|
||||||
//GcoreEndpointReader ep = new GcoreEndpointReader(ScopeProvider.instance.get());
|
logger.info("Retrieved Recipients: " + recipients);
|
||||||
|
return recipients;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error retrieving recipients: " + e.getLocalizedMessage(), e);
|
||||||
|
logger.info("Use the default admins how workaround ");
|
||||||
|
return DMPMClientConfiguratorManager.getInstance().getDefaultAdmins();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
String serviceAddress = getRequestMessage(USER_ROLES_ADDRESS_PATH);
|
private String retrieveDataMinerManagers() throws Exception {
|
||||||
//String serviceAddress = InfraRetrieval.findEmailingSystemAddress(ScopeProvider.instance.get());
|
// Try to retrieve a url like this:
|
||||||
|
// https://api.d4science.org/social-networking-library-ws/rest/2/users/get-usernames-by-role?role-name=DataMiner-Manager&gcube-token=xxx-xxxx-xxxx-xxx
|
||||||
|
|
||||||
|
String requestAdminsUrl = getRequestMessage(USER_ROLES_ADDRESS_PATH);
|
||||||
|
|
||||||
//String serviceAddress = ep.getResourceEntyName();//"https://socialnetworking1.d4science.org/social-networking-library-ws/rest/";
|
logger.info("Request Admins Url: " + requestAdminsUrl);
|
||||||
// serviceAddress = serviceAddress.endsWith("/") ? serviceAddress : serviceAddress + "/";
|
|
||||||
// serviceAddress+= "2/users/get-usernames-by-role?role-name=DataMiner-Manager&gcube-token=" + SecurityTokenProvider.instance.get();
|
|
||||||
|
|
||||||
this.logger.debug("Admin roles url is " + serviceAddress);
|
|
||||||
|
|
||||||
CloseableHttpClient client = HttpClientBuilder.create().build();
|
CloseableHttpClient client = HttpClientBuilder.create().build();
|
||||||
HttpGet getReq = new HttpGet(serviceAddress);
|
HttpGet getReq = new HttpGet(requestAdminsUrl);
|
||||||
getReq.setHeader("accept", JSON_MIME_TYPE);
|
getReq.setHeader("accept", JSON_MIME_TYPE);
|
||||||
getReq.setHeader("content-type", JSON_MIME_TYPE);
|
getReq.setHeader("content-type", JSON_MIME_TYPE);
|
||||||
this.logger.info(EntityUtils.toString(client.execute(getReq).getEntity()));
|
logger.info("Response: " + EntityUtils.toString(client.execute(getReq).getEntity()));
|
||||||
|
|
||||||
return EntityUtils.toString(client.execute(getReq).getEntity());
|
return EntityUtils.toString(client.execute(getReq).getEntity());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String getAdmins(){
|
|
||||||
try{
|
|
||||||
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]);
|
|
||||||
}
|
|
||||||
s.add(this.username(SecurityTokenProvider.instance.get()));
|
|
||||||
}
|
|
||||||
return s.toString().replace("[", "").replace("]", "");
|
|
||||||
}
|
|
||||||
catch(Exception a){return DMPMClientConfiguratorManager.getInstance().getDefaultAdmins(); }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
|
|
||||||
//ScopeProvider.instance.set("/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab");
|
|
||||||
//SecurityTokenProvider.instance.set("3a23bfa4-4dfe-44fc-988f-194b91071dd2-843339462");
|
|
||||||
|
|
||||||
//ScopeProvider.instance.set("/gcube/devNext");
|
|
||||||
//SecurityTokenProvider.instance.set("aa6eec71-fe07-43ab-bd1c-f03df293e430-98187548");
|
|
||||||
|
|
||||||
//NotificationHelper nh = new NotificationHelper();
|
|
||||||
|
|
||||||
SendMail sm = new SendMail();
|
|
||||||
|
|
||||||
//ScopeProvider.instance.set("/gcube/devNext/NextNext");
|
|
||||||
//SecurityTokenProvider.instance.set("708e7eb8-11a7-4e9a-816b-c9ed7e7e99fe-98187548");
|
|
||||||
//System.out.println(sm.getRootToken());
|
|
||||||
//sm.getGenericResourceByName("");
|
|
||||||
|
|
||||||
ScopeProvider.instance.set("/gcube/preprod/preVRE");
|
|
||||||
SecurityTokenProvider.instance.set("2eceaf27-0e22-4dbe-8075-e09eff199bf9-98187548");
|
|
||||||
|
|
||||||
|
|
||||||
//sm.sendNotification(nh.getFailedSubject(), nh.getFailedBody("test failed"));
|
|
||||||
// sm.username(SecurityTokenProvider.instance.get());
|
|
||||||
//sm.retrieveAdminRole();
|
|
||||||
//sm.getAdminRoles();
|
|
||||||
System.out.println(sm.getAdmins());
|
|
||||||
//System.out.println(sm.getAdmins());
|
|
||||||
//sm.sendNotification("test", "test");
|
|
||||||
//System.out.println(sm.getSocialService());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package org.gcube.dataanalysis.dataminer.poolmanager.util.exception;
|
||||||
|
|
||||||
public class EMailException extends Exception {
|
public class EMailException extends Exception {
|
||||||
|
|
||||||
|
|
||||||
private static final String MESSAGE = "Unable to send email notification";
|
private static final String MESSAGE = "Unable to send email notification";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,11 +13,16 @@ public class EMailException extends Exception {
|
||||||
super(MESSAGE);
|
super(MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public EMailException(Throwable cause) {
|
public EMailException(String message) {
|
||||||
super (MESSAGE,cause);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EMailException(String message, Throwable e) {
|
||||||
|
super(message, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EMailException(Throwable e) {
|
||||||
|
super(MESSAGE, e);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
/LICENSE.md
|
||||||
|
/README.md
|
||||||
|
/changelog.xml
|
||||||
|
/gcube-app.xml
|
||||||
|
/profile.xml
|
|
@ -0,0 +1,38 @@
|
||||||
|
package org.gcube.dataanalysis.dataminerpoolmanager;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
import org.gcube.dataanalysis.dataminer.poolmanager.util.SendMail;
|
||||||
|
|
||||||
|
public class SendMailTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
// NotificationHelper nh = new NotificationHelper();
|
||||||
|
|
||||||
|
SendMail sm = new SendMail();
|
||||||
|
|
||||||
|
ScopeProvider.instance.set("/gcube/devNext/NextNext");
|
||||||
|
SecurityTokenProvider.instance.set("xxxx-xxxx-xxx");
|
||||||
|
// System.out.println(sm.getRootToken());
|
||||||
|
// sm.getGenericResourceByName("");
|
||||||
|
|
||||||
|
//ScopeProvider.instance.set("/gcube/devsec/devVRE");
|
||||||
|
//SecurityTokenProvider.instance.set("xxxx-xxxx-xxxx");
|
||||||
|
|
||||||
|
// sm.sendNotification(nh.getFailedSubject(), nh.getFailedBody("test
|
||||||
|
// failed"));
|
||||||
|
// sm.username(SecurityTokenProvider.instance.get());
|
||||||
|
// sm.retrieveAdminRole();
|
||||||
|
// sm.getAdminRoles();
|
||||||
|
sm.sendNotification("Test DMPoolManager Notification", "This is only a test please cancel this message.");
|
||||||
|
// System.out.println(sm.getAdmins());
|
||||||
|
// sm.sendNotification("test", "test");
|
||||||
|
// System.out.println(sm.getSocialService());
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error in sent mail: "+e.getLocalizedMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue