From 841fd5c2a6f6bd148fb9e07e17acc047b50fb508 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Mon, 4 May 2015 13:10:14 +0000 Subject: [PATCH] Updated to support several Access Point for each Resolver Introduced Entry Names in Uri-Resolver-Map Updated pom version at 1.1.0 git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/uri-resolver-manager@114649 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 4 +- pom.xml | 10 +- .../UriResolverManager.java | 207 +++++++++++++++--- .../entity/ApplicationType.java | 66 ++++++ .../uriresolvermanager/entity/Resolver.java | 78 +++++++ .../entity/ServiceAccessPoint.java | 95 ++++++++ .../readers/RuntimeResourceReader.java | 145 +++++++----- .../readers/UriResolverMapReader.java | 111 ++++++---- src/test/java/UriResolverManagerMain.java | 41 ++++ src/test/java/UriResolverManagerTest.java | 30 ++- templates/changelog.xml | 4 + 11 files changed, 661 insertions(+), 130 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/ApplicationType.java create mode 100644 src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/Resolver.java create mode 100644 src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/ServiceAccessPoint.java create mode 100644 src/test/java/UriResolverManagerMain.java diff --git a/.classpath b/.classpath index 12b4479..187b5ad 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,6 @@ - + @@ -23,5 +23,5 @@ - + diff --git a/pom.xml b/pom.xml index e4bed75..9812622 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ 4.0.0 org.gcube.portlets.user uri-resolver-manager - 1.0.1-SNAPSHOT + 1.1.0-SNAPSHOT jar uri-resolver-manager The URI Resolver Manager @@ -117,16 +117,20 @@ 20090211 + log4j log4j - compile org.slf4j slf4j-log4j12 - + compile + + + org.slf4j + slf4j-api compile 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 cc87cd6..c87e2b3 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java @@ -10,6 +10,8 @@ import java.util.Timer; import java.util.TimerTask; import org.gcube.common.scope.api.ScopeProvider; +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.UriResolverMapException; @@ -20,10 +22,13 @@ import org.gcube.portlets.user.urlshortener.UrlShortener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + + /** - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Oct 14, 2014 + * The Class UriResolverManager. * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 4, 2015 */ public class UriResolverManager { @@ -39,7 +44,7 @@ public class UriResolverManager { public static int RESET_TIME = RESET_DELAY; //10 MINUTES private UriResolverMapReader uriResolverMapReader; - private Map applicationTypes; + private Map applicationTypes; private String scope; private String applicationType; private RuntimeResourceReader reader; @@ -48,29 +53,46 @@ public class UriResolverManager { * A lock to prevent reader = null; */ private int usingReader = 0; + + private ServiceAccessPoint serviceAccessPoint; + + private Timer timer; + /** + * Lock reader. + */ public synchronized void lockReader() { usingReader++; } + /** + * Release reader. + */ public synchronized void releaseReader() { usingReader--; } + /** + * Count readers. + * + * @return the int + */ public synchronized int countReaders() { return usingReader; } public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class); + /** - * Instance a UriResolverManager + * 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 - * @param applicationType a key Application Type {@link UriResolverManager#getApplicationTypes()} - * @throws Exception + * + * @throws UriResolverMapException the uri resolver map exception + * @throws IllegalArgumentException the illegal argument exception */ - public UriResolverManager(String applicationType) throws UriResolverMapException, IllegalArgumentException{ + public UriResolverManager() throws UriResolverMapException, IllegalArgumentException{ try { this.scope = ScopeProvider.instance.get(); @@ -80,35 +102,66 @@ public class UriResolverManager { this.uriResolverMapReader = new UriResolverMapReader(this.scope); this.applicationTypes = uriResolverMapReader.getApplicationTypes(); - + this.setTimerUriResolverReader(RESET_DELAY, RESET_TIME); } catch (Exception e) { throw new UriResolverMapException("Map Application Type - Resources not found in IS"); } + } + + /** + * 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 + * @throws IllegalArgumentException the illegal argument exception + */ + public UriResolverManager(String applicationType) throws UriResolverMapException, IllegalArgumentException{ + this(); if(!this.applicationTypes.containsKey(applicationType)){ throw new IllegalArgumentException("Application type '"+applicationType +"' not found in Application Types: "+getApplicationTypes()); } - this.applicationType = applicationType; - this.reloadRuntimeResourceParameter(RESET_DELAY, RESET_TIME); + } + + + + /** + * 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 + * @return the link + * @throws IllegalArgumentException the illegal argument exception + * @throws UriResolverMapException the uri resolver map exception + */ + public String getLink(String applicationType, Map parameters, boolean shortLink) throws IllegalArgumentException, UriResolverMapException{ + this.applicationType = applicationType; + return getLink(parameters, shortLink); } /** - * - * @param parameters the map of the parameters sent with HTTP request (link) generated + * 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 - * @return - * @throws IllegalArgumentException - * @throws UriResolverMapException + * @return the link + * @throws IllegalArgumentException the illegal argument exception + * @throws UriResolverMapException the uri resolver map exception */ public String getLink(Map parameters, boolean shortLink) throws IllegalArgumentException, UriResolverMapException{ - String resourceName = this.applicationTypes.get(applicationType); + 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 { @@ -117,10 +170,20 @@ public class UriResolverManager { if(reader==null){ logger.info("Runtime Resource Reader is null, istancing..."); ScopeProvider.instance.set(this.scope); - reader = new RuntimeResourceReader(this.scope, resourceName); + reader = new RuntimeResourceReader(this.scope, resolver.getResourceName()); } - List resourceParameters = reader.getServiceParameters(); + 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()); + serviceAccessPoint = reader.getServiceAccessPointForEntryName(resolver.getEntryName()); + if(serviceAccessPoint==null) + throw new UriResolverMapException("Entry Name "+resolver.getEntryName() +" not found in Resource name: "+resolver.getResourceName()); + } + + List resourceParameters = serviceAccessPoint.getServiceParameters(); //CHECK PARAMETERS for (ServiceParameter serviceParameter : resourceParameters) { @@ -131,7 +194,7 @@ public class UriResolverManager { } } - String baseURI = reader.getServiceBaseURI(); + String baseURI = serviceAccessPoint.getServiceUrl(); releaseReader(); @@ -150,16 +213,18 @@ public class UriResolverManager { } } } catch (IllegalArgumentException e){ + logger.error("Uri Resolver IllegalArgumentException: ", e); throw e; } catch (Exception e) { - logger.error("Uri Resolver error: ", e); + logger.error("Uri Resolver Exception: ", e); throw new UriResolverMapException("Uri Resolver error: " +e.getMessage()); } return link; } /** - * + * Gets the application types. + * * @return the Application Types available */ public Set getApplicationTypes(){ @@ -167,18 +232,72 @@ public class UriResolverManager { } /** - * - * @return a map Application Type - Resource Name + * Discovery service parameters. + * + * @param resolver the resolver + * @return the list + * @throws IllegalArgumentException the illegal argument exception + * @throws Exception the exception */ - public Map getCapabilities(){ - return this.applicationTypes; + public List 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()); + + 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!!"); + serviceAccessPoint = reader.getServiceAccessPoints().get(0); + }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()); + } + + return serviceAccessPoint.getServiceParameters(); + } catch (Exception e) { + logger.error("Uri Resolver error: ", e); + throw new UriResolverMapException("Uri Resolver error: " +e.getMessage()); + } } /** - * + * Gets the resolver. + * + * @param applicationType the application type + * @return the resolver */ - public void reloadRuntimeResourceParameter(long delay, long period) { - Timer timer = new Timer(true); + public Resolver getResolver(String applicationType){ + return this.applicationTypes.get(applicationType); + } + + /** + * Gets the capabilities. + * + * @return a map Application Type - Resolver + */ + public Map getCapabilities(){ + return this.applicationTypes; + } + + /** + * Sets the timer uri resolver reader. + * + * @param delay the delay + * @param period the period + */ + public void setTimerUriResolverReader(long delay, long period) { + + cancelTimerUriResolverReader(); + + timer = new Timer(true); timer.schedule(new TimerTask() { @Override @@ -194,5 +313,33 @@ public class UriResolverManager { } }, delay, period); } + + + /** + * Cancel timer uri resolver reader. + */ + public void cancelTimerUriResolverReader(){ + if(timer!=null) + timer.cancel(); + } + + + /** + * Invalid uri resolver reader. + */ + public void invalidUriResolverReader(){ + reader = null; + } + +// public static void main(String[] args) { +// try { +// String scope ="/gcube"; +// UriResolverMapReader resolver = new UriResolverMapReader(scope); +// System.out.println(resolver); +// } catch (Exception e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } } diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/ApplicationType.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/ApplicationType.java new file mode 100644 index 0000000..35e74e7 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/ApplicationType.java @@ -0,0 +1,66 @@ +/** + * + */ +package org.gcube.portlets.user.uriresolvermanager.entity; + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Apr 29, 2015 + */ +public class ApplicationType { + + String resourceName; + ServiceAccessPoint accessPoint; + + /** + * @param resourceName + * @param accessPoint + */ + public ApplicationType(String resourceName, ServiceAccessPoint accessPoint) { + this.resourceName = resourceName; + this.accessPoint = accessPoint; + } + + /** + * @return the resourceName + */ + public String getResourceName() { + return resourceName; + } + + /** + * @return the accessPoint + */ + public ServiceAccessPoint getAccessPoint() { + return accessPoint; + } + + /** + * @param resourceName the resourceName to set + */ + public void setResourceName(String resourceName) { + this.resourceName = resourceName; + } + + /** + * @param accessPoint the accessPoint to set + */ + public void setAccessPoint(ServiceAccessPoint accessPoint) { + this.accessPoint = accessPoint; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ApplicationType [resourceName="); + builder.append(resourceName); + builder.append(", accessPoint="); + builder.append(accessPoint); + 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 new file mode 100644 index 0000000..95d87dc --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/Resolver.java @@ -0,0 +1,78 @@ +/** + * + */ +package org.gcube.portlets.user.uriresolvermanager.entity; + + +/** + * The Class Resolver. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 4, 2015 + */ +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; + } + + /** + * 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; + } + /* (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/ServiceAccessPoint.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/ServiceAccessPoint.java new file mode 100644 index 0000000..1745119 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/entity/ServiceAccessPoint.java @@ -0,0 +1,95 @@ +/** + * + */ +package org.gcube.portlets.user.uriresolvermanager.entity; + +import java.util.List; + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Apr 29, 2015 + */ +public class ServiceAccessPoint { + + private String serviceUrl; + private String entryName; + private List serviceParameters; + + /** + * + */ + public ServiceAccessPoint() { + } + + /** + * @param entryName + * @param serviceUrl + * @param serviceParameters + */ + public ServiceAccessPoint(String entryName, String serviceUrl, + List serviceParameters) { + super(); + this.entryName = entryName; + this.serviceUrl = serviceUrl; + this.serviceParameters = serviceParameters; + } + + /** + * @return the entryName + */ + public String getEntryName() { + return entryName; + } + + /** + * @return the serviceUrl + */ + public String getServiceUrl() { + return serviceUrl; + } + + /** + * @return the serviceParameters + */ + public List getServiceParameters() { + return serviceParameters; + } + + /** + * @param entryName the entryName to set + */ + public void setEntryName(String entryName) { + this.entryName = entryName; + } + + /** + * @param serviceUrl the serviceUrl to set + */ + public void setServiceUrl(String serviceUrl) { + this.serviceUrl = serviceUrl; + } + + /** + * @param serviceParameters the serviceParameters to set + */ + public void setServiceParameters(List serviceParameters) { + this.serviceParameters = serviceParameters; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("AccessPoint [entryName="); + builder.append(entryName); + builder.append(", serviceUrl="); + builder.append(serviceUrl); + builder.append(", serviceParameters="); + builder.append(serviceParameters); + builder.append("]"); + return builder.toString(); + } +} 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 8e62cf2..459aa29 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 @@ -15,6 +15,7 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.Property; 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; @@ -22,39 +23,47 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Oct 7, 2014 + * The Class RuntimeResourceReader. * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Apr 30, 2015 */ public class RuntimeResourceReader { public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class); - public List serviceParameters; + public List serviceAccessPoints; private String resourceName; private String scope; - private String serviceBaseURI; + private String entryName; /** - * @throws Exception - * + * 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; this.resourceName = resourceName; readResource(scope, resourceName); } + /** - * - * @param scope + * Read resource. + * + * @param scope the scope + * @param resourceName the resource name * @return the application URI - * @throws Exception + * @throws Exception the exception */ - protected String readResource(String scope, String resourceName) throws Exception { + private void readResource(String scope, String resourceName) throws Exception { try{ logger.info("Tentative read resource: "+resourceName+", scope: "+scope); @@ -82,30 +91,33 @@ public class RuntimeResourceReader { Group accessPoints = se.profile().accessPoints(); if(accessPoints.size()==0) throw new Exception("Accesspoint in resource "+resourceName+" not found"); - AccessPoint ap = accessPoints.iterator().next(); + Iterator acIt = accessPoints.iterator(); + serviceAccessPoints = new ArrayList(accessPoints.size()); - - Group properties = ap.properties(); - - if(properties.size()==0){ - logger.warn("Properties in resource "+resourceName+" not found"); - }else{ - - serviceParameters = new ArrayList(properties.size()); + while(acIt.hasNext()){ - Iterator iter = properties.iterator(); + AccessPoint ap = acIt.next(); + + Group properties = ap.properties(); + + if(properties.size()==0){ + logger.warn("Properties in resource "+resourceName+" not found"); + }else{ - while (iter.hasNext()) { + List serviceParameters = new ArrayList(properties.size()); - Property prop = iter.next(); + Iterator iter = properties.iterator(); + + while (iter.hasNext()) { + + 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)); } } - - logger.info("returning URI: "+ap.address()); - this.serviceBaseURI = ap.address(); - return serviceBaseURI; // parameters.setUser(ap.username()); //username // // String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password()); @@ -114,46 +126,67 @@ public class RuntimeResourceReader { // Group properties = ap.properties(); }catch (Exception e) { - logger.error("Sorry, an error occurred on reading the resource "+resourceName+ "Runtime Reosurces",e); - throw new Exception("Sorry, an error occurred on reading the resource "+resourceName+ "Runtime Reosurces"); + logger.error("Sorry, an error occurred on reading the resource "+resourceName+ " Runtime Reosurce",e); + throw new Exception("Sorry, an error occurred on reading the resource "+resourceName+ " Runtime Reosurce"); } } + /** + * + * @param entryName + * @return + */ + public ServiceAccessPoint getServiceAccessPointForEntryName(String entryName){ + + for (ServiceAccessPoint serviceAccessPoint : serviceAccessPoints) { + if(serviceAccessPoint.getEntryName().equals(entryName)) + return serviceAccessPoint; + } + return null; + } + /** + * Gets the resource name. + * + * @return the resource name + */ public String getResourceName() { return resourceName; } - public List getServiceParameters() { - return serviceParameters; + /** + * Gets the entry name. + * + * @return the entryName + */ + public String getEntryName() { + return entryName; } - public String getServiceBaseURI() { - return serviceBaseURI; + /** + * @return the scope + */ + public String getScope() { + return scope; } - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("RuntimeResourceReader [serviceParameters="); - builder.append(serviceParameters); - builder.append(", resourceName="); - builder.append(resourceName); - builder.append(", scope="); - builder.append(scope); - builder.append(", serviceBaseURI="); - builder.append(serviceBaseURI); - builder.append("]"); - return builder.toString(); + + /** + * @return the serviceAccessPoints + */ + public List getServiceAccessPoints() { + return serviceAccessPoints; } - - /*public static void main(String[] args) { - try { - RuntimeResourceReader resolver = new RuntimeResourceReader("/gcube", "Gis-Resolver"); - System.out.println(resolver); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - }*/ + +// 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 +// e.printStackTrace(); +// } +// } } 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 9eb151a..1a86e42 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,6 +12,7 @@ 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; @@ -21,33 +22,33 @@ import org.slf4j.LoggerFactory; import org.w3c.dom.Node; import org.xml.sax.InputSource; + /** - * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Oct 13, 2014 + * The Class UriResolverMapReader. * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 4, 2015 */ public class UriResolverMapReader { /** * */ - private static final String URIRESOLVERMAP = "UriResolverMap"; - /** - * - */ - private static final String URI_RESOLVER_MAP = "Uri-Resolver-Map"; + public static final String URIRESOLVERMAP_SECONDARY_TYPE = "UriResolverMap"; + public static final String URI_RESOLVER_MAP_RESOURCE_NAME = "Uri-Resolver-Map"; + private Logger logger = LoggerFactory.getLogger(UriResolverMapReader.class); private String secondaryType; private String scope; private String resourceName; - - private Map applicationTypes; //A map ApplicationType - Resource Name + private Map applicationTypes; //A map ApplicationType - Resolver + /** - * + * Instantiates a new uri resolver map reader. + * * @param scope - the scope to be searched - * @throws Exception + * @throws Exception the exception */ public UriResolverMapReader(String scope) throws Exception { this.scope = scope; @@ -56,18 +57,18 @@ public class UriResolverMapReader { throw new Exception("Scope is null, set scope provider!"); } - this.resourceName = URI_RESOLVER_MAP; - this.secondaryType = URIRESOLVERMAP; + this.resourceName = URI_RESOLVER_MAP_RESOURCE_NAME; + this.secondaryType = URIRESOLVERMAP_SECONDARY_TYPE; 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} and secondaryType {@value #URIRESOLVERMAP} - * @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile + * resource name {@value #URI_RESOLVER_MAP_RESOURCE_NAME} and secondaryType {@value #URIRESOLVERMAP_SECONDARY_TYPE} + * * @return the applicationProfile profile - * @throws Exception + * @throws Exception the exception */ private void readProfileFromInfrastrucure() throws Exception { @@ -98,17 +99,18 @@ public class UriResolverMapReader { 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()); + applicationTypes = new HashMap(currValue.size()); // List appTypes = currValue; //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){ - applicationTypes.put(at, resources.get(0)); - logger.info("Stored: "+at +" -> Resource: "+ resources.get(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!"); } @@ -122,6 +124,13 @@ public class UriResolverMapReader { } + /** + * Gets the gcube generic query string. + * + * @param secondaryType the secondary type + * @param name the name + * @return the gcube generic query string + */ public static String getGcubeGenericQueryString(String secondaryType, String name){ return "for $profile in collection('/db/Profiles/GenericResource')//Resource " + @@ -131,26 +140,46 @@ public class UriResolverMapReader { } - - + /** + * Gets the application types. + * + * @return the applicationTypes + */ + public Map getApplicationTypes() { + return applicationTypes; + } + + /** + * Gets the secondary type. + * + * @return the secondary type + */ public String getSecondaryType() { return secondaryType; } + /** + * Gets the scope. + * + * @return the scope + */ public String getScope() { return scope; } - public Map getApplicationTypes() { - return applicationTypes; - } - - + /** + * Gets the resource name. + * + * @return the resource name + */ public String getResourceName() { return resourceName; } + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { StringBuilder builder = new StringBuilder(); @@ -166,14 +195,20 @@ public class UriResolverMapReader { return builder.toString(); } - /*public static void main(String[] args) { - - String scope ="/gcube"; - try { - UriResolverMapReader resolver = new UriResolverMapReader(scope); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - }*/ + +// /** +// * 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(); +// } +// } } diff --git a/src/test/java/UriResolverManagerMain.java b/src/test/java/UriResolverManagerMain.java new file mode 100644 index 0000000..0355920 --- /dev/null +++ b/src/test/java/UriResolverManagerMain.java @@ -0,0 +1,41 @@ +import java.util.HashMap; +import java.util.Map; + +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; + +/** + * + */ + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Oct 20, 2014 + * + */ +public class UriResolverManagerMain { + + public static void main(String[] args) { + try { + ScopeProvider.instance.set("/gcube/devsec/devVRE"); + UriResolverManager resolver = new UriResolverManager("GIS"); + System.out.println(resolver.getCapabilities()); + System.out.println(resolver.getApplicationTypes()); +// System.out.println(resolver.discoveryServiceParameters(resolver.getResolver("SMP-ID"))); + + Map params = new HashMap(); + params.put("gis-UUID", "5ac49f44-999f-4efe-a32b-af71da2b39ac"); + params.put("scope", "/gcube/devsec/devVRE"); + String shortLink = resolver.getLink(params, true); + System.out.println(shortLink); //true, link is shorted otherwise none + } catch (UriResolverMapException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + }catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/UriResolverManagerTest.java b/src/test/java/UriResolverManagerTest.java index d6546d3..3395f00 100644 --- a/src/test/java/UriResolverManagerTest.java +++ b/src/test/java/UriResolverManagerTest.java @@ -38,7 +38,7 @@ public class UriResolverManagerTest { } } - @Test +// @Test public void testSMP() { try { @@ -59,6 +59,32 @@ public class UriResolverManagerTest { } } + + /** + * Thread safe + */ +// @Test + public void testSMPID(){ + + try { + ScopeProvider.instance.set("/gcube/devsec/devVRE"); + UriResolverManager resolver; + resolver = new UriResolverManager("SMP-ID"); + Map params = new HashMap(); + params.put("smp-id","553f9265e4b0567b75021fce"); + params.put("fileName", "dog"); + params.put("contentType", "image/jpg"); + 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(); + } + } + /** * Thread safe */ @@ -109,5 +135,7 @@ public class UriResolverManagerTest { e.printStackTrace(); } } + + } diff --git a/templates/changelog.xml b/templates/changelog.xml index dce38e8..ce56883 100644 --- a/templates/changelog.xml +++ b/templates/changelog.xml @@ -1,4 +1,8 @@ + + Updated to support several Access Point for each Resolver + Introduced Entry Names in Uri-Resolver-Map + First Release