Added code to discover Application Profile DataCatalogueMapScopesUrls
Minor fixes git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@131116 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
336b41f723
commit
51ae09387d
|
@ -256,9 +256,7 @@ public class CKanRunningCluster {
|
|||
|
||||
// set the scope back
|
||||
ScopeProvider.instance.set(currentScope);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -60,7 +60,6 @@ import eu.trentorise.opendata.jackan.model.CkanUser;
|
|||
*/
|
||||
public class CKanUtilsImpl implements CKanUtils{
|
||||
|
||||
|
||||
// TO BE REMOVED TODO
|
||||
public final static String PRODUCTION_SCOPE_ROOT = "/d4science.research-infrastructures.eu";
|
||||
public final static String PRODUCTION_CKAN_ORGNAME_ROOT = "d4science";
|
||||
|
|
|
@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory;
|
|||
public class CkanUtilsFactory {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CkanUtilsFactory.class);
|
||||
private static final long MAX_LIFETIME = 1000 * 60 * 5; // 5 MINUTES
|
||||
private static final long MAX_LIFETIME = 1000 * 60 * 2; // 2 MINUTES
|
||||
private static CkanUtilsFactory instance = new CkanUtilsFactory();
|
||||
private static ConcurrentHashMap<String, CacheBean> cache;
|
||||
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
package org.gcube.datacatalogue.ckanutillibrary.utils;
|
||||
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.client;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
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.exceptions.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;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.w3c.dom.Node;
|
||||
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
|
||||
* @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";
|
||||
|
||||
/**
|
||||
* Get the scope in which discover for this url. If the Application Profile doesn't contain it, the current scope (taken
|
||||
* from ScopeProvider is returned).
|
||||
* @param url
|
||||
* @return the scope to be used
|
||||
*/
|
||||
public String getScopePerUrl(String url){
|
||||
|
||||
logger.debug("Request scope for ckan portlet at url " + url);
|
||||
|
||||
if(url == null || url.isEmpty())
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
String scopeToReturn = null;
|
||||
|
||||
String scope = ScopeProvider.instance.get();
|
||||
String rootScopeForInfrastructure = "/" + PortalContext.getConfiguration().getInfrastructureName();
|
||||
|
||||
// set this scope
|
||||
ScopeProvider.instance.set(rootScopeForInfrastructure);
|
||||
|
||||
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");
|
||||
|
||||
DiscoveryClient<String> client = client();
|
||||
List<String> appProfile = client.submit(q);
|
||||
|
||||
if (appProfile == null || appProfile.size() == 0)
|
||||
throw new ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure");
|
||||
else{
|
||||
String elem = appProfile.get(0);
|
||||
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement();
|
||||
XPathHelper helper = new XPathHelper(node);
|
||||
|
||||
List<String> urls = helper.evaluate("/Resource/Profile/Body/EndPoint/URL/text()");
|
||||
if (urls != null && urls.size() > 0) {
|
||||
boolean foundScope = false;
|
||||
for (int i = 0; i < urls.size(); i++) {
|
||||
if (urls.get(i).trim().compareTo(url) == 0) { // url found
|
||||
scopeToReturn = helper.evaluate("/Resource/Profile/Body/EndPoint/Scope/text()").get(i);
|
||||
foundScope = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!foundScope || scopeToReturn == null || scopeToReturn.isEmpty()){
|
||||
logger.debug("Scope is missing for url " + url + ". Returning " + scope);
|
||||
scopeToReturn = scope;
|
||||
}
|
||||
}
|
||||
else
|
||||
throw
|
||||
new ApplicationProfileNotFoundException("Your applicationProfile EndPoint was not found in the profile, consider adding <EndPoint><Scope> element in <Body>");
|
||||
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
|
||||
return scopeToReturn;
|
||||
}
|
||||
}
|
|
@ -6,10 +6,12 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.models.CkanDatasetRelationship;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.models.DatasetRelationships;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.models.RolesIntoOrganization;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.utils.ApplicationProfileScopePerUrlReader;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import eu.trentorise.opendata.jackan.model.CkanOrganization;
|
||||
|
@ -159,4 +161,13 @@ public class TestCKanLib {
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void getScopePerUrl(){
|
||||
|
||||
ScopeProvider.instance.set("/gcube");
|
||||
String url = "https://dev4.d4science.org/group/devvre/ckan";
|
||||
String scopeToUse = new ApplicationProfileScopePerUrlReader().getScopePerUrl(url);
|
||||
logger.debug("Retrieved scope is " + scopeToUse);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue