From 8d4b6d6c3f51425d729dc7dda7960506feb83695 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Fri, 5 Nov 2021 17:57:37 +0100 Subject: [PATCH] starting #22385 --- .classpath | 13 +- CHANGELOG.md | 5 + pom.xml | 14 +- .../UriResolverManager.java | 229 +++++++++--------- .../entity/GenericResolver.java | 101 ++++++++ .../uriresolvermanager/entity/Resolver.java | 80 ++---- .../exception/NotImplementedException.java | 32 +++ .../readers/RuntimeResourceReader.java | 2 +- .../readers/UriResolverMapReader.java | 132 +++++----- .../resolvers/CatalogueResolverCaller.java | 126 ++++++++++ .../resolvers/GisResolverManager.java | 22 -- .../resolvers/LinkResolver.java | 13 - src/test/java/UriResolverManagerTest.java | 28 ++- 13 files changed, 522 insertions(+), 275 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/GenericResolver.java create mode 100644 src/main/java/org/gcube/portlets/user/uriresolvermanager/exception/NotImplementedException.java create mode 100644 src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/CatalogueResolverCaller.java delete mode 100644 src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/GisResolverManager.java delete mode 100644 src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/LinkResolver.java diff --git a/.classpath b/.classpath index 466571e..d346edf 100644 --- a/.classpath +++ b/.classpath @@ -6,6 +6,12 @@ + + + + + + @@ -13,12 +19,6 @@ - - - - - - @@ -30,5 +30,6 @@ + diff --git a/CHANGELOG.md b/CHANGELOG.md index 885d3c5..3a62e5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v1.5.0-SNAPSHOT] - 2021-11-05 + +### Enhancement + +* [22385] Integrated with Catalogue Resolver ## [v1.4.2] - 2021-04-21 diff --git a/pom.xml b/pom.xml index ec8ad0c..486e84d 100644 --- a/pom.xml +++ b/pom.xml @@ -11,17 +11,17 @@ 4.0.0 org.gcube.portlets.user uri-resolver-manager - 1.4.2 + 1.5.0-SNAPSHOT jar uri-resolver-manager The URI Resolver Manager - + scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git https://code-repo.d4science.org/gCubeSystem/${project.artifactId} - + Francesco Mangiacrapa @@ -101,10 +101,16 @@ 20090211 - + + org.slf4j + slf4j-log4j12 + 1.6.4 + provided + org.slf4j slf4j-api + 1.6.4 provided diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java index 6a6740a..a1b4ec4 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java @@ -14,6 +14,7 @@ import org.gcube.portlets.user.uriresolvermanager.entity.Resolver; import org.gcube.portlets.user.uriresolvermanager.entity.ServiceAccessPoint; import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter; import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException; +import org.gcube.portlets.user.uriresolvermanager.exception.NotImplementedException; import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException; import org.gcube.portlets.user.uriresolvermanager.readers.RuntimeResourceReader; import org.gcube.portlets.user.uriresolvermanager.readers.UriResolverMapReader; @@ -25,20 +26,19 @@ import org.slf4j.LoggerFactory; /** * The Class UriResolverManager. * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Sep 6, 2016 + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Sep 6, 2016 */ public class UriResolverManager { /** * 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 */ - //public static int RESET_TIME = RESET_DELAY; //15 MINUTES + // public static int RESET_TIME = RESET_DELAY; //15 MINUTES private UriResolverMapReader uriResolverMapReader; private Map applicationTypes; @@ -79,32 +79,34 @@ public class UriResolverManager { public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class); - /** - * Instantiates a new uri resolver manager. - * 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 + * Instantiates a new uri resolver manager. 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 UriResolverMapException the uri resolver map exception * @throws IllegalArgumentException the illegal argument exception */ - public UriResolverManager() throws UriResolverMapException, IllegalArgumentException{ + public UriResolverManager() throws UriResolverMapException, IllegalArgumentException { try { String scope = ScopeProvider.instance.get(); - logger.info("UriResolverManager is using scope: "+scope+", read from ScopeProvider"); + logger.info("UriResolverManager is using scope: " + scope + ", read from ScopeProvider"); - if(scope == null) + if (scope == null) 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){ - logger.error("UriResolverMapException: ",e); + // this.setTimerUriResolverReader(RESET_DELAY, RESET_TIME); + } catch (UriResolverMapException e) { + logger.error("UriResolverMapException: ", e); throw e; } catch (Exception e) { - logger.error("UriResolverManager: ",e); + logger.error("UriResolverManager: ", e); throw new UriResolverMapException("Map Application Type - Resources not found in IS"); } } @@ -116,40 +118,42 @@ public class UriResolverManager { * @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()); + 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()} - * 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 + * Instance a UriResolverManager 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()} - * @throws UriResolverMapException the uri resolver map exception + * @param applicationType a (valid) key Application Type + * {@link UriResolverManager#getApplicationTypes()} + * @throws UriResolverMapException the uri resolver map exception * @throws IllegalArgumentException the illegal argument exception */ - public UriResolverManager(String applicationType) throws UriResolverMapException, IllegalArgumentException{ + public UriResolverManager(String applicationType) throws UriResolverMapException, IllegalArgumentException { this(); setApplicationType(applicationType); } - - /** * Gets the link. * * @param applicationType the application type - * @param parameters the map of the parameters sent as HTTP query string - * @param shortLink if true the link is shorted otherwise none + * @param parameters the map of the parameters sent as HTTP query string + * @param shortLink if true the link is shorted otherwise none * @return the link * @throws IllegalArgumentException the illegal argument exception - * @throws UriResolverMapException the uri resolver map exception + * @throws UriResolverMapException the uri resolver map exception */ - public String getLink(String applicationType, Map parameters, boolean shortLink) throws IllegalArgumentException, UriResolverMapException{ + public String getLink(String applicationType, Map parameters, boolean shortLink) + throws IllegalArgumentException, UriResolverMapException { this.applicationType = applicationType; return getLink(parameters, shortLink); } @@ -158,102 +162,114 @@ public class UriResolverManager { * Gets the link. * * @param parameters the map of the parameters sent as HTTP query string - * @param shortLink if true the link is shorted otherwise none + * @param shortLink if true the link is shorted otherwise none * @return the link * @throws IllegalArgumentException the illegal argument exception - * @throws UriResolverMapException the uri resolver map exception + * @throws UriResolverMapException the uri resolver map exception */ - public String getLink(Map parameters, boolean shortLink) throws IllegalArgumentException, UriResolverMapException{ + public String getLink(Map parameters, boolean shortLink) + throws IllegalArgumentException, UriResolverMapException { - if(applicationType==null) + if (applicationType == null) throw new IllegalArgumentException("Application type is null"); Resolver resolver = this.applicationTypes.get(applicationType); String link; - if(parameters==null) + if (parameters == null) throw new IllegalArgumentException("Input Map parameters is null"); try { - //lockReader(); + // lockReader(); - if(reader==null){ + if (reader == null) { logger.info("Runtime Resource Reader is null, istancing..."); 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!!"); + 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); - }else{ - logger.warn("Reading Access Point for Entry Name: "+resolver.getEntryName()); + } else { + logger.warn("Reading Access Point for Entry Name: " + resolver.getEntryName()); serviceAccessPoint = reader.getServiceAccessPointForEntryName(resolver.getEntryName()); - if(serviceAccessPoint==null) - throw new UriResolverMapException("Entry Name "+resolver.getEntryName() +" not found in Resource name: "+resolver.getResourceName()); + if (serviceAccessPoint == null) + throw new UriResolverMapException("Entry Name " + resolver.getEntryName() + + " not found in Resource name: " + resolver.getResourceName()); } List resourceParameters = serviceAccessPoint.getServiceParameters(); - //CHECK PARAMETERS + // CHECK PARAMETERS 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"); + 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(); - //Encoding only the query string - + + try { + return resolver.getLink(baseURI, parameters); + } catch (NotImplementedException e) { + logger.info("Get Link not implemented, going to default implementation by GET Request"); + } + + // 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 + if (!shortLink) { + // not shortening so returning the link with the query string with only the + // parameters encoded queryString = UrlEncoderUtil.encodeQuery(parameters); - String toReturn = String.format("%s?%s", baseURI,queryString); - logger.info("returning link with encoded parameters in the query string: "+toReturn); + String toReturn = String.format("%s?%s", baseURI, queryString); + logger.info("returning link with encoded parameters in the query string: " + toReturn); return toReturn; } - + queryString = UrlEncoderUtil.toQueryString(parameters); - linkDecoded = String.format("%s?%s", baseURI,queryString); + linkDecoded = String.format("%s?%s", baseURI, queryString); link = linkDecoded; - logger.info("Created HTTP URI request (link): "+link); - if(shortLink){ - try{ + logger.info("Created HTTP URI request (link): " + link); + if (shortLink) { + try { String queryStringEncoded = UrlEncoderUtil.encodeString(queryString); - link = String.format("%s?%s", baseURI,queryStringEncoded); - logger.info("Encoded link is: "+link); + link = String.format("%s?%s", baseURI, queryStringEncoded); + logger.info("Encoded link is: " + link); logger.info("Shortner start.."); UrlShortener shortener = new UrlShortener(); String shortedLink = shortener.shorten(link); - logger.info("Shorted link is: "+shortedLink); - if(shortedLink!=null && shortedLink.equals(link)) { - //here the short link and the input link are identical - //so the shortening did not work - //I'm returning the decoded link because it is directly consumable via browser - logger.debug("Shorted link is equal to input link, returning decoded link: "+linkDecoded); + logger.info("Shorted link is: " + shortedLink); + if (shortedLink != null && shortedLink.equals(link)) { + // here the short link and the input link are identical + // so the shortening did not work + // I'm returning the decoded link because it is directly consumable via browser + logger.debug("Shorted link is equal to input link, returning decoded link: " + linkDecoded); link = linkDecoded; - }else { - //here the link is really shorted + } else { + // here the link is really shorted logger.debug("The link is really shorted, returning it"); link = shortedLink; } - - }catch(Exception e){ - logger.warn("An error occurred during link shortening: ",e); - //here I'm returning the decoded link in case of error on shortening it + + } catch (Exception e) { + logger.warn("An error occurred during link shortening: ", e); + // here I'm returning the decoded link in case of error on shortening it link = linkDecoded; } } - } catch (IllegalArgumentException e){ + } catch (IllegalArgumentException e) { logger.error("Uri Resolver IllegalArgumentException: ", e); throw e; } catch (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; @@ -264,7 +280,7 @@ public class UriResolverManager { * * @return the Application Types available */ - public Set getApplicationTypes(){ + public Set getApplicationTypes() { return this.applicationTypes.keySet(); } @@ -274,37 +290,40 @@ public class UriResolverManager { * @param resolver the resolver * @return the list * @throws IllegalArgumentException the illegal argument exception - * @throws Exception the exception + * @throws Exception the exception */ - public List discoveryServiceParameters(Resolver resolver) throws IllegalArgumentException, Exception{ + public List discoveryServiceParameters(Resolver resolver) + throws IllegalArgumentException, Exception { try { String scope = ScopeProvider.instance.get(); - logger.info("SiscoveryServiceParameters is using scope: "+scope+", read from ScopeProvider"); + logger.info("SiscoveryServiceParameters is using scope: " + scope + ", read from ScopeProvider"); - if(scope == null) + if (scope == null) throw new UriResolverMapException("Scope is null, set scope into ScopeProvider!"); - if(resolver == null) + if (resolver == null) throw new IllegalArgumentException("Resolver is null, set Resolver"); 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!!"); + 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); - }else{ - logger.info("Reading Access Point for entryname: "+resolver.getEntryName()); + } else { + logger.info("Reading Access Point for entryname: " + resolver.getEntryName()); serviceAccessPoint = reader.getServiceAccessPointForEntryName(resolver.getEntryName()); - if(serviceAccessPoint==null) - throw new UriResolverMapException("Entry Name "+resolver.getEntryName() +" not found in Resource name: "+resolver.getResourceName()); + 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()); + throw new UriResolverMapException("Uri Resolver error: " + e.getMessage()); } } @@ -314,7 +333,7 @@ public class UriResolverManager { * @param applicationType the application type * @return the resolver */ - public Resolver getResolver(String applicationType){ + public Resolver getResolver(String applicationType) { return this.applicationTypes.get(applicationType); } @@ -323,14 +342,14 @@ public class UriResolverManager { * * @return a map Application Type - Resolver */ - public Map getCapabilities(){ + public Map getCapabilities() { return this.applicationTypes; } /** * Sets the timer uri resolver reader. * - * @param delay the delay + * @param delay the delay * @param period the period */ public void setTimerUriResolverReader(long delay, long period) { @@ -344,43 +363,35 @@ public class UriResolverManager { public void run() { logger.info("Timer Reset Runtime Resource running.."); int counters = countReaders(); - if(counters==0){ + if (counters == 0) { logger.info("Reader not locked, resetting"); reader = null; - }else - logger.info("Reader locked, counters is/are:"+counters+", skipping"); + } else + logger.info("Reader locked, counters is/are:" + counters + ", skipping"); } }, delay, period); } - - /** * Cancel timer uri resolver reader. */ - public void cancelTimerUriResolverReader(){ - if(timer!=null) + public void cancelTimerUriResolverReader() { + if (timer != null) timer.cancel(); } - /** * Invalid uri resolver reader. */ - public void invalidUriResolverReader(){ + public void invalidUriResolverReader() { 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(); - } - }*/ + * 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(); } } + */ } diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/GenericResolver.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/GenericResolver.java new file mode 100644 index 0000000..6487929 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/GenericResolver.java @@ -0,0 +1,101 @@ +/** + * + */ +package org.gcube.portlets.user.uriresolvermanager.entity; + +import java.util.Map; + +import org.gcube.portlets.user.uriresolvermanager.exception.NotImplementedException; + +/** + * The Class Resolver. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it May 4, 2015 + */ +public class GenericResolver implements Resolver { + + private String resourceName; + private String entryName; + + /** + * Instantiates a new resolver. + * + * @param resourceName the resource name + * @param entryName the entry name + */ + public GenericResolver(String resourceName, String entryName) { + super(); + this.resourceName = resourceName; + this.entryName = entryName; + } + + /** + * Gets the resource name. + * + * @return the resourceName + */ + public String getResourceName() { + return resourceName; + } + + /** + * Gets the entry name. + * + * @return the entryName + */ + public String getEntryName() { + return entryName; + } + + /** + * Sets the resource name. + * + * @param resourceName the resourceName to set + */ + public void setResourceName(String resourceName) { + this.resourceName = resourceName; + } + + /** + * Sets the entry name. + * + * @param entryName the entryName to set + */ + public void setEntryName(String entryName) { + this.entryName = entryName; + } + + + /** + * Gets the link. + * + * @param baseURI the base URI + * @param parameters the parameters + * @return the link + * @throws Exception the exception + */ + public String getLink(String baseURI, Map parameters) throws Exception{ + throw new NotImplementedException("getLink Method not Implement"); + } + + /** + * To string. + * + * @return the string + */ + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Resolver [resourceName="); + builder.append(resourceName); + builder.append(", entryName="); + builder.append(entryName); + builder.append("]"); + return builder.toString(); + } +} diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/Resolver.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/Resolver.java index 95d87dc..9dfc8b7 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/Resolver.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/Resolver.java @@ -1,78 +1,38 @@ -/** - * - */ package org.gcube.portlets.user.uriresolvermanager.entity; +import java.util.Map; /** - * The Class Resolver. + * The Interface Resolver. * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * May 4, 2015 + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Nov 5, 2021 */ -public class Resolver { - - private String resourceName; - private String entryName; - - /** - * Instantiates a new resolver. - * - * @param resourceName the resource name - * @param entryName the entry name - */ - public Resolver(String resourceName, String entryName) { - super(); - this.resourceName = resourceName; - this.entryName = entryName; - } - +public interface Resolver { + /** * Gets the resource name. * - * @return the resourceName + * @return the resource name */ - public String getResourceName() { - return resourceName; - } - + public String getResourceName(); + /** * Gets the entry name. * - * @return the entryName + * @return the entry name */ - public String getEntryName() { - return entryName; - } - + public String getEntryName(); + /** - * Sets the resource name. + * Gets the link. * - * @param resourceName the resourceName to set + * @param baseURI the base URI + * @param parameters the parameters + * @return the link + * @throws Exception the exception */ - public void setResourceName(String resourceName) { - this.resourceName = resourceName; - } - - /** - * Sets the entry name. - * - * @param entryName the entryName to set - */ - public void setEntryName(String entryName) { - this.entryName = entryName; - } - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Resolver [resourceName="); - builder.append(resourceName); - builder.append(", entryName="); - builder.append(entryName); - builder.append("]"); - return builder.toString(); - } + public String getLink(String baseURI, Map parameters) throws Exception; + } diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/exception/NotImplementedException.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/exception/NotImplementedException.java new file mode 100644 index 0000000..fc1fb7a --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/exception/NotImplementedException.java @@ -0,0 +1,32 @@ +package org.gcube.portlets.user.uriresolvermanager.exception; + +/** + * The Class NotImplementedException. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Nov 5, 2021 + */ +public class NotImplementedException extends Exception { + + /** + * + */ + private static final long serialVersionUID = -7350078577553202213L; + + /** + * Instantiates a new not implemented exception. + */ + public NotImplementedException() { + } + + /** + * Instantiates a new not implemented exception. + * + * @param message the message + */ + public NotImplementedException(String message) { + super(message); + } + +} diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/readers/RuntimeResourceReader.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/readers/RuntimeResourceReader.java index f90013d..3a22695 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/readers/RuntimeResourceReader.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/readers/RuntimeResourceReader.java @@ -63,7 +63,7 @@ public class RuntimeResourceReader { try{ String scope = ScopeProvider.instance.get(); - logger.info("Tentative read resource: "+resourceName+", scope: "+scope); + logger.info("Trying to read resource: "+resourceName+", in the scope: "+scope); this.resourceName = resourceName; diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/readers/UriResolverMapReader.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/readers/UriResolverMapReader.java index 3bdd0cd..42ead25 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/readers/UriResolverMapReader.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/readers/UriResolverMapReader.java @@ -12,7 +12,9 @@ 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.GenericResolver; import org.gcube.portlets.user.uriresolvermanager.entity.Resolver; +import org.gcube.portlets.user.uriresolvermanager.resolvers.CatalogueResolverCaller; 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; @@ -21,12 +23,10 @@ import org.slf4j.LoggerFactory; import org.w3c.dom.Node; import org.xml.sax.InputSource; - /** * The Class UriResolverMapReader. * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * May 4, 2015 + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it May 4, 2015 */ public class UriResolverMapReader { @@ -37,13 +37,13 @@ public class UriResolverMapReader { public static final String URI_RESOLVER_MAP_RESOURCE_NAME = "Uri-Resolver-Map"; // private Logger logger = LoggerFactory.getLogger(UriResolverMapReader.class); - //TODO TEMP SOLUTION IN ORDER TO PRINT USING ALSO LOG4J INTO GEOEXPLORER PORTLET + // TODO TEMP SOLUTION IN ORDER TO PRINT USING ALSO LOG4J INTO GEOEXPLORER + // PORTLET private Logger logger = LoggerFactory.getLogger(UriResolverMapReader.class); private String secondaryType; private String scope; private String resourceName; - private Map applicationTypes; //A map ApplicationType - Resolver - + private Map applicationTypes; // A map ApplicationType - Resolver /** * Instantiates a new uri resolver map reader. @@ -57,67 +57,83 @@ public class UriResolverMapReader { readProfileFromInfrastrucure(); } - /** - * this method looks up the generic resource among the ones available in the infrastructure using scope provider {@link ScopeProvider.instance.get()} - * resource name {@value #URI_RESOLVER_MAP_RESOURCE_NAME} and secondaryType {@value #URIRESOLVERMAP_SECONDARY_TYPE} + * this method looks up the generic resource among the ones available in the + * infrastructure using scope provider {@link ScopeProvider.instance.get()} + * resource name {@value #URI_RESOLVER_MAP_RESOURCE_NAME} and secondaryType + * {@value #URIRESOLVERMAP_SECONDARY_TYPE} * * @return the applicationProfile profile * @throws Exception the exception */ private void readProfileFromInfrastrucure() throws Exception { - String queryString = getGcubeGenericQueryString(secondaryType, resourceName); + String queryString = getGcubeGenericQueryString(secondaryType, resourceName); - 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("Trying to fetch in the scope: " + ScopeProvider.instance.get() + " the Generic Resouce with name: " + + resourceName + " secondary type: " + secondaryType); + logger.info(queryString); + try { - Query q = new QueryBox(queryString); - logger.debug("new query box works"); + Query q = new QueryBox(queryString); + logger.debug("new query box works"); - DiscoveryClient client = client(); - logger.info("submitting query is: "+queryString); - List appUriResolverMap = client.submit(q); - logger.debug("submit query works"); + DiscoveryClient client = client(); + logger.info("submitting query is: " + queryString); + List appUriResolverMap = client.submit(q); + logger.debug("submit query works"); - if (appUriResolverMap == null || appUriResolverMap.size() == 0){ - 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 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.debug("Building new XPathHelper.."); - XPathHelper helper = new XPathHelper(node); - List currValue = null; - 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()); - applicationTypes = new HashMap(currValue.size()); + if (appUriResolverMap == null || appUriResolverMap.size() == 0) { + 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 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.debug("Building new XPathHelper.."); + XPathHelper helper = new XPathHelper(node); + List currValue = null; + 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()); + applicationTypes = new HashMap(currValue.size()); // List appTypes = currValue; - //FOR EACH APPLICATION TYPE - for (String at : currValue) { - logger.info("Application Type "+at); + // FOR EACH APPLICATION TYPE + for (String at : currValue) { + logger.info("Application Type " + at); // currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Scope/text()"); - List resources = helper.evaluate("/Resource/Profile/Body/access_point[application_type='"+at+"']/resource/text()"); - List entryNames = helper.evaluate("/Resource/Profile/Body/access_point[application_type='"+at+"']/entryname/text()"); - if(resources!=null && resources.size()>0){ - Resolver resolver = new Resolver(resources.get(0), entryNames.get(0)); - applicationTypes.put(at, resolver); - logger.info("Stored: "+at +" -> Resolver: "+ resolver); - }else - logger.warn("Skipping Type "+at+" mapping to runtime resource not found!"); - } + List resources = helper.evaluate( + "/Resource/Profile/Body/access_point[application_type='" + at + "']/resource/text()"); + List entryNames = helper.evaluate( + "/Resource/Profile/Body/access_point[application_type='" + at + "']/entryname/text()"); + if (resources != null && resources.size() > 0) { + String resoureName = resources.get(0); + String entryName = entryNames.get(0); + Resolver resolver; + if (entryName.equals("ctlg")) { + resolver = new CatalogueResolverCaller(resoureName, entryName); + } else + resolver = new GenericResolver(resoureName, entryName); + + applicationTypes.put(at, resolver); + logger.info("Stored: " + at + " -> Resolver: " + resolver); + } else + logger.warn("Skipping Type " + at + " mapping to runtime resource not found!"); } + } } } catch (Exception 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 Generic Resourc with secondaryType: "+secondaryType+" and name "+resourceName+" from the infrastructure"); + 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"); } } @@ -126,15 +142,14 @@ public class UriResolverMapReader { * Gets the gcube generic query string. * * @param secondaryType the secondary type - * @param name the name + * @param name the name * @return the gcube generic query string */ - public static String getGcubeGenericQueryString(String secondaryType, String name){ + public static String getGcubeGenericQueryString(String secondaryType, String name) { - return "for $profile in collection('/db/Profiles/GenericResource')//Resource " + - "where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"' and $profile/Profile/Name/string() " + - " eq '" + name + "'" + - "return $profile"; + return "for $profile in collection('/db/Profiles/GenericResource')//Resource " + + "where $profile/Profile/SecondaryType/string() eq '" + secondaryType + + "' and $profile/Profile/Name/string() " + " eq '" + name + "'" + "return $profile"; } @@ -156,7 +171,6 @@ public class UriResolverMapReader { return secondaryType; } - /** * Gets the scope. * @@ -175,7 +189,9 @@ public class UriResolverMapReader { return resourceName; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see java.lang.Object#toString() */ @Override diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/CatalogueResolverCaller.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/CatalogueResolverCaller.java new file mode 100644 index 0000000..6078380 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/CatalogueResolverCaller.java @@ -0,0 +1,126 @@ +package org.gcube.portlets.user.uriresolvermanager.resolvers; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Map; + +import org.gcube.portlets.user.uriresolvermanager.entity.GenericResolver; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class CatalogueResolverCaller. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Nov 5, 2021 + */ +public class CatalogueResolverCaller extends GenericResolver { + + public static final Logger LOG = LoggerFactory.getLogger(CatalogueResolverCaller.class); + + /** + * Instantiates a new catalogue resolver wrapper. + * + * @param resourceName the resource name + * @param entryName the entry name + */ + public CatalogueResolverCaller(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 parameters) throws Exception { + + HttpURLConnection con = null; + String theResponse = null; + try { + + URL urlObj = new URL(baseURI); + con = (HttpURLConnection) urlObj.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + //con.setRequestProperty("Accept", "application/json"); + + con.setDoOutput(true); + con.setReadTimeout(60000); + con.setConnectTimeout(60000); + + 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); + Integer code = null; + try { + + OutputStream os = con.getOutputStream(); + os.write(toJSON.getBytes("UTF-8")); + os.close(); + + code = con.getResponseCode(); + theResponse = readResponse(con.getInputStream()); + + if (!((200 <= code) && (code <= 208))) { + throw new Exception("CatalogueResolver returned code: " + code + ". Response is: " + theResponse); + } + } catch (IOException e) { + theResponse = readResponse(con.getInputStream()); + LOG.error("CatalogueResolver returned code: " + code + ". Response is: " + theResponse); + throw e; + } + + } catch (Exception e) { + throw e; + } finally { + try { + if (con != null) + con.disconnect(); + } catch (Exception e) { + // silent + } + } + LOG.info("Got Link: " + theResponse); + return theResponse; + + } + + /** + * Read response. + * + * @param ris the ris + * @return the string + * @throws IOException Signals that an I/O exception has occurred. + */ + private String readResponse(InputStream ris) throws IOException { + // Receive the response from the server + InputStream in = new BufferedInputStream(ris); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + } + + return result.toString(); + } + +} diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/GisResolverManager.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/GisResolverManager.java deleted file mode 100644 index 0884ea3..0000000 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/GisResolverManager.java +++ /dev/null @@ -1,22 +0,0 @@ -/** - * - */ -package org.gcube.portlets.user.uriresolvermanager.resolvers; - - -/** - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Oct 14, 2014 - * - */ -public class GisResolverManager implements LinkResolver{ - - /* (non-Javadoc) - * @see org.gcube.portlets.user.uriresolvermanager.LinkResolver#getLink() - */ - public String getLink() { - // TODO Auto-generated method stub - return null; - } - -} diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/LinkResolver.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/LinkResolver.java deleted file mode 100644 index 11523ba..0000000 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/resolvers/LinkResolver.java +++ /dev/null @@ -1,13 +0,0 @@ -/** - * - */ -package org.gcube.portlets.user.uriresolvermanager.resolvers; - -/** - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Oct 14, 2014 - * - */ -public interface LinkResolver { - String getLink(); -} diff --git a/src/test/java/UriResolverManagerTest.java b/src/test/java/UriResolverManagerTest.java index 901a7f1..62795a3 100644 --- a/src/test/java/UriResolverManagerTest.java +++ b/src/test/java/UriResolverManagerTest.java @@ -5,6 +5,7 @@ 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; /** * @@ -16,12 +17,12 @@ import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapExcept * */ public class UriResolverManagerTest { - + //@Test public void testUriResolverManger(){ UriResolverManager manager; try { - ScopeProvider.instance.set("/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab"); + ScopeProvider.instance.set("/gcube/devsec/devVRE"); manager = new UriResolverManager(); System.out.println(manager.getCapabilities()); System.out.println(manager.getApplicationTypes()); @@ -34,6 +35,29 @@ public class UriResolverManagerTest { } } + + //@Test + public void testCTLG(){ + + try { + ScopeProvider.instance.set("/gcube/devsec/devVRE"); + UriResolverManager resolver; + resolver = new UriResolverManager("CTLG"); + Map params = new HashMap(); + 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) { + e.printStackTrace(); + } + } + //@Test public void testGIS() {