#22385 Integrated with Catalogue Resolver and SHUB Resolver

task_22385
Francesco Mangiacrapa 2 years ago
parent 3039279092
commit 4b11aebf62

@ -7,7 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Enhancement
* [22385] Integrated with Catalogue Resolver
* [22385] Integrated with Catalogue Resolver and SHUB Resolver
## [v1.4.2] - 2021-04-21

@ -37,9 +37,6 @@
<properties>
<distroDirectory>${project.basedir}/distro</distroDirectory>
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
<!-- Convenience property to set the GWT version -->
<gwtVersion>2.5.1</gwtVersion>
<!-- GWT needs at least java 1.5 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
@ -103,14 +100,14 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>provided</scope>
</dependency>

@ -30,16 +30,6 @@ import org.slf4j.LoggerFactory;
*/
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 applicationType;
@ -174,7 +164,7 @@ public class UriResolverManager {
throw new IllegalArgumentException("Application type is null");
Resolver resolver = this.applicationTypes.get(applicationType);
String link;
String link = null;
if (parameters == null)
throw new IllegalArgumentException("Input Map parameters is null");
@ -201,7 +191,7 @@ public class UriResolverManager {
}
List<ServiceParameter> resourceParameters = serviceAccessPoint.getServiceParameters();
LOG.debug("Service parameters are: " + resourceParameters);
// CHECK PARAMETERS
for (ServiceParameter serviceParameter : resourceParameters) {
if (serviceParameter.isMandatory()) {
@ -215,36 +205,44 @@ public class UriResolverManager {
String baseURI = serviceAccessPoint.getServiceUrl();
try {
return resolver.getLink(baseURI, parameters);
link = resolver.getLink(baseURI, parameters);
if (shortLink)
return shortTheLink(link);
return link;
} catch (NotImplementedException e) {
LOG.info("Get Link not implemented, going to default implementation by GET Request");
LOG.info("Specialized getLink not implemented, going to default implementation");
}
// Encoding only the query string
String linkDecoded = null;
String queryString = null;
if (!shortLink) {
// not shortening so returning the link with the query string with only the
// parameters encoded
LOG.info("getLink implemented via GET request and encoded query-string");
queryString = UrlEncoderUtil.encodeQuery(parameters);
String toReturn = String.format("%s?%s", baseURI, queryString);
LOG.info("returning link with encoded parameters in the query string: " + toReturn);
return toReturn;
}
// Not specialized getLink has been implemented
// Short link required
// Going to build the GET request with query string decoded
LOG.info("getLink implemented via GET request and (decoded) query-string");
queryString = UrlEncoderUtil.toQueryString(parameters);
linkDecoded = String.format("%s?%s", baseURI, queryString);
link = linkDecoded;
LOG.info("Created HTTP URI request (link): " + link);
LOG.info("Created HTTP link: " + link);
if (shortLink) {
try {
LOG.info("Short link requested, so encoding query string is required...");
String queryStringEncoded = UrlEncoderUtil.encodeString(queryString);
link = String.format("%s?%s", baseURI, queryStringEncoded);
LOG.info("Encoded link is: " + link);
LOG.info("Shortner start..");
UrlShortener shortener = new UrlShortener();
String shortedLink = shortener.shorten(link);
LOG.info("Shortner starts..");
String shortedLink = shortTheLink(link);
LOG.info("Shorted link is: " + shortedLink);
if (shortedLink != null && shortedLink.equals(link)) {
// here the short link and the input link are identical
@ -275,6 +273,20 @@ public class UriResolverManager {
return link;
}
private String shortTheLink(String link) {
String toReturnLink = link;
try {
UrlShortener shortener = new UrlShortener();
String shortedLink = shortener.shorten(link);
LOG.info("Shorted link is: " + shortedLink);
toReturnLink = shortedLink;
} catch (Exception e) {
LOG.warn("Returning source link, an error occurred during link shortening: ", e);
}
return toReturnLink;
}
/**
* Gets the application types.
*
@ -388,10 +400,4 @@ public class UriResolverManager {
reader = null;
}
/*
* public static void main(String[] args) { try { UriResolverManager manager =
* new UriResolverManager(); System.out.println(manager.getCapabilities());
* System.out.println(manager.getApplicationTypes()); } catch (Exception e) { //
* TODO Auto-generated catch block e.printStackTrace(); } }
*/
}

@ -22,12 +22,10 @@ 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
* Apr 30, 2015
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Apr 30, 2015
*/
public class RuntimeResourceReader {
@ -39,7 +37,6 @@ public class RuntimeResourceReader {
private String entryName;
/**
* Instantiates a new runtime resource reader.
*
@ -54,49 +51,53 @@ public class RuntimeResourceReader {
/**
* Read resource.
*
* @param scope the scope
* @param scope the scope
* @param resourceName the resource name
* @return the application URI
* @throws Exception the exception
*/
private void readResource(String resourceName) throws Exception {
try{
try {
String scope = ScopeProvider.instance.get();
logger.info("Trying to read resource: "+resourceName+", in the scope: "+scope);
logger.info("Trying to read resource: " + resourceName + ", in the scope: " + scope);
this.resourceName = resourceName;
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);
List<ServiceEndpoint> r = client.submit(query);
if (r == null || r.isEmpty()) throw new Exception("Cannot retrieve the runtime resource with name: "+resourceName +" in the scope: "+scope);
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){
String msg = "Runtime reosource with resource name: "+resourceName +" is null in the scope: "+scope;
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");
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()){
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{
if (properties.size() == 0) {
logger.warn("Properties in resource " + resourceName + " not found");
} else {
List<ServiceParameter> serviceParameters = new ArrayList<ServiceParameter>(properties.size());
@ -111,17 +112,19 @@ public class RuntimeResourceReader {
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 Resource",e);
throw new Exception("Sorry, an error occurred on reading the resource "+resourceName+ " Runtime Reosurce");
logger.debug("Found properties: " + serviceAccessPoints);
// 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 Resource", e);
throw new Exception(
"Sorry, an error occurred on reading the resource " + resourceName + " Runtime Reosurce");
}
}
@ -131,10 +134,10 @@ public class RuntimeResourceReader {
* @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) {
if(serviceAccessPoint.getEntryName().equals(entryName))
if (serviceAccessPoint.getEntryName().equals(entryName))
return serviceAccessPoint;
}
return null;
@ -167,7 +170,9 @@ public class RuntimeResourceReader {
return serviceAccessPoints;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
@ -184,8 +189,6 @@ public class RuntimeResourceReader {
return builder.toString();
}
// public static void main(String[] args) {
// try {
// RuntimeResourceReader rr = new RuntimeResourceReader("/gcube", "Gis-Resolver");

@ -14,7 +14,8 @@ import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.uriresolvermanager.entity.GenericResolver;
import org.gcube.portlets.user.uriresolvermanager.entity.Resolver;
import org.gcube.portlets.user.uriresolvermanager.resolvers.CatalogueResolverCaller;
import org.gcube.portlets.user.uriresolvermanager.resolvers.CatalogueResolverCallBuilder;
import org.gcube.portlets.user.uriresolvermanager.resolvers.SHUBResolverCallBuilder;
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;
@ -116,11 +117,13 @@ public class UriResolverMapReader {
String resoureName = resources.get(0);
String entryName = entryNames.get(0);
Resolver resolver;
if (entryName.equals("ctlg")) {
resolver = new CatalogueResolverCaller(resoureName, entryName);
} else
if (entryName.equalsIgnoreCase("ctlg")) {
resolver = new CatalogueResolverCallBuilder(resoureName, entryName);
} else if (entryName.equalsIgnoreCase("shub")) {
resolver = new SHUBResolverCallBuilder(resoureName, entryName);
} else {
resolver = new GenericResolver(resoureName, entryName);
}
applicationTypes.put(at, resolver);
logger.info("Stored: " + at + " -> Resolver: " + resolver);
} else

@ -15,39 +15,40 @@ import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class CatalogueResolverCaller.
* The Class CatalogueResolverCallBuilder.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Nov 5, 2021
* Nov 5, 2021
*/
public class CatalogueResolverCaller extends GenericResolver {
public class CatalogueResolverCallBuilder extends GenericResolver {
public static final Logger LOG = LoggerFactory.getLogger(CatalogueResolverCaller.class);
private static final int _60SEC = 60000;
public static final Logger LOG = LoggerFactory.getLogger(CatalogueResolverCallBuilder.class);
/**
* Instantiates a new catalogue resolver wrapper.
*
* @param resourceName the resource name
* @param entryName the entry name
* @param entryName the entry name
*/
public CatalogueResolverCaller(String resourceName, String entryName) {
public CatalogueResolverCallBuilder(String resourceName, String entryName) {
super(resourceName, entryName);
}
/**
* Gets the link.
*
* @param baseURI the base URI
* @param baseURI the base URI
* @param parameters the parameters
* @return the link
* @throws Exception the exception
*/
@Override
public String getLink(String baseURI, Map<String, String> parameters) throws Exception {
LOG.debug("called getLink: " + baseURI + " parameters: "+parameters);
HttpURLConnection con = null;
String theResponse = null;
try {
@ -56,19 +57,19 @@ public class CatalogueResolverCaller extends GenericResolver {
con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
//con.setRequestProperty("Accept", "application/json");
// con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
con.setReadTimeout(60000);
con.setConnectTimeout(60000);
con.setReadTimeout(_60SEC);
con.setConnectTimeout(_60SEC);
JSONObject jObj = new org.json.JSONObject();
for (String key : parameters.keySet()) {
jObj.put(key, parameters.get(key));
}
String toJSON = jObj.toString();
LOG.info("Submitting JSON: "+toJSON);
LOG.info("Submitting JSON: " + toJSON);
Integer code = null;
try {
@ -89,6 +90,7 @@ public class CatalogueResolverCaller extends GenericResolver {
}
} catch (Exception e) {
LOG.error(CatalogueResolverCallBuilder.class.getSimpleName() + " error: ", e);
throw e;
} finally {
try {

@ -0,0 +1,66 @@
package org.gcube.portlets.user.uriresolvermanager.resolvers;
import java.util.Map;
import org.gcube.portlets.user.uriresolvermanager.entity.GenericResolver;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class SHUBResolverCallBuilder.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Nov 8, 2021
*/
public class SHUBResolverCallBuilder extends GenericResolver {
public static final Logger LOG = LoggerFactory.getLogger(SHUBResolverCallBuilder.class);
/**
* Instantiates a new catalogue resolver wrapper.
*
* @param resourceName the resource name
* @param entryName the entry name
*/
public SHUBResolverCallBuilder(String resourceName, String entryName) {
super(resourceName, entryName);
}
/**
* Gets the link.
*
* @param baseURI the base URI
* @param parameters the parameters
* @return the link
* @throws Exception the exception
*/
@Override
public String getLink(String baseURI, Map<String, String> parameters) throws Exception {
LOG.debug("called getLink: " + baseURI + " parameters: "+parameters);
String toReturn = null;
try {
String idValue = parameters.get("id");
String pathURI = baseURI;
if (idValue != null) {
pathURI = String.format("%s/%s", pathURI, idValue);
parameters.remove("id");
}
toReturn = pathURI;
String queryString = UrlEncoderUtil.encodeQuery(parameters);
if(!queryString.isEmpty())
toReturn = String.format("%s?%s", pathURI, queryString);
} catch (Exception e) {
LOG.error(SHUBResolverCallBuilder.class.getSimpleName() + " error: ", e);
throw e;
}
LOG.info("Got Link: " + toReturn);
return toReturn;
}
}

@ -62,7 +62,7 @@ public class UrlEncoderUtil {
String encodedQuery = "";
if (theString == null || theString.isEmpty())
return theString;
return encodedQuery;
try {
encodedQuery = URLEncoder.encode(theString, charset);

@ -17,9 +17,9 @@ import org.junit.Test;
*
*/
public class UriResolverManagerTest {
//@Test
public void testUriResolverManger(){
// @Test
public void testUriResolverManger() {
UriResolverManager manager;
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
@ -35,29 +35,49 @@ public class UriResolverManagerTest {
}
}
//@Test
public void testCTLG(){
public void testCTLG() {
try {
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver;
resolver = new UriResolverManager("CTLG");
Map<String, String> params = new HashMap<String, String>();
params.put("gcube_scope","/gcube/devsec/devVRE");
params.put("entity_context","dataset");
params.put("entity_name","sarda-sarda");
String shortLink = resolver.getLink(params, true); //true, link is shorted otherwise none
params.put("gcube_scope", "/gcube/devsec/devVRE");
params.put("entity_context", "dataset");
params.put("entity_name", "sarda-sarda");
String shortLink = resolver.getLink(params, true); // true, link is shorted otherwise none
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
//@Test
public void testSHUB() {
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver;
resolver = new UriResolverManager("SHUB");
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1dac6703-8eb0-4838-83a8-5006f5074e9b");
params.put("content-disposition", "inline");
String shortLink = resolver.getLink(params, true); // true, link is shorted otherwise none
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//@Test
public void testGIS() {
@ -69,59 +89,59 @@ public class UriResolverManagerTest {
params.put("gis-UUID", "1a657005-29c6-4528-a115-69640c4c2900");
params.put("scope", "/pred4s/preprod/preVRE");
String shortLink = resolver.getLink(params, false);
System.out.println(shortLink); //true, link is shorted otherwise none
System.out.println(shortLink); // true, link is shorted otherwise none
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
// @Test
// @Test
public void testSMP() {
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver = new UriResolverManager("SMP");
Map<String, String> params = new HashMap<String, String>();
params.put("smp-uri","smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y");
params.put("smp-uri",
"smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y");
params.put("fileName", "wikipediaLogo");
params.put("contentType", "");
String shortLink = resolver.getLink(params, true); //true, link is shorted otherwise none
String shortLink = resolver.getLink(params, true); // true, link is shorted otherwise none
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Thread safe
*/
// @Test
public void testSMPID(){
// @Test
public void testSMPID() {
try {
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver;
resolver = new UriResolverManager("SMP-ID");
Map<String, String> params = new HashMap<String, String>();
params.put("geo-exp","553f9265e4b0567b75021fce");
params.put("geo-exp", "553f9265e4b0567b75021fce");
// params.put("fileName", "dog");
// params.put("contentType", "image/jpg");
String shortLink = resolver.getLink(params, true); //true, link is shorted otherwise none
String shortLink = resolver.getLink(params, true); // true, link is shorted otherwise none
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
@ -129,45 +149,45 @@ public class UriResolverManagerTest {
/**
* Thread safe
*/
//@Test
public void test2(){
// @Test
public void test2() {
//create thread to print counter value
Thread t = new Thread(new Runnable() {
// create thread to print counter value
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
@Override
public void run() {
while (true) {
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver;
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver;
resolver = new UriResolverManager("GIS");
Map<String, String> params = new HashMap<String, String>();
params.put("gis-UUID", "eb1a1b63-f324-47ee-9522-b8f5803e19ec");
params.put("scope", "/gcube/devsec/devVRE");
String shortLink = resolver.getLink(params, true);
System.out.println(shortLink); //true, link is shorted otherwise none
System.out.println("Thread "+Thread.currentThread().getId() +" reading counter is: " + resolver.countReaders());
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}catch (UriResolverMapException e) {
Map<String, String> params = new HashMap<String, String>();
params.put("gis-UUID", "eb1a1b63-f324-47ee-9522-b8f5803e19ec");
params.put("scope", "/gcube/devsec/devVRE");
String shortLink = resolver.getLink(params, true);
System.out.println(shortLink); // true, link is shorted otherwise none
System.out.println("Thread " + Thread.currentThread().getId() + " reading counter is: "
+ resolver.countReaders());
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (UriResolverMapException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
});
t.start();
t.start();
try {
Thread.sleep(1000);

Loading…
Cancel
Save