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.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.encryption.encrypter.StringEncrypter;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.NodeList;
/**
* @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_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";
public static final String GENERIC_RESOURCE_CKAN_ORGANIZATIONS = "CKANOrganizations";
protected Set<String> getSupportedOrganizationsFromIS() {
Set<String> supportedOrganizations = new HashSet<>();
@ -221,14 +222,19 @@ public class CKANInstance {
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();
try {
GenericResource genericResource = resources.get(0);
String body = genericResource.profile().body().getTextContent();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(body);
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);
}
}catch (Exception e) {
supportedOrganizations.clear();
supportedOrganizations.add(currentOrganizationName);
}
}

View File

@ -278,32 +278,22 @@ public class CKANPackageTest extends ContextTest {
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) {
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();
stringWriter.append("<");
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME_PLURAL);
stringWriter.append(">");
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
ArrayNode arrayNode = objectNode.putArray(CKANInstance.GENERIC_RESOURCE_CKAN_ORGANIZATIONS);
for(String organizationName : organizations) {
stringWriter.append("<");
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME);
stringWriter.append(">");
stringWriter.append(organizationName);
stringWriter.append("</");
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME);
stringWriter.append(">");
arrayNode.add(organizationName);
}
stringWriter.append("</");
stringWriter.append(CKANInstance.GENERIC_RESOURCE_TAG_NAME_PLURAL);
stringWriter.append(">");
return stringWriter.toString();
return objectMapper.writeValueAsString(objectNode);
}
protected void createGenericResourceForSupportedOrganizations(List<String> organizations) throws Exception {
String xml = createXMLFragment(organizations);
createGenericResource(xml);
String json = createGRBody(organizations);
createGenericResource(json);
}
// @Test
@ -629,7 +619,7 @@ public class CKANPackageTest extends ContextTest {
@Ignore
// @Test
public void deleteAllTest() {
public void deleteAllInASpecificOrganization() {
CKANPackage ckanPackage = new CKANPackage();
MultivaluedMap<String, String> mvm = new MultivaluedHashMap<String,String>();
mvm.add(GCatConstants.Q_KEY, "organization:devvre");