From bf637e439f5aac75a098d11891c90542fcad72e2 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 10 May 2021 17:05:05 +0200 Subject: [PATCH] Rationalizing configuration got from IS --- .../gcat/persistence/ckan/CKANInstance.java | 104 ++++++++++++++++++ .../gcat/persistence/ckan/CKANPackage.java | 86 +++------------ .../persistence/ckan/CKANPackageTest.java | 16 +-- 3 files changed, 125 insertions(+), 81 deletions(-) diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANInstance.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANInstance.java index 56daf36..916164a 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANInstance.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANInstance.java @@ -4,22 +4,28 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.WebApplicationException; import org.gcube.common.encryption.encrypter.StringEncrypter; +import org.gcube.common.resources.gcore.GenericResource; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.scope.impl.ScopeBean; import org.gcube.gcat.utils.ContextUtility; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.w3c.dom.NodeList; /** * @author Luca Frosini (ISTI - CNR) @@ -39,6 +45,7 @@ public class CKANInstance { private final static String SOCIAL_POST = "SOCIAL_POST"; private final static String ALERT_USERS_ON_POST_CREATION = "ALERT_USERS_ON_POST_CREATION"; private final static String URL_RESOLVER = "URL_RESOLVER"; + private final static String MODERATION_ENABLED_KEY = "MODERATION_ENABLED"; private static final Map ckanInstancePerScope; @@ -48,6 +55,15 @@ public class CKANInstance { protected boolean notificationToUsersEnabled; protected String uriResolverURL; + protected boolean moderationEnabled; + + protected final String currentContext; + protected final ScopeBean currentScopeBean; + + protected final String currentOrganizationName; + protected final Set supportedOrganizations; + + static { ckanInstancePerScope = new HashMap(); } @@ -62,6 +78,14 @@ public class CKANInstance { return ckanInstance; } + + private CKANInstance() { + currentContext = ContextUtility.getCurrentContext(); + currentScopeBean = new ScopeBean(currentContext); + currentOrganizationName = CKANPackage.getOrganizationName(currentScopeBean); + supportedOrganizations = getSupportedOrganizationsFromIS(); + } + /** * Retrieve endpoints information from IS for DataCatalogue URL * @return list of endpoints for ckan data catalogue @@ -153,6 +177,15 @@ public class CKANInstance { uriResolverURL = accessPoint.propertyMap().get(URL_RESOLVER).value(); } + + moderationEnabled = false; + if(accessPoint.propertyMap().containsKey(MODERATION_ENABLED_KEY)) { + if(accessPoint.propertyMap().get(MODERATION_ENABLED_KEY).value().trim() + .equalsIgnoreCase("true")) { + moderationEnabled = true; + } + } + } } catch(WebApplicationException e) { @@ -163,6 +196,48 @@ public class CKANInstance { } + public static final String GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS = "ApplicationProfile"; + public static final String GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS = "Supported CKAN Organizations"; + public static final String GENERIC_RESOURCE_TAG_NAME = "CKANOrganization"; + public static final String GENERIC_RESOURCE_TAG_NAME_PLURAL = "CKANOrganizations"; + + protected Set getSupportedOrganizationsFromIS() { + Set supportedOrganizations = new HashSet<>(); + + SimpleQuery query = ICFactory.queryFor(GenericResource.class); + query.addCondition(String.format("$resource/Profile/SecondaryType/text() eq '%s'", + GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS)); + query.addCondition( + String.format("$resource/Profile/Name/text() eq '%s'", GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS)); + + DiscoveryClient client = ICFactory.clientFor(GenericResource.class); + List resources = client.submit(query); + + if(resources == null || resources.size() == 0) { + logger.info( + "{} with SecondaryType {} and Name %s not found. Item will be only be created in {} CKAN organization", + GenericResource.class.getSimpleName(), GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS, + GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, currentOrganizationName); + supportedOrganizations.add(currentOrganizationName); + } else { + GenericResource genericResource = resources.get(0); + NodeList nodeList = genericResource.profile().body().getChildNodes(); + if(nodeList != null && nodeList.getLength() > 0) { + for(int i = 0; i < nodeList.getLength(); i++) { + NodeList nl = nodeList.item(i).getChildNodes(); + String organization = nl.item(0).getNodeValue(); + supportedOrganizations.add(organization); + } + } + } + + logger.debug("Supported CKAN Organization for current Context ({}) are {}", currentContext, + supportedOrganizations); + + return supportedOrganizations; + } + + public String getUriResolverURL() throws Exception { return uriResolverURL; } @@ -179,8 +254,37 @@ public class CKANInstance { return notificationToUsersEnabled; } + public boolean isModerationEnabled() { + return moderationEnabled; + } + public String getSysAdminToken() throws Exception { return sysAdminToken; } + + + public String getCurrentContext() { + return currentContext; + } + + + public ScopeBean getCurrentScopeBean() { + return currentScopeBean; + } + + + public String getCurrentOrganizationName() { + return currentOrganizationName; + } + + + public Set getSupportedOrganizations() { + return supportedOrganizations; + } + + + public void setModerationEnabled(boolean moderationEnabled) { + this.moderationEnabled = moderationEnabled; + } } diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java index fa058d9..af7a2d1 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java @@ -17,25 +17,18 @@ import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response.Status; -import org.gcube.common.resources.gcore.GenericResource; +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.ObjectNode; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.oldutils.Validator; import org.gcube.gcat.profile.MetadataUtility; import org.gcube.gcat.social.SocialPost; -import org.gcube.gcat.utils.ContextUtility; import org.gcube.gcat.utils.URIResolver; -import org.gcube.resources.discovery.client.api.DiscoveryClient; -import org.gcube.resources.discovery.client.queries.api.SimpleQuery; -import org.gcube.resources.discovery.icclient.ICFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.NodeList; - -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.ObjectNode; /** * @author Luca Frosini (ISTI - CNR) @@ -77,11 +70,6 @@ public class CKANPackage extends CKAN { ORGANIZATION_REGEX_PATTERN = Pattern.compile(ORGANIZATION_REGEX); } - public static final String GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS = "ApplicationProfile"; - public static final String GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS = "Supported CKAN Organizations"; - public static final String GENERIC_RESOURCE_TAG_NAME = "CKANOrganization"; - public static final String GENERIC_RESOURCE_TAG_NAME_PLURAL = "CKANOrganizations"; - protected static final String LICENSE_KEY = "license_id"; protected static final String EXTRAS_ITEM_URL_KEY = "Item URL"; @@ -118,16 +106,14 @@ public class CKANPackage extends CKAN { protected String itemID; - protected final ScopeBean currentScopeBean; - protected final String currentContext; - protected final CKANUser ckanUser; - protected String currentOrganizationName; - protected Set supportedOrganizations; + protected final CKANInstance ckanInstance; + protected final Set supportedOrganizations; public CKANPackage() { super(); + LIST = ITEM_LIST; CREATE = ITEM_CREATE; READ = ITEM_SHOW; @@ -136,55 +122,16 @@ public class CKANPackage extends CKAN { DELETE = ITEM_DELETE; PURGE = ITEM_PURGE; managedResources = new ArrayList(); - currentContext = ContextUtility.getCurrentContext(); - currentScopeBean = new ScopeBean(currentContext); + ckanUser = CKANUserCache.getCurrrentCKANUser(); - getSupportedOrganizationsFromIS(); - } - - protected Set getSupportedOrganizationsFromIS() { - if(supportedOrganizations == null) { - - // TODO Add a VRE based cache - - supportedOrganizations = new HashSet<>(); - - SimpleQuery query = ICFactory.queryFor(GenericResource.class); - query.addCondition(String.format("$resource/Profile/SecondaryType/text() eq '%s'", - GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS)); - query.addCondition( - String.format("$resource/Profile/Name/text() eq '%s'", GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS)); - - DiscoveryClient client = ICFactory.clientFor(GenericResource.class); - List resources = client.submit(query); - - if(resources == null || resources.size() == 0) { - logger.info( - "{} with SecondaryType {} and Name %s not found. Item will be only be created in {} CKAN organization", - GenericResource.class.getSimpleName(), GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS, - GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, getOrganizationName()); - supportedOrganizations.add(getOrganizationName()); - } else { - GenericResource genericResource = resources.get(0); - NodeList nodeList = genericResource.profile().body().getChildNodes(); - if(nodeList != null && nodeList.getLength() > 0) { - for(int i = 0; i < nodeList.getLength(); i++) { - NodeList nl = nodeList.item(i).getChildNodes(); - String organization = nl.item(0).getNodeValue(); - supportedOrganizations.add(organization); - } - } - } - - } + + ckanInstance = CKANInstance.getInstance(); + supportedOrganizations = ckanInstance.getSupportedOrganizations(); for(String supportedOrganization : supportedOrganizations) { ckanUser.addUserToOrganization(supportedOrganization); } - logger.debug("Supported CKAN Organization for current Context ({}) are {}", currentContext, - supportedOrganizations); - return supportedOrganizations; } /* @@ -195,13 +142,6 @@ public class CKANPackage extends CKAN { return contextName.toLowerCase().replace(" ", "_"); } - protected String getOrganizationName() { - if(currentOrganizationName == null) { - this.currentOrganizationName = CKANPackage.getOrganizationName(currentScopeBean); - } - return currentOrganizationName; - } - protected CKANOrganization checkGotOrganization(String gotOrganization) throws ForbiddenException { if(!supportedOrganizations.contains(gotOrganization)) { String error = String.format( @@ -226,7 +166,7 @@ public class CKANPackage extends CKAN { if(ckanOrganization == null) { // owner organization must be specified if the token belongs to a VRE - String organizationFromContext = getOrganizationName(); + String organizationFromContext = ckanInstance.getCurrentOrganizationName(); ckanOrganization = checkGotOrganization(organizationFromContext); objectNode.put(OWNER_ORG_KEY, organizationFromContext); } @@ -613,7 +553,7 @@ public class CKANPackage extends CKAN { } String catalogueItemURL = ""; - if(currentScopeBean.is(Type.VRE)) { + if(ckanInstance.getCurrentScopeBean().is(Type.VRE)) { catalogueItemURL = addItemURLViaResolver(jsonNode); } @@ -629,7 +569,7 @@ public class CKANPackage extends CKAN { // Actions performed after a package has been correctly created on ckan. String title = result.get(TITLE_KEY).asText(); - if(currentScopeBean.is(Type.VRE)) { + if(ckanInstance.getCurrentScopeBean().is(Type.VRE)) { sendSocialPost(title, catalogueItemURL); } diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java index 62f9180..7ee5c88 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java @@ -145,8 +145,8 @@ public class CKANPackageTest extends ContextTest { protected void createGenericResource(String xml) throws Exception { GenericResource genericResource = instantiateGenericResource( - CKANPackage.GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS, - CKANPackage.GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, xml); + CKANInstance.GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS, + CKANInstance.GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, xml); RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); genericResource = registryPublisher.create(genericResource); StringWriter stringWriter = new StringWriter(); @@ -161,19 +161,19 @@ public class CKANPackageTest extends ContextTest { } StringWriter stringWriter = new StringWriter(); stringWriter.append("<"); - stringWriter.append(CKANPackage.GENERIC_RESOURCE_TAG_NAME_PLURAL); + stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME_PLURAL); stringWriter.append(">"); for(String organizationName : organizations) { stringWriter.append("<"); - stringWriter.append(CKANPackage.GENERIC_RESOURCE_TAG_NAME); + stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME); stringWriter.append(">"); stringWriter.append(organizationName); stringWriter.append(""); } stringWriter.append(""); return stringWriter.toString(); } @@ -208,8 +208,8 @@ public class CKANPackageTest extends ContextTest { @Test public void testGetSupportedOrganizationsFromIS() throws ObjectNotFound, Exception { ContextTest.setContextByName("/gcube/devNext/NextNext"); - CKANPackage ckanPackage = new CKANPackage(); - Set organizations = ckanPackage.getSupportedOrganizationsFromIS(); + CKANInstance ckanInstance = CKANInstance.getInstance(); + Set organizations = ckanInstance.getSupportedOrganizationsFromIS(); Assert.assertTrue(organizations.size()>0); }