Migrated code to social-service-client
This commit is contained in:
parent
83cbdd04bc
commit
fae090d6bc
|
@ -1,86 +0,0 @@
|
|||
package org.gcube.gcat.moderation.thread.social.messages;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonGetter;
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class Message {
|
||||
|
||||
public class Recipient {
|
||||
|
||||
String id;
|
||||
|
||||
public Recipient(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@JsonProperty(value = "body")
|
||||
protected String message;
|
||||
@JsonProperty(value = "subject")
|
||||
protected String subject;
|
||||
@JsonIgnore
|
||||
protected Collection<String> users;
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public Collection<String> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public void setUsers(Collection<String> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public void addUser(String user) {
|
||||
this.users.add(user);
|
||||
}
|
||||
|
||||
@JsonGetter(value = "recipients")
|
||||
public List<Recipient> getRecipients() {
|
||||
List<Recipient> recipients = new ArrayList<>();
|
||||
for(String username : users) {
|
||||
recipients.add(new Recipient(username));
|
||||
}
|
||||
return recipients;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Message [message=" + message + ", subject=" + subject + ", users=" + users + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,18 +1,9 @@
|
|||
package org.gcube.gcat.moderation.thread.social.messages;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||
import org.gcube.common.authorization.utils.socialservice.SocialService;
|
||||
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
|
||||
import org.gcube.gcat.utils.Constants;
|
||||
import org.gcube.gcat.utils.HTTPUtility;
|
||||
import org.gcube.social_networking.social_networking_client_library.MessageClient;
|
||||
import org.gcube.social_networking.socialnetworking.model.beans.Message;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -25,9 +16,6 @@ public class SocialMessage extends Thread {
|
|||
|
||||
public static final String ITEM_URL = "Item URL";
|
||||
|
||||
// https://wiki.gcube-system.org/gcube/Social_Networking_Service#Send_a_message
|
||||
protected static final String SOCIAL_SERVICE_SEND_MESSAGE_PATH = "/2/messages/write-message";
|
||||
|
||||
protected static final String RESPONSE_SUCCESS_KEY = "success";
|
||||
protected static final String RESPONSE_MESSAGE_KEY = "message";
|
||||
|
||||
|
@ -61,27 +49,13 @@ public class SocialMessage extends Thread {
|
|||
}
|
||||
|
||||
public void sendSocialMessage() {
|
||||
|
||||
try {
|
||||
String basePath = SocialService.getSocialService().getServiceBasePath();
|
||||
if(basePath == null) {
|
||||
logger.info("Unable to send a message because there is no social networking service available");
|
||||
return;
|
||||
}
|
||||
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
|
||||
|
||||
MessageClient messageClient = new MessageClient();
|
||||
String ret = messageClient.writeMessage(message);
|
||||
|
||||
String messageString = objectMapper.writeValueAsString(message);
|
||||
logger.debug("The message that is going to be send is\n{}", messageString);
|
||||
|
||||
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath);
|
||||
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
|
||||
gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
|
||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
||||
gxhttpStringRequest.setSecurityToken(secretManager.getCurrentSecretHolder().getSecrets().first().getToken());
|
||||
gxhttpStringRequest.path(SOCIAL_SERVICE_SEND_MESSAGE_PATH);
|
||||
|
||||
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(messageString);
|
||||
String ret = HTTPUtility.getResultAsString(httpURLConnection);
|
||||
JsonNode jsonNode = objectMapper.readTree(ret);
|
||||
if(jsonNode.get(RESPONSE_SUCCESS_KEY).asBoolean()) {
|
||||
logger.info("Message sent : {}", messageString);
|
||||
|
@ -92,7 +66,6 @@ public class SocialMessage extends Thread {
|
|||
} catch(Exception e) {
|
||||
logger.error("Unable to send the message : " + message.toString(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package org.gcube.gcat.moderation.thread.social.messages;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||
|
@ -11,6 +13,8 @@ import org.gcube.gcat.api.moderation.Moderated;
|
|||
import org.gcube.gcat.moderation.thread.ModerationThread;
|
||||
import org.gcube.gcat.social.SocialUsers;
|
||||
import org.gcube.gcat.utils.Constants;
|
||||
import org.gcube.social_networking.socialnetworking.model.beans.Message;
|
||||
import org.gcube.social_networking.socialnetworking.model.beans.Recipient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -139,12 +143,21 @@ public class SocialMessageModerationThread extends ModerationThread {
|
|||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
protected List<Recipient> getRecipients(Set<String> users) {
|
||||
List<Recipient> recipients = new ArrayList<>();
|
||||
for(String username : users) {
|
||||
recipients.add(new Recipient(username));
|
||||
}
|
||||
return recipients;
|
||||
}
|
||||
|
||||
protected Message getMessage(String messageString) throws Exception {
|
||||
Message message = new Message();
|
||||
message.setMessage(messageString);
|
||||
message.setBody(messageString);
|
||||
message.setSubject(getSubject());
|
||||
Set<String> moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
|
||||
message.setUsers(moderators);
|
||||
List<Recipient> recipients = getRecipients(moderators);
|
||||
message.setRecipients(recipients);
|
||||
return message;
|
||||
}
|
||||
|
||||
|
@ -153,7 +166,8 @@ public class SocialMessageModerationThread extends ModerationThread {
|
|||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
||||
String username = secretManager.getUser().getUsername();
|
||||
Message message = getMessage(messageString);
|
||||
message.addUser(username);
|
||||
List<Recipient> recipients = message.getRecipients();
|
||||
recipients.add(new Recipient(username));
|
||||
Secret secret = Constants.getCatalogueSecret();
|
||||
secretManager.startSession(secret);
|
||||
try {
|
||||
|
@ -176,7 +190,8 @@ public class SocialMessageModerationThread extends ModerationThread {
|
|||
Message message = getMessage(stringBuffer.toString());
|
||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
||||
String username = secretManager.getUser().getUsername();
|
||||
message.addUser(username);
|
||||
List<Recipient> recipients = message.getRecipients();
|
||||
recipients.add(new Recipient(username));
|
||||
Secret secret = Constants.getCatalogueSecret();
|
||||
secretManager.startSession(secret);
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue