Rationalizing configuration got from IS
This commit is contained in:
parent
7051a5b952
commit
bf637e439f
|
@ -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<String,CKANInstance> 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<String> supportedOrganizations;
|
||||
|
||||
|
||||
static {
|
||||
ckanInstancePerScope = new HashMap<String,CKANInstance>();
|
||||
}
|
||||
|
@ -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<String> getSupportedOrganizationsFromIS() {
|
||||
Set<String> 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<GenericResource> client = ICFactory.clientFor(GenericResource.class);
|
||||
List<GenericResource> 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<String> getSupportedOrganizations() {
|
||||
return supportedOrganizations;
|
||||
}
|
||||
|
||||
|
||||
public void setModerationEnabled(boolean moderationEnabled) {
|
||||
this.moderationEnabled = moderationEnabled;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String> supportedOrganizations;
|
||||
protected final CKANInstance ckanInstance;
|
||||
protected final Set<String> 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<CKANResource>();
|
||||
currentContext = ContextUtility.getCurrentContext();
|
||||
currentScopeBean = new ScopeBean(currentContext);
|
||||
|
||||
ckanUser = CKANUserCache.getCurrrentCKANUser();
|
||||
getSupportedOrganizationsFromIS();
|
||||
}
|
||||
|
||||
protected Set<String> 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<GenericResource> client = ICFactory.clientFor(GenericResource.class);
|
||||
List<GenericResource> 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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(CKANPackage.GENERIC_RESOURCE_TAG_NAME);
|
||||
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME);
|
||||
stringWriter.append(">");
|
||||
}
|
||||
stringWriter.append("</");
|
||||
stringWriter.append(CKANPackage.GENERIC_RESOURCE_TAG_NAME_PLURAL);
|
||||
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME_PLURAL);
|
||||
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<String> organizations = ckanPackage.getSupportedOrganizationsFromIS();
|
||||
CKANInstance ckanInstance = CKANInstance.getInstance();
|
||||
Set<String> organizations = ckanInstance.getSupportedOrganizationsFromIS();
|
||||
Assert.assertTrue(organizations.size()>0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue