4941: Uri Resolver Manager: it must use the original scope instead of root scope

Task-Url: https://support.d4science.org/issues/4941

Uri Resolver Manager has been updated in order to use scope setted in the scope provider without changing it

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/uri-resolver-manager@131147 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-09-06 13:09:25 +00:00
parent 65c48e3a4c
commit 9c3513ffe6
9 changed files with 170 additions and 218 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" output="target/uri-resolver-manager-1.1.0-SNAPSHOT/WEB-INF/classes" path="src/main/java"> <classpathentry kind="src" output="target/uri-resolver-manager-1.3.0-SNAPSHOT/WEB-INF/classes" path="src/main/java">
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
@ -23,5 +23,5 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="output" path="target/uri-resolver-manager-1.1.0-SNAPSHOT/WEB-INF/classes"/> <classpathentry kind="output" path="target/uri-resolver-manager-1.3.0-SNAPSHOT/WEB-INF/classes"/>
</classpath> </classpath>

View File

@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.portlets.user</groupId> <groupId>org.gcube.portlets.user</groupId>
<artifactId>uri-resolver-manager</artifactId> <artifactId>uri-resolver-manager</artifactId>
<version>1.2.0-SNAPSHOT</version> <version>1.3.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>uri-resolver-manager</name> <name>uri-resolver-manager</name>
<description>The URI Resolver Manager</description> <description>The URI Resolver Manager</description>

View File

@ -22,13 +22,11 @@ import org.gcube.portlets.user.urlshortener.UrlShortener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* The Class UriResolverManager. * The Class UriResolverManager.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* May 4, 2015 * Sep 6, 2016
*/ */
public class UriResolverManager { public class UriResolverManager {
@ -85,8 +83,8 @@ public class UriResolverManager {
/** /**
* Instantiates a new uri resolver manager. * Instantiates a new uri resolver manager.
* Precondition: set the scope provider {@link ScopeProvider.instance.get()} * Precondition: set the scope into ScopeProvider {@link ScopeProvider#get()}
* The scope is used to look up the generic resource {@link UriResolverMapReader#URI_RESOLVER_MAP} available in the infrastructure to map ApplicationType with its Resolver * The scope is used to look up the generic resource with name: {@link UriResolverMapReader#URI_RESOLVER_MAP_RESOURCE_NAME}, secondary type: {@link UriResolverMapReader#URIRESOLVERMAP_SECONDARY_TYPE} from IS to map ApplicationType with its Resolver
* *
* @throws UriResolverMapException the uri resolver map exception * @throws UriResolverMapException the uri resolver map exception
* @throws IllegalArgumentException the illegal argument exception * @throws IllegalArgumentException the illegal argument exception
@ -95,11 +93,12 @@ public class UriResolverManager {
try { try {
this.scope = ScopeProvider.instance.get(); this.scope = ScopeProvider.instance.get();
logger.info("UriResolverManager is using scope: "+scope+", read from ScopeProvider");
if(this.scope == null) if(this.scope == null)
throw new UriResolverMapException("Scope is null, set scope provider!"); throw new UriResolverMapException("Scope is null, set scope into ScopeProvider!");
this.uriResolverMapReader = new UriResolverMapReader(this.scope); this.uriResolverMapReader = new UriResolverMapReader();
this.applicationTypes = uriResolverMapReader.getApplicationTypes(); this.applicationTypes = uriResolverMapReader.getApplicationTypes();
this.setTimerUriResolverReader(RESET_DELAY, RESET_TIME); this.setTimerUriResolverReader(RESET_DELAY, RESET_TIME);
} catch (UriResolverMapException e){ } catch (UriResolverMapException e){
@ -112,8 +111,10 @@ public class UriResolverManager {
} }
/** /**
* Sets the application type.
*
* @param applicationType the applicationType to set * @param applicationType the applicationType to set
* @throws IllegalArgumentException * @throws IllegalArgumentException the illegal argument exception
*/ */
public void setApplicationType(String applicationType) throws IllegalArgumentException { public void setApplicationType(String applicationType) throws IllegalArgumentException {
if(!this.applicationTypes.containsKey(applicationType)){ if(!this.applicationTypes.containsKey(applicationType)){
@ -180,8 +181,7 @@ public class UriResolverManager {
if(reader==null){ if(reader==null){
logger.info("Runtime Resource Reader is null, istancing..."); logger.info("Runtime Resource Reader is null, istancing...");
ScopeProvider.instance.set(this.scope); reader = new RuntimeResourceReader(resolver.getResourceName());
reader = new RuntimeResourceReader(this.scope, resolver.getResourceName());
} }
if(resolver.getEntryName()==null || resolver.getEntryName().isEmpty()){ if(resolver.getEntryName()==null || resolver.getEntryName().isEmpty()){
@ -259,7 +259,7 @@ public class UriResolverManager {
if(resolver == null) if(resolver == null)
throw new IllegalArgumentException("Resolver is null, set Resolver"); throw new IllegalArgumentException("Resolver is null, set Resolver");
RuntimeResourceReader reader = new RuntimeResourceReader(this.scope, resolver.getResourceName()); RuntimeResourceReader reader = new RuntimeResourceReader(resolver.getResourceName());
ServiceAccessPoint serviceAccessPoint = null; ServiceAccessPoint serviceAccessPoint = null;
if(resolver.getEntryName()==null || resolver.getEntryName().isEmpty()){ if(resolver.getEntryName()==null || resolver.getEntryName().isEmpty()){

View File

@ -1,7 +1,19 @@
package org.gcube.portlets.user.uriresolvermanager.readers; package org.gcube.portlets.user.uriresolvermanager.readers;
/**
* The Class ApplicationProfileException.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 6, 2016
*/
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ApplicationProfileException extends Exception { public class ApplicationProfileException extends Exception {
/**
* Instantiates a new application profile exception.
*
* @param message the message
*/
public ApplicationProfileException(String message) { public ApplicationProfileException(String message) {
super(message); super(message);
} }

View File

@ -1,7 +1,19 @@
package org.gcube.portlets.user.uriresolvermanager.readers; package org.gcube.portlets.user.uriresolvermanager.readers;
/**
* The Class RuntimeResourceException.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 6, 2016
*/
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class RuntimeResourceException extends Exception { public class RuntimeResourceException extends Exception {
/**
* Instantiates a new runtime resource exception.
*
* @param message the message
*/
public RuntimeResourceException(String message) { public RuntimeResourceException(String message) {
super(message); super(message);
} }

View File

@ -17,7 +17,6 @@ import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceAccessPoint; import org.gcube.portlets.user.uriresolvermanager.entity.ServiceAccessPoint;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter; import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter;
import org.gcube.portlets.user.uriresolvermanager.util.ScopeUtil;
import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -38,21 +37,18 @@ public class RuntimeResourceReader {
private String resourceName; private String resourceName;
private String scope;
private String entryName; private String entryName;
/** /**
* Instantiates a new runtime resource reader. * Instantiates a new runtime resource reader.
* *
* @param scope the scope
* @param resourceName the resource name * @param resourceName the resource name
* @throws Exception the exception * @throws Exception the exception
*/ */
public RuntimeResourceReader(String scope, String resourceName) throws Exception { public RuntimeResourceReader(String resourceName) throws Exception {
this.scope = scope;
this.resourceName = resourceName; this.resourceName = resourceName;
readResource(scope, resourceName); readResource(resourceName);
} }
/** /**
@ -63,18 +59,13 @@ public class RuntimeResourceReader {
* @return the application URI * @return the application URI
* @throws Exception the exception * @throws Exception the exception
*/ */
private void readResource(String scope, String resourceName) throws Exception { private void readResource(String resourceName) throws Exception {
try{ try{
String scope = ScopeProvider.instance.get();
logger.info("Tentative read resource: "+resourceName+", scope: "+scope); logger.info("Tentative read resource: "+resourceName+", scope: "+scope);
this.resourceName = resourceName; this.resourceName = resourceName;
this.scope = scope;
String infraName = ScopeUtil.getInfrastructureNameFromScope(scope);
logger.info("Instancing root scope: "+infraName);
ScopeProvider.instance.set(infraName);
SimpleQuery query = queryFor(ServiceEndpoint.class); SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Name/string() eq '"+resourceName+"'"); query.addCondition("$resource/Profile/Name/string() eq '"+resourceName+"'");
@ -82,11 +73,14 @@ public class RuntimeResourceReader {
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class); DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> r = client.submit(query); List<ServiceEndpoint> r = client.submit(query);
if (r == null || r.isEmpty()) throw new Exception("Cannot retrieve the runtime resource: "+resourceName); if (r == null || r.isEmpty()) throw new Exception("Cannot retrieve the runtime resource with name: "+resourceName +" in the scope: "+scope);
ServiceEndpoint se = r.get(0); ServiceEndpoint se = r.get(0);
if(se.profile()==null) if(se.profile()==null){
throw new Exception("IS profile is null for resource: "+resourceName); String msg = "Runtime reosource with resource name: "+resourceName +" is null in the scope: "+scope;
logger.error(msg);
throw new Exception(msg);
}
Group<AccessPoint> accessPoints = se.profile().accessPoints(); Group<AccessPoint> accessPoints = se.profile().accessPoints();
if(accessPoints.size()==0) throw new Exception("Accesspoint in resource "+resourceName+" not found"); if(accessPoints.size()==0) throw new Exception("Accesspoint in resource "+resourceName+" not found");
@ -126,15 +120,16 @@ public class RuntimeResourceReader {
// Group<Property> properties = ap.properties(); // Group<Property> properties = ap.properties();
}catch (Exception e) { }catch (Exception e) {
logger.error("Sorry, an error occurred on reading the resource "+resourceName+ " Runtime Reosurce",e); logger.error("Sorry, an error occurred on reading the resource "+resourceName+ " Runtime Resource",e);
throw new Exception("Sorry, an error occurred on reading the resource "+resourceName+ " Runtime Reosurce"); throw new Exception("Sorry, an error occurred on reading the resource "+resourceName+ " Runtime Reosurce");
} }
} }
/** /**
* Gets the service access point for entry name.
* *
* @param entryName * @param entryName the entry name
* @return * @return the service access point for entry name
*/ */
public ServiceAccessPoint getServiceAccessPointForEntryName(String entryName){ public ServiceAccessPoint getServiceAccessPointForEntryName(String entryName){
@ -164,19 +159,33 @@ public class RuntimeResourceReader {
} }
/** /**
* @return the scope * Gets the service access points.
*/ *
public String getScope() {
return scope;
}
/**
* @return the serviceAccessPoints * @return the serviceAccessPoints
*/ */
public List<ServiceAccessPoint> getServiceAccessPoints() { public List<ServiceAccessPoint> getServiceAccessPoints() {
return serviceAccessPoints; return serviceAccessPoints;
} }
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("RuntimeResourceReader [serviceAccessPoints=");
builder.append(serviceAccessPoints);
builder.append(", resourceName=");
builder.append(resourceName);
builder.append(", entryName=");
builder.append(entryName);
builder.append("]");
return builder.toString();
}
// public static void main(String[] args) { // public static void main(String[] args) {
// try { // try {
// RuntimeResourceReader rr = new RuntimeResourceReader("/gcube", "Gis-Resolver"); // RuntimeResourceReader rr = new RuntimeResourceReader("/gcube", "Gis-Resolver");

View File

@ -13,7 +13,6 @@ import javax.xml.parsers.DocumentBuilderFactory;
import org.gcube.common.resources.gcore.utils.XPathHelper; import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.uriresolvermanager.entity.Resolver; import org.gcube.portlets.user.uriresolvermanager.entity.Resolver;
import org.gcube.portlets.user.uriresolvermanager.util.ScopeUtil;
import org.gcube.resources.discovery.client.api.DiscoveryClient; 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.Query;
import org.gcube.resources.discovery.client.queries.impl.QueryBox; import org.gcube.resources.discovery.client.queries.impl.QueryBox;
@ -49,15 +48,9 @@ public class UriResolverMapReader {
/** /**
* Instantiates a new uri resolver map reader. * Instantiates a new uri resolver map reader.
* *
* @param scope - the scope to be searched
* @throws Exception the exception * @throws Exception the exception
*/ */
public UriResolverMapReader(String scope) throws Exception { public UriResolverMapReader() throws Exception {
this.scope = scope;
if(scope==null){
throw new Exception("Scope is null, set scope provider!");
}
this.resourceName = URI_RESOLVER_MAP_RESOURCE_NAME; this.resourceName = URI_RESOLVER_MAP_RESOURCE_NAME;
this.secondaryType = URIRESOLVERMAP_SECONDARY_TYPE; this.secondaryType = URIRESOLVERMAP_SECONDARY_TYPE;
@ -76,37 +69,31 @@ public class UriResolverMapReader {
String queryString = getGcubeGenericQueryString(secondaryType, resourceName); String queryString = getGcubeGenericQueryString(secondaryType, resourceName);
logger.info("Trying to fetch applicationProfile profile from the infrastructure for " + secondaryType + " scope: " + scope); logger.info("Trying to fetch in the scope: "+ScopeProvider.instance.get()+" the Generic Resouce with name: "+resourceName + " secondary type: "+secondaryType);
logger.info(queryString); logger.info(queryString);
try { try {
logger.info("Getting Infrastructure Name...: ");
String infra = ScopeUtil.getInfrastructureNameFromScope(this.scope);
logger.info("Infrastructure Name returned: "+infra);
ScopeProvider.instance.set(infra);
logger.info("scope provider set instance: "+infra);
Query q = new QueryBox(queryString); Query q = new QueryBox(queryString);
logger.info("new query box works"); logger.debug("new query box works");
DiscoveryClient<String> client = client(); DiscoveryClient<String> client = client();
logger.info("submitting query is: "+queryString); logger.info("submitting query is: "+queryString);
List<String> appUriResolverMap = client.submit(q); List<String> appUriResolverMap = client.submit(q);
logger.info("submit query works"); logger.debug("submit query works");
if (appUriResolverMap == null || appUriResolverMap.size() == 0){ if (appUriResolverMap == null || appUriResolverMap.size() == 0){
logger.error("Your applicationProfile with secondaryType: "+secondaryType+" and name "+resourceName+" is not registered in the infrastructure, scope: "+ScopeProvider.instance.get()); logger.error("ApplicationProfile with secondaryType: "+secondaryType+" and name: "+resourceName+" is not registered in the infrastructure, scope: "+ScopeProvider.instance.get());
throw new ApplicationProfileException("Your applicationProfile with secondaryType: "+secondaryType+" and name "+resourceName+" is not registered in the infrastructure"); throw new ApplicationProfileException("ApplicationProfile with secondaryType: "+secondaryType+" and name: "+resourceName+" is not registered in the scope: "+ScopeProvider.instance.get());
}else { }else {
logger.info("Building new DocumentBuilder.."); logger.info("Building map applications type - resource");
logger.debug("Building new DocumentBuilder..");
String elem = appUriResolverMap.get(0); String elem = appUriResolverMap.get(0);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement();
logger.info("Building new XPathHelper.."); logger.debug("Building new XPathHelper..");
XPathHelper helper = new XPathHelper(node); XPathHelper helper = new XPathHelper(node);
List<String> currValue = null; List<String> currValue = null;
logger.info("Evaluating XPath.."); logger.debug("Evaluating XPath..");
currValue = helper.evaluate("/Resource/Profile/Body/access_point/application_type/text()"); currValue = helper.evaluate("/Resource/Profile/Body/access_point/application_type/text()");
if (currValue != null && currValue.size() > 0) { if (currValue != null && currValue.size() > 0) {
logger.info("Application Types are: "+currValue.size()); logger.info("Application Types are: "+currValue.size());
@ -129,11 +116,8 @@ public class UriResolverMapReader {
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Error while trying to fetch applicationProfile with secondaryType: "+secondaryType+" and name "+resourceName+" from the infrastructure", e); logger.error("Error while trying to fetch Generic Resource with secondaryType: "+secondaryType+" and name "+resourceName+" from the infrastructure", e);
throw new ApplicationProfileException("Error while trying to fetch applicationProfile with secondaryType: "+secondaryType+" and name "+resourceName+" from the infrastructure"); throw new ApplicationProfileException("Error while trying to fetch Generic Resourc with secondaryType: "+secondaryType+" and name "+resourceName+" from the infrastructure");
}finally{
ScopeProvider.instance.reset();
} }
} }
@ -208,21 +192,4 @@ public class UriResolverMapReader {
builder.append("]"); builder.append("]");
return builder.toString(); return builder.toString();
} }
// /**
// * The main method.
// *
// * @param args the arguments
// */
// public static void main(String[] args) {
// String scope ="/gcube";
// try {
// UriResolverMapReader resolver = new UriResolverMapReader(scope);
// System.out.println(resolver);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
} }

View File

@ -1,45 +0,0 @@
/**
*
*/
package org.gcube.portlets.user.uriresolvermanager.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Oct 13, 2014
*
*/
public class ScopeUtil {
private static final String SCOPE_SEPARATOR = "/";
public static final Logger logger = LoggerFactory.getLogger(ScopeUtil.class);
public static String getInfrastructureNameFromScope(String scope) throws Exception{
if(scope==null || scope.isEmpty()){
throw new Exception("Scope is null or empty");
}
if(!scope.startsWith(SCOPE_SEPARATOR)){
logger.warn("Input scope: "+scope+" has not / is a really scope?");
scope = SCOPE_SEPARATOR+scope;
logger.warn("Tentative as scope: "+scope);
}
String[] splitScope = scope.split(SCOPE_SEPARATOR);
String rootScope = SCOPE_SEPARATOR + splitScope[1];
if(rootScope.length()<2){
throw new Exception("Infrastructure name not found in "+scope);
}
logger.trace("scope is "+ scope +" infra: "+rootScope);
return rootScope;
}
}

View File

@ -5,7 +5,6 @@ import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException; import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException;
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException; import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException;
import org.junit.Test;
/** /**
* *
@ -18,7 +17,7 @@ import org.junit.Test;
*/ */
public class UriResolverManagerTest { public class UriResolverManagerTest {
@Test // @Test
public void testUriResolverManger(){ public void testUriResolverManger(){
UriResolverManager manager; UriResolverManager manager;
try { try {
@ -154,6 +153,4 @@ public class UriResolverManagerTest {
} }
} }
} }