diff --git a/.classpath b/.classpath index 0be8ab5..ca9c31d 100644 --- a/.classpath +++ b/.classpath @@ -18,11 +18,11 @@ - + + - + - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 6e80039..443e085 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,8 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.8 +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml index f4bf050..4f92af5 100644 --- a/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,5 +1,5 @@ - + diff --git a/distro/changelog.xml b/distro/changelog.xml index 00529c0..67cc056 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,4 +1,8 @@ + + Some minor changes + Added method to delete/purge group diff --git a/pom.xml b/pom.xml index d51bcb0..662e9d2 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.data-catalogue ckan-util-library - 2.2.0-SNAPSHOT + 2.2.1-SNAPSHOT jar CKan utility library diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/ApplicationProfileScopePerUrlReader.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/ApplicationProfileScopePerUrlReader.java index ec42dfa..e0d621e 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/ApplicationProfileScopePerUrlReader.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/ApplicationProfileScopePerUrlReader.java @@ -11,7 +11,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.gcube.common.portal.PortalContext; import org.gcube.common.resources.gcore.utils.XPathHelper; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.datacatalogue.ckanutillibrary.server.exceptions.ApplicationProfileNotFoundException; +import org.gcube.datacatalogue.ckanutillibrary.shared.ex.ApplicationProfileNotFoundException; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.impl.QueryBox; @@ -24,14 +24,16 @@ import org.xml.sax.InputSource; * This class has a method that tries to read the application profile whose structure contains * couples Scope/Current_Url to check where the the needed information needs to be discovered according to the current * portlet url. It means that the scope in which the ckan related information will be discovered could be different wrt the running one. - * @author Costantino Perciante at ISTI-CNR - * (costantino.perciante@isti.cnr.it) + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ public class ApplicationProfileScopePerUrlReader { - //logger private static final Logger logger = LoggerFactory.getLogger(ApplicationProfileScopePerUrlReader.class); private final static String APPLICATION_PROFILE_NAME = "DataCatalogueMapScopesUrls"; + private final static String QUERY = "for $profile in collection('/db/Profiles/GenericResource')//Resource " + + "where $profile/Profile/SecondaryType/string() eq 'ApplicationProfile' and $profile/Profile/Name/string() " + + " eq '" + APPLICATION_PROFILE_NAME + "'" + + "return $profile"; /** * Get the scope in which discover for this url. If the Application Profile doesn't contain it, the current scope (taken @@ -41,17 +43,17 @@ public class ApplicationProfileScopePerUrlReader { */ public static String getScopePerUrl(String url){ - logger.debug("Request scope for ckan portlet at url " + url); + logger.info("Request scope for ckan portlet at url " + url); String scope = ScopeProvider.instance.get(); String scopeToReturn = scope; String rootScopeForInfrastructure = "/" + PortalContext.getConfiguration().getInfrastructureName(); - + if(url == null || url.isEmpty()){ - - logger.debug("The url passed is null or empty! Returning current scope [" + scope + "]"); + + logger.info("The url passed is null or empty! Returning current scope [" + scope + "]"); return scope; - + } // set this scope @@ -60,10 +62,7 @@ public class ApplicationProfileScopePerUrlReader { logger.debug("Trying to fetch applicationProfile profile from the infrastructure for " + APPLICATION_PROFILE_NAME + " scope: " + rootScopeForInfrastructure); try { - Query q = new QueryBox("for $profile in collection('/db/Profiles/GenericResource')//Resource " + - "where $profile/Profile/SecondaryType/string() eq 'ApplicationProfile' and $profile/Profile/Name/string() " + - " eq '" + APPLICATION_PROFILE_NAME + "'" + - "return $profile"); + Query q = new QueryBox(QUERY); DiscoveryClient client = client(); List appProfile = client.submit(q); @@ -94,12 +93,10 @@ public class ApplicationProfileScopePerUrlReader { else throw new ApplicationProfileNotFoundException("Your applicationProfile EndPoint was not found in the profile, consider adding element in "); - } } catch (Exception e) { logger.error("Error while trying to fetch applicationProfile profile from the infrastructure", e); }finally{ - // set back the scope ScopeProvider.instance.set(scope); } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java index 5e46282..f36db41 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogue.java @@ -5,10 +5,10 @@ import java.sql.SQLException; import java.util.List; import java.util.Map; -import org.gcube.datacatalogue.ckanutillibrary.server.models.CKanUserWrapper; -import org.gcube.datacatalogue.ckanutillibrary.server.models.CkanDatasetRelationship; -import org.gcube.datacatalogue.ckanutillibrary.server.models.DatasetRelationships; -import org.gcube.datacatalogue.ckanutillibrary.server.models.ResourceBean; +import org.gcube.datacatalogue.ckanutillibrary.shared.CKanUserWrapper; +import org.gcube.datacatalogue.ckanutillibrary.shared.CkanDatasetRelationship; +import org.gcube.datacatalogue.ckanutillibrary.shared.DatasetRelationships; +import org.gcube.datacatalogue.ckanutillibrary.shared.ResourceBean; import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg; import org.json.simple.JSONObject; @@ -19,7 +19,7 @@ import eu.trentorise.opendata.jackan.model.CkanOrganization; import eu.trentorise.opendata.jackan.model.CkanResource; /** - * This is the data-catalogue-library interface that shows the utility methods. + * This is the data-catalogue-library interface. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ public interface DataCatalogue { @@ -103,7 +103,7 @@ public interface DataCatalogue { * @return */ List getOrganizationsNames(); - + /** * Retrieve the organization with this name */ @@ -150,10 +150,11 @@ public interface DataCatalogue { * @param resources * @param setPublic (manage visibility: Admin role is needed) * @return the id of the dataset on success, null otherwise + * @throws Exception */ String createCKanDataset(String apiKey, String title, String name, String organizationNameOrId, String author, String authorMail, String maintainer, String maintainerMail, long version, String description, String licenseId, - List tags, Map customFields, List resources, boolean setPublic); + List tags, Map customFields, List resources, boolean setPublic) throws Exception; /** * Create a dataset with those information. The method allows to have multiple values for the same custom field key. @@ -175,10 +176,11 @@ public interface DataCatalogue { * @param resources * @param setPublic (manage visibility: Admin role is needed) * @return the id of the dataset on success, null otherwise + * @throws Exception */ String createCKanDatasetMultipleCustomFields(String apiKey, String title, String name, String organizationNameOrId, String author, String authorMail, String maintainer, String maintainerMail, long version, String description, String licenseId, - List tags, Map> customFields, List resources, boolean setPublic); + List tags, Map> customFields, List resources, boolean setPublic) throws Exception; /** * Given the id or the name of the dataset it returns its current url by contacting the uri resolver. @@ -188,7 +190,7 @@ public interface DataCatalogue { * @return The url of the dataset on success, null otherwise */ String getUrlFromDatasetIdOrName(String datasetIdOrName); - + /** * Given the id or the name of the dataset it returns its current url by contacting the uri resolver. * If no uri resolver is available, an url that is not guaranteed to be long term valid will be generated. @@ -515,7 +517,7 @@ public interface DataCatalogue { * @return */ boolean deleteGroup(String groupName, boolean purge); - + /** * The method returns the role the user has in the groups he/she belongs to (it uses the db, so it is much faster) * @param username @@ -523,7 +525,7 @@ public interface DataCatalogue { * @return */ Map>getUserRoleByGroup(String username, String apiKey); - + /** * The method returns the role the user has in the organizations he/she belongs to (it uses the db, so it is much faster) * @param username diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueFactory.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueFactory.java index aaccb9c..536033e 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueFactory.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueFactory.java @@ -10,38 +10,32 @@ import org.slf4j.LoggerFactory; * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ public class DataCatalogueFactory { - + private static final Logger logger = LoggerFactory.getLogger(DataCatalogueFactory.class); private static final long MAX_LIFETIME = 1000 * 60 * 30; // 30 MINUTES private static DataCatalogueFactory instance = new DataCatalogueFactory(); private static ConcurrentHashMap cache; - - /** - * Private constructor for the hashmap's object values - * @author Costantino Perciante at ISTI-CNR - * (costantino.perciante@isti.cnr.it) - * - */ + private class CacheBean{ DataCatalogueImpl utils; long ttl; - + public CacheBean(long ttl, DataCatalogueImpl utils){ this.ttl = ttl; this.utils = utils; } } - + /** * Private constructor */ private DataCatalogueFactory(){ - + logger.debug("Ckan factory object build"); cache = new ConcurrentHashMap(); - + } - + /** * Get the factory instance * @return @@ -50,7 +44,7 @@ public class DataCatalogueFactory { logger.debug("Factory requested"); return instance; } - + /** * Retrieve the ckan utils information for the given scope * @param scope @@ -58,9 +52,10 @@ public class DataCatalogueFactory { * @throws Exception */ public DataCatalogueImpl getUtilsPerScope(String scope) throws Exception{ + if(scope == null || scope.isEmpty()) throw new IllegalArgumentException("Invalid scope given!"); - + if(cache.containsKey(scope) && !expired(cache.get(scope))){ return cache.get(scope).utils; } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java index 45d0f81..4ccf5ee 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java @@ -3,9 +3,7 @@ package org.gcube.datacatalogue.ckanutillibrary.server; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; -import java.io.BufferedReader; import java.io.File; -import java.io.InputStreamReader; import java.math.BigDecimal; import java.net.URLEncoder; import java.nio.charset.Charset; @@ -30,14 +28,14 @@ import net.htmlparser.jericho.Renderer; import net.htmlparser.jericho.Segment; import net.htmlparser.jericho.Source; -import org.gcube.datacatalogue.ckanutillibrary.server.models.CKanUserWrapper; -import org.gcube.datacatalogue.ckanutillibrary.server.models.CkanDatasetRelationship; -import org.gcube.datacatalogue.ckanutillibrary.server.models.DatasetRelationships; -import org.gcube.datacatalogue.ckanutillibrary.server.models.ResourceBean; -import org.gcube.datacatalogue.ckanutillibrary.server.models.State; import org.gcube.datacatalogue.ckanutillibrary.server.utils.UtilMethods; import org.gcube.datacatalogue.ckanutillibrary.server.utils.url.EntityContext; +import org.gcube.datacatalogue.ckanutillibrary.shared.CKanUserWrapper; +import org.gcube.datacatalogue.ckanutillibrary.shared.CkanDatasetRelationship; +import org.gcube.datacatalogue.ckanutillibrary.shared.DatasetRelationships; +import org.gcube.datacatalogue.ckanutillibrary.shared.ResourceBean; import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg; +import org.gcube.datacatalogue.ckanutillibrary.shared.State; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; @@ -72,6 +70,7 @@ import eu.trentorise.opendata.jackan.model.CkanUser; * This is the Ckan Utils implementation class. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ +@SuppressWarnings("unchecked") public class DataCatalogueImpl implements DataCatalogue{ private static final Logger logger = LoggerFactory.getLogger(DataCatalogueImpl.class); @@ -88,6 +87,9 @@ public class DataCatalogueImpl implements DataCatalogue{ private boolean MANAGE_PRODUCT_BUTTON; private String CONTEXT; + private final static String PATH_SET_PRIVATE_DATASET = "/api/3/action/bulk_update_private"; + private final static String PATH_SET_PUBLIC_DATASET = "/api/3/action/bulk_update_public"; + // ckan client private CkanClient client; @@ -95,7 +97,7 @@ public class DataCatalogueImpl implements DataCatalogue{ private ConcurrentHashMap apiKeysMap; // apikey bean expires after X minutes in the above map - private static final int EXPIRE_KEY_TIME = 5 * 60 * 1000; + private static final int EXPIRE_KEY_TIME = 60 * 60 * 1000; private class CKANTokenBean{ private String apiKey; @@ -158,6 +160,15 @@ public class DataCatalogueImpl implements DataCatalogue{ return URI_RESOLVER_URL; } + /** + * Check if the manage product is enabled + * @return + */ + @Override + public boolean isManageProductEnabled() { + return MANAGE_PRODUCT_BUTTON; + } + /** * Retrieve connection from the pool * @return a connection available within the pool @@ -175,15 +186,6 @@ public class DataCatalogueImpl implements DataCatalogue{ } - /** - * Check if the manage product is enabled - * @return - */ - @Override - public boolean isManageProductEnabled() { - return MANAGE_PRODUCT_BUTTON; - } - /** * Tries to close a connection * @param connection @@ -250,14 +252,16 @@ public class DataCatalogueImpl implements DataCatalogue{ }catch(Exception e){ logger.error("Unable to retrieve key for user " + ckanUsername, e); - return null; }finally{ closeConnection(connection); } + + return null; } @Override public CKanUserWrapper getUserFromApiKey(String apiKey) { + logger.debug("Request user whose api key is = " + apiKey.substring(0, 3) + "*************"); // checks @@ -550,7 +554,7 @@ public class DataCatalogueImpl implements DataCatalogue{ } /** - * Set dataset private + * Set dataset private/public * @param priv * @param organizationId (NOTE: The ID, not the name!) * @param datasetId (NOTE: The ID, not the name!) @@ -568,52 +572,38 @@ public class DataCatalogueImpl implements DataCatalogue{ checkArgument(!datasetId.isEmpty()); checkArgument(!organizationId.isEmpty()); - String pathSetPrivate = "/api/3/action/bulk_update_private"; - String pathSetPublic = "/api/3/action/bulk_update_public"; + JSONObject obj = new JSONObject(); + obj.put("org_id", organizationId); - // Request parameters to be replaced - String parameter = "{" - + "\"org_id\":\"ORGANIZATION_ID\"," - + "\"datasets\":[\"DATASET_ID\"]" - + "}"; + JSONArray array = new JSONArray(); + array.add(datasetId); + obj.put("datasets", array); - // replace with right data - parameter = parameter.replace("ORGANIZATION_ID", organizationId); - parameter = parameter.replace("DATASET_ID", datasetId); + try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) { + HttpPost request; - if(priv){ - try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) { - HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + pathSetPrivate); - request.addHeader("Authorization", apiKey); - StringEntity params = new StringEntity(parameter); - request.setEntity(params); - HttpResponse response = httpClient.execute(request); - logger.debug("[PRIVATE]Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); + if(priv) + request = new HttpPost(CKAN_CATALOGUE_URL + PATH_SET_PRIVATE_DATASET); + else + request = new HttpPost(CKAN_CATALOGUE_URL + PATH_SET_PUBLIC_DATASET); - if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) - return true; + logger.info("Excuting request for making dataset with id " + datasetId + " " + (priv? "private" : "public")); - }catch (Exception ex) { - logger.error("Error while trying to set private the dataset ", ex); - } - }else - { - try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) { - HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + pathSetPublic); - StringEntity params = new StringEntity(parameter); - request.addHeader("Authorization", apiKey); - request.setEntity(params); - HttpResponse response = httpClient.execute(request); - logger.debug("[PUBLIC]Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); + request.addHeader("Authorization", apiKey); + StringEntity params = new StringEntity(obj.toJSONString()); + request.setEntity(params); + HttpResponse response = httpClient.execute(request); - if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) - return true; + logger.info("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); - }catch (Exception ex) { - logger.error("Error while trying to set public the dataset ", ex); - } + if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) + return true; + + }catch (Exception ex) { + logger.error("Error while trying to set private the dataset ", ex); } + return false; } @@ -695,7 +685,7 @@ public class DataCatalogueImpl implements DataCatalogue{ String authorMail, String maintainer, String maintainerMail, long version, String description, String licenseId, List tags, Map customFields, - List resources, boolean setPublic) { + List resources, boolean setPublic) throws Exception{ // delegate the private method return createCkanDatasetBody(apiKey, @@ -712,7 +702,7 @@ public class DataCatalogueImpl implements DataCatalogue{ String authorMail, String maintainer, String maintainerMail, long version, String description, String licenseId, List tags, Map> customFieldsMultiple, - List resources, boolean setPublic) { + List resources, boolean setPublic) throws Exception{ // delegate the private method return createCkanDatasetBody(apiKey, @@ -730,7 +720,7 @@ public class DataCatalogueImpl implements DataCatalogue{ long version, String description, String licenseId, List tags, Map customFields, Map> customFieldsMultipleValues, - List resources, boolean setPublic){ + List resources, boolean setPublic) throws Exception{ // checks (minimum) checkNotNull(apiKey); @@ -743,18 +733,15 @@ public class DataCatalogueImpl implements DataCatalogue{ CheckedCkanClient client = new CheckedCkanClient(CKAN_CATALOGUE_URL, apiKey); - // get client from apiKey String ckanUsername = getUserFromApiKey(apiKey).getName(); - - // create the base dataset and fill it CkanDataset dataset = new CkanDataset(); - // get the name from the title String nameToUse = name; if(nameToUse == null) nameToUse = UtilMethods.fromProductTitleToName(title); logger.debug("Name of the dataset is going to be " + nameToUse + ". Title is going to be " + title); + dataset.setName(nameToUse); dataset.setTitle(title); @@ -773,67 +760,54 @@ public class DataCatalogueImpl implements DataCatalogue{ Renderer htmlRend = new Renderer(htmlSeg); dataset.setNotes(htmlRend.toString()); - logger.debug("Description (escaped is ) " + htmlRend.toString()); + logger.debug("Description escaped " + htmlRend.toString()); } dataset.setLicenseId(licenseId); // set the tags, if any if(tags != null && !tags.isEmpty()){ - - // convert to ckan tags List ckanTags = new ArrayList(tags.size()); for (String stringTag : tags) { ckanTags.add(new CkanTag(stringTag)); } - dataset.setTags(ckanTags); } // set the custom fields, if any + List extras = new ArrayList(); + if(customFields != null && !customFields.isEmpty()){ - // iterate and create Iterator> iterator = customFields.entrySet().iterator(); - List extras = new ArrayList(); - while (iterator.hasNext()) { - Map.Entry entry = (Map.Entry) iterator.next(); extras.add(new CkanPair(entry.getKey(), entry.getValue())); - } - dataset.setExtras(extras); }else if(customFieldsMultipleValues != null && !customFieldsMultipleValues.isEmpty()){ - // iterate and create Iterator>> iterator = customFieldsMultipleValues.entrySet().iterator(); - - List extras = new ArrayList(); - while (iterator.hasNext()) { - Map.Entry> entry = (Map.Entry>) iterator.next(); List valuesForEntry = entry.getValue(); for (String value : valuesForEntry) { extras.add(new CkanPair(entry.getKey(), value)); } } - - // sort them - Collections.sort(extras, new Comparator() { - @Override public int compare(CkanPair b1, CkanPair b2) { - return b1.getKey().compareTo(b2.getKey()); - } - - }); - - dataset.setExtras(extras); - } + // sort them + Collections.sort(extras, new Comparator() { + @Override public int compare(CkanPair b1, CkanPair b2) { + return b1.getKey().compareTo(b2.getKey()); + } + + }); + + dataset.setExtras(extras); + // check if we need to add the resources if(resources != null && !resources.isEmpty()){ @@ -842,8 +816,6 @@ public class DataCatalogueImpl implements DataCatalogue{ try{ List resourcesCkan = new ArrayList(); - - for(ResourceBean resource: resources){ CkanResource newResource = new CkanResource(); @@ -861,6 +833,7 @@ public class DataCatalogueImpl implements DataCatalogue{ }catch(Exception e){ logger.error("Unable to add those resources to the dataset", e); + throw e; } } @@ -868,11 +841,9 @@ public class DataCatalogueImpl implements DataCatalogue{ // try to create CkanDataset res = null; try{ - res = client.createDataset(dataset); if(res != null){ - logger.debug("Dataset with name " + res.getName() + " has been created. Setting visibility"); // set visibility @@ -889,19 +860,15 @@ public class DataCatalogueImpl implements DataCatalogue{ boolean searchableSet = setSearchableField(res.getId(), true); logger.info("Was searchable set to True? " + searchableSet); } - return res.getId(); } }catch(Exception e){ - - // try to update logger.error("Error while creating the dataset.", e); - + throw e; } return null; - } @Override @@ -972,7 +939,6 @@ public class DataCatalogueImpl implements DataCatalogue{ * @param entityName * @return the url for the product */ - @SuppressWarnings("unchecked") private String getUrlForProduct(String context, EntityContext entityContext, String entityName, boolean unencrypted){ String toReturn = null; @@ -1034,24 +1000,17 @@ public class DataCatalogueImpl implements DataCatalogue{ // we need to use the apis to make it String path = "/api/3/action/organization_member_create"; - // Request parameters to be replaced - String parameter = "{" - + "\"id\":\"ORGANIZATION_ID_NAME\"," - + "\"username\":\"USERNAME_ID_NAME\"," - + "\"role\":\"ROLE\"" - + "}"; + JSONObject obj = new JSONObject(); + obj.put("id", organizationNameToCheck); + obj.put("username", ckanUsername); + obj.put("role", RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck)); - // replace those values - parameter = parameter.replace("ORGANIZATION_ID_NAME", organizationNameToCheck); - parameter = parameter.replace("USERNAME_ID_NAME", ckanUsername); - parameter = parameter.replace("ROLE", RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck)); - - logger.debug("API request for organization membership is going to be " + parameter); + logger.debug("API request for organization membership is going to be " + obj.toJSONString()); try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) { HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path); request.addHeader("Authorization", CKAN_TOKEN_SYS); // sys token - StringEntity params = new StringEntity(parameter); + StringEntity params = new StringEntity(obj.toJSONString()); request.setEntity(params); HttpResponse response = httpClient.execute(request); logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); @@ -1124,24 +1083,17 @@ public class DataCatalogueImpl implements DataCatalogue{ // we need to use the apis to make it String path = "/api/3/action/group_member_create"; - // Request parameters to be replaced - String parameter = "{" - + "\"id\":\"ORGANIZATION_ID_NAME\"," - + "\"username\":\"USERNAME_ID_NAME\"," - + "\"role\":\"ROLE\"" - + "}"; + JSONObject obj = new JSONObject(); + obj.put("id", groupNameToCheck); + obj.put("username", ckanUsername); + obj.put("role", RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck)); - // replace those values - parameter = parameter.replace("ORGANIZATION_ID_NAME", groupNameToCheck); - parameter = parameter.replace("USERNAME_ID_NAME", ckanUsername); - parameter = parameter.replace("ROLE", RolesCkanGroupOrOrg.convertToCkanCapacity(correspondentRoleToCheck)); - - logger.debug("API request for organization membership is going to be " + parameter); + logger.debug("API request for organization membership is going to be " + obj.toJSONString()); try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) { HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path); request.addHeader("Authorization", CKAN_TOKEN_SYS); // sys token - StringEntity params = new StringEntity(parameter); + StringEntity params = new StringEntity(obj.toJSONString()); request.setEntity(params); HttpResponse response = httpClient.execute(request); logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); @@ -1195,26 +1147,18 @@ public class DataCatalogueImpl implements DataCatalogue{ // we need to use the apis to make it String path = "/api/3/action/package_relationship_create"; - // Request parameters to be replaced - String parameter = "{" - + "\"subject\":\"SUBJECT\"," - + "\"object\":\"OBJECT\"," - + "\"type\":\"RELATIONSHIP\"," - + "\"comment\" : \"COMMENT\"" - + "}"; - - // replace those values - parameter = parameter.replace("SUBJECT", datasetIdSubject); - parameter = parameter.replace("OBJECT", datasetIdObject); - parameter = parameter.replace("RELATIONSHIP", relation.toString()); + JSONObject obj = new JSONObject(); + obj.put("subject", datasetIdSubject); + obj.put("object", datasetIdObject); + obj.put("type", relation.toString()); if(relationComment != null && !relationComment.isEmpty()) - parameter = parameter.replace("COMMENT", relationComment); + obj.put("comment", relationComment); - logger.debug("API request for relationship create is going to be " + parameter); + logger.debug("API request for relationship create is going to be " + obj.toJSONString()); HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path); request.addHeader("Authorization", apiKey); - StringEntity params = new StringEntity(parameter); + StringEntity params = new StringEntity(obj.toJSONString()); request.setEntity(params); HttpResponse response = httpClient.execute(request); logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); @@ -1246,23 +1190,16 @@ public class DataCatalogueImpl implements DataCatalogue{ // we need to use the apis to make it String path = "/api/3/action/package_relationship_delete"; - // Request parameters to be replaced - String parameter = "{" - + "\"subject\":\"SUBJECT\"," - + "\"object\":\"OBJECT\"," - + "\"type\":\"RELATIONSHIP\"" - + "}"; + JSONObject obj = new JSONObject(); + obj.put("subject", datasetIdSubject); + obj.put("object", datasetIdObject); + obj.put("type", relation.toString()); - // replace those values - parameter = parameter.replace("SUBJECT", datasetIdSubject); - parameter = parameter.replace("OBJECT", datasetIdObject); - parameter = parameter.replace("RELATIONSHIP", relation.toString()); - - logger.debug("API request for delete relationship is going to be " + parameter); + logger.debug("API request for delete relationship is going to be " + obj.toJSONString()); HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path); request.addHeader("Authorization", apiKey); - StringEntity params = new StringEntity(parameter); + StringEntity params = new StringEntity(obj.toJSONString()); request.setEntity(params); HttpResponse response = httpClient.execute(request); logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); @@ -1275,7 +1212,6 @@ public class DataCatalogueImpl implements DataCatalogue{ return false; } - @SuppressWarnings("unchecked") @Override public List getRelationshipDatasets( String datasetIdSubject, String datasetIdObject, String apiKey) { @@ -1293,47 +1229,25 @@ public class DataCatalogueImpl implements DataCatalogue{ // we need to use the apis to make it String path = "/api/3/action/package_relationships_list"; - String parameter; + JSONObject obj = new JSONObject(); + obj.put("id", datasetIdSubject); // Request parameters to be replaced - if(datasetIdObject == null || datasetIdObject.isEmpty()) - parameter = "{" - + "\"id\":\"SUBJECT\"" - + "}"; - else - parameter = "{" - + "\"id\":\"SUBJECT\"," - + "\"id2\":\"OBJECT\"" - + "}"; - - // replace those values - parameter = parameter.replace("SUBJECT", datasetIdSubject); if(datasetIdObject != null && !datasetIdObject.isEmpty()) - parameter = parameter.replace("OBJECT", datasetIdObject); + obj.put("id2", datasetIdObject); - logger.debug("API request for getting relationship is going to be " + parameter); + logger.debug("API request for getting relationship is going to be " + obj.toJSONString()); HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path); request.addHeader("Authorization", apiKey); - StringEntity params = new StringEntity(parameter); + StringEntity params = new StringEntity(obj.toJSONString()); request.setEntity(params); HttpResponse response = httpClient.execute(request); logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ - // parse the json and convert to java beans - BufferedReader br = new BufferedReader( - new InputStreamReader((response.getEntity().getContent()))); - - String output; - String res = ""; - while ((output = br.readLine()) != null) { - res += output; - } - - if(res == "") - return toReturn; + String res = EntityUtils.toString(response.getEntity()); // parse the json object returned JSONParser parser = new JSONParser(); @@ -1432,7 +1346,6 @@ public class DataCatalogueImpl implements DataCatalogue{ return toReturn; } - @SuppressWarnings("unchecked") @Override public boolean assignDatasetToGroup(String groupNameOrId, String datasetNameOrId, String apiKey) { @@ -1491,26 +1404,23 @@ public class DataCatalogueImpl implements DataCatalogue{ // now we patch the dataset with the new group String pathUpdatePatch = CKAN_CATALOGUE_URL + "/api/3/action/package_patch"; - String parameterPostPatch = "{\"id\":\"PACKAGE_ID\", \"groups\":[GROUPS]}"; - parameterPostPatch = parameterPostPatch.replace("PACKAGE_ID", datasetNameOrId); - String singleGroup = "{\"name\":\"GROUP_ID\"}"; - - // evaluate parameterPostPatch - String replaceGROUPS = ""; + + JSONObject req = new JSONObject(); + req.put("id", datasetNameOrId); + + JSONArray groups = new JSONArray(); for (int i = 0; i < fetchedGroups.size(); i++) { - replaceGROUPS += singleGroup.replace("GROUP_ID", fetchedGroups.get(i)); - if(i != fetchedGroups.size() - 1) - replaceGROUPS += ","; + JSONObject groupJSON = new JSONObject(); + groupJSON.put("name", fetchedGroups.get(i)); + groups.add(groupJSON); } + req.put("groups", groups); - // replace this into parameterPostPatch - parameterPostPatch = parameterPostPatch.replace("GROUPS", replaceGROUPS); - - logger.debug("Request for patch is going to be " + parameterPostPatch); + logger.debug("Request for patch is going to be " + req.toJSONString()); HttpPost request = new HttpPost(pathUpdatePatch); request.addHeader("Authorization", CKAN_TOKEN_SYS); - StringEntity params = new StringEntity(parameterPostPatch); + StringEntity params = new StringEntity(req.toJSONString()); request.setEntity(params); HttpResponse responsePatch = httpClient.execute(request); logger.debug("Response code is " + responsePatch.getStatusLine().getStatusCode() + " and response message is " + responsePatch.getStatusLine().getReasonPhrase()); @@ -1529,7 +1439,7 @@ public class DataCatalogueImpl implements DataCatalogue{ return false; } - @SuppressWarnings("unchecked") + @Override public boolean removeDatasetFromGroup(String groupNameOrId, String datasetNameOrId, String apiKey) { @@ -1714,6 +1624,7 @@ public class DataCatalogueImpl implements DataCatalogue{ return toReturn; } + @Override public boolean deleteProduct(String datasetId, String apiKey, boolean purge) { @@ -1738,8 +1649,9 @@ public class DataCatalogueImpl implements DataCatalogue{ String path = CKAN_CATALOGUE_URL + "/api/3/action/dataset_purge"; HttpPost request = new HttpPost(path); request.addHeader("Authorization", CKAN_TOKEN_SYS); // this must be a sys_admin key - String entityBody = "{\"id\": \"" + datasetId + "\"}"; - StringEntity params = new StringEntity(entityBody); + JSONObject object = new JSONObject(); + object.put("id", datasetId); + StringEntity params = new StringEntity(object.toJSONString()); request.setEntity(params); HttpResponse response = httpClient.execute(request); @@ -1795,20 +1707,14 @@ public class DataCatalogueImpl implements DataCatalogue{ // Patch package path String patchPackage = CKAN_CATALOGUE_URL + "/api/3/action/package_patch"; - // Request parameters to be replaced - String parameter = "{" - + "\"id\":\"DATASET_ID\"," - + "\"searchable\":\"SEARCHABLE\"" - + "}"; - - // replace with right data - parameter = parameter.replace("DATASET_ID", datasetId); - parameter = parameter.replace("SEARCHABLE", searchableAsString); - + JSONObject obj = new JSONObject(); + obj.put("id", datasetId); + obj.put("searchable", searchableAsString); + try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) { HttpPost request = new HttpPost(patchPackage); request.addHeader("Authorization", CKAN_TOKEN_SYS); - StringEntity params = new StringEntity(parameter); + StringEntity params = new StringEntity(obj.toJSONString()); request.setEntity(params); HttpResponse response = httpClient.execute(request); logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); @@ -1888,7 +1794,7 @@ public class DataCatalogueImpl implements DataCatalogue{ } } - @SuppressWarnings("unchecked") + @Override public boolean patchResource(String resourceId, String url, String name, String description, String urlType, String apiKey) { @@ -1940,7 +1846,6 @@ public class DataCatalogueImpl implements DataCatalogue{ return false; } - @SuppressWarnings("unchecked") @Override public boolean patchProductCustomFields(String productId, String apiKey, Map> customFieldsToChange) { @@ -2052,7 +1957,7 @@ public class DataCatalogueImpl implements DataCatalogue{ return false; } - @SuppressWarnings("unchecked") + @Override public boolean removeCustomField(String productId, String key, String value, String apiKey) { @@ -2127,7 +2032,6 @@ public class DataCatalogueImpl implements DataCatalogue{ } - @SuppressWarnings("unchecked") @Override public boolean removeTag(String productId, String apiKey, String tagToRemove) { @@ -2255,7 +2159,7 @@ public class DataCatalogueImpl implements DataCatalogue{ return null; } - @SuppressWarnings("unchecked") + @Override public boolean setGroupParent(String parentName, String groupName) { diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueRunningCluster.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueRunningCluster.java index ea7283d..5c48471 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueRunningCluster.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueRunningCluster.java @@ -18,10 +18,10 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.Property; import org.gcube.common.resources.gcore.utils.XPathHelper; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.datacatalogue.ckanutillibrary.server.exceptions.ApplicationProfileNotFoundException; -import org.gcube.datacatalogue.ckanutillibrary.server.exceptions.NoApplicationProfileMasterException; -import org.gcube.datacatalogue.ckanutillibrary.server.exceptions.NoDataCatalogueRuntimeResourceException; -import org.gcube.datacatalogue.ckanutillibrary.server.exceptions.ServiceEndPointException; +import org.gcube.datacatalogue.ckanutillibrary.shared.ex.ApplicationProfileNotFoundException; +import org.gcube.datacatalogue.ckanutillibrary.shared.ex.NoApplicationProfileMasterException; +import org.gcube.datacatalogue.ckanutillibrary.shared.ex.NoDataCatalogueRuntimeResourceException; +import org.gcube.datacatalogue.ckanutillibrary.shared.ex.ServiceEndPointException; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; @@ -49,7 +49,7 @@ public class DataCatalogueRunningCluster { private final static String RUNTIME_CATALOGUE_RESOURCE_NAME = "CKanDataCatalogue"; private final static String PLATFORM_CATALOGUE_NAME = "Tomcat"; - // api key property + // api key property for SYSADMIN private final static String API_KEY_PROPERTY = "API_KEY"; // property to retrieve the master service endpoint into the /root scope @@ -58,7 +58,7 @@ public class DataCatalogueRunningCluster { // url of the http uri for this scope private final static String URL_RESOLVER = "URL_RESOLVER"; - + // retrieved data private List datacatalogueUrls = new ArrayList(); private List hostsDB = new ArrayList(); @@ -87,198 +87,223 @@ public class DataCatalogueRunningCluster { // set the scope ScopeProvider.instance.set(scope); + logger.debug("Retrieving database information."); + List resources = getConfigurationFromISFORDB(); - - if (resources.size() == 0){ - throw new NoDataCatalogueRuntimeResourceException("There is no Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" and Platform " + PLATFORM_DB_NAME + " in this scope."); - } - else { - try{ - - if(resources.size() > 1){ - boolean oneWasMaster = false; - - logger.info("Too many Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" in this scope.. Looking for the one that has the property " + IS_MASTER_ROOT_KEY_PROPERTY); - - for (ServiceEndpoint res : resources) { - - Iterator accessPointIterator = res.profile().accessPoints().iterator(); - - while (accessPointIterator.hasNext()) { - ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator - .next(); - - // get the is master property - Property entry = accessPoint.propertyMap().get(IS_MASTER_ROOT_KEY_PROPERTY); - String isMaster = entry != null ? entry.value() : null; - - if(isMaster == null || !isMaster.equals("true")) - continue; - - // set this variable - oneWasMaster = true; - - // add this host - hostsDB.add(accessPoint.address().split(":")[0]); - - // save the port - int port = Integer.parseInt(accessPoint.address().split(":")[1]); - portsDB.add(port); - - // save the name of the cluster (this should be unique) - nameDB = accessPoint.name(); - - // save user and password - passwordDB = StringEncrypter.getEncrypter().decrypt(accessPoint.password()); - userDB = accessPoint.username(); - - // now break - break; - } - } - - // if none of them was master, throw an exception - if(!oneWasMaster) - throw new NoApplicationProfileMasterException("There is no application profile with MASTER property"); - }else{ - logger.debug(resources.toString()); - for (ServiceEndpoint res : resources) { - - Iterator accessPointIterator = res.profile().accessPoints().iterator(); - - while (accessPointIterator.hasNext()) { - ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator - .next(); - - // add this host - hostsDB.add(accessPoint.address().split(":")[0]); - - // save the port - int port = Integer.parseInt(accessPoint.address().split(":")[1]); - portsDB.add(port); - - // save the name of the cluster (this should be unique) - nameDB = accessPoint.name(); - - // save user and password - passwordDB = StringEncrypter.getEncrypter().decrypt(accessPoint.password()); - userDB = accessPoint.username(); - - } - } - } - }catch(Exception e ){ - throw new ServiceEndPointException(e.toString()); - } - } + evaluateRightConfigurationDB(resources); logger.debug("Retrieving ckan data catalogue service end point information and sysadmin token."); + resources = getConfigurationFromISFORCatalogueUrl(); - - if (resources.size() == 0){ - logger.error("There is no Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" and Platform " + PLATFORM_CATALOGUE_NAME + " in this scope."); - throw new NoDataCatalogueRuntimeResourceException(); - } - else { - logger.debug(resources.toString()); - try{ - if(resources.size() > 1){ - boolean oneWasMaster = false; - - logger.info("Too many Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" in this scope.. Looking for the one that has the property " + IS_MASTER_ROOT_KEY_PROPERTY); - - for (ServiceEndpoint res : resources) { - - Iterator accessPointIterator = res.profile().accessPoints().iterator(); - - while (accessPointIterator.hasNext()) { - ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator - .next(); - - // get the is master property - Property entry = accessPoint.propertyMap().get(IS_MASTER_ROOT_KEY_PROPERTY); - String isMaster = entry != null ? entry.value() : null; - - if(isMaster == null || !isMaster.equals("true")) - continue; - - // set this variable - oneWasMaster = true; - - // add this host - datacatalogueUrls.add(accessPoint.address()); - - // retrieve sys admin token - sysAdminToken = accessPoint.propertyMap().get(API_KEY_PROPERTY).value(); - sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken); - - // retrieve URL_RESOLVER - if(accessPoint.propertyMap().containsKey(URL_RESOLVER)) - urlResolver = accessPoint.propertyMap().get(URL_RESOLVER).value(); - - // break now - break; - } - } - - // if none of them was master, throw an exception - if(!oneWasMaster) - throw new NoApplicationProfileMasterException("There is no application profile with MASTER property"); - - }else{ - for (ServiceEndpoint res : resources) { - - Iterator accessPointIterator = res.profile().accessPoints().iterator(); - - while (accessPointIterator.hasNext()) { - ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator - .next(); - - // add this host - datacatalogueUrls.add(accessPoint.address()); - - // retrieve sys admin token - sysAdminToken = accessPoint.propertyMap().get(API_KEY_PROPERTY).value(); - sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken); - - // get the is manage product property - Property entry = accessPoint.propertyMap().get(IS_MANAGE_PRODUCT_ENABLED); - String isManageProduct = entry != null ? entry.value() : null; - - if(isManageProduct != null && isManageProduct.equals("true")){ - logger.info("Manage product is enabled in this scope"); - manageProductEnabled = true; - } - - // retrieve URL_RESOLVER - if(accessPoint.propertyMap().containsKey(URL_RESOLVER)) - urlResolver = accessPoint.propertyMap().get(URL_RESOLVER).value(); - } - } - } - }catch(Exception e){ - throw new ServiceEndPointException("There is no service end point for such information"); - } - } - + evaluateRightConfigurationCatalogue(resources); // finally get the url in which the ckan portlet is deployed + logger.debug("Looking for portlet url in " + ScopeProvider.instance.get() + " scope" ); + portletUrl = getPortletUrlFromInfrastrucure(); }catch(Exception e) { logger.warn("The following error occurred: " + e.toString()); + throw e; }finally{ ScopeProvider.instance.set(currentScope); } } + /** + * Evaluate the right configuration about ckan + * @param resources + * @throws NoDataCatalogueRuntimeResourceException + * @throws ServiceEndPointException + */ + private void evaluateRightConfigurationCatalogue( + List resources) throws NoDataCatalogueRuntimeResourceException, ServiceEndPointException { + if (resources.size() == 0){ + logger.error("There is no Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" and Platform " + PLATFORM_CATALOGUE_NAME + " in this scope."); + throw new NoDataCatalogueRuntimeResourceException(); + } + else { + logger.debug(resources.toString()); + try{ + if(resources.size() > 1){ + boolean oneWasMaster = false; + + logger.info("Too many Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" in this scope.. Looking for the one that has the property " + IS_MASTER_ROOT_KEY_PROPERTY); + + for (ServiceEndpoint res : resources) { + + Iterator accessPointIterator = res.profile().accessPoints().iterator(); + + while (accessPointIterator.hasNext()) { + ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator + .next(); + + // get the is master property + Property entry = accessPoint.propertyMap().get(IS_MASTER_ROOT_KEY_PROPERTY); + String isMaster = entry != null ? entry.value() : null; + + if(isMaster == null || !isMaster.equals("true")) + continue; + + // set this variable + oneWasMaster = true; + + // add this host + datacatalogueUrls.add(accessPoint.address()); + + // retrieve sys admin token + sysAdminToken = accessPoint.propertyMap().get(API_KEY_PROPERTY).value(); + sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken); + + // retrieve URL_RESOLVER + if(accessPoint.propertyMap().containsKey(URL_RESOLVER)) + urlResolver = accessPoint.propertyMap().get(URL_RESOLVER).value(); + + // break now + break; + } + } + + // if none of them was master, throw an exception + if(!oneWasMaster) + throw new NoApplicationProfileMasterException("There is no application profile with MASTER property"); + + }else{ + + ServiceEndpoint res = resources.get(0); + Iterator accessPointIterator = res.profile().accessPoints().iterator(); + + while (accessPointIterator.hasNext()) { + ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator + .next(); + + // add this host + datacatalogueUrls.add(accessPoint.address()); + + // retrieve sys admin token + sysAdminToken = accessPoint.propertyMap().get(API_KEY_PROPERTY).value(); + sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken); + + // get the is manage product property + Property entry = accessPoint.propertyMap().get(IS_MANAGE_PRODUCT_ENABLED); + String isManageProduct = entry != null ? entry.value() : null; + + if(isManageProduct != null && isManageProduct.equals("true")){ + logger.info("Manage product is enabled in this scope"); + manageProductEnabled = true; + } + + // retrieve URL_RESOLVER + if(accessPoint.propertyMap().containsKey(URL_RESOLVER)) + urlResolver = accessPoint.propertyMap().get(URL_RESOLVER).value(); + } + } + + }catch(Exception e){ + throw new ServiceEndPointException("There is no service end point for such information"); + } + } + + + } + + /** + * Retrieve the right DB information + * @param resources + * @throws ServiceEndPointException + * @throws NoDataCatalogueRuntimeResourceException + */ + private void evaluateRightConfigurationDB(List resources) throws ServiceEndPointException, NoDataCatalogueRuntimeResourceException { + + if (resources.size() == 0){ + throw new NoDataCatalogueRuntimeResourceException("There is no Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" and Platform " + PLATFORM_DB_NAME + " in this scope."); + } + else { + try{ + + if(resources.size() > 1){ + boolean oneWasMaster = false; + + logger.info("Too many Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" in this scope.. Looking for the one that has the property " + IS_MASTER_ROOT_KEY_PROPERTY); + + for (ServiceEndpoint res : resources) { + + Iterator accessPointIterator = res.profile().accessPoints().iterator(); + + while (accessPointIterator.hasNext()) { + ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator + .next(); + + // get the is master property + Property entry = accessPoint.propertyMap().get(IS_MASTER_ROOT_KEY_PROPERTY); + String isMaster = entry != null ? entry.value() : null; + + if(isMaster == null || !isMaster.equals("true")) + continue; + + // set this variable + oneWasMaster = true; + + // add this host + hostsDB.add(accessPoint.address().split(":")[0]); + + // save the port + int port = Integer.parseInt(accessPoint.address().split(":")[1]); + portsDB.add(port); + + // save the name of the cluster (this should be unique) + nameDB = accessPoint.name(); + + // save user and password + passwordDB = StringEncrypter.getEncrypter().decrypt(accessPoint.password()); + userDB = accessPoint.username(); + + // now break + break; + } + } + + // if none of them was master, throw an exception + if(!oneWasMaster) + throw new NoApplicationProfileMasterException("There is no application profile with MASTER property"); + }else{ + logger.debug(resources.toString()); + ServiceEndpoint res = resources.get(0); + Iterator accessPointIterator = res.profile().accessPoints().iterator(); + + while (accessPointIterator.hasNext()) { + ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator + .next(); + + // add this host + hostsDB.add(accessPoint.address().split(":")[0]); + + // save the port + int port = Integer.parseInt(accessPoint.address().split(":")[1]); + portsDB.add(port); + + // save the name of the cluster (this should be unique) + nameDB = accessPoint.name(); + + // save user and password + passwordDB = StringEncrypter.getEncrypter().decrypt(accessPoint.password()); + userDB = accessPoint.username(); + + } + } + }catch(Exception e ){ + throw new ServiceEndPointException(e.toString()); + } + } + } + /** * Retrieve endpoints information from IS for DB * @return list of endpoints for ckan database * @throws Exception */ - private List getConfigurationFromISFORDB() throws Exception{ + private static List getConfigurationFromISFORDB() throws Exception{ SimpleQuery query = queryFor(ServiceEndpoint.class); query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_DB_RESOURCE_NAME +"'"); @@ -294,7 +319,7 @@ public class DataCatalogueRunningCluster { * @return list of endpoints for ckan data catalogue * @throws Exception */ - private List getConfigurationFromISFORCatalogueUrl() throws Exception{ + private static List getConfigurationFromISFORCatalogueUrl() throws Exception{ SimpleQuery query = queryFor(ServiceEndpoint.class); query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_CATALOGUE_RESOURCE_NAME +"'"); @@ -309,7 +334,7 @@ public class DataCatalogueRunningCluster { * Retrieve the url of the ckan portlet deployed into this scope * @return */ - private String getPortletUrlFromInfrastrucure() { + private static String getPortletUrlFromInfrastrucure() { String scope = ScopeProvider.instance.get(); logger.debug("Trying to fetch applicationProfile profile from the infrastructure for " + APPLICATION_PROFILE_NAME + " scope: " + scope); diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/utils/UtilMethods.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/utils/UtilMethods.java index 51d23aa..7b16723 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/utils/UtilMethods.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/utils/UtilMethods.java @@ -110,5 +110,5 @@ public class UtilMethods { throw new IllegalArgumentException("Key or scope null"); return key.concat(scope); } - + } diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/CKanUserWrapper.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/CKanUserWrapper.java similarity index 97% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/CKanUserWrapper.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/CKanUserWrapper.java index e7c1ab2..ccc7c49 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/CKanUserWrapper.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/CKanUserWrapper.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.models; +package org.gcube.datacatalogue.ckanutillibrary.shared; import java.io.Serializable; diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/CkanDatasetRelationship.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/CkanDatasetRelationship.java similarity index 96% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/CkanDatasetRelationship.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/CkanDatasetRelationship.java index 5149420..c5d12d8 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/CkanDatasetRelationship.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/CkanDatasetRelationship.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.models; +package org.gcube.datacatalogue.ckanutillibrary.shared; import static com.google.common.base.Preconditions.checkNotNull; diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/DatasetRelationships.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/DatasetRelationships.java similarity index 90% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/DatasetRelationships.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/DatasetRelationships.java index c807575..657fb09 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/DatasetRelationships.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/DatasetRelationships.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.models; +package org.gcube.datacatalogue.ckanutillibrary.shared; /** * Allowed relationships between packages(datasets). Some of them are not supported yet due to the problem diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/ResourceBean.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ResourceBean.java similarity index 97% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/ResourceBean.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ResourceBean.java index c8e7cce..2b124c3 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/ResourceBean.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ResourceBean.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.models; +package org.gcube.datacatalogue.ckanutillibrary.shared; import java.io.Serializable; diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/State.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/State.java similarity index 72% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/State.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/State.java index d898e82..ddc8fc2 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/models/State.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/State.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.models; +package org.gcube.datacatalogue.ckanutillibrary.shared; /** * The current state of this group/user diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/ApplicationProfileNotFoundException.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/ApplicationProfileNotFoundException.java similarity index 83% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/ApplicationProfileNotFoundException.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/ApplicationProfileNotFoundException.java index 71656cb..384b652 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/ApplicationProfileNotFoundException.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/ApplicationProfileNotFoundException.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.exceptions; +package org.gcube.datacatalogue.ckanutillibrary.shared.ex; @SuppressWarnings("serial") /** diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/NoApplicationProfileMasterException.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/NoApplicationProfileMasterException.java similarity index 90% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/NoApplicationProfileMasterException.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/NoApplicationProfileMasterException.java index 3766467..feca2ae 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/NoApplicationProfileMasterException.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/NoApplicationProfileMasterException.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.exceptions; +package org.gcube.datacatalogue.ckanutillibrary.shared.ex; /** * Thrown when there are more than one application profile, but none of them was set as master diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/NoDataCatalogueRuntimeResourceException.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/NoDataCatalogueRuntimeResourceException.java similarity index 88% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/NoDataCatalogueRuntimeResourceException.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/NoDataCatalogueRuntimeResourceException.java index 218c222..1f35604 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/NoDataCatalogueRuntimeResourceException.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/NoDataCatalogueRuntimeResourceException.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.exceptions; +package org.gcube.datacatalogue.ckanutillibrary.shared.ex; /** * No Data Catalogue node found. diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/ServiceEndPointException.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/ServiceEndPointException.java similarity index 89% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/ServiceEndPointException.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/ServiceEndPointException.java index 0f42299..bfcecaf 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/ServiceEndPointException.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/ServiceEndPointException.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.exceptions; +package org.gcube.datacatalogue.ckanutillibrary.shared.ex; /** * Exception thrown when it is not possible retrieve information from the ServiceEndpoint diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/TooManyRunningClustersException.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/TooManyRunningClustersException.java similarity index 88% rename from src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/TooManyRunningClustersException.java rename to src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/TooManyRunningClustersException.java index 28c6ae7..4f73f02 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/exceptions/TooManyRunningClustersException.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/shared/ex/TooManyRunningClustersException.java @@ -1,4 +1,4 @@ -package org.gcube.datacatalogue.ckanutillibrary.server.exceptions; +package org.gcube.datacatalogue.ckanutillibrary.shared.ex; /** * Too many clusters in this scope exception. diff --git a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/TestDataCatalogueLib.java b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/TestDataCatalogueLib.java index fc01d93..47be9f0 100644 --- a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/TestDataCatalogueLib.java +++ b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/TestDataCatalogueLib.java @@ -10,10 +10,10 @@ import java.util.Map.Entry; import java.util.UUID; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.datacatalogue.ckanutillibrary.server.models.CKanUserWrapper; -import org.gcube.datacatalogue.ckanutillibrary.server.models.CkanDatasetRelationship; -import org.gcube.datacatalogue.ckanutillibrary.server.models.DatasetRelationships; import org.gcube.datacatalogue.ckanutillibrary.server.utils.UtilMethods; +import org.gcube.datacatalogue.ckanutillibrary.shared.CKanUserWrapper; +import org.gcube.datacatalogue.ckanutillibrary.shared.CkanDatasetRelationship; +import org.gcube.datacatalogue.ckanutillibrary.shared.DatasetRelationships; import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg; import org.slf4j.LoggerFactory;