Allowed organizations in Generic Resource are now defined as JSON

This commit is contained in:
Luca Frosini 2021-12-14 19:14:30 +01:00
parent 196d362416
commit dda7bf6234
2 changed files with 25 additions and 29 deletions

View File

@ -13,6 +13,9 @@ import java.util.Set;
import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
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.common.authorization.utils.manager.SecretManager; import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.encryption.encrypter.StringEncrypter; import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.common.resources.gcore.GenericResource; import org.gcube.common.resources.gcore.GenericResource;
@ -25,7 +28,6 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory; import org.gcube.resources.discovery.icclient.ICFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.NodeList;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
@ -199,8 +201,7 @@ public class CKANInstance {
public static final String GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS = "ApplicationProfile"; 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_NAME_FOR_ORGANIZATIONS = "Supported CKAN Organizations";
public static final String GENERIC_RESOURCE_TAG_NAME = "CKANOrganization"; public static final String GENERIC_RESOURCE_CKAN_ORGANIZATIONS = "CKANOrganizations";
public static final String GENERIC_RESOURCE_TAG_NAME_PLURAL = "CKANOrganizations";
protected Set<String> getSupportedOrganizationsFromIS() { protected Set<String> getSupportedOrganizationsFromIS() {
Set<String> supportedOrganizations = new HashSet<>(); Set<String> supportedOrganizations = new HashSet<>();
@ -221,14 +222,19 @@ public class CKANInstance {
GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, currentOrganizationName); GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, currentOrganizationName);
supportedOrganizations.add(currentOrganizationName); supportedOrganizations.add(currentOrganizationName);
} else { } else {
GenericResource genericResource = resources.get(0); try {
NodeList nodeList = genericResource.profile().body().getChildNodes(); GenericResource genericResource = resources.get(0);
if(nodeList != null && nodeList.getLength() > 0) { String body = genericResource.profile().body().getTextContent();
for(int i = 0; i < nodeList.getLength(); i++) { ObjectMapper objectMapper = new ObjectMapper();
NodeList nl = nodeList.item(i).getChildNodes(); JsonNode jsonNode = objectMapper.readTree(body);
String organization = nl.item(0).getNodeValue(); ArrayNode array = (ArrayNode) jsonNode.get(GENERIC_RESOURCE_CKAN_ORGANIZATIONS);
for(int i = 0; i < array.size(); i++) {
String organization = array.get(i).asText();
supportedOrganizations.add(organization); supportedOrganizations.add(organization);
} }
}catch (Exception e) {
supportedOrganizations.clear();
supportedOrganizations.add(currentOrganizationName);
} }
} }

View File

@ -278,32 +278,22 @@ public class CKANPackageTest extends ContextTest {
genericResource.id(), stringWriter.toString()); genericResource.id(), stringWriter.toString());
} }
protected String createXMLFragment(List<String> organizations) throws Exception { protected String createGRBody(List<String> organizations) throws Exception {
if(organizations==null || organizations.size()<1) { if(organizations==null || organizations.size()<1) {
throw new Exception("Unable to create the XML fragment for the generic resource with empty organization list"); throw new Exception("Unable to create the body for the generic resource with empty organization list");
} }
StringWriter stringWriter = new StringWriter(); ObjectMapper objectMapper = new ObjectMapper();
stringWriter.append("<"); ObjectNode objectNode = objectMapper.createObjectNode();
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME_PLURAL); ArrayNode arrayNode = objectNode.putArray(CKANInstance.GENERIC_RESOURCE_CKAN_ORGANIZATIONS);
stringWriter.append(">");
for(String organizationName : organizations) { for(String organizationName : organizations) {
stringWriter.append("<"); arrayNode.add(organizationName);
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME);
stringWriter.append(">");
stringWriter.append(organizationName);
stringWriter.append("</");
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME);
stringWriter.append(">");
} }
stringWriter.append("</"); return objectMapper.writeValueAsString(objectNode);
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME_PLURAL);
stringWriter.append(">");
return stringWriter.toString();
} }
protected void createGenericResourceForSupportedOrganizations(List<String> organizations) throws Exception { protected void createGenericResourceForSupportedOrganizations(List<String> organizations) throws Exception {
String xml = createXMLFragment(organizations); String json = createGRBody(organizations);
createGenericResource(xml); createGenericResource(json);
} }
// @Test // @Test
@ -629,7 +619,7 @@ public class CKANPackageTest extends ContextTest {
@Ignore @Ignore
// @Test // @Test
public void deleteAllTest() { public void deleteAllInASpecificOrganization() {
CKANPackage ckanPackage = new CKANPackage(); CKANPackage ckanPackage = new CKANPackage();
MultivaluedMap<String, String> mvm = new MultivaluedHashMap<String,String>(); MultivaluedMap<String, String> mvm = new MultivaluedHashMap<String,String>();
mvm.add(GCatConstants.Q_KEY, "organization:devvre"); mvm.add(GCatConstants.Q_KEY, "organization:devvre");