Removed zulip dependency which is not used at the moment

This commit is contained in:
Luca Frosini 2022-11-03 09:26:35 +01:00
parent 9cd3144099
commit 020c4edd32
4 changed files with 167 additions and 144 deletions

View File

@ -3,7 +3,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for gCube Catalogue (gCat) Service # Changelog for gCube Catalogue (gCat) Service
## [v2.4.1] ## [v2.4.1-SNAPSHOT]
- Integrating Sphinx for documentation [#23833] - Integrating Sphinx for documentation [#23833]
- Migrated Social service interaction to social-service-client [#23151] - Migrated Social service interaction to social-service-client [#23151]

View File

@ -12,7 +12,7 @@
<groupId>org.gcube.data-catalogue</groupId> <groupId>org.gcube.data-catalogue</groupId>
<artifactId>gcat</artifactId> <artifactId>gcat</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<version>2.4.1</version> <version>2.4.1-SNAPSHOT</version>
<name>gCube Catalogue (gCat) Service</name> <name>gCube Catalogue (gCat) Service</name>
<description> <description>
This service allows any client to publish on the gCube Catalogue. This service allows any client to publish on the gCube Catalogue.
@ -199,22 +199,28 @@
<version>20211205</version> <version>20211205</version>
</dependency> </dependency>
<!-- Used to detect Resource MimeTypes -->
<dependency> <dependency>
<groupId>org.apache.tika</groupId> <groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId> <artifactId>tika-core</artifactId>
<version>2.1.0</version> <version>2.1.0</version>
</dependency> </dependency>
<!-- Used by Validator -->
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>3.12.0</version> <version>3.12.0</version>
</dependency> </dependency>
<!--
Zulip is not used at the moment
<dependency> <dependency>
<groupId>io.taliox</groupId> <groupId>io.taliox</groupId>
<artifactId>zulip-java-rest</artifactId> <artifactId>zulip-java-rest</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
</dependency> </dependency>
-->
<dependency> <dependency>
<groupId>org.gcube.common</groupId> <groupId>org.gcube.common</groupId>

View File

@ -1,29 +1,29 @@
package org.gcube.gcat.moderation.thread.zulip; package org.gcube.gcat.moderation.thread.zulip;
import java.util.Set; //import java.util.Set;
//
import javax.ws.rs.InternalServerErrorException; //import javax.ws.rs.InternalServerErrorException;
//
import org.gcube.com.fasterxml.jackson.databind.JsonNode; //import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; //import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; //import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.authorization.utils.manager.SecretManager; //import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret; //import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.gcat.api.moderation.CMItemStatus; import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.moderation.Moderated; //import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.moderation.thread.ModerationThread; import org.gcube.gcat.moderation.thread.ModerationThread;
import org.gcube.gcat.moderation.thread.zulip.ZulipResponse.Result; //import org.gcube.gcat.moderation.thread.zulip.ZulipResponse.Result;
import org.gcube.gcat.social.SocialUsers; //import org.gcube.gcat.social.SocialUsers;
import org.gcube.gcat.utils.Constants; //import org.gcube.gcat.utils.Constants;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import io.taliox.zulip.ZulipRestExecutor; //import io.taliox.zulip.ZulipRestExecutor;
import io.taliox.zulip.calls.ZulipRestAPICall; //import io.taliox.zulip.calls.ZulipRestAPICall;
import io.taliox.zulip.calls.messages.PostMessage; //import io.taliox.zulip.calls.messages.PostMessage;
import io.taliox.zulip.calls.streams.GetStreamID; //import io.taliox.zulip.calls.streams.GetStreamID;
import io.taliox.zulip.calls.streams.PostCreateStream; //import io.taliox.zulip.calls.streams.PostCreateStream;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
@ -32,122 +32,140 @@ public class ZulipStream extends ModerationThread {
private static final Logger logger = LoggerFactory.getLogger(ZulipStream.class); private static final Logger logger = LoggerFactory.getLogger(ZulipStream.class);
public static final String TOPICS_KEY = "topics"; @Override
public static final String NAME_KEY = "name"; protected void postMessage(String message) throws Exception {
public static final String MAX_ID_KEY = "max_id"; logger.info("gCat is sending a message to the {} for item '{}' (id={}). ItemStatus={}, Message=\"{}\"",
public static final String INITIAL_TOPIC_NAME = "hello"; ZulipStream.class.getSimpleName(), itemName, itemID, cmItemStatus, message);
protected ZulipRestExecutor gCatZulipRestExecutor;
protected ZulipRestExecutor userZulipRestExecutor;
protected String streamName;
protected String streamDescription;
public ZulipStream() {
super();
} }
protected ZulipRestExecutor getZulipRestExecutor() { @Override
ZulipAuth zulipAuth = new ZulipAuth(SecretManagerProvider.instance.get().getUser().getUsername()); public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception {
return new ZulipRestExecutor(zulipAuth.getEmail(), zulipAuth.getAPIKey(), zulipAuth.getSite()); logger.info("{} is sending a message to the {} for item '{}' (id={}). ItemStatus={}, Message=\"{}\"",
} SecretManagerProvider.instance.get().getUser().getUsername(),
ZulipStream.class.getSimpleName(), itemName, itemID, cmItemStatus, userMessage);
public ZulipRestExecutor getGCatZulipRestExecutor() throws Exception {
if(gCatZulipRestExecutor==null) {
SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = Constants.getCatalogueSecret();
secretManager.startSession(secret);
gCatZulipRestExecutor = getZulipRestExecutor();
secretManager.endSession();
}
return gCatZulipRestExecutor;
}
public ZulipRestExecutor getUserZulipRestExecutor() {
if(userZulipRestExecutor==null) {
userZulipRestExecutor = getZulipRestExecutor();
}
return userZulipRestExecutor;
}
protected String getStreamName() {
if(streamName==null) {
streamName = String.format("Item '%s' moderation", itemID);
}
return streamName;
}
protected Integer getStreamID() throws Exception {
GetStreamID getStreamID = new GetStreamID(getStreamName());
ZulipResponse zulipResponse = executeZulipCall(gCatZulipRestExecutor, getStreamID);
JsonNode response = zulipResponse.getResponse();
return response.get("stream_id").asInt();
}
protected String getStreamDescription() {
if(streamDescription==null) {
streamDescription = String.format("This stream is used to discuss about the moderation of the item '%s' with id '%s'", itemName, itemID);
}
return streamDescription;
}
protected ZulipResponse executeZulipCall(ZulipRestExecutor zulipRestExecutor, ZulipRestAPICall call) throws Exception {
logger.trace("Going to execute {}", call);
String responseString = zulipRestExecutor.executeCall(call);
logger.trace("Response from {} is {}", call.getClass().getSimpleName(), responseString);
ZulipResponse zulipResponse = new ZulipResponse(responseString);
if(zulipResponse.getResponseResult()==Result.error) {
throw new InternalServerErrorException(zulipResponse.getResponseMessage());
}
return zulipResponse;
} }
@Override @Override
protected void createModerationThread() throws Exception { protected void createModerationThread() throws Exception {
ArrayNode streamsArrayNode = objectMapper.createArrayNode(); logger.info("Creating {} for item '{}' (id={})", ZulipStream.class.getSimpleName(), itemName, itemID);
ObjectNode streamobjectNode = objectMapper.createObjectNode();
streamobjectNode.put("name", getStreamName());
streamobjectNode.put("description", getStreamDescription());
streamsArrayNode.add(streamobjectNode);
ArrayNode principalsArrayNode = objectMapper.createArrayNode();
// Going to add the item creator
String itemCreatorEmail = ckanUser.getEMail();
principalsArrayNode.add(itemCreatorEmail);
getGCatZulipRestExecutor();
principalsArrayNode.add(gCatZulipRestExecutor.httpController.getUserName());
// Going to add the catalogue moderators
Set<String> moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
for(String moderator : moderators) {
principalsArrayNode.add(moderator);
} }
PostCreateStream postCreateStream = new PostCreateStream(streamsArrayNode.toString()); // public static final String TOPICS_KEY = "topics";
postCreateStream.setPrincipals(principalsArrayNode.toString()); // public static final String NAME_KEY = "name";
postCreateStream.setInvite_only(true); // public static final String MAX_ID_KEY = "max_id";
postCreateStream.setAnnounce(false); // public static final String INITIAL_TOPIC_NAME = "hello";
//
executeZulipCall(gCatZulipRestExecutor, postCreateStream); // protected ZulipRestExecutor gCatZulipRestExecutor;
} // protected ZulipRestExecutor userZulipRestExecutor;
//
protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, String message) throws Exception { // protected String streamName;
PostMessage postMessage = new PostMessage(getStreamName(), cmItemStatus.getFancyValue(), message); // protected String streamDescription;
logger.debug("Going to send the following message: {}", message); //
executeZulipCall(zulipRestExecutor, postMessage); // public ZulipStream() {
} // super();
// }
@Override //
protected void postMessage(String message) throws Exception { // protected ZulipRestExecutor getZulipRestExecutor() {
postMessageToStream(getGCatZulipRestExecutor(), message); // ZulipAuth zulipAuth = new ZulipAuth(SecretManagerProvider.instance.get().getUser().getUsername());
} // return new ZulipRestExecutor(zulipAuth.getEmail(), zulipAuth.getAPIKey(), zulipAuth.getSite());
// }
@Override //
public void postUserMessage(CMItemStatus cmItemStatus, String message) throws Exception { // public ZulipRestExecutor getGCatZulipRestExecutor() throws Exception {
this.cmItemStatus = cmItemStatus; // if(gCatZulipRestExecutor==null) {
postMessageToStream(getUserZulipRestExecutor(), message); // SecretManager secretManager = SecretManagerProvider.instance.get();
} // Secret secret = Constants.getCatalogueSecret();
// secretManager.startSession(secret);
// gCatZulipRestExecutor = getZulipRestExecutor();
// secretManager.endSession();
// }
// return gCatZulipRestExecutor;
// }
//
// public ZulipRestExecutor getUserZulipRestExecutor() {
// if(userZulipRestExecutor==null) {
// userZulipRestExecutor = getZulipRestExecutor();
// }
// return userZulipRestExecutor;
// }
//
// protected String getStreamName() {
// if(streamName==null) {
// streamName = String.format("Item '%s' moderation", itemID);
// }
// return streamName;
// }
//
// protected Integer getStreamID() throws Exception {
// GetStreamID getStreamID = new GetStreamID(getStreamName());
// ZulipResponse zulipResponse = executeZulipCall(gCatZulipRestExecutor, getStreamID);
// JsonNode response = zulipResponse.getResponse();
// return response.get("stream_id").asInt();
// }
//
// protected String getStreamDescription() {
// if(streamDescription==null) {
// streamDescription = String.format("This stream is used to discuss about the moderation of the item '%s' with id '%s'", itemName, itemID);
// }
// return streamDescription;
// }
//
// protected ZulipResponse executeZulipCall(ZulipRestExecutor zulipRestExecutor, ZulipRestAPICall call) throws Exception {
// logger.trace("Going to execute {}", call);
// String responseString = zulipRestExecutor.executeCall(call);
// logger.trace("Response from {} is {}", call.getClass().getSimpleName(), responseString);
// ZulipResponse zulipResponse = new ZulipResponse(responseString);
// if(zulipResponse.getResponseResult()==Result.error) {
// throw new InternalServerErrorException(zulipResponse.getResponseMessage());
// }
// return zulipResponse;
// }
//
// @Override
// protected void createModerationThread() throws Exception {
// ArrayNode streamsArrayNode = objectMapper.createArrayNode();
// ObjectNode streamobjectNode = objectMapper.createObjectNode();
// streamobjectNode.put("name", getStreamName());
// streamobjectNode.put("description", getStreamDescription());
// streamsArrayNode.add(streamobjectNode);
//
// ArrayNode principalsArrayNode = objectMapper.createArrayNode();
// // Going to add the item creator
// String itemCreatorEmail = ckanUser.getEMail();
// principalsArrayNode.add(itemCreatorEmail);
//
// getGCatZulipRestExecutor();
//
// principalsArrayNode.add(gCatZulipRestExecutor.httpController.getUserName());
//
// // Going to add the catalogue moderators
// Set<String> moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
// for(String moderator : moderators) {
// principalsArrayNode.add(moderator);
// }
//
// PostCreateStream postCreateStream = new PostCreateStream(streamsArrayNode.toString());
// postCreateStream.setPrincipals(principalsArrayNode.toString());
// postCreateStream.setInvite_only(true);
// postCreateStream.setAnnounce(false);
//
// executeZulipCall(gCatZulipRestExecutor, postCreateStream);
// }
//
// protected void postMessageToStream(ZulipRestExecutor zulipRestExecutor, String message) throws Exception {
// PostMessage postMessage = new PostMessage(getStreamName(), cmItemStatus.getFancyValue(), message);
// logger.debug("Going to send the following message: {}", message);
// executeZulipCall(zulipRestExecutor, postMessage);
// }
//
// @Override
// protected void postMessage(String message) throws Exception {
// postMessageToStream(getGCatZulipRestExecutor(), message);
// }
//
// @Override
// public void postUserMessage(CMItemStatus cmItemStatus, String message) throws Exception {
// this.cmItemStatus = cmItemStatus;
// postMessageToStream(getUserZulipRestExecutor(), message);
// }
} }

View File

@ -14,11 +14,10 @@ import java.util.regex.Pattern;
import javax.ws.rs.BadRequestException; import javax.ws.rs.BadRequestException;
import javax.ws.rs.ForbiddenException; import javax.ws.rs.ForbiddenException;
import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotAllowedException; import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.MultivaluedMap;
import org.apache.http.MethodNotSupportedException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
@ -1469,11 +1468,11 @@ public class CKANPackage extends CKAN implements Moderated {
break; break;
case REJECTED: case REJECTED:
throw new MethodNotSupportedException("You can't approve a rejected item. The item must be updated first. The update will set the item in pending, than it can be approved/rejected."); throw new BadRequestException("You can't approve a rejected item. The item must be updated first. The update will set the item in pending, than it can be approved/rejected.");
case PENDING: case PENDING:
if(!ckanUser.isCatalogueModerator()) { if(!ckanUser.isCatalogueModerator()) {
throw new MethodNotSupportedException("Only catalogue moderator can approve a pending item."); throw new NotAuthorizedException("Only catalogue moderator can approve a pending item.");
} }
setToApproved(result); setToApproved(result);
@ -1502,7 +1501,7 @@ public class CKANPackage extends CKAN implements Moderated {
} }
return getAsCleanedString(result); return getAsCleanedString(result);
} }
throw new MethodNotSupportedException("The approve operation is available only in moderation mode"); throw new BadRequestException("The approve operation is available only in moderation mode");
}catch(WebApplicationException e) { }catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
@ -1518,7 +1517,7 @@ public class CKANPackage extends CKAN implements Moderated {
CMItemStatus cmItemStatus = getCMItemStatus(); CMItemStatus cmItemStatus = getCMItemStatus();
switch (cmItemStatus) { switch (cmItemStatus) {
case APPROVED: case APPROVED:
throw new MethodNotSupportedException("You can't rejected an approved item. The item must be updated first. The update will set the item in pending, than it can be approved/rejected."); throw new BadRequestException("You can't rejected an approved item. The item must be updated first. The update will set the item in pending, than it can be approved/rejected.");
case REJECTED: case REJECTED:
// Nothing TO DO // Nothing TO DO
@ -1526,7 +1525,7 @@ public class CKANPackage extends CKAN implements Moderated {
case PENDING: case PENDING:
if(!ckanUser.isCatalogueModerator()) { if(!ckanUser.isCatalogueModerator()) {
throw new MethodNotSupportedException("Only catalogue moderator can reject a pending item."); throw new NotAuthorizedException("Only catalogue moderator can reject a pending item.");
} }
setToRejected(result); setToRejected(result);
@ -1549,7 +1548,7 @@ public class CKANPackage extends CKAN implements Moderated {
} }
return getAsCleanedString(result); return getAsCleanedString(result);
} }
throw new MethodNotSupportedException("The reject operation is available only in moderation mode"); throw new BadRequestException("The reject operation is available only in moderation mode");
}catch(WebApplicationException e) { }catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
@ -1573,7 +1572,7 @@ public class CKANPackage extends CKAN implements Moderated {
if(!ckanUser.isCatalogueModerator()) { if(!ckanUser.isCatalogueModerator()) {
// Users that are not // Users that are not
if(!isItemCreator()) { if(!isItemCreator()) {
throw new NotAllowedException("Only item creator and " + Moderated.CATALOGUE_MODERATOR + "s are entitled to partecipate to the moderation discussion thread."); throw new NotAuthorizedException("Only item creator and " + Moderated.CATALOGUE_MODERATOR + "s are entitled to partecipate to the moderation discussion thread.");
}else { }else {
moderationThread.setItemAuthor(true); moderationThread.setItemAuthor(true);
} }
@ -1585,7 +1584,7 @@ public class CKANPackage extends CKAN implements Moderated {
moderationThread.postUserMessage(cmItemStatus, message); moderationThread.postUserMessage(cmItemStatus, message);
return; return;
} }
throw new MethodNotSupportedException("The message operation is available only in moderation mode"); throw new BadRequestException("The message operation is available only in moderation mode");
}catch(WebApplicationException e) { }catch(WebApplicationException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {