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

@ -1,5 +1,5 @@
/** /**
* *
*/ */
package org.gcube.portlets.user.uriresolvermanager; package org.gcube.portlets.user.uriresolvermanager;
@ -22,32 +22,30 @@ 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 {
/** /**
* Time to reload Runtime Resource Configuration * Time to reload Runtime Resource Configuration
*/ */
public static int RESET_DELAY = 15*60*1000; //15 MINUTES public static int RESET_DELAY = 15*60*1000; //15 MINUTES
/** /**
* Time to reload Runtime Resource Configuration * Time to reload Runtime Resource Configuration
*/ */
public static int RESET_TIME = RESET_DELAY; //15 MINUTES public static int RESET_TIME = RESET_DELAY; //15 MINUTES
private UriResolverMapReader uriResolverMapReader; private UriResolverMapReader uriResolverMapReader;
private Map<String, Resolver> applicationTypes; private Map<String, Resolver> applicationTypes;
private String scope; private String scope;
private String applicationType; private String applicationType;
private RuntimeResourceReader reader; private RuntimeResourceReader reader;
/** /**
* A lock to prevent reader = null; * A lock to prevent reader = null;
*/ */
@ -56,7 +54,7 @@ public class UriResolverManager {
private ServiceAccessPoint serviceAccessPoint; private ServiceAccessPoint serviceAccessPoint;
private Timer timer; private Timer timer;
/** /**
* Lock reader. * Lock reader.
*/ */
@ -79,27 +77,28 @@ public class UriResolverManager {
public synchronized int countReaders() { public synchronized int countReaders() {
return usingReader; return usingReader;
} }
public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class); public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class);
/** /**
* 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
*/ */
public UriResolverManager() throws UriResolverMapException, IllegalArgumentException{ public UriResolverManager() throws UriResolverMapException, IllegalArgumentException{
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){
@ -110,22 +109,24 @@ public class UriResolverManager {
throw new UriResolverMapException("Map Application Type - Resources not found in IS"); throw new UriResolverMapException("Map Application Type - Resources not found in IS");
} }
} }
/** /**
* 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)){
throw new IllegalArgumentException("Application type '"+applicationType +"' not found in Application Types: "+getApplicationTypes()); throw new IllegalArgumentException("Application type '"+applicationType +"' not found in Application Types: "+getApplicationTypes());
} }
this.applicationType = applicationType; this.applicationType = applicationType;
} }
/** /**
* Instance a UriResolverManager * Instance a UriResolverManager
* Precondition: set the scope provider {@link ScopeProvider.instance.get()} * Precondition: set the scope provider {@link ScopeProvider.instance.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 {@link UriResolverMapReader#URI_RESOLVER_MAP} available in the infrastructure to map ApplicationType with its Resolver
* *
* @param applicationType a (valid) key Application Type {@link UriResolverManager#getApplicationTypes()} * @param applicationType a (valid) key Application Type {@link UriResolverManager#getApplicationTypes()}
@ -136,9 +137,9 @@ public class UriResolverManager {
this(); this();
setApplicationType(applicationType); setApplicationType(applicationType);
} }
/** /**
* Gets the link. * Gets the link.
* *
@ -153,7 +154,7 @@ public class UriResolverManager {
this.applicationType = applicationType; this.applicationType = applicationType;
return getLink(parameters, shortLink); return getLink(parameters, shortLink);
} }
/** /**
* Gets the link. * Gets the link.
* *
@ -164,26 +165,25 @@ public class UriResolverManager {
* @throws UriResolverMapException the uri resolver map exception * @throws UriResolverMapException the uri resolver map exception
*/ */
public String getLink(Map<String, String> parameters, boolean shortLink) throws IllegalArgumentException, UriResolverMapException{ public String getLink(Map<String, String> parameters, boolean shortLink) throws IllegalArgumentException, UriResolverMapException{
if(applicationType==null) if(applicationType==null)
throw new IllegalArgumentException("Application type is null"); throw new IllegalArgumentException("Application type is null");
Resolver resolver = this.applicationTypes.get(applicationType); Resolver resolver = this.applicationTypes.get(applicationType);
String link; String link;
if(parameters==null) if(parameters==null)
throw new IllegalArgumentException("Input Map parameters is null"); throw new IllegalArgumentException("Input Map parameters is null");
try { try {
lockReader(); lockReader();
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()){
logger.warn("The entryname to "+resolver.getResourceName() +" is null or empty, reading first Access Point!!"); logger.warn("The entryname to "+resolver.getResourceName() +" is null or empty, reading first Access Point!!");
serviceAccessPoint = reader.getServiceAccessPoints().get(0); serviceAccessPoint = reader.getServiceAccessPoints().get(0);
@ -195,24 +195,24 @@ public class UriResolverManager {
} }
List<ServiceParameter> resourceParameters = serviceAccessPoint.getServiceParameters(); List<ServiceParameter> resourceParameters = serviceAccessPoint.getServiceParameters();
//CHECK PARAMETERS //CHECK PARAMETERS
for (ServiceParameter serviceParameter : resourceParameters) { for (ServiceParameter serviceParameter : resourceParameters) {
if(serviceParameter.isMandatory()){ if(serviceParameter.isMandatory()){
if(!parameters.containsKey(serviceParameter.getKey())){ if(!parameters.containsKey(serviceParameter.getKey())){
throw new IllegalArgumentException("Mandatory service key (parameter) '"+serviceParameter.getKey() +"' not found into input map"); throw new IllegalArgumentException("Mandatory service key (parameter) '"+serviceParameter.getKey() +"' not found into input map");
} }
} }
} }
String baseURI = serviceAccessPoint.getServiceUrl(); String baseURI = serviceAccessPoint.getServiceUrl();
releaseReader(); releaseReader();
String params = UrlEncoderUtil.encodeQuery(parameters); String params = UrlEncoderUtil.encodeQuery(parameters);
link = baseURI+"?"+params; link = baseURI+"?"+params;
logger.info("Created HTTP URI request (link): "+link); logger.info("Created HTTP URI request (link): "+link);
if(shortLink){ if(shortLink){
try{ try{
logger.info("Shortner start.."); logger.info("Shortner start..");
@ -229,10 +229,10 @@ public class UriResolverManager {
} catch (Exception e) { } catch (Exception e) {
logger.error("Uri Resolver Exception: ", e); logger.error("Uri Resolver Exception: ", e);
throw new UriResolverMapException("Uri Resolver error: " +e.getMessage()); throw new UriResolverMapException("Uri Resolver error: " +e.getMessage());
} }
return link; return link;
} }
/** /**
* Gets the application types. * Gets the application types.
* *
@ -241,7 +241,7 @@ public class UriResolverManager {
public Set<String> getApplicationTypes(){ public Set<String> getApplicationTypes(){
return this.applicationTypes.keySet(); return this.applicationTypes.keySet();
} }
/** /**
* Discovery service parameters. * Discovery service parameters.
* *
@ -252,15 +252,15 @@ public class UriResolverManager {
*/ */
public List<ServiceParameter> discoveryServiceParameters(Resolver resolver) throws IllegalArgumentException, Exception{ public List<ServiceParameter> discoveryServiceParameters(Resolver resolver) throws IllegalArgumentException, Exception{
try { try {
if(this.scope == null) if(this.scope == null)
throw new IllegalArgumentException("Scope is null, set ScopeProvider"); throw new IllegalArgumentException("Scope is null, set ScopeProvider");
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()){
logger.warn("The entryname to "+resolver.getResourceName() +" is null or empty, reading first Access Point!!"); logger.warn("The entryname to "+resolver.getResourceName() +" is null or empty, reading first Access Point!!");
@ -271,14 +271,14 @@ public class UriResolverManager {
if(serviceAccessPoint==null) if(serviceAccessPoint==null)
throw new UriResolverMapException("Entry Name "+resolver.getEntryName() +" not found in Resource name: "+resolver.getResourceName()); throw new UriResolverMapException("Entry Name "+resolver.getEntryName() +" not found in Resource name: "+resolver.getResourceName());
} }
return serviceAccessPoint.getServiceParameters(); return serviceAccessPoint.getServiceParameters();
} catch (Exception e) { } catch (Exception e) {
logger.error("Uri Resolver error: ", e); logger.error("Uri Resolver error: ", e);
throw new UriResolverMapException("Uri Resolver error: " +e.getMessage()); throw new UriResolverMapException("Uri Resolver error: " +e.getMessage());
} }
} }
/** /**
* Gets the resolver. * Gets the resolver.
* *
@ -288,7 +288,7 @@ public class UriResolverManager {
public Resolver getResolver(String applicationType){ public Resolver getResolver(String applicationType){
return this.applicationTypes.get(applicationType); return this.applicationTypes.get(applicationType);
} }
/** /**
* Gets the capabilities. * Gets the capabilities.
* *
@ -305,11 +305,11 @@ public class UriResolverManager {
* @param period the period * @param period the period
*/ */
public void setTimerUriResolverReader(long delay, long period) { public void setTimerUriResolverReader(long delay, long period) {
cancelTimerUriResolverReader(); cancelTimerUriResolverReader();
timer = new Timer(true); timer = new Timer(true);
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
@ -320,12 +320,12 @@ public class UriResolverManager {
reader = null; reader = null;
}else }else
logger.info("Reader locked, counters is/are:"+counters+", skipping"); logger.info("Reader locked, counters is/are:"+counters+", skipping");
} }
}, delay, period); }, delay, period);
} }
/** /**
* Cancel timer uri resolver reader. * Cancel timer uri resolver reader.
@ -334,7 +334,7 @@ public class UriResolverManager {
if(timer!=null) if(timer!=null)
timer.cancel(); timer.cancel();
} }
/** /**
* Invalid uri resolver reader. * Invalid uri resolver reader.
@ -342,7 +342,7 @@ public class UriResolverManager {
public void invalidUriResolverReader(){ public void invalidUriResolverReader(){
reader = null; reader = null;
} }
/* /*
public static void main(String[] args) { public static void main(String[] args) {
try { try {

View File

@ -1,8 +1,20 @@
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 {
public ApplicationProfileException(String message) {
/**
* Instantiates a new application profile exception.
*
* @param message the message
*/
public ApplicationProfileException(String message) {
super(message); super(message);
} }
} }

View File

@ -1,8 +1,20 @@
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 {
public RuntimeResourceException(String message) {
/**
* Instantiates a new runtime resource exception.
*
* @param message the message
*/
public RuntimeResourceException(String message) {
super(message); super(message);
} }
} }

View File

@ -1,5 +1,5 @@
/** /**
* *
*/ */
package org.gcube.portlets.user.uriresolvermanager.readers; package org.gcube.portlets.user.uriresolvermanager.readers;
@ -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;
@ -31,30 +30,27 @@ import org.slf4j.LoggerFactory;
* Apr 30, 2015 * Apr 30, 2015
*/ */
public class RuntimeResourceReader { public class RuntimeResourceReader {
public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class); public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class);
public List<ServiceAccessPoint> serviceAccessPoints; public List<ServiceAccessPoint> serviceAccessPoints;
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);
} }
/** /**
* Read resource. * Read resource.
* *
@ -63,81 +59,80 @@ 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+"'");
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");
Iterator<AccessPoint> acIt = accessPoints.iterator(); Iterator<AccessPoint> acIt = accessPoints.iterator();
serviceAccessPoints = new ArrayList<ServiceAccessPoint>(accessPoints.size()); serviceAccessPoints = new ArrayList<ServiceAccessPoint>(accessPoints.size());
while(acIt.hasNext()){ while(acIt.hasNext()){
AccessPoint ap = acIt.next(); AccessPoint ap = acIt.next();
Group<Property> properties = ap.properties(); Group<Property> properties = ap.properties();
if(properties.size()==0){ if(properties.size()==0){
logger.warn("Properties in resource "+resourceName+" not found"); logger.warn("Properties in resource "+resourceName+" not found");
}else{ }else{
List<ServiceParameter> serviceParameters = new ArrayList<ServiceParameter>(properties.size()); List<ServiceParameter> serviceParameters = new ArrayList<ServiceParameter>(properties.size());
Iterator<Property> iter = properties.iterator(); Iterator<Property> iter = properties.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Property prop = iter.next(); Property prop = iter.next();
serviceParameters.add(new ServiceParameter(prop.value(), true)); serviceParameters.add(new ServiceParameter(prop.value(), true));
} }
serviceAccessPoints.add(new ServiceAccessPoint(ap.name(), ap.address(), serviceParameters)); serviceAccessPoints.add(new ServiceAccessPoint(ap.name(), ap.address(), serviceParameters));
} }
} }
// parameters.setUser(ap.username()); //username // parameters.setUser(ap.username()); //username
// //
// String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password()); // String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password());
// //
// parameters.setPassword(decryptedPassword); //password // parameters.setPassword(decryptedPassword); //password
// 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 *
* @return * @param entryName the entry name
* @return the service access point for entry name
*/ */
public ServiceAccessPoint getServiceAccessPointForEntryName(String entryName){ public ServiceAccessPoint getServiceAccessPointForEntryName(String entryName){
for (ServiceAccessPoint serviceAccessPoint : serviceAccessPoints) { for (ServiceAccessPoint serviceAccessPoint : serviceAccessPoints) {
if(serviceAccessPoint.getEntryName().equals(entryName)) if(serviceAccessPoint.getEntryName().equals(entryName))
return serviceAccessPoint; return serviceAccessPoint;
@ -162,26 +157,40 @@ public class RuntimeResourceReader {
public String getEntryName() { public String getEntryName() {
return entryName; return entryName;
} }
/**
* @return the scope
*/
public String getScope() {
return scope;
}
/** /**
* Gets the service access points.
*
* @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");
// System.out.println(rr); // System.out.println(rr);
// //
// System.out.println(rr.getServiceAccessPointForEntryName("gis")); // System.out.println(rr.getServiceAccessPointForEntryName("gis"));
// } catch (Exception e) { // } catch (Exception e) {
// // TODO Auto-generated catch block // // TODO Auto-generated catch block

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 {
} }
} }
} }