diff --git a/pom.xml b/pom.xml index 4b4b461..031ef7b 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.spatial.data sdi-service - 0.0.1-SNAPSHOT + 1.0.0-SNAPSHOT SDI Service REST Interface towards SDI facilities war diff --git a/src/main/java/org/gcube/spatial/data/sdi/LocalConfiguration.java b/src/main/java/org/gcube/spatial/data/sdi/LocalConfiguration.java new file mode 100644 index 0000000..d6873a0 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/LocalConfiguration.java @@ -0,0 +1,61 @@ +package org.gcube.spatial.data.sdi; + +import java.net.URL; +import java.util.Properties; + +import javax.servlet.ServletContext; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class LocalConfiguration { + + + final static public String GEONETWORK_CACHE_TTL="gn.cache.TTL"; + final static public String GEONETWORK_SE_CATEGORY="gn.se.category"; + final static public String GEONETWORK_SE_PLATFORM="gn.se.platform"; + final static public String GEONETWORK_SE_PRIORITY="gn.se.priority"; + final static public String GEONETWORK_SE_ENDPOINT_NAME="gn.se.endpointName"; + + + final static public String GEOSERVER_CACHE_TTL="gs.cache.TTL"; + + + final static public String THREDDS_CACHE_TTL="th.cache.TTL"; + final static public String THREDDS_SE_CATEGORY="th.se.category"; + final static public String THREDDS_SE_PLATFORM="th.se.platform"; + final static public String THREDDS_GE_SERVICE_CLASS="th.ge.serviceClass"; + final static public String THREDDS_GE_SERVICE_NAME="th.ge.serviceName"; + + + + static LocalConfiguration instance=null; + + + public static synchronized LocalConfiguration get(){ + return instance; + } + + public static void init(URL propertiesURL){ + instance=new LocalConfiguration(propertiesURL); + } + + private Properties props=new Properties(); + + private LocalConfiguration(URL propertiesURL) { + try{ + log.debug("Loading {} ",propertiesURL); + props.load(propertiesURL.openStream()); + }catch(Exception e){ + throw new RuntimeException(e); + } + } + + public String getProperty(String property){ + return props.getProperty(property); + } + + public String getProperty(String property,String defaultValue){ + return props.getProperty(property, defaultValue); + } +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/SDIService.java b/src/main/java/org/gcube/spatial/data/sdi/SDIService.java index 43bbe95..56006b4 100644 --- a/src/main/java/org/gcube/spatial/data/sdi/SDIService.java +++ b/src/main/java/org/gcube/spatial/data/sdi/SDIService.java @@ -1,5 +1,7 @@ package org.gcube.spatial.data.sdi; +import java.net.URL; + import javax.ws.rs.ApplicationPath; import org.gcube.smartgears.ContextProvider; @@ -9,7 +11,8 @@ import org.gcube.spatial.data.sdi.rest.GeoNetwork; import org.glassfish.jersey.server.ResourceConfig; import io.swagger.jaxrs.config.BeanConfig; - +import lombok.extern.slf4j.Slf4j; +@Slf4j @ApplicationPath(Constants.APPLICATION) public class SDIService extends ResourceConfig{ @@ -26,8 +29,16 @@ public class SDIService extends ResourceConfig{ String hostName=configuration.hostname(); Integer port=configuration.port(); - - + try{ + URL resourceUrl = context.application().getResource("/WEB-INF/config.properties"); + LocalConfiguration.init(resourceUrl); + }catch(Throwable t){ + log.debug("Listing available paths"); + for(Object obj:context.application().getResourcePaths("/WEB-INF")) + log.debug("OBJ : {} ",obj); + + throw new RuntimeException("Unable to load configuration properties",t); + } diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/GISManager.java b/src/main/java/org/gcube/spatial/data/sdi/engine/GISManager.java new file mode 100644 index 0000000..582077d --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/GISManager.java @@ -0,0 +1,10 @@ +package org.gcube.spatial.data.sdi.engine; + +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.GeoServerClusterConfiguration; + +public interface GISManager { + + public GeoServerClusterConfiguration getConfiguration() throws ConfigurationNotFoundException; + +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/GeoNetworkManager.java b/src/main/java/org/gcube/spatial/data/sdi/engine/GeoNetworkManager.java new file mode 100644 index 0000000..f88f3ac --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/GeoNetworkManager.java @@ -0,0 +1,10 @@ +package org.gcube.spatial.data.sdi.engine; + +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.GeoNetworkConfiguration; + +public interface GeoNetworkManager { + + public GeoNetworkConfiguration getConfiguration() throws ConfigurationNotFoundException; + +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/ThreddsManager.java b/src/main/java/org/gcube/spatial/data/sdi/engine/ThreddsManager.java new file mode 100644 index 0000000..9796485 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/ThreddsManager.java @@ -0,0 +1,10 @@ +package org.gcube.spatial.data.sdi.engine; + +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.ThreddsConfiguration; + +public interface ThreddsManager { + + public ThreddsConfiguration getConfiguration() throws ConfigurationNotFoundException; + +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/GISManagerImpl.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/GISManagerImpl.java new file mode 100644 index 0000000..a027fd2 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/GISManagerImpl.java @@ -0,0 +1,27 @@ +package org.gcube.spatial.data.sdi.engine.impl; + +import javax.inject.Singleton; + +import org.gcube.spatial.data.sdi.LocalConfiguration; +import org.gcube.spatial.data.sdi.engine.GISManager; +import org.gcube.spatial.data.sdi.engine.impl.cache.Cache; +import org.gcube.spatial.data.sdi.engine.impl.cache.GeoServerClusterRetriever; +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.GeoServerClusterConfiguration; + +@Singleton +public class GISManagerImpl implements GISManager { + + private Cache theCache=null; + + public GISManagerImpl() { + theCache=Cache.getCache(new GeoServerClusterRetriever(), Long.parseLong(LocalConfiguration.get().getProperty(LocalConfiguration.GEOSERVER_CACHE_TTL)), "GeoCluster - cache"); + } + + + @Override + public GeoServerClusterConfiguration getConfiguration() throws ConfigurationNotFoundException { + return theCache.get(); + } + +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/GeoNetworkManagerImpl.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/GeoNetworkManagerImpl.java new file mode 100644 index 0000000..154e7fd --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/GeoNetworkManagerImpl.java @@ -0,0 +1,29 @@ +package org.gcube.spatial.data.sdi.engine.impl; + +import javax.inject.Singleton; + +import org.gcube.spatial.data.sdi.LocalConfiguration; +import org.gcube.spatial.data.sdi.engine.GeoNetworkManager; +import org.gcube.spatial.data.sdi.engine.impl.cache.Cache; +import org.gcube.spatial.data.sdi.engine.impl.cache.GeoNetworkRetriever; +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.GeoNetworkConfiguration; + +@Singleton +public class GeoNetworkManagerImpl implements GeoNetworkManager { + + private Cache cache=null; + + + public GeoNetworkManagerImpl() { + cache=Cache.getCache(new GeoNetworkRetriever(), + Long.parseLong(LocalConfiguration.get().getProperty(LocalConfiguration.GEONETWORK_CACHE_TTL)),"GeoNetwork - cache"); + } + + + @Override + public GeoNetworkConfiguration getConfiguration() throws ConfigurationNotFoundException { + return cache.get(); + } + +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/SDIManagerImpl.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/SDIManagerImpl.java index cda91e0..a463042 100644 --- a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/SDIManagerImpl.java +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/SDIManagerImpl.java @@ -2,9 +2,14 @@ package org.gcube.spatial.data.sdi.engine.impl; import java.util.Arrays; +import javax.inject.Inject; import javax.inject.Singleton; +import org.gcube.spatial.data.geonetwork.utils.ScopeUtils; +import org.gcube.spatial.data.sdi.engine.GISManager; +import org.gcube.spatial.data.sdi.engine.GeoNetworkManager; import org.gcube.spatial.data.sdi.engine.SDIManager; +import org.gcube.spatial.data.sdi.engine.ThreddsManager; import org.gcube.spatial.data.sdi.model.ScopeConfiguration; import org.gcube.spatial.data.sdi.model.credentials.AccessType; import org.gcube.spatial.data.sdi.model.credentials.Credentials; @@ -14,26 +19,61 @@ import org.gcube.spatial.data.sdi.model.service.GeoServerConfiguration; import org.gcube.spatial.data.sdi.model.service.ThreddsConfiguration; import org.gcube.spatial.data.sdi.model.service.Version; +import lombok.extern.slf4j.Slf4j; +@Slf4j @Singleton public class SDIManagerImpl implements SDIManager { +// @Inject + GeoNetworkManager geonetworkManager; +// @Inject + ThreddsManager threddsManager; +// @Inject + GISManager gisManager; + + + + + @Inject + public SDIManagerImpl(GeoNetworkManager geonetworkManager, ThreddsManager threddsManager, GISManager gisManager) { + super(); + this.geonetworkManager = geonetworkManager; + this.threddsManager = threddsManager; + this.gisManager = gisManager; + } + + + + + @Override public ScopeConfiguration getContextConfiguration() { - GeoNetworkConfiguration gn=new GeoNetworkConfiguration(new Version(2,2,2), "http://geonetwork.d4science.org/geonetwork", - Arrays.asList(new Credentials("GNuser","pwd",AccessType.ADMIN)), "vre_group", "vre_shared", "global public access"); - ThreddsConfiguration thredds=new ThreddsConfiguration(new Version(2,2,2), "http://thredds.d4sciecne.org/thredds", - Arrays.asList(new Credentials("THuser","pwd",AccessType.ADMIN))); - GeoServerConfiguration gs1=new GeoServerConfiguration(new Version(2,10,1), "http://geoserver1.d4science.org/geoserver", - Arrays.asList(new Credentials("GS1user","pwd",AccessType.ADMIN)), "context_confidential_ws", "context_only_ws", "context_ws", "public_access_ws"); + + // TODO filter info by user role - GeoServerConfiguration gs2=new GeoServerConfiguration(new Version(2,6,1), "http://geoserver2.d4science.org/geoserver", - Arrays.asList(new Credentials("GS1user","pwd",AccessType.ADMIN)), "context_confidential_ws", "context_only_ws", "context_ws", "public_access_ws"); + ScopeConfiguration toReturn=new ScopeConfiguration(); + toReturn.setContextName(ScopeUtils.getCurrentScopeName()); + try{ + toReturn.setGeonetworkConfiguration(geonetworkManager.getConfiguration()); + }catch(Exception e){ + log.warn("Scope is not well configured. Missing GeoNetwork. ",e); + } - ScopeConfiguration toReturn=new ScopeConfiguration("/some/context", gn, new GeoServerClusterConfiguration(Arrays.asList(gs1,gs2)), thredds); + try{ + toReturn.setThreddsConfiguration(threddsManager.getConfiguration()); + }catch(Exception e){ + log.warn("THREDDS not found in current scope {} ",ScopeUtils.getCurrentScope()); + } + + try{ + toReturn.setGeoserverClusterConfiguration(gisManager.getConfiguration()); + }catch(Exception e){ + log.warn("GeoServer not found in current scope {} ",ScopeUtils.getCurrentScope()); + } return toReturn; } - + } diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/ThreddsManagerImpl.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/ThreddsManagerImpl.java new file mode 100644 index 0000000..d85bae5 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/ThreddsManagerImpl.java @@ -0,0 +1,27 @@ +package org.gcube.spatial.data.sdi.engine.impl; + +import javax.inject.Singleton; + +import org.gcube.spatial.data.sdi.LocalConfiguration; +import org.gcube.spatial.data.sdi.engine.ThreddsManager; +import org.gcube.spatial.data.sdi.engine.impl.cache.Cache; +import org.gcube.spatial.data.sdi.engine.impl.cache.ThreddsRetriever; +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.ThreddsConfiguration; + +@Singleton +public class ThreddsManagerImpl implements ThreddsManager { + + private Cache cache=null; + + public ThreddsManagerImpl() { + cache=Cache.getCache(new ThreddsRetriever(), + Long.parseLong(LocalConfiguration.get().getProperty(LocalConfiguration.THREDDS_CACHE_TTL)), "THREDDS - CACHE"); + } + + @Override + public ThreddsConfiguration getConfiguration() throws ConfigurationNotFoundException { + return cache.get(); + } + +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/Cache.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/Cache.java new file mode 100644 index 0000000..7d14ec9 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/Cache.java @@ -0,0 +1,53 @@ +package org.gcube.spatial.data.sdi.engine.impl.cache; + +import java.util.concurrent.ConcurrentHashMap; + +import org.gcube.spatial.data.geonetwork.utils.ScopeUtils; +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.GeoService; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class Cache { + + private long objectsTTL; + private ConcurrentHashMap> theCache; + private ObjectRetriever retriever; + private String cacheName; + + + + + private Cache(long objectsTTL, ObjectRetriever retriever, String cacheName) { + super(); + this.objectsTTL = objectsTTL; + this.retriever = retriever; + this.cacheName=cacheName; + theCache=new ConcurrentHashMap<>(); + + } + + public synchronized T get() throws ConfigurationNotFoundException{ + String key=ScopeUtils.getCurrentScope(); + log.info("Getting object from cache{} , key is {} ",cacheName,key); + if((!theCache.containsKey(key))||(!theCache.get(key).isValid(objectsTTL))) + theCache.put(key, new CachedObject(retriever.getObject())); + return theCache.get(key).getTheObject(); + } + + public void invalidate(){ + String key=ScopeUtils.getCurrentScope(); + log.info("Invalidating cache {} under scope {} ",cacheName,key); + if(theCache.containsKey(key))theCache.get(key).invalidate(); + } + + public void invalidateAll(){ + for(CachedObject obj:theCache.values())obj.invalidate(); + } + + public static Cache getCache(ObjectRetriever retriever, long objectsTTL,String cacheName){ + return new Cache(objectsTTL,retriever,cacheName); + } + +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/CachedObject.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/CachedObject.java new file mode 100644 index 0000000..d3d093f --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/CachedObject.java @@ -0,0 +1,33 @@ +package org.gcube.spatial.data.sdi.engine.impl.cache; + +public class CachedObject { + + + + + private long lastUpdate=System.currentTimeMillis(); + + + private T theObject; + + + + + public CachedObject(T theObject) { + super(); + this.theObject = theObject; + } + + public T getTheObject() { + return theObject; + } + + + public boolean isValid(long TTL){ + return System.currentTimeMillis()-lastUpdate { + + @Override + public GeoNetworkConfiguration getObject() throws ConfigurationNotFoundException { + //TODO skip library + //TODO use both GCoreEndpoints and ServiceEndpoint + + +// log.info("Gathering geonetwork information under scope {} ",ScopeUtils.getCurrentScope()); +// LocalConfiguration config=LocalConfiguration.get(); +// String category=config.getProperty(LocalConfiguration.GEONETWORK_SE_CATEGORY); +// String platformName=config.getProperty(LocalConfiguration.GEONETWORK_SE_PLATFORM); +// String priorityProperty=config.getProperty(LocalConfiguration.GEONETWORK_SE_PRIORITY); +// String endpointName=config.getProperty(LocalConfiguration.GEONETWORK_SE_ENDPOINT_NAME); +// ServiceEndpoint se=getTheRightServiceEndpoint(ISUtils.queryForServiceEndpoints(category, platformName), endpointName, priorityProperty); +// AccessPoint access=getTheRightAccessPoint(se, endpointName, priorityProperty); +// + + try{ + //INIT LIB + GISInterface gis=GISInterface.get(); + GeoNetworkAdministration gnAdmin=(GeoNetworkAdministration) gis.getGeoNewtorkPublisher(); + Configuration config=gnAdmin.getConfiguration(); + + Version version=config.getGeoNetworkVersion().equals(ServerAccess.Version.TRE)?new Version(3,0,0):new Version(2,6,0); + String baseEndpoint=config.getGeoNetworkEndpoint(); + ScopeConfiguration scopeConfig=config.getScopeConfiguration(); + List accessibleCredentials=new ArrayList(); + for(Account acc: scopeConfig.getAccounts().values()){ + accessibleCredentials.add(fromGeoNetworkAccount(acc)); + } + + Credentials adminCredentials=fromGeoNetworkAccount(config.getAdminAccount()); + // GN Lib doesn't expose ADMIN account type + adminCredentials.setAccessType(AccessType.ADMIN); + accessibleCredentials.add(adminCredentials); + return new GeoNetworkConfiguration(version, baseEndpoint, accessibleCredentials, scopeConfig.getPrivateGroup()+"", scopeConfig.getPublicGroup()+"", "3"); + }catch(Exception e){ + log.warn("Unable to gather geonetwork information",e); + throw new ConfigurationNotFoundException("Unable to gather information on geonetwork. Please contact administrator.",e); + } + } + + + + protected static final Credentials fromGeoNetworkAccount(Account toTranslate){ + switch(toTranslate.getType()){ + case CKAN : return new Credentials(toTranslate.getUser(),toTranslate.getPassword(),AccessType.CKAN); + case SCOPE : return new Credentials(toTranslate.getUser(),toTranslate.getPassword(),AccessType.CONTEXT_USER); + default : throw new RuntimeException("Unrecognized account type "+toTranslate); + } + + } + + + + + protected static final ServiceEndpoint getTheRightServiceEndpoint(Listresources, String endpointName,String priorityProperty){ + ServiceEndpoint toReturn=null; + int priority=1000; + for(ServiceEndpoint resource: resources){ + Iterator points=resource.profile().accessPoints().iterator(); + + while(points.hasNext()){ + AccessPoint point= points.next(); + log.debug(point.toString()); + if(point.name().equals(endpointName)){ + Map properties=point.propertyMap(); + if(properties.containsKey(priorityProperty)){ + int currentPriority=Integer.parseInt(properties.get(priorityProperty).value()); + if(toReturn==null||(currentPriority points=resource.profile().accessPoints().iterator(); + + while(points.hasNext()){ + AccessPoint point= points.next(); + log.debug(point.toString()); + if(point.name().equals(endpointName)){ + Map properties=point.propertyMap(); + if(properties.containsKey(priorityProperty)){ + int currentPriority=Integer.parseInt(properties.get(priorityProperty).value()); + if(toReturn==null||(currentPriority{ + + + @Override + public GeoServerClusterConfiguration getObject() throws ConfigurationNotFoundException { + //TODO skip library + //TODO use both GCoreEndpoints and ServiceEndpoint + + log.info("Retrieving GeoServer cluster configuration under scope {}",ScopeUtils.getCurrentScope()); + try{ + GISInterface gis=GISInterface.get(); + ArrayList availableInstances=new ArrayList<>(); + for(AbstractGeoServerDescriptor desc: gis.getCurrentCacheElements(true)){ + try{ + availableInstances.add(translate(desc)); + }catch(Throwable t){ + log.warn("Unable to translate descriptor for endpoint"+desc.getUrl(),t); + } + } + + return new GeoServerClusterConfiguration(availableInstances); + }catch(Exception e){ + log.warn("Unable to gather geoserver cluster configuration on scope "+ScopeUtils.getCurrentScope(),e); + throw new ConfigurationNotFoundException("Unable to gather geoserver cluster configuration. Please ontact administrator.",e); + } + } + + + private static final GeoServerConfiguration translate(AbstractGeoServerDescriptor desc){ + Version version=new Version(2,1,2); + String baseEndpoint=desc.getUrl(); + List accessibleCredentials=Collections.singletonList(new Credentials(desc.getUser(), desc.getPassword(), AccessType.ADMIN)); + String confidentialWorkspace=null; + String contextVisibilityWorkspace=null; + String sharedWorkspace=null; + String publicWorkspace=null; + return new GeoServerConfiguration(version, baseEndpoint, accessibleCredentials, confidentialWorkspace, contextVisibilityWorkspace, sharedWorkspace, publicWorkspace); + } +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ISUtils.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ISUtils.java new file mode 100644 index 0000000..8b40482 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ISUtils.java @@ -0,0 +1,46 @@ +package org.gcube.spatial.data.sdi.engine.impl.cache; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.List; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.spatial.data.geonetwork.utils.ScopeUtils; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class ISUtils { + + static List queryForServiceEndpoints(String category, String platformName){ + log.debug("Querying for Service Endpoints [category : {} , platformName : {}, currentScope : {} ]",category,platformName,ScopeUtils.getCurrentScope()); + + SimpleQuery query = queryFor(ServiceEndpoint.class); + + query.addCondition("$resource/Profile/Category/text() eq '"+category+"'") + .addCondition("$resource/Profile/Platform/Name/text() eq '"+platformName+"'"); + // .setResult("$resource/Profile/AccessPoint"); + + DiscoveryClient client = clientFor(ServiceEndpoint.class); + + return client.submit(query); + } + + static List queryForGCoreEndpoint(String serviceClass,String serviceName){ + log.debug("Querying for GCore Endpoints [ServiceClass : {} , ServiceName : {}, currentScope : {} ]",serviceClass,serviceName,ScopeUtils.getCurrentScope()); + + + SimpleQuery query =queryFor(GCoreEndpoint.class); + query.addCondition("$resource/Profile/ServiceClass/text() eq '"+serviceClass+"'") + .addCondition("$resource/Profile/ServiceName/text() eq '"+serviceName+"'"); + // .setResult("$resource/Profile/AccessPoint"); + + DiscoveryClient client = clientFor(GCoreEndpoint.class); + + return client.submit(query); + } +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ObjectRetriever.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ObjectRetriever.java new file mode 100644 index 0000000..81db24a --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ObjectRetriever.java @@ -0,0 +1,9 @@ +package org.gcube.spatial.data.sdi.engine.impl.cache; + +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; + +public interface ObjectRetriever { + + public T getObject()throws ConfigurationNotFoundException; + +} diff --git a/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ThreddsRetriever.java b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ThreddsRetriever.java new file mode 100644 index 0000000..10a5c61 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/sdi/engine/impl/cache/ThreddsRetriever.java @@ -0,0 +1,87 @@ +package org.gcube.spatial.data.sdi.engine.impl.cache; + +import java.util.Collections; +import java.util.List; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.common.Platform; +import org.gcube.spatial.data.geonetwork.utils.ScopeUtils; +import org.gcube.spatial.data.sdi.LocalConfiguration; +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.credentials.AccessType; +import org.gcube.spatial.data.sdi.model.credentials.Credentials; +import org.gcube.spatial.data.sdi.model.service.ThreddsConfiguration; +import org.gcube.spatial.data.sdi.model.service.Version; + +import lombok.extern.slf4j.Slf4j; +@Slf4j +public class ThreddsRetriever implements ObjectRetriever { + + @Override + public ThreddsConfiguration getObject() throws ConfigurationNotFoundException{ + log.info("Loading Thredds information from IS. Current Scope is {} ",ScopeUtils.getCurrentScope()); + + + LocalConfiguration config=LocalConfiguration.get(); + + + // Try to look for GCore Endpoints first + String geClass=config.getProperty(LocalConfiguration.THREDDS_GE_SERVICE_CLASS); + String geName=config.getProperty(LocalConfiguration.THREDDS_GE_SERVICE_NAME); + List gCoreEndpoints=ISUtils.queryForGCoreEndpoint(geClass, geName); + if(gCoreEndpoints!=null&&!gCoreEndpoints.isEmpty()){ + log.debug("Found {} GCore Endpoints ",gCoreEndpoints.size()); + for(int i=0;i threddsSE=ISUtils.queryForServiceEndpoints(seCategory, sePlatform); + if(threddsSE!=null&&!threddsSE.isEmpty()){ + log.debug("Found {} Service Endpoints ",threddsSE.size()); + for(int i=0;i{ + + private GISManager manager; + + public GISManagerFactory() { + manager=new GISManagerImpl(){ + @Override + public GeoServerClusterConfiguration getConfiguration() throws ConfigurationNotFoundException { + TokenSetter.set(TestCommon.TEST_SCOPE); + return super.getConfiguration(); + } + }; + } + + + @Override + public void dispose(GISManager arg0) { + // TODO Auto-generated method stub + + } + + @Override + public GISManager provide() { + return manager; + } +} diff --git a/src/test/java/org/gcube/spatial/data/sdi/test/GeoNetworkManagerFactory.java b/src/test/java/org/gcube/spatial/data/sdi/test/GeoNetworkManagerFactory.java new file mode 100644 index 0000000..0588b91 --- /dev/null +++ b/src/test/java/org/gcube/spatial/data/sdi/test/GeoNetworkManagerFactory.java @@ -0,0 +1,33 @@ +package org.gcube.spatial.data.sdi.test; + +import org.gcube.spatial.data.sdi.engine.GeoNetworkManager; +import org.gcube.spatial.data.sdi.engine.impl.GeoNetworkManagerImpl; +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.GeoNetworkConfiguration; +import org.glassfish.hk2.api.Factory; + +public class GeoNetworkManagerFactory implements Factory{ + + private GeoNetworkManager manager; + + public GeoNetworkManagerFactory() { + manager=new GeoNetworkManagerImpl(){ + @Override + public GeoNetworkConfiguration getConfiguration() throws ConfigurationNotFoundException { + TokenSetter.set(TestCommon.TEST_SCOPE); + return super.getConfiguration(); + } + }; + } + + @Override + public void dispose(GeoNetworkManager arg0) { + // TODO Auto-generated method stub + + } + + @Override + public GeoNetworkManager provide() { + return manager; + } +} diff --git a/src/test/java/org/gcube/spatial/data/sdi/test/MainTest.java b/src/test/java/org/gcube/spatial/data/sdi/test/MainTest.java index b4eeac4..b900244 100644 --- a/src/test/java/org/gcube/spatial/data/sdi/test/MainTest.java +++ b/src/test/java/org/gcube/spatial/data/sdi/test/MainTest.java @@ -5,7 +5,10 @@ import javax.ws.rs.core.MediaType; import org.gcube.spatial.data.sdi.Constants; import org.gcube.spatial.data.sdi.SDIService; +import org.gcube.spatial.data.sdi.engine.GISManager; +import org.gcube.spatial.data.sdi.engine.GeoNetworkManager; import org.gcube.spatial.data.sdi.engine.SDIManager; +import org.gcube.spatial.data.sdi.engine.ThreddsManager; import org.gcube.spatial.data.sdi.rest.GeoNetwork; import org.glassfish.hk2.utilities.binding.AbstractBinder; import org.glassfish.jersey.server.ResourceConfig; @@ -16,6 +19,7 @@ import io.swagger.jaxrs.config.BeanConfig; public class MainTest extends JerseyTest{ + public static class MyBinder extends AbstractBinder{ public MyBinder() { @@ -27,6 +31,9 @@ public class MainTest extends JerseyTest{ protected void configure() { // bindFactory(GeoNetworkProviderFactory.class).to(GeoNetworkProvider.class); bindFactory(SDIManagerFactory.class).to(SDIManager.class); + bindFactory(ThreddsManagerFactory.class).to(ThreddsManager.class); + bindFactory(GeoNetworkManagerFactory.class).to(GeoNetworkManager.class); + bindFactory(GISManagerFactory.class).to(GISManager.class); } } diff --git a/src/test/java/org/gcube/spatial/data/sdi/test/SDIManagerFactory.java b/src/test/java/org/gcube/spatial/data/sdi/test/SDIManagerFactory.java index 17eb192..d67b277 100644 --- a/src/test/java/org/gcube/spatial/data/sdi/test/SDIManagerFactory.java +++ b/src/test/java/org/gcube/spatial/data/sdi/test/SDIManagerFactory.java @@ -2,12 +2,22 @@ package org.gcube.spatial.data.sdi.test; import org.gcube.spatial.data.sdi.engine.SDIManager; import org.gcube.spatial.data.sdi.engine.impl.SDIManagerImpl; +import org.gcube.spatial.data.sdi.model.ScopeConfiguration; import org.glassfish.hk2.api.Factory; public class SDIManagerFactory implements Factory{ + SDIManager manager=null; + + public SDIManagerFactory() { - // TODO Auto-generated constructor stub + manager=new SDIManagerImpl(new GeoNetworkManagerFactory().provide(),new ThreddsManagerFactory().provide(),new GISManagerFactory().provide()){ + @Override + public ScopeConfiguration getContextConfiguration() { + TokenSetter.set(TestCommon.TEST_SCOPE); + return super.getContextConfiguration(); + } + }; } @Override @@ -18,6 +28,6 @@ public class SDIManagerFactory implements Factory{ @Override public SDIManager provide() { - return new SDIManagerImpl(); + return manager; } } diff --git a/src/test/java/org/gcube/spatial/data/sdi/test/TestCommon.java b/src/test/java/org/gcube/spatial/data/sdi/test/TestCommon.java new file mode 100644 index 0000000..cc0a074 --- /dev/null +++ b/src/test/java/org/gcube/spatial/data/sdi/test/TestCommon.java @@ -0,0 +1,7 @@ +package org.gcube.spatial.data.sdi.test; + +public class TestCommon { + + public static final String TEST_SCOPE="/gcube/devsec"; + +} diff --git a/src/test/java/org/gcube/spatial/data/sdi/test/ThreddsManagerFactory.java b/src/test/java/org/gcube/spatial/data/sdi/test/ThreddsManagerFactory.java new file mode 100644 index 0000000..31c1d4e --- /dev/null +++ b/src/test/java/org/gcube/spatial/data/sdi/test/ThreddsManagerFactory.java @@ -0,0 +1,35 @@ +package org.gcube.spatial.data.sdi.test; + +import org.gcube.spatial.data.sdi.engine.ThreddsManager; +import org.gcube.spatial.data.sdi.engine.impl.ThreddsManagerImpl; +import org.gcube.spatial.data.sdi.engine.impl.faults.ConfigurationNotFoundException; +import org.gcube.spatial.data.sdi.model.service.ThreddsConfiguration; +import org.glassfish.hk2.api.Factory; + +public class ThreddsManagerFactory implements Factory{ + + private ThreddsManager manager; + + + public ThreddsManagerFactory() { + manager=new ThreddsManagerImpl(){ + @Override + public ThreddsConfiguration getConfiguration() throws ConfigurationNotFoundException { + TokenSetter.set(TestCommon.TEST_SCOPE); + return super.getConfiguration(); + } + }; + } + + + @Override + public ThreddsManager provide() { + return manager; + } + + @Override + public void dispose(ThreddsManager arg0) { + // TODO Auto-generated method stub + + } +}