From 804a7991fa23932467b6d2373ccde4d57a6ea796 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Fri, 28 Sep 2018 09:19:25 +0000 Subject: [PATCH] Task #12500 completed git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@171735 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 1 + distro/changelog.xml | 1 + .../server/DataCatalogueImpl.java | 4 +- .../server/RuntimeResourceReader.java | 164 ++++++++++++++++++ .../server/TestDataCatalogueLib.java | 36 ++-- 5 files changed, 194 insertions(+), 12 deletions(-) create mode 100644 src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/RuntimeResourceReader.java diff --git a/.classpath b/.classpath index 610dcae..a26c2a7 100644 --- a/.classpath +++ b/.classpath @@ -15,6 +15,7 @@ + diff --git a/distro/changelog.xml b/distro/changelog.xml index 8652a90..1d4daeb 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -3,6 +3,7 @@ Added the possibility to deny social post on catalogue-ws #12514 + [Task #12500] Changing log from error to warning Enhance timeout in requests to CKAN in ckan-util-library #12050 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 cb43f26..efc7e66 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/server/DataCatalogueImpl.java @@ -2663,7 +2663,9 @@ public class DataCatalogueImpl implements DataCatalogue{ return client.getOrganization(ckanName); }catch(Exception e){ - logger.error("Failed to retrieve the organization with name" + name, e); + logger.warn("Failed to retrieve the organization with name" +name+ " on the ckan: "+client.getCatalogUrl()); + //OLD PRINTING. FIXING Task #12500 + //logger.error("Failed to retrieve the organization with name" + name, e); } return null; } diff --git a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/RuntimeResourceReader.java b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/RuntimeResourceReader.java new file mode 100644 index 0000000..7736eee --- /dev/null +++ b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/RuntimeResourceReader.java @@ -0,0 +1,164 @@ +package org.gcube.datacatalogue.ckanutillibrary.server; +/** + * + */ + + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.ArrayList; +import java.util.List; + +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class RuntimeResourceReader. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Mar 10, 2017 + */ +public class RuntimeResourceReader { + + public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class); + + private List scopes = new ArrayList(); + + /** + * Instantiates a new runtime resource reader. + * + * @param scope the scope + * @param platformName the platform name + * @param endPoint the end point + * @throws Exception the exception + */ + public RuntimeResourceReader(String scope, String platformName, String runtimeName, String endPoint) throws Exception { + read(scope, platformName, runtimeName, endPoint); + } + + + /** + * Read. + * + * @param scope the scope + * @param runtimeName the runtime name + * @param endPoint the end point + * @return the server parameters + * @throws Exception the exception + */ + private void read(String scope, String platformName, String runtimeName, String endPoint) throws Exception{ + + + String originalScope = null; + try{ + + originalScope = ScopeProvider.instance.get(); + + ScopeProvider.instance.set(scope); + + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq 'Application'"); + //query.addCondition("$resource/Profile/Category/Name/text() eq '"+runtimeName+"'"); + + if(platformName!=null && !platformName.isEmpty()) + query.addCondition("$resource/Profile/Platform/Name/text() eq '"+platformName+"'"); + + if(endPoint!=null && !endPoint.isEmpty()) + query.addCondition("$resource/Profile/AccessPoint/Interface/Endpoint/text() eq '"+endPoint+"'"); + + logger.info("RuntimeReader, using scope: "+scope + ", query is: "+query.toString()); + + DiscoveryClient client = clientFor(ServiceEndpoint.class); + + List r = client.submit(query); + if (r == null || r.isEmpty()) throw new Exception("Cannot retrieve the runtime resource: "+runtimeName); + + + for (ServiceEndpoint serviceEndpoint : r) { + System.out.println(serviceEndpoint.toString()); + } + + ServiceEndpoint se = r.get(0); + if(se.profile()==null) + throw new Exception("IS profile is null for resource: "+platformName); + + + scopes.addAll(se.scopes().asCollection()); + + }catch (Exception e) { + System.out.println(e); + logger.error("Sorry, an error occurred on reading parameters in Runtime Resources",e); + }finally{ + if(originalScope!=null && !originalScope.isEmpty()){ + ScopeProvider.instance.set(originalScope); + logger.info("scope provider setted to orginal scope: "+originalScope); + }else{ + ScopeProvider.instance.reset(); + logger.info("scope provider reset"); + } + } + + return; + } + + + /** + * Gets the scopes. + * + * @return the scopes + */ + public List getScopes() { + + return scopes; + } + + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("RuntimeResourceReader [scopes="); + builder.append(scopes); + builder.append("]"); + return builder.toString(); + } + + + +// /** +// * The main method. +// * +// * @param args the arguments +// */ +// public static void main(String[] args) { +// +// String scope = "/d4science.research-infrastructures.eu"; +// String platformName = null; +// String runtimeName = "CKanDataCatalogue"; +// String endPoint = null; +// RuntimeResourceReader reader; +// try { +// reader = new RuntimeResourceReader(scope, platformName, runtimeName, endPoint); +// System.out.println(reader.toString()); +// +// System.out.println("Scopes are: "+reader.getScopes().size()); +// } +// catch (Exception e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// +// } + + +} 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 bd06576..10f10bf 100644 --- a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/TestDataCatalogueLib.java +++ b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/TestDataCatalogueLib.java @@ -32,7 +32,8 @@ public class TestDataCatalogueLib { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestDataCatalogueLib.class); private DataCatalogueFactory factory; - private String scope = "/gcube/devNext/NextNext"; + //private String scope = "/gcube/devNext/NextNext"; + private String scope = "/d4science.research-infrastructures.eu"; private String testUser = "costantino_perciante"; String subjectId = "aa_father4"; String objectId = "bb_son4"; @@ -41,7 +42,7 @@ public class TestDataCatalogueLib { public void before() throws Exception{ factory = DataCatalogueFactory.getFactory(); } - + //@Test public void getSysadminEmail() throws Exception{ DataCatalogueImpl utils = factory.getUtilsPerScope(scope); @@ -55,7 +56,7 @@ public class TestDataCatalogueLib { logger.debug("Statistics " + utils.getStatistics()); } - + //@Test public void getLandingPages() throws Exception{ @@ -64,7 +65,7 @@ public class TestDataCatalogueLib { } - //@Test + //@Test public void getDatasetIdsFromDB() throws Exception{ DataCatalogueImpl utils = factory.getUtilsPerScope(scope); List ids = utils.getProductsIdsInGroupOrOrg("aquamaps", true, 0, Integer.MAX_VALUE); @@ -190,7 +191,7 @@ public class TestDataCatalogueLib { System.out.println("Testing getOrganizationsByUser"); DataCatalogueImpl instance = factory.getUtilsPerScope(scope); - String username = "francescomangiacrapa"; + String username = "massimilianoassante"; List organizations = instance.getOrganizationsByUser(username); System.out.println("organizations for user " + username + " are: "); @@ -230,7 +231,7 @@ public class TestDataCatalogueLib { Iterator>> iterator = orgs.entrySet().iterator(); while (iterator.hasNext()) { - Map.Entry> entry = (Map.Entry>) iterator + Map.Entry> entry = iterator .next(); logger.debug("Org is " + entry.getKey() + " and role is " + entry.getValue().get(0)); @@ -315,7 +316,7 @@ public class TestDataCatalogueLib { // dataset.setPriv(!dataset.isPriv()); // CkanDataset datasetUpd = client.updateDataset(dataset); // logger.debug("Private value is " + datasetUpd.isPriv()); - // + // } @@ -495,13 +496,26 @@ public class TestDataCatalogueLib { } //@Test + //UPDATED By FRANCESCO FOR FIXING Incident #12563 public void getUserRoleByGroup() throws Exception{ DataCatalogueImpl instance = factory.getUtilsPerScope(scope); - String username = "costantino_perciante"; + String username = "massimiliano_assante"; long init = System.currentTimeMillis(); - instance.getUserRoleByGroup(username, instance.getApiKeyFromUsername(username)); - long end = System.currentTimeMillis(); - logger.debug("Time taken " + (end - init)); + + //Fixing Incident #12563 + try{ + String apiKey = instance.getApiKeyFromUsername(username); + logger.debug("ApiKey is "+apiKey+ " retrieved for user: "+username); + //Fixing Incident #12563 + if(apiKey!=null && !apiKey.isEmpty()){ + instance.getUserRoleByGroup(username, apiKey); + long end = System.currentTimeMillis(); + logger.debug("Time taken " + (end - init)); + }else + logger.warn("The API_KEY for "+username+ " is null or empty in the catalogue: "+instance.getCatalogueUrl()); + }catch(Exception e){ + logger.error("Error on recovery the user groups for "+username, e); + } } //@Test