Adding message facilities

This commit is contained in:
Luca Frosini 2022-03-25 18:38:26 +01:00
parent 1a581e39ef
commit 9bb4c11602
6 changed files with 279 additions and 2 deletions

View File

@ -12,7 +12,7 @@
<groupId>org.gcube.data-catalogue</groupId>
<artifactId>gcat</artifactId>
<packaging>war</packaging>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.0</version>
<name>gCube Catalogue (gCat) Service</name>
<description>
This service allows any client to publish on the gCube Catalogue.
@ -35,7 +35,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.1.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>

View File

@ -14,12 +14,14 @@ public class FakeModerationThread extends ModerationThread {
@Override
protected void postMessage(CMItemStatus cmItemStatus, String message) throws Exception {
logger.info("gCat is sending a message to the {} for item '{}' (id={}). ItemStatus={}, Message=\"{}\"",
ModerationThread.class.getSimpleName(), itemName, itemID, cmItemStatus, message);
}
@Override
public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception {
logger.info("{} is sending a message to the {} for item '{}' (id={}). ItemStatus={}, Message=\"{}\"",
SecretManagerProvider.instance.get().getUser().getUsername(),
ModerationThread.class.getSimpleName(), itemName, itemID, cmItemStatus, userMessage);
@ -27,6 +29,7 @@ public class FakeModerationThread extends ModerationThread {
@Override
protected void createModerationThread() throws Exception {
logger.info("Creating {} for item '{}' (id={})", ModerationThread.class.getSimpleName(), itemName, itemID);
}

View File

@ -0,0 +1,61 @@
package org.gcube.gcat.moderation.thread;
import java.io.StringWriter;
import java.util.Set;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.social.Message;
import org.gcube.gcat.social.SocialUsers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class MessageModerationThread extends ModerationThread {
private static final Logger logger = LoggerFactory.getLogger(MessageModerationThread.class);
protected String getSubject(CMItemStatus cmItemStatus) {
StringWriter stringWriter = new StringWriter();
stringWriter.append(cmItemStatus.getFancyValue());
stringWriter.append(itemName);
stringWriter.append(itemID);
return stringWriter.toString();
}
protected Message getMessage(CMItemStatus cmItemStatus, String messageString) throws Exception {
Message message = new Message();
message.setMessage(messageString);
message.setSubject(getSubject(cmItemStatus));
Set<String> moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
message.setUsers(moderators);
return message;
}
@Override
protected void postMessage(CMItemStatus cmItemStatus, String message) throws Exception {
}
@Override
public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception {
logger.info("{} is sending a message to the {} for item '{}' (id={}). ItemStatus={}, Message=\"{}\"",
SecretManagerProvider.instance.get().getUser().getUsername(),
ModerationThread.class.getSimpleName(), itemName, itemID, cmItemStatus, userMessage);
}
@Override
protected void createModerationThread() throws Exception {
// Nothing to do
}
}

View File

@ -0,0 +1,58 @@
package org.gcube.gcat.rest;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.ext.Provider;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.GCubeSecret;
import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Provider
@PreMatching
public class RequestFilter implements ContainerRequestFilter, ContainerResponseFilter {
private final static Logger logger = LoggerFactory.getLogger(RequestFilter.class);
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
logger.trace("PreMatching RequestFilter");
SecretManagerProvider.instance.reset();
SecretManager secretManager = new SecretManager();
String token = AccessTokenProvider.instance.get();
if(token!=null) {
Secret secret = new JWTSecret(token);
secretManager.addSecret(secret);
}
token = SecurityTokenProvider.instance.get();
if(token!=null) {
Secret secret = new GCubeSecret(token);
secretManager.addSecret(secret);
}
SecretManagerProvider.instance.set(secretManager);
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
logger.trace("ResponseFilter");
SecretManagerProvider.instance.reset();
}
}

View File

@ -0,0 +1,43 @@
package org.gcube.gcat.social;
import java.util.Collection;
/**
* @author Luca Frosini (ISTI-CNR)
*/
public class Message {
protected String message;
protected String subject;
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;
}
public Collection<String> getUsers() {
return users;
}
public void setUsers(Collection<String> users) {
this.users = users;
}
@Override
public String toString() {
return "Message [message=" + message + ", subject=" + subject + ", users=" + users + "]";
}
}

View File

@ -0,0 +1,112 @@
package org.gcube.gcat.social;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
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.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
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.api.configuration.CatalogueConfiguration;
import org.gcube.gcat.configuration.CatalogueConfigurationFactory;
import org.gcube.gcat.persistence.ckan.CKANUserCache;
import org.gcube.gcat.utils.Constants;
import org.gcube.gcat.utils.HTTPUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class SocialMessage extends Thread {
private static final Logger logger = LoggerFactory.getLogger(SocialMessage.class);
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";
protected final ObjectMapper objectMapper;
protected Message message;
protected String itemURL;
protected String itemTitle;
public SocialMessage() throws Exception {
super();
this.objectMapper = new ObjectMapper();
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
@Override
public void run() {
try {
CatalogueConfiguration instance = CatalogueConfigurationFactory.getInstance();
logger.info("Going to send Message about the Item {} available at {}", message, itemURL);
// write message
sendSocialMessage();
} catch(Exception e) {
logger.error("Error while executing post creation actions", e);
}
}
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 + "/";
logger.debug("The message that is going to be send is\n{}", message);
String messageString = objectMapper.writeValueAsString(message);
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest.newRequest(basePath);
gxhttpStringRequest.from(Constants.CATALOGUE_NAME);
gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
gxhttpStringRequest.setSecurityToken(Constants.getCatalogueSecret().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);
} else {
logger.info("Failed to write the message {}. Reason {}", messageString,
jsonNode.get(RESPONSE_MESSAGE_KEY).asText());
}
} catch(Exception e) {
logger.error("Unable to send the message : " + message.toString(), e);
}
}
}