Changes made due to gcat-api reorganization

This commit is contained in:
Luca Frosini 2021-12-02 11:50:18 +01:00
parent 0476395fd0
commit b438820f86
12 changed files with 90 additions and 62 deletions

27
pom.xml
View File

@ -135,19 +135,7 @@
<version>1.8</version> <version>1.8</version>
</dependency> </dependency>
<!-- Used to automatically convert XML to JSON -->
<!-- <dependency> -->
<!-- <groupId>commons-lang</groupId> -->
<!-- <artifactId>commons-lang</artifactId> -->
<!-- <version>2.3</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>commons-io</groupId> -->
<!-- <artifactId>commons-io</artifactId> -->
<!-- <version>2.6</version> -->
<!-- </dependency> -->
<!-- Used to automatically convert XML to JSON -->
<!-- <dependency> --> <!-- <dependency> -->
<!-- <groupId>org.json</groupId> --> <!-- <groupId>org.json</groupId> -->
<!-- <artifactId>json</artifactId> --> <!-- <artifactId>json</artifactId> -->
@ -171,6 +159,19 @@
<artifactId>zulip-java-rest</artifactId> <artifactId>zulip-java-rest</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gcube-jackson-core</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gcube-jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gcube-jackson-databind</artifactId>
</dependency>
<!-- Test libraries --> <!-- Test libraries -->
<dependency> <dependency>

View File

@ -1,7 +1,7 @@
package org.gcube.gcat.moderation.thread; package org.gcube.gcat.moderation.thread;
import org.gcube.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.gcat.api.CMItemStatus; import org.gcube.gcat.api.moderation.CMItemStatus;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -1,7 +1,7 @@
package org.gcube.gcat.moderation.thread; package org.gcube.gcat.moderation.thread;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.gcat.api.CMItemStatus; import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUser;
/** /**

View File

@ -8,8 +8,8 @@ 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.gcat.api.CMItemStatus; import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.GCatConstants; 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;
@ -118,7 +118,7 @@ public class ZulipStream extends ModerationThread {
principalsArrayNode.add(gCatZulipRestExecutor.httpController.getUserName()); principalsArrayNode.add(gCatZulipRestExecutor.httpController.getUserName());
// Going to add the catalogue moderators // Going to add the catalogue moderators
Set<String> moderators = SocialUsers.getUsernamesByRole(GCatConstants.CATALOGUE_MODERATOR); Set<String> moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
for(String moderator : moderators) { for(String moderator : moderators) {
principalsArrayNode.add(moderator); principalsArrayNode.add(moderator);
} }

View File

@ -24,11 +24,12 @@ 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.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.gcat.api.CMItemStatus;
import org.gcube.gcat.api.CMItemVisibility;
import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.api.Role; import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.interfaces.Moderated; import org.gcube.gcat.api.moderation.CMItemVisibility;
import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.api.moderation.ModerationContent;
import org.gcube.gcat.api.roles.Role;
import org.gcube.gcat.moderation.thread.ModerationThread; import org.gcube.gcat.moderation.thread.ModerationThread;
import org.gcube.gcat.oldutils.Validator; import org.gcube.gcat.oldutils.Validator;
import org.gcube.gcat.profile.MetadataUtility; import org.gcube.gcat.profile.MetadataUtility;
@ -208,7 +209,7 @@ public class CKANPackage extends CKAN implements Moderated {
JsonNode extra = extras.get(i); JsonNode extra = extras.get(i);
if(extra.has(EXTRAS_KEY_KEY) && if(extra.has(EXTRAS_KEY_KEY) &&
extra.get(EXTRAS_KEY_KEY)!=null && extra.get(EXTRAS_KEY_KEY)!=null &&
extra.get(EXTRAS_KEY_KEY).asText().startsWith(GCatConstants.SYSTEM_CM_PREFIX)) { extra.get(EXTRAS_KEY_KEY).asText().startsWith(Moderated.SYSTEM_CM_PREFIX)) {
foundOne = true; foundOne = true;
}else { }else {
newExtras.add(extra.deepCopy()); newExtras.add(extra.deepCopy());
@ -822,7 +823,7 @@ public class CKANPackage extends CKAN implements Moderated {
try { try {
setApiKey(CKANUtility.getSysAdminAPI()); setApiKey(CKANUtility.getSysAdminAPI());
readItem(); readItem();
if(ckanUser.getRole()!=Role.ADMIN && !isItemCreator()) { if(ckanUser.getRole()!=Role.ADMIN && !isItemCreator()) {
throw new ForbiddenException("Only " + Role.ADMIN.getPortalRole() + "s and item creator are entitled to purge an the item"); throw new ForbiddenException("Only " + Role.ADMIN.getPortalRole() + "s and item creator are entitled to purge an the item");
} }
@ -866,7 +867,7 @@ public class CKANPackage extends CKAN implements Moderated {
if(result.has(EXTRAS_KEY)) { if(result.has(EXTRAS_KEY)) {
ArrayNode extras = (ArrayNode) result.get(EXTRAS_KEY); ArrayNode extras = (ArrayNode) result.get(EXTRAS_KEY);
for(JsonNode extra : extras) { for(JsonNode extra : extras) {
if(extra.has(EXTRAS_KEY_KEY) && extra.get(EXTRAS_KEY_KEY).asText().compareTo(GCatConstants.SYSTEM_CM_ITEM_STATUS) == 0) { if(extra.has(EXTRAS_KEY_KEY) && extra.get(EXTRAS_KEY_KEY).asText().compareTo(Moderated.SYSTEM_CM_ITEM_STATUS) == 0) {
cmItemStatusString = extra.get(EXTRAS_VALUE_KEY).asText(); cmItemStatusString = extra.get(EXTRAS_VALUE_KEY).asText();
found = true; found = true;
break; break;
@ -895,8 +896,8 @@ public class CKANPackage extends CKAN implements Moderated {
CMItemStatus cmItemStatus = null; CMItemStatus cmItemStatus = null;
try { try {
MultivaluedMap<String,String> queryParameters = uriInfo.getQueryParameters(); MultivaluedMap<String,String> queryParameters = uriInfo.getQueryParameters();
if(queryParameters.containsKey(GCatConstants.CM_ITEM_STATUS_QUERY_PARAMETER)) { if(queryParameters.containsKey(Moderated.CM_ITEM_STATUS_QUERY_PARAMETER)) {
String cmItemStatusString = queryParameters.getFirst(GCatConstants.CM_ITEM_STATUS_QUERY_PARAMETER); String cmItemStatusString = queryParameters.getFirst(Moderated.CM_ITEM_STATUS_QUERY_PARAMETER);
cmItemStatus = CMItemStatus.getCMItemStatusFromValue(cmItemStatusString); cmItemStatus = CMItemStatus.getCMItemStatusFromValue(cmItemStatusString);
} }
}catch (Exception e) { }catch (Exception e) {
@ -1094,12 +1095,12 @@ public class CKANPackage extends CKAN implements Moderated {
} }
protected void setToRejected(JsonNode jsonNode) { protected void setToRejected(JsonNode jsonNode) {
addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_STATUS, CMItemStatus.REJECTED.getValue()); addExtraField(jsonNode, Moderated.SYSTEM_CM_ITEM_STATUS, CMItemStatus.REJECTED.getValue());
} }
protected void setItemToPending(JsonNode jsonNode) { protected void setItemToPending(JsonNode jsonNode) {
if(isModerationEnabled()) { if(isModerationEnabled()) {
addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_STATUS, CMItemStatus.PENDING.getValue()); addExtraField(jsonNode, Moderated.SYSTEM_CM_ITEM_STATUS, CMItemStatus.PENDING.getValue());
CMItemVisibility cmItemVisibility = CMItemVisibility.PUBLIC; CMItemVisibility cmItemVisibility = CMItemVisibility.PUBLIC;
@ -1109,7 +1110,7 @@ public class CKANPackage extends CKAN implements Moderated {
cmItemVisibility = CMItemVisibility.RESTRICTED; cmItemVisibility = CMItemVisibility.RESTRICTED;
} }
} }
addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_VISIBILITY, cmItemVisibility.getValue()); addExtraField(jsonNode, Moderated.SYSTEM_CM_ITEM_VISIBILITY, cmItemVisibility.getValue());
((ObjectNode) jsonNode).put(PRIVATE_KEY, true); ((ObjectNode) jsonNode).put(PRIVATE_KEY, true);
((ObjectNode) jsonNode).put(SEARCHABLE_KEY, false); ((ObjectNode) jsonNode).put(SEARCHABLE_KEY, false);
@ -1123,23 +1124,23 @@ public class CKANPackage extends CKAN implements Moderated {
CMItemVisibility cmItemVisibility = null; CMItemVisibility cmItemVisibility = null;
for(JsonNode extra : extras) { for(JsonNode extra : extras) {
if(extra.has(EXTRAS_KEY_KEY) && extra.get(EXTRAS_KEY_KEY)!=null && extra.get(EXTRAS_KEY_KEY).asText().compareTo(GCatConstants.SYSTEM_CM_ITEM_STATUS) == 0) { if(extra.has(EXTRAS_KEY_KEY) && extra.get(EXTRAS_KEY_KEY)!=null && extra.get(EXTRAS_KEY_KEY).asText().compareTo(Moderated.SYSTEM_CM_ITEM_STATUS) == 0) {
((ObjectNode) extra).put(EXTRAS_VALUE_KEY, CMItemStatus.APPROVED.getValue()); ((ObjectNode) extra).put(EXTRAS_VALUE_KEY, CMItemStatus.APPROVED.getValue());
approvedSet = true; approvedSet = true;
} }
if(extra.has(EXTRAS_KEY_KEY) && extra.get(EXTRAS_KEY_KEY)!=null && extra.get(EXTRAS_KEY_KEY).asText().compareTo(GCatConstants.SYSTEM_CM_ITEM_VISIBILITY) == 0) { if(extra.has(EXTRAS_KEY_KEY) && extra.get(EXTRAS_KEY_KEY)!=null && extra.get(EXTRAS_KEY_KEY).asText().compareTo(Moderated.SYSTEM_CM_ITEM_VISIBILITY) == 0) {
cmItemVisibility = CMItemVisibility.getCMItemStatusFromValue(extra.get(EXTRAS_VALUE_KEY).asText()); cmItemVisibility = CMItemVisibility.getCMItemStatusFromValue(extra.get(EXTRAS_VALUE_KEY).asText());
} }
} }
if(!approvedSet) { if(!approvedSet) {
addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_STATUS, CMItemStatus.APPROVED.getValue()); addExtraField(jsonNode, Moderated.SYSTEM_CM_ITEM_STATUS, CMItemStatus.APPROVED.getValue());
} }
if(cmItemVisibility==null) { if(cmItemVisibility==null) {
cmItemVisibility = CMItemVisibility.PUBLIC; cmItemVisibility = CMItemVisibility.PUBLIC;
addExtraField(jsonNode, GCatConstants.SYSTEM_CM_ITEM_VISIBILITY, cmItemVisibility.getValue()); addExtraField(jsonNode, Moderated.SYSTEM_CM_ITEM_VISIBILITY, cmItemVisibility.getValue());
} }
@ -1252,9 +1253,6 @@ public class CKANPackage extends CKAN implements Moderated {
default: default:
break; break;
} }
return getAsCleanedString(result); return getAsCleanedString(result);
} }
throw new MethodNotSupportedException("The reject operation is available only in moderation mode"); throw new MethodNotSupportedException("The reject operation is available only in moderation mode");
@ -1279,7 +1277,7 @@ public class CKANPackage extends CKAN implements Moderated {
if(!ckanUser.getPortalUser().isCatalogueModerator()) { if(!ckanUser.getPortalUser().isCatalogueModerator()) {
// Users that are not // Users that are not
if(!isItemCreator()) { if(!isItemCreator()) {
throw new NotAllowedException("Only item creator and " + GCatConstants.CATALOGUE_MODERATOR + "s are entitled to partecipate to the moderation discussion thread."); throw new NotAllowedException("Only item creator and " + Moderated.CATALOGUE_MODERATOR + "s are entitled to partecipate to the moderation discussion thread.");
} }
} }
@ -1296,4 +1294,32 @@ public class CKANPackage extends CKAN implements Moderated {
} }
} }
public String moderate(String json) {
try {
ModerationContent moderationContent = mapper.readValue(json, ModerationContent.class);
String message = moderationContent.getMessage();
if(moderationContent.getCMItemStatus() !=null) {
CMItemStatus cmItemStatus = moderationContent.getCMItemStatus();
switch (cmItemStatus) {
case APPROVED:
return approve(message);
case REJECTED:
return reject(message);
default:
throw new BadRequestException("Allowed moderation operations are approve, reject and message");
}
}else {
if(message==null || message.compareTo("")==0) {
throw new BadRequestException("Allowed moderation operations are approve, reject and message");
}
message(message);
return null;
}
}catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new InternalServerErrorException(e);
}
}
} }

View File

@ -8,7 +8,7 @@ import javax.ws.rs.core.Response.Status;
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.gcat.api.Role; import org.gcube.gcat.api.roles.Role;
import org.gcube.gcat.social.PortalUser; import org.gcube.gcat.social.PortalUser;
import org.gcube.gcat.utils.RandomString; import org.gcube.gcat.utils.RandomString;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -13,7 +13,7 @@ import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.Resources;
import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatDiscovery; import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatDiscovery;
import org.gcube.datacatalogue.metadatadiscovery.reader.QueryForResourceUtil; import org.gcube.datacatalogue.metadatadiscovery.reader.QueryForResourceUtil;
import org.gcube.gcat.api.Role; import org.gcube.gcat.api.roles.Role;
import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUser;
import org.gcube.gcat.persistence.ckan.CKANUserCache; import org.gcube.gcat.persistence.ckan.CKANUserCache;
import org.gcube.gcat.utils.Constants; import org.gcube.gcat.utils.Constants;

View File

@ -19,7 +19,7 @@ import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.Resources;
import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatDiscovery; import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatDiscovery;
import org.gcube.datacatalogue.metadatadiscovery.reader.QueryForResourceUtil; import org.gcube.datacatalogue.metadatadiscovery.reader.QueryForResourceUtil;
import org.gcube.gcat.api.Role; import org.gcube.gcat.api.roles.Role;
import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUser;
import org.gcube.gcat.persistence.ckan.CKANUserCache; import org.gcube.gcat.persistence.ckan.CKANUserCache;
import org.gcube.gcat.utils.Constants; import org.gcube.gcat.utils.Constants;

View File

@ -13,6 +13,8 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.xml.ws.WebServiceException; import javax.xml.ws.WebServiceException;
import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.ResourceInitializer;
@ -129,22 +131,20 @@ public class Item extends REST<CKANPackage> implements org.gcube.gcat.api.interf
return delete(name, new Boolean(purge)); return delete(name, new Boolean(purge));
} }
@Override @POST
public String approve(String moderatorMessage) { @Path("/{" + ITEM_ID_PARAMETER + "}")
// TODO Auto-generated method stub @Consumes(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
return null; @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8)
} public Response moderate(@PathParam(ITEM_ID_PARAMETER) String id, String json) {
setCalledMethod("POST /" + COLLECTION_PARAMETER + "/{" + ID_PARAMETER + "}");
@Override CKANPackage ckanPackage = getInstance();
public String reject(String moderatorMessage) { ckanPackage.setName(id);
// TODO Auto-generated method stub String ret = ckanPackage.moderate(json);
return null; ResponseBuilder responseBuilder = Response.status(Status.ACCEPTED);
} if(ret!=null) {
responseBuilder.entity(ret).type(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8);
@Override }
public void message(String message) { return responseBuilder.build();
// TODO Auto-generated method stub
} }
} }

View File

@ -8,7 +8,7 @@ import java.util.List;
import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.InternalServerErrorException;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.gcat.utils.HTTPUtility; import org.gcube.gcat.utils.HTTPUtility;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
@ -123,7 +123,7 @@ public class PortalUser {
public boolean isCatalogueModerator() { public boolean isCatalogueModerator() {
if(catalogueModerator == null) { if(catalogueModerator == null) {
catalogueModerator = getRoles().contains(GCatConstants.CATALOGUE_MODERATOR); catalogueModerator = getRoles().contains(Moderated.CATALOGUE_MODERATOR);
} }
return catalogueModerator; return catalogueModerator;
} }

View File

@ -76,7 +76,7 @@ public class Constants {
} }
} }
public static String getJWTAccessToken() throws Exception { private static String getJWTAccessToken() throws Exception {
String contextToAuthorise = SecretManager.instance.get().getContext(); String contextToAuthorise = SecretManager.instance.get().getContext();
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(contextToAuthorise), contextToAuthorise, null); TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(contextToAuthorise), contextToAuthorise, null);
return tr.getAccessToken(); return tr.getAccessToken();

View File

@ -29,8 +29,9 @@ import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.gcat.ContextTest; import org.gcube.gcat.ContextTest;
import org.gcube.gcat.api.CMItemStatus;
import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.api.moderation.CMItemStatus;
import org.gcube.gcat.api.moderation.Moderated;
import org.gcube.informationsystem.publisher.RegistryPublisher; import org.gcube.informationsystem.publisher.RegistryPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory; import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.junit.Assert; import org.junit.Assert;
@ -95,7 +96,7 @@ public class CKANPackageTest extends ContextTest {
@Override @Override
public MultivaluedMap<String, String> getQueryParameters() { public MultivaluedMap<String, String> getQueryParameters() {
MultivaluedMap<String, String> mvm = new MultivaluedHashMap<String,String>(); MultivaluedMap<String, String> mvm = new MultivaluedHashMap<String,String>();
mvm.add(GCatConstants.CM_ITEM_STATUS_QUERY_PARAMETER, CMItemStatus.PENDING.getValue()); mvm.add(Moderated.CM_ITEM_STATUS_QUERY_PARAMETER, CMItemStatus.PENDING.getValue());
return mvm; return mvm;
} }