changed LOG. Fixed marsh/unmarsh for CkanResource

This commit is contained in:
Francesco Mangiacrapa 2021-02-11 11:03:22 +01:00
parent 4b05ed4e97
commit e5fcb94260
7 changed files with 305 additions and 252 deletions

View File

@ -98,11 +98,13 @@ public class MarshUnmarshCkanObject {
* To json value resource.
*
* @param resource the resource
* @param method the method
* @return the string
* @throws JsonProcessingException the json processing exception
*/
public static String toJsonValueResource(CkanResource resource) throws JsonProcessingException{
return ExtendCkanClient.getObjectMapper().writeValueAsString(resource);
public static String toJsonValueResource(CkanResource resource, METHOD method) throws JsonProcessingException{
ObjectMapper om = getObjectMapper(method, CkanResource.class);
return om.writeValueAsString(resource);
}

View File

@ -37,8 +37,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
@ -74,6 +72,8 @@ import org.gcube.datacatalogue.ckanutillibrary.shared.jackan.model.exceptions.Ck
import org.gcube.datacatalogue.ckanutillibrary.shared.jackan.model.exceptions.CkanNotFoundException;
import org.gcube.datacatalogue.ckanutillibrary.shared.jackan.model.exceptions.CkanValidationException;
import org.gcube.datacatalogue.ckanutillibrary.shared.jackan.model.exceptions.JackanException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
@ -147,7 +147,7 @@ public class CkanClient {
*/
public static final String NONE = "None";
private static final Logger LOG = Logger.getLogger(CkanClient.class.getName());
private static final Logger LOG = LoggerFactory.getLogger(CkanClient.class.getName());
private static final String COULDNT_JSONIZE = "Couldn't jsonize the provided ";
@ -261,7 +261,7 @@ public class CkanClient {
checkNotNull(clazz, "Invalid class! If you don't know the class just use Object.class");
if (OBJECT_MAPPERS_FOR_POSTING.get(clazz.getName()) == null) {
LOG.log(Level.FINE, "Generating ObjectMapper for posting class {0}", clazz);
LOG.info("Generating ObjectMapper for posting class {0}", clazz);
ObjectMapper om = new ObjectMapper();
configureObjectMapperForPosting(om, clazz);
OBJECT_MAPPERS_FOR_POSTING.put(clazz.getName(), om);
@ -508,7 +508,7 @@ public class CkanClient {
String returnedText;
try {
LOG.log(Level.FINE, "getting {0}", fullUrl);
LOG.info("getting {0}", fullUrl);
Request request = Request.Get(fullUrl);
configureRequest(request);
@ -594,8 +594,8 @@ public class CkanClient {
String returnedText;
try {
LOG.log(Level.FINE, "Posting to url {0}", fullUrl);
LOG.log(Level.FINE, "Sending body:{0}", body);
LOG.info("Posting to url {0}", fullUrl);
LOG.info("Sending body:{0}", body);
Request request = Request.Post(fullUrl);
configureRequest(request);
@ -655,7 +655,7 @@ public class CkanClient {
String returnedText;
try {
LOG.log(Level.FINE, "Posting to url {0}", fullUrl);
LOG.info("Posting to url {0}", fullUrl);
Request request = Request.Post(fullUrl);
configureRequest(request);
@ -887,7 +887,7 @@ public class CkanClient {
*/
private synchronized int getApiVersion(int number) {
String fullUrl = catalogUrl + "/api/" + number;
LOG.log(Level.FINE, "getting {0}", fullUrl);
LOG.info("getting {0}", fullUrl);
try {
Request request = Request.Get(fullUrl);
configureRequest(request);

View File

@ -22,8 +22,6 @@ import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.gcube.com.fasterxml.jackson.core.JsonGenerator;
import org.gcube.com.fasterxml.jackson.core.JsonParser;
@ -36,6 +34,8 @@ import org.gcube.com.fasterxml.jackson.databind.SerializerProvider;
import org.gcube.com.fasterxml.jackson.databind.module.SimpleModule;
import org.gcube.com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.gcube.datacatalogue.ckanutillibrary.shared.jackan.model.CkanDataset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Custom Jackson module to serialize/deserialize as JSON Ckan objects with
@ -55,7 +55,7 @@ import org.gcube.datacatalogue.ckanutillibrary.shared.jackan.model.CkanDataset;
*/
public class JackanModule extends SimpleModule {
private static final Logger LOG = Logger.getLogger(JackanModule.class.getName());
private static final Logger LOG = LoggerFactory.getLogger(JackanModule.class.getName());
public JackanModule() {
@ -69,7 +69,7 @@ public class JackanModule extends SimpleModule {
jgen.writeString(str);
}
catch (Exception ex) {
LOG.log(Level.SEVERE, "Couldn't format timestamp " + value + ", writing 'null'", ex);
LOG.warn("Couldn't format timestamp " + value + ", writing 'null'", ex);
jgen.writeNull();
}
}
@ -88,7 +88,7 @@ public class JackanModule extends SimpleModule {
return CkanClient.parseTimestamp(str);
}
catch (IllegalArgumentException ex) {
LOG.log(Level.SEVERE, "Couldn't parse timestamp " + str + ", returning null", ex);
LOG.warn("Couldn't parse timestamp " + str + ", returning null", ex);
return null;
}
}
@ -97,7 +97,7 @@ public class JackanModule extends SimpleModule {
return null;
}
LOG.log(Level.SEVERE, "Unrecognized json token for timestamp {0}, returning null", t.asString());
LOG.warn("Unrecognized json token for timestamp {0}, returning null", t.asString());
return null;
}
@ -111,7 +111,7 @@ public class JackanModule extends SimpleModule {
*/
public static class GroupOrgPackagesDeserializer extends JsonDeserializer<List<CkanDataset>> {
private static final Logger LOG = Logger.getLogger(GroupOrgPackagesDeserializer.class.getName());
private static final Logger LOG = LoggerFactory.getLogger(GroupOrgPackagesDeserializer.class.getName());
@Override
public List<CkanDataset> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
@ -127,7 +127,7 @@ public class JackanModule extends SimpleModule {
});
}
LOG.log(Level.SEVERE, "Unrecognized token {0} for 'packages' field, returning an empty array.", t.asString());
LOG.warn("Unrecognized token {0} for 'packages' field, returning an empty array.", t.asString());
return new ArrayList();
}
}

View File

@ -19,6 +19,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.datacatalogue.ckanutillibrary.ckan.ExtendCkanClient;
@ -29,6 +30,8 @@ import org.gcube.datacatalogue.ckanutillibrary.gcat.GCatCaller;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueRunningCluster.ACCESS_LEVEL_TO_CATALOGUE_PORTLET;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CKANConveter;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.GCubeUtils;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.GCubeUtils.GCUBE_SCOPE_LEVEL;
import org.gcube.datacatalogue.ckanutillibrary.server.utils.url.EntityContext;
import org.gcube.datacatalogue.ckanutillibrary.shared.LandingPages;
import org.gcube.datacatalogue.ckanutillibrary.shared.ResourceBean;
@ -46,7 +49,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This is the Ckan Utils implementation class.
*
@ -214,7 +216,7 @@ public class DataCatalogueImpl implements DataCatalogue {
LOG.debug("Request for CKAN licenses");
// get the url and the api key of the user
List<String> result = new ArrayList<String>();
//retrieve the list of available licenses
// retrieve the list of available licenses
List<CkanLicense> licenses = ckanCaller.getLicenseList();
for (CkanLicense ckanLicense : licenses) {
@ -230,12 +232,16 @@ public class DataCatalogueImpl implements DataCatalogue {
@Override
public List<CkanLicense> getLicenses() {
LOG.debug("Request for CKAN licenses (original jackan objects are going to be retrieved)");
//retrieve the list of available licenses
// retrieve the list of available licenses
return ckanCaller.getLicenseList();
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue#getDataset(java.lang.String, java.lang.String)
/*
* (non-Javadoc)
*
* @see
* org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue#getDataset(java.
* lang.String, java.lang.String)
*/
@Override
public CkanDataset getDataset(String datasetId, String apiKey) {
@ -245,25 +251,25 @@ public class DataCatalogueImpl implements DataCatalogue {
checkNotNull(datasetId);
checkArgument(!datasetId.isEmpty());
try{
if(apiKey!=null && !apiKey.isEmpty()) {
LOG.info("API-KEY found. Calling the "+ExtendCkanClient.class.getSimpleName());
try {
if (apiKey != null && !apiKey.isEmpty()) {
LOG.info("API-KEY found. Calling the " + ExtendCkanClient.class.getSimpleName());
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, apiKey);
return client.getDataset(datasetId);
}
String authzToken = SecurityTokenProvider.instance.get();
if(authzToken!=null && !authzToken.isEmpty()) {
if (authzToken != null && !authzToken.isEmpty()) {
LOG.info("gcube-token found. Calling the gCat client");
String jsonDataset = gCatCaller.getDatasetForName(datasetId);
return MarshUnmarshCkanObject.toCkanDataset(jsonDataset, METHOD.TO_READ);
}
LOG.info("No api-key or gcube-token found. Calling Ckan Client without API-KEY");
return ckanCaller.getDataset(datasetId);
}catch(Exception e){
} catch (Exception e) {
LOG.error("Unable to retrieve such dataset, returning null ...", e);
}
@ -279,42 +285,42 @@ public class DataCatalogueImpl implements DataCatalogue {
checkNotNull(datasetIdOrName);
checkArgument(!datasetIdOrName.isEmpty());
String url = null;
try{
try {
// get the dataset from name
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
CkanDataset dataset = client.getDataset(datasetIdOrName);
String name = dataset.getName();
if(dataset != null){
if (dataset != null) {
if(getUriResolverUrl() != null)
if (getUriResolverUrl() != null)
url = getUrlForProduct(CONTEXT, EntityContext.DATASET, name);
if(url == null || url.isEmpty())
url = getPortletUrl() + "?" + URLEncoder.encode("path=/dataset/" + name, "UTF-8");
if (url == null || url.isEmpty())
url = getPortletUrl() + "?" + URLEncoder.encode("path=/dataset/" + name, "UTF-8");
}
}catch(Exception e){
} catch (Exception e) {
LOG.error("Error while retrieving dataset with id/name=" + datasetIdOrName, e);
} //requestEntity.put("clear_url", Boolean.toString(unencrypted));
} // requestEntity.put("clear_url", Boolean.toString(unencrypted));
return url;
}
@Override
public Map<String, Map<CkanGroup, RolesCkanGroupOrOrg>> getUserRoleByGroup(String username) {
LOG.info("Get user role by group called. The username is: "+username);
LOG.info("Get user role by group called. The username is: " + username);
checkNotNull(username);
Map<String, Map<CkanGroup, RolesCkanGroupOrOrg>> toReturn = new HashMap<String, Map<CkanGroup,RolesCkanGroupOrOrg>>();
Map<String, Map<CkanGroup, RolesCkanGroupOrOrg>> toReturn = new HashMap<String, Map<CkanGroup, RolesCkanGroupOrOrg>>();
try{
try {
String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username);
Map<String, RolesCkanGroupOrOrg> partialResult = dbCaller.getGroupsByUserFromDB(ckanUsername);
for (String groupID : partialResult.keySet()) {
CkanGroup group = ckanCaller.getGroup(groupID);
@ -322,27 +328,26 @@ public class DataCatalogueImpl implements DataCatalogue {
subMap.put(group, partialResult.get(groupID));
toReturn.put(groupID, subMap);
}
LOG.debug("Returning map " + toReturn);
LOG.info("Found " + toReturn.size() +" group/s with the user "+username);
}catch(Exception e){
LOG.error("Failed to retrieve roles of user in his/her own groups",e);
LOG.info("Found " + toReturn.size() + " group/s with the user " + username);
} catch (Exception e) {
LOG.error("Failed to retrieve roles of user in his/her own groups", e);
}
return toReturn;
}
@Override
public Map<String, Map<CkanOrganization, RolesCkanGroupOrOrg>> getUserRoleByOrganization(
String username) {
LOG.info("Get user role by organization called. The username is: "+username);
public Map<String, Map<CkanOrganization, RolesCkanGroupOrOrg>> getUserRoleByOrganization(String username) {
LOG.info("Get user role by organization called. The username is: " + username);
checkNotNull(username);
Map<String, Map<CkanOrganization, RolesCkanGroupOrOrg>> toReturn = new HashMap<String, Map<CkanOrganization,RolesCkanGroupOrOrg>>();
Map<String, Map<CkanOrganization, RolesCkanGroupOrOrg>> toReturn = new HashMap<String, Map<CkanOrganization, RolesCkanGroupOrOrg>>();
try{
try {
String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username);
Map<String, RolesCkanGroupOrOrg> partialResult = dbCaller.getOrganizationsByUserFromDB(ckanUsername);
@ -353,13 +358,13 @@ public class DataCatalogueImpl implements DataCatalogue {
HashMap<CkanOrganization, RolesCkanGroupOrOrg> subMap = new HashMap<CkanOrganization, RolesCkanGroupOrOrg>();
subMap.put(org, partialResult.get(orgID));
toReturn.put(orgID, subMap);
LOG.debug("For organisation: " + org.getName() + ", the user "+username+" has role: "+subMap);
LOG.debug("For organisation: " + org.getName() + ", the user " + username + " has role: " + subMap);
}
LOG.debug("Returning map " + toReturn);
LOG.info("Found " + toReturn.size() +" organization/s with the user "+username);
}catch(Exception e){
LOG.error("Failed to retrieve roles of user in his/her own groups",e);
LOG.info("Found " + toReturn.size() + " organization/s with the user " + username);
} catch (Exception e) {
LOG.error("Failed to retrieve roles of user in his/her own groups", e);
}
return toReturn;
@ -368,11 +373,12 @@ public class DataCatalogueImpl implements DataCatalogue {
/**
* Retrieve an url for the tuple scope, entity, entity name
*
* @param context
* @param entityContext
* @param entityName
*/
private String getUrlForProduct(String context, EntityContext entityContext, String entityName){
private String getUrlForProduct(String context, EntityContext entityContext, String entityName) {
String toReturn = null;
@ -390,19 +396,19 @@ public class DataCatalogueImpl implements DataCatalogue {
HttpResponse response = httpClient.execute(httpPostRequest);
if(response.getStatusLine().getStatusCode() != 200)
if (response.getStatusLine().getStatusCode() != 200)
throw new Exception("There was an error while creating an url " + response.getStatusLine());
toReturn = EntityUtils.toString(response.getEntity());
LOG.debug("Result is " + toReturn);
}catch(Exception e){
} catch (Exception e) {
LOG.error("Failed to get an url for this product", e);
}
return toReturn;
return toReturn;
}
@Override
public LandingPages getLandingPages() throws Exception {
@ -413,8 +419,6 @@ public class DataCatalogueImpl implements DataCatalogue {
landingPages.setUrlTypes(PORTLET_URL_FOR_SCOPE + "?path=/type/");
return landingPages;
}
@Override
public String getUriResolverUrl() {
@ -442,43 +446,23 @@ public class DataCatalogueImpl implements DataCatalogue {
// list to return
List<CkanOrganization> toReturn = new ArrayList<CkanOrganization>();
try{
try {
Map<String, RolesCkanGroupOrOrg> partialResult = dbCaller.getOrganizationsByUserFromDB(ckanUsername);
for (String orgID : partialResult.keySet()) {
CkanOrganization org = ckanCaller.getOrganization(orgID,false);
CkanOrganization org = ckanCaller.getOrganization(orgID, false);
LOG.debug("User " + ckanUsername + " is into " + org.getName());
toReturn.add(org);
}
// get the list of all organizations
//List<CkanOrganization> organizations = ckanCaller.getOrganizationList();
// iterate over them
/*for (CkanOrganization ckanOrganization : organizations) {
// get the list of users in it (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO)
List<CkanUser> users = ckanCaller.getOrganization(ckanOrganization.getName()).getUsers();
// check if the current user is among them
for (CkanUser ckanUser : users) {
if(ckanUser.getName().equals(ckanUsername)){
LOG.debug("User " + ckanUsername + " is into " + ckanOrganization.getName());
toReturn.add(ckanOrganization);
break;
}
}
}*/
}catch(Exception e){
} catch (Exception e) {
LOG.error("Unable to get user's organizations", e);
}
return toReturn;
}
@Override
public List<CkanGroup> getGroupsByUser(String username) {
LOG.debug("Requested groups for user " + username);
@ -490,46 +474,25 @@ public class DataCatalogueImpl implements DataCatalogue {
// list to return
List<CkanGroup> toReturn = new ArrayList<CkanGroup>();
try{
try {
Map<String, RolesCkanGroupOrOrg> partialResult = dbCaller.getGroupsByUserFromDB(ckanUsername);
for (String groupID : partialResult.keySet()) {
CkanGroup group = ckanCaller.getGroup(groupID,false);
CkanGroup group = ckanCaller.getGroup(groupID, false);
LOG.debug("User " + ckanUsername + " is into " + group.getName());
toReturn.add(group);
}
/*
// get the list of all organizations
List<CkanGroup> groups = ckanCaller.getGroupList();
// iterate over them
for (CkanGroup ckanGroup : groups) {
List<CkanUser> users = ckanCaller.getGroup(ckanGroup.getName()).getUsers();
// check if the current user is among them
for (CkanUser ckanUser : users) {
if(ckanUser.getName().equals(ckanUsername)){
LOG.debug("User " + ckanUsername + " is into " + ckanGroup.getName());
toReturn.add(ckanGroup);
break;
}
}
}*/
}catch(Exception e){
} catch (Exception e) {
LOG.error("Unable to get user's groups", e);
}
return toReturn;
}
@Override
public List<String> getOrganizationsIds(){
public List<String> getOrganizationsIds() {
List<String> toReturn = new ArrayList<String>();
List<CkanOrganization> orgs = ckanCaller.getOrganizationList();
@ -543,7 +506,7 @@ public class DataCatalogueImpl implements DataCatalogue {
}
@Override
public List<String> getOrganizationsNames(){
public List<String> getOrganizationsNames() {
List<String> toReturn = new ArrayList<String>();
List<CkanOrganization> orgs = ckanCaller.getOrganizationList();
@ -577,7 +540,7 @@ public class DataCatalogueImpl implements DataCatalogue {
return orgsName;
}
@Override
public String getRoleOfUserInOrganization(String username, String orgName) {
@ -586,19 +549,19 @@ public class DataCatalogueImpl implements DataCatalogue {
String apiKey = getApiKeyFromUsername(username);
String usernameCkan = CatalogueUtilMethods.fromUsernameToCKanUsername(username);
try{
try {
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, apiKey);
List<CkanUser> users = client.getOrganization(orgName).getUsers();
for (CkanUser ckanUser : users) {
if(ckanUser.getName().equals(usernameCkan)){
if (ckanUser.getName().equals(usernameCkan)) {
toReturn = ckanUser.getCapacity();
break;
}
}
}catch(Exception e){
LOG.error("Unable to retrieve the role the user has into organization: "+orgName, e);
} catch (Exception e) {
LOG.error("Unable to retrieve the role the user has into organization: " + orgName, e);
}
return toReturn;
@ -613,10 +576,10 @@ public class DataCatalogueImpl implements DataCatalogue {
*/
@Override
public boolean checkValidUser(String username) {
String apiKey = getApiKeyFromUsername(username);
return apiKey != null;
}
private String getApiKeyFromUsername(String username) {
@ -631,64 +594,144 @@ public class DataCatalogueImpl implements DataCatalogue {
String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username);
// check in the hashmap first
if(apiKeysMap.containsKey(ckanUsername)){
if (apiKeysMap.containsKey(ckanUsername)) {
CKANTokenBean bean = apiKeysMap.get(ckanUsername);
if(bean.timestamp + EXPIRE_KEY_TIME > System.currentTimeMillis()){ // it's still ok
if (bean.timestamp + EXPIRE_KEY_TIME > System.currentTimeMillis()) { // it's still ok
return bean.apiKey;
}
}
LOG.debug("Api key was not in cache or it expired");
try{
try {
String apiToReturn = dbCaller.getApiKeyFromUsername(username, State.ACTIVE.name().toLowerCase());
// save into the hashmap
if(apiToReturn != null)
if (apiToReturn != null)
apiKeysMap.put(ckanUsername, new CKANTokenBean(apiToReturn, System.currentTimeMillis()));
return apiToReturn;
}catch(Exception e){
} catch (Exception e) {
LOG.error("Unable to retrieve key for user " + ckanUsername, e);
}
return null;
}
@Override
public boolean existProductWithNameOrId(String nameOrId) {
checkNotNull(nameOrId);
checkArgument(!nameOrId.isEmpty());
try{
try {
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
CkanDataset product = client.getDataset(nameOrId);
return product != null;
}catch(Exception e){
} catch (Exception e) {
LOG.debug("A dataset with name " + nameOrId + " doesn't exist");
return false;
}
}
@Override
public CkanOrganization getOrganizationByIdOrName(String idOrName) {
checkNotNull(idOrName);
String ckanName = idOrName.toLowerCase();
try{
try {
return ckanCaller.getOrganization(ckanName);
}catch(Exception e){
LOG.warn("Failed to retrieve the organization with name " +idOrName+ " on the ckan: "+ckanCaller.getCatalogUrl(), e);
} catch (Exception e) {
LOG.warn("Failed to retrieve the organization with name " + idOrName + " on the ckan: "
+ ckanCaller.getCatalogUrl(), e);
}
return null;
}
/**
* Check if the user has this role into the organization/group with
* groupOrOrganization name
*
* @param ckanUsername
* @param organizationName
* @param correspondentRoleToCheck
* @return true if he has the role, false otherwise
*/
protected boolean isRoleAlreadySet(String ckanUsername, String groupOrOrganization,
RolesCkanGroupOrOrg correspondentRoleToCheck, boolean group) throws Exception {
// get the users (if you try ckanOrganization.getUsers() it returns null.. maybe
// a bug TODO)
List<CkanUser> users;
if (group)
users = ckanCaller.getGroup(groupOrOrganization).getUsers();
else
users = ckanCaller.getOrganization(groupOrOrganization).getUsers();
for (CkanUser ckanUser : users) {
if (ckanUser.getName().equals(ckanUsername))
if (ckanUser.getCapacity().equals(RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck)))
return true;
else
break;
}
return false;
}
/**
* Just check if the group exists
*
* @param nameOrId
* @param client
* @return
*/
private CkanGroup groupExists(String nameOrId) {
CkanGroup toReturn = null;
try {
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
toReturn = client.getGroup(nameOrId);
} catch (JackanException je) {
LOG.error("The group " + nameOrId + " doesn't exist");
}
return toReturn;
}
/*
*
*
*
*
*
*
*
*
*
* WRITE OPERATIONS
*
*
*
*
*
*
*
*
*
*
*/
@Override
public boolean checkRoleIntoOrganization(String username, String organizationName,
@ -804,58 +847,6 @@ public class DataCatalogueImpl implements DataCatalogue {
return false;
}
/**
* Check if the user has this role into the organization/group with groupOrOrganization name
* @param ckanUsername
* @param organizationName
* @param correspondentRoleToCheck
* @return true if he has the role, false otherwise
*/
protected boolean isRoleAlreadySet(String ckanUsername, String groupOrOrganization, RolesCkanGroupOrOrg correspondentRoleToCheck, boolean group) throws Exception{
// get the users (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO)
List<CkanUser> users;
if(group)
users = ckanCaller.getGroup(groupOrOrganization).getUsers();
else
users = ckanCaller.getOrganization(groupOrOrganization).getUsers();
for (CkanUser ckanUser : users) {
if(ckanUser.getName().equals(ckanUsername))
if(ckanUser.getCapacity().equals(RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck)))
return true;
else
break;
}
return false;
}
/*
*
*
*
*
*
*
*
*
*
* WRITE OPERATIONS
*
*
*
*
*
*
*
*
*
*
*/
@Override
public String createCkanDatasetMultipleCustomFields(String username, String title, String name, String organizationName,
String author, String authorMail, String maintainer, String maintainerMail, long version,
@ -871,10 +862,30 @@ public class DataCatalogueImpl implements DataCatalogue {
try {
//TODO IF THE SCOPE IS ROOT OR VO WE NEED TO PASS THE ORGNAME TO gCAT, no otherwise
//Business logic to pass the organization name to gCat if and only if the scope is of ROOT or VO (not VRE)
String scope = ScopeProvider.instance.get();
if(scope==null)
throw new Exception("No scope detected. You must set it");
GCUBE_SCOPE_LEVEL gCubeScopeLevel = GCubeUtils.toGCubeLevel(scope);
String toPassOrganizationToGcat = null;
if(gCubeScopeLevel!=null) {
switch (gCubeScopeLevel) {
case ROOT:
case VO:
toPassOrganizationToGcat = organizationName;
break;
case VRE:
toPassOrganizationToGcat = null;
default:
break;
}
}
// String ckanUsername = getUserFromApiKey(apiKey).getName();
CkanDataset dataset = CKANConveter.toCkanDataset(ckanCaller, username, title, name, organizationName, author, authorMail,
CkanDataset dataset = CKANConveter.toCkanDataset(ckanCaller, username, title, name, toPassOrganizationToGcat, author, authorMail,
maintainer, maintainerMail, version, description, licenseId, tags, null, customFieldsMultiple,
resources, setPublic, setSearchable);
@ -917,50 +928,51 @@ public class DataCatalogueImpl implements DataCatalogue {
}
@Override
@Override
public boolean patchFieldsForDataset(String datasetId, Map<String, String> mapFields) throws Exception {
LOG.info("Called patch the fields "+mapFields+" for dataset id: "+datasetId);
LOG.info("Called patch the fields " + mapFields + " for dataset id: " + datasetId);
checkNotNull(datasetId);
checkNotNull(mapFields);
try {
JSONObject jsonObj = new JSONObject();
for (String key : mapFields.keySet()) {
jsonObj.put(key, mapFields.get(key));
}
LOG.debug("Json Dataset is: " + jsonObj);
gCatCaller.patchDataset(datasetId, jsonObj);
LOG.info("Patch operation for dataset "+datasetId+" terminates without errors");
LOG.info("Patch operation for dataset " + datasetId + " terminates without errors");
return true;
}catch (Exception e) {
LOG.error("Error occurred trying to patch the dataset with id: "+datasetId, e.getMessage());
throw new Exception("Unable to patch the dataset. Error: "+e.getMessage());
} catch (Exception e) {
LOG.error("Error occurred trying to patch the dataset with id: " + datasetId, e.getMessage());
throw new Exception("Unable to patch the dataset. Error: " + e.getMessage());
}
}
@Override
public boolean setSearchableFieldForDataset(String datasetId, boolean searchable) throws Exception {
LOG.info("Set searchalbe field as "+searchable+" for dataset id: "+datasetId);
LOG.info("Set searchalbe field as " + searchable + " for dataset id: " + datasetId);
checkNotNull(datasetId);
try {
String searchableAsString = searchable ? "True" : "False";
JSONObject jsonObj = new JSONObject();
jsonObj.put("id", datasetId); //just to be sure
jsonObj.put("id", datasetId); // just to be sure
jsonObj.put("searchable", searchableAsString);
LOG.debug("Json Dataset is: " + jsonObj);
gCatCaller.patchDataset(datasetId, jsonObj);
LOG.info("Set 'searchable' field for dataset "+datasetId+" terminates without errors");
LOG.info("Set 'searchable' field for dataset " + datasetId + " terminates without errors");
return true;
}catch (Exception e) {
LOG.error("Error occured trying to set the 'searchable' field for the dataset with id: "+datasetId, e.getMessage());
throw new Exception("Unable to set the 'searchable' field for this dataset. Error: "+e.getMessage());
} catch (Exception e) {
LOG.error("Error occured trying to set the 'searchable' field for the dataset with id: " + datasetId,
e.getMessage());
throw new Exception("Unable to set the 'searchable' field for this dataset. Error: " + e.getMessage());
}
}
@ -971,23 +983,23 @@ public class DataCatalogueImpl implements DataCatalogue {
// checks
checkNotNull(resourceBean);
if(CatalogueUtilMethods.resourceExists(resourceBean.getUrl())){
if (CatalogueUtilMethods.resourceExists(resourceBean.getUrl())) {
CkanResource resource = CKANConveter.toCkanResource(CKAN_CATALOGUE_URL, resourceBean);
String jsonValueResource = MarshUnmarshCkanObject.toJsonValueResource(resource);
LOG.trace("Serialized resource is: " + jsonValueResource);
String jsonValueResource = MarshUnmarshCkanObject.toJsonValueResource(resource, METHOD.TO_CREATE);
LOG.debug("Serialized resource is: " + jsonValueResource);
jsonValueResource = gCatCaller.addResourceToDataset(resourceBean.getDatasetId(), jsonValueResource);
LOG.debug("Added resource to dataset is: " + jsonValueResource);
CkanResource createdRes = MarshUnmarshCkanObject.toCkanResource(jsonValueResource);
if(createdRes != null){
LOG.debug("Resource " + createdRes.getName() + " is now available");
if (createdRes != null) {
LOG.debug("Resource " + createdRes.getName() + " added correclty");
return createdRes.getId();
}
}else
} else
throw new Exception("It seems there is no is no resource at this url " + resourceBean.getUrl());
return null;
@ -1048,27 +1060,5 @@ public class DataCatalogueImpl implements DataCatalogue {
return toCreate;
}
/**
* Just check if the group exists
* @param nameOrId
* @param client
* @return
*/
private CkanGroup groupExists(String nameOrId){
CkanGroup toReturn = null;
try{
ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, CKAN_TOKEN_SYS);
toReturn = client.getGroup(nameOrId);
}catch(JackanException je){
LOG.error("The group "+nameOrId+" doesn't exist");
}
return toReturn;
}
}

View File

@ -114,6 +114,7 @@ public class CKANConveter {
}
dataset.setTags(ckanTags);
dataset.setNumTags(ckanTags.size());
}
// set the custom fields, if any

View File

@ -0,0 +1,61 @@
package org.gcube.datacatalogue.ckanutillibrary.server.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class GCubeUtils.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Feb 10, 2021
*/
public class GCubeUtils {
/**
* The Enum GCUBE_SCOPE_LEVEL.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Feb 10, 2021
*/
public static enum GCUBE_SCOPE_LEVEL {
ROOT, VO, VRE
}
private static final Logger LOG = LoggerFactory.getLogger(CatalogueUtilMethods.class);
/**
* To G cube level.
*
* @param scope the scope
* @return the gcube scope level
* @throws IllegalArgumentException the illegal argument exception
*/
public static GCUBE_SCOPE_LEVEL toGCubeLevel(String scope) throws IllegalArgumentException {
LOG.debug("called toGCubeLevel on " + scope);
if (scope == null || scope.isEmpty())
throw new IllegalArgumentException("Scope is null or empty");
if (!scope.startsWith("/")) {
throw new IllegalArgumentException("Scope should start with '/' ->" + scope);
}
if (scope.endsWith("/")) {
throw new IllegalArgumentException("Scope should not end with '/' ->" + scope);
}
String[] splits = scope.split("/");
if (splits.length > 4)
throw new IllegalArgumentException("Scope is invalid, too many '/' ->" + scope);
if (splits.length == 2) // is a root VO
return GCUBE_SCOPE_LEVEL.ROOT;
else if (splits.length == 3) {// is a VO
return GCUBE_SCOPE_LEVEL.VO;
} else if (splits.length == 4) {// is a VRE
return GCUBE_SCOPE_LEVEL.VRE;
}
return null;
}
}

View File

@ -238,6 +238,8 @@ public class TestDataCatalogueLib {
customFieldsMultiple.put("key-random-"+new Random().nextInt(10), values);
}
customFieldsMultiple.put("empty-key", Arrays.asList(""));
customFieldsMultiple.put("system:type", Arrays.asList("EmptyProfile"));
boolean setSearchable = true;
@ -248,8 +250,6 @@ public class TestDataCatalogueLib {
//System.out.println("Org name by VRE: "+orgName);
LOG.debug("Org name by VRE: "+orgName);
orgName = null;
int random = new Random().nextInt();
String datasetTitle = "a dataset created by catalogue-util-library "+random;
String datasetName = datasetTitle.replace(" ", "");
@ -272,7 +272,7 @@ public class TestDataCatalogueLib {
setSearchable,
true);
LOG.info(createdDataset);
LOG.info("Created the dataset with id: " +createdDataset);
}catch (Exception e) {
e.printStackTrace();
}
@ -293,7 +293,6 @@ public class TestDataCatalogueLib {
LOG.info("Created the group: "+ckanGroup);
}
@Test
public void testAddResource() throws Exception{
@ -301,9 +300,9 @@ public class TestDataCatalogueLib {
SecurityTokenProvider.instance.set(authorizationToken);
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
CkanDataset dataset = instance.getDataset("adatasetcreatedbycatalogue-util-library-1644679434", null);
CkanDataset dataset = instance.getDataset("adatasetcreatedbycatalogue-util-library-873805063", null);
for (int i = 0; i < 5; i++) {
for (int i = 0; i < 1; i++) {
ResourceBean resourceBean = new ResourceBean("https://google.com", "resouce "+i, "description "+i, null, testUser, dataset.getId(), null);
instance.addResourceToDataset(resourceBean);
}
@ -459,7 +458,7 @@ public class TestDataCatalogueLib {
// DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
//
// String username = "francescomangiacrapa";
// List<CkanOrganization> organizations = instance.getOrganizationsByUser(username);
// List<CkanO)rganization> organizations = instance.getOrganizationsByUser(username);
//
// System.out.println("organizations for user " + username + " are: ");
//