From 2fdd25e71408440025f8333a56593eddc1c9d7d9 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Wed, 7 Sep 2016 13:54:30 +0000 Subject: [PATCH] 4953: Uri Resolver: remove jumps of scope from Readers (for Generic and Runtime resource) Task-Url: https://support.d4science.org/issues/4953 Application Profile Reader updated in order to read Resource by root scope git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@131178 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../ApplicationProfileReader.java | 17 ++++++- .../applicationprofile/ScopeUtil.java | 46 +++++++++---------- .../resolver/gis/GeoRuntimeReader.java | 4 +- .../resolver/gis/GisResolver.java | 7 ++- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java index 954c038..1c2a7aa 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java +++ b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java @@ -11,6 +11,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.api.ScopeProvider; 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; @@ -73,17 +74,21 @@ public class ApplicationProfileReader { ApplicationProfile appProf = new ApplicationProfile(); String queryString = GcubeQuery.getGcubeGenericQueryString(secondaryType, appId); + String originalScope = null; - logger.info("Trying to fetch ApplicationProfile in the scope: "+scope+", SecondaryType: " + secondaryType + ", AppId: " + appId); try { + originalScope = ScopeProvider.instance.get(); + String infra = ScopeUtil.getInfrastructureNameFromScope(scope); + ScopeProvider.instance.set(infra); + logger.info("Trying to fetch ApplicationProfile in the infra scope: "+infra+", SecondaryType: " + secondaryType + ", AppId: " + appId); Query q = new QueryBox(queryString); DiscoveryClient client = client(); List appProfile = client.submit(q); if (appProfile == null || appProfile.size() == 0) - throw new ApplicationProfileNotFoundException("ApplicationProfile with SecondaryType: " + secondaryType + ", AppId: " + appId +" is not registered in the scope: "+scope); + throw new ApplicationProfileNotFoundException("ApplicationProfile with SecondaryType: " + secondaryType + ", AppId: " + appId +" is not registered in the infra scope: "+infra); else { String elem = appProfile.get(0); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); @@ -168,6 +173,14 @@ public class ApplicationProfileReader { } catch (Exception e) { logger.error("Error while trying to fetch applicationProfile profile from the infrastructure", e); return null; + }finally{ + if(originalScope!=null && !originalScope.isEmpty()){ + ScopeProvider.instance.set(originalScope); + logger.info("scope provider setted to orginal scope: "+originalScope); + }else{ + ScopeProvider.instance.reset(); + logger.info("scope provider reset"); + } } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ScopeUtil.java b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ScopeUtil.java index a13b632..58ae066 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ScopeUtil.java +++ b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ScopeUtil.java @@ -17,27 +17,27 @@ public class ScopeUtil { public static final Logger logger = LoggerFactory.getLogger(ScopeUtil.class); -// public static String getInfrastructureNameFromScope(String scope) throws Exception{ -// -// if(scope==null || scope.isEmpty()){ -// throw new Exception("Scope is null or empty"); -// } -// -// if(!scope.startsWith(SCOPE_SEPARATOR)){ -// logger.warn("Input scope: "+scope+" not have / is a really scope?"); -// scope = SCOPE_SEPARATOR+scope; -// logger.warn("Tentative as scope: "+scope); -// } -// -// String[] splitScope = scope.split(SCOPE_SEPARATOR); -// -// String rootScope = SCOPE_SEPARATOR + splitScope[1]; -// -// if(rootScope.length()<2){ -// throw new Exception("Infrastructure name not found in "+scope); -// } -// -// return rootScope; -// -// } + public static String getInfrastructureNameFromScope(String scope) throws Exception{ + + if(scope==null || scope.isEmpty()){ + throw new Exception("Scope is null or empty"); + } + + if(!scope.startsWith(SCOPE_SEPARATOR)){ + logger.warn("Input scope: "+scope+" not have / is a really scope?"); + scope = SCOPE_SEPARATOR+scope; + logger.warn("Tentative as scope: "+scope); + } + + String[] splitScope = scope.split(SCOPE_SEPARATOR); + + String rootScope = SCOPE_SEPARATOR + splitScope[1]; + + if(rootScope.length()<2){ + throw new Exception("Infrastructure name not found in "+scope); + } + + return rootScope; + + } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/GeoRuntimeReader.java b/src/main/java/org/gcube/datatransfer/resolver/gis/GeoRuntimeReader.java index 2799cc1..6d3961a 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/GeoRuntimeReader.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/GeoRuntimeReader.java @@ -34,7 +34,7 @@ public class GeoRuntimeReader { public static enum GEO_SERVICE{GEOSERVER, GEONETWORK}; - public static final Logger logger = LoggerFactory.getLogger(GisResolver.class); + public static final Logger logger = LoggerFactory.getLogger(GeoRuntimeReader.class); private ServerParameters getParameters(String scope, GEO_SERVICE geoservice) throws Exception { @@ -54,6 +54,8 @@ public class GeoRuntimeReader { SimpleQuery query = queryFor(ServiceEndpoint.class); query.addCondition("$resource/Profile/Name/string() eq '"+resourceName+"'"); + logger.info("GeoRuntimeReader, using scope: "+scope + ", to get resource: "+resourceName); + DiscoveryClient client = clientFor(ServiceEndpoint.class); List r = client.submit(query); diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java b/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java index e405ca7..82f4630 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/GisResolver.java @@ -18,6 +18,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader; import org.gcube.datatransfer.resolver.gis.GeoRuntimeReader.GEO_SERVICE; import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter.GeonetworkLoginLevel; @@ -202,7 +203,7 @@ public class GisResolver extends HttpServlet{ logger.info("SCOPE is: " + scope); try { - + ScopeProvider.instance.set(scope); ServerParameters geonetworkParams = getCachedServerParameters(scope); String wmsRequest = getLayerWmsRequest(scope, gisUUID, geonetworkParams); logger.info("wms url is: " + wmsRequest); @@ -233,6 +234,9 @@ public class GisResolver extends HttpServlet{ String error = "Sorry, an error occurred on resolving request with UUID "+gisUUID+" and scope "+scope+". Please, contact support!"; sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error); return; + }finally{ + logger.error("Resetting scope"); + ScopeProvider.instance.reset(); } } @@ -367,6 +371,7 @@ public class GisResolver extends HttpServlet{ String scope = "/gcube/devsec/devVRE"; String UUID = "177e1c3c-4a22-4ad9-b015-bfc443d16cb8"; try { + ScopeProvider.instance.set(scope); ServerParameters geonetworkParams = gisResolver.getCachedServerParameters(scope); String wmsRequest = gisResolver.getLayerWmsRequest(scope, UUID, geonetworkParams); logger.info("Final url is: " + wmsRequest);