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"?>
<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>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
@ -23,5 +23,5 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</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>

View File

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

View File

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

View File

@ -1,8 +1,20 @@
package org.gcube.portlets.user.uriresolvermanager.readers;
/**
* The Class ApplicationProfileException.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 6, 2016
*/
@SuppressWarnings("serial")
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);
}
}

View File

@ -1,8 +1,20 @@
package org.gcube.portlets.user.uriresolvermanager.readers;
/**
* The Class RuntimeResourceException.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Sep 6, 2016
*/
@SuppressWarnings("serial")
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);
}
}

View File

@ -1,5 +1,5 @@
/**
*
*
*/
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.portlets.user.uriresolvermanager.entity.ServiceAccessPoint;
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.queries.api.SimpleQuery;
import org.slf4j.Logger;
@ -31,30 +30,27 @@ import org.slf4j.LoggerFactory;
* Apr 30, 2015
*/
public class RuntimeResourceReader {
public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class);
public List<ServiceAccessPoint> serviceAccessPoints;
private String resourceName;
private String scope;
private String entryName;
/**
* Instantiates a new runtime resource reader.
*
* @param scope the scope
* @param resourceName the resource name
* @throws Exception the exception
*/
public RuntimeResourceReader(String scope, String resourceName) throws Exception {
this.scope = scope;
public RuntimeResourceReader(String resourceName) throws Exception {
this.resourceName = resourceName;
readResource(scope, resourceName);
readResource(resourceName);
}
/**
* Read resource.
*
@ -63,81 +59,80 @@ public class RuntimeResourceReader {
* @return the application URI
* @throws Exception the exception
*/
private void readResource(String scope, String resourceName) throws Exception {
private void readResource(String resourceName) throws Exception {
try{
String scope = ScopeProvider.instance.get();
logger.info("Tentative read resource: "+resourceName+", scope: "+scope);
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);
query.addCondition("$resource/Profile/Name/string() eq '"+resourceName+"'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
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);
if(se.profile()==null)
throw new Exception("IS profile is null for resource: "+resourceName);
if(se.profile()==null){
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();
if(accessPoints.size()==0) throw new Exception("Accesspoint in resource "+resourceName+" not found");
Iterator<AccessPoint> acIt = accessPoints.iterator();
serviceAccessPoints = new ArrayList<ServiceAccessPoint>(accessPoints.size());
while(acIt.hasNext()){
AccessPoint ap = acIt.next();
Group<Property> properties = ap.properties();
if(properties.size()==0){
logger.warn("Properties in resource "+resourceName+" not found");
}else{
List<ServiceParameter> serviceParameters = new ArrayList<ServiceParameter>(properties.size());
Iterator<Property> iter = properties.iterator();
while (iter.hasNext()) {
Property prop = iter.next();
serviceParameters.add(new ServiceParameter(prop.value(), true));
}
serviceAccessPoints.add(new ServiceAccessPoint(ap.name(), ap.address(), serviceParameters));
}
}
// parameters.setUser(ap.username()); //username
//
//
// String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password());
//
//
// parameters.setPassword(decryptedPassword); //password
// Group<Property> properties = ap.properties();
}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");
}
}
/**
*
* @param entryName
* @return
* Gets the service access point for entry name.
*
* @param entryName the entry name
* @return the service access point for entry name
*/
public ServiceAccessPoint getServiceAccessPointForEntryName(String entryName){
for (ServiceAccessPoint serviceAccessPoint : serviceAccessPoints) {
if(serviceAccessPoint.getEntryName().equals(entryName))
return serviceAccessPoint;
@ -162,26 +157,40 @@ public class RuntimeResourceReader {
public String getEntryName() {
return entryName;
}
/**
* @return the scope
*/
public String getScope() {
return scope;
}
/**
* Gets the service access points.
*
* @return the serviceAccessPoints
*/
public List<ServiceAccessPoint> getServiceAccessPoints() {
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) {
// try {
// RuntimeResourceReader rr = new RuntimeResourceReader("/gcube", "Gis-Resolver");
// System.out.println(rr);
//
//
// System.out.println(rr.getServiceAccessPointForEntryName("gis"));
// } catch (Exception e) {
// // 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.scope.api.ScopeProvider;
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.queries.api.Query;
import org.gcube.resources.discovery.client.queries.impl.QueryBox;
@ -49,15 +48,9 @@ public class UriResolverMapReader {
/**
* Instantiates a new uri resolver map reader.
*
* @param scope - the scope to be searched
* @throws Exception the exception
*/
public UriResolverMapReader(String scope) throws Exception {
this.scope = scope;
if(scope==null){
throw new Exception("Scope is null, set scope provider!");
}
public UriResolverMapReader() throws Exception {
this.resourceName = URI_RESOLVER_MAP_RESOURCE_NAME;
this.secondaryType = URIRESOLVERMAP_SECONDARY_TYPE;
@ -76,37 +69,31 @@ public class UriResolverMapReader {
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);
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);
logger.info("new query box works");
logger.debug("new query box works");
DiscoveryClient<String> client = client();
logger.info("submitting query is: "+queryString);
List<String> appUriResolverMap = client.submit(q);
logger.info("submit query works");
logger.debug("submit query works");
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());
throw new ApplicationProfileException("Your applicationProfile with secondaryType: "+secondaryType+" and name "+resourceName+" is not registered in the infrastructure");
logger.error("ApplicationProfile with secondaryType: "+secondaryType+" and name: "+resourceName+" is not registered in the infrastructure, scope: "+ScopeProvider.instance.get());
throw new ApplicationProfileException("ApplicationProfile with secondaryType: "+secondaryType+" and name: "+resourceName+" is not registered in the scope: "+ScopeProvider.instance.get());
}else {
logger.info("Building new DocumentBuilder..");
logger.info("Building map applications type - resource");
logger.debug("Building new DocumentBuilder..");
String elem = appUriResolverMap.get(0);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
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);
List<String> currValue = null;
logger.info("Evaluating XPath..");
logger.debug("Evaluating XPath..");
currValue = helper.evaluate("/Resource/Profile/Body/access_point/application_type/text()");
if (currValue != null && currValue.size() > 0) {
logger.info("Application Types are: "+currValue.size());
@ -129,11 +116,8 @@ public class UriResolverMapReader {
}
} catch (Exception e) {
logger.error("Error while trying to fetch applicationProfile 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");
}finally{
ScopeProvider.instance.reset();
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 Generic Resourc with secondaryType: "+secondaryType+" and name "+resourceName+" from the infrastructure");
}
}
@ -208,21 +192,4 @@ public class UriResolverMapReader {
builder.append("]");
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.exception.IllegalArgumentException;
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException;
import org.junit.Test;
/**
*
@ -18,7 +17,7 @@ import org.junit.Test;
*/
public class UriResolverManagerTest {
@Test
// @Test
public void testUriResolverManger(){
UriResolverManager manager;
try {
@ -154,6 +153,4 @@ public class UriResolverManagerTest {
}
}
}