From 4683ebb9f504949fe6dc65484be639b847cd1533 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Tue, 20 Dec 2016 14:27:08 +0000 Subject: [PATCH] Work ultimated on Task #6262 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@141276 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../resolver/UriResolverRewriteFilter.java | 10 +- .../ApplicationProfileReader.java | 3 +- .../applicationprofile/GcubeQuery.java | 41 ++++- ...tionProfileReaderForCatalogueResolver.java | 170 ++++++------------ .../resolver/catalogue/CatalogueResolver.java | 26 ++- 5 files changed, 116 insertions(+), 134 deletions(-) diff --git a/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java b/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java index f98b127..2139572 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java @@ -49,6 +49,8 @@ public class UriResolverRewriteFilter implements Filter{ protected static final Logger logger = LoggerFactory.getLogger(UriResolverRewriteFilter.class); private FilterConfig config; + //private ApplicationProfileReaderForCatalogueResolver appPrfCatResolver = new ApplicationProfileReaderForCatalogueResolver(scope, useRootScope) + /** * Gets the config. * @@ -67,6 +69,7 @@ public class UriResolverRewriteFilter implements Filter{ logger.trace("run destroy"); } + /* (non-Javadoc) * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) */ @@ -96,8 +99,9 @@ public class UriResolverRewriteFilter implements Filter{ String newURI = SERVLET_CATALOGUE; if(pathSplit.length==5){ logger.info("Resolving a clear URL to catalogue..."); + logger.debug("found VRE name: "+pathSplit[2]); - String gcubeScope = CatalogueRequestParameter.GCUBE_SCOPE.getKey() +"="+"/gcube/devNext/NextNext"; + String gcubeScope = CatalogueRequestParameter.GCUBE_SCOPE.getKey() +"="+pathSplit[2]; String eC = CatalogueRequestParameter.ENTITY_CONTEXT.getKey() +"="+pathSplit[3]; logger.debug("found context name: "+eC); String eN = CatalogueRequestParameter.ENTITY_NAME.getKey() +"="+pathSplit[4]; @@ -166,7 +170,7 @@ public class UriResolverRewriteFilter implements Filter{ this.config = config; } - public static void main(String[] args) { + /*public static void main(String[] args) { String split = "/catalogue/NextNext/dataset/sarda-sarda"; @@ -180,5 +184,5 @@ public class UriResolverRewriteFilter implements Filter{ System.out.println(array[2]); - } + }*/ } 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 d3336a0..6288a90 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java +++ b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/ApplicationProfileReader.java @@ -12,6 +12,7 @@ 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.datatransfer.resolver.applicationprofile.GcubeQuery.FIELD_TYPE; 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; @@ -76,7 +77,7 @@ public class ApplicationProfileReader { private ApplicationProfile readProfileFromInfrastrucure() { ApplicationProfile appProf = new ApplicationProfile(); - String queryString = GcubeQuery.getGcubeGenericQueryString(secondaryType, appId); + String queryString = GcubeQuery.getGcubeGenericResource(secondaryType, FIELD_TYPE.APP_ID, appId); String originalScope = null; try { diff --git a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/GcubeQuery.java b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/GcubeQuery.java index 0ebd6c5..611d3f8 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/GcubeQuery.java +++ b/src/main/java/org/gcube/datatransfer/resolver/applicationprofile/GcubeQuery.java @@ -1,27 +1,50 @@ package org.gcube.datatransfer.resolver.applicationprofile; + /** * The Class GcubeQuery. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Jan 14, 2016 + * Dec 20, 2016 */ public class GcubeQuery { /** - * Gets the gcube generic query string. + * The Enum FIELD_TYPE. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Dec 20, 2016 + */ + public static enum FIELD_TYPE {RESOURCE_NAME, APP_ID} + + + /** + * Gets the gcube generic resource. * * @param secondaryType the secondary type - * @param appId the app id - * @return the gcube generic query string + * @param type the type + * @param fieldValue the field value + * @return the gcube generic resource */ - public static String getGcubeGenericQueryString(String secondaryType, String appId){ + public static String getGcubeGenericResource(String secondaryType, FIELD_TYPE type, String fieldValue){ - return "for $profile in collection('/db/Profiles/GenericResource')//Resource " + - "where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"' and $profile/Profile/Body/AppId/string() " + - " eq '" + appId + "'" + - "return $profile"; + String query = "for $profile in collection('/db/Profiles/GenericResource')//Resource " + + "where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"'"; + switch (type) { + case APP_ID: + query+= " and $profile/Profile/Body/AppId/string() eq '" + fieldValue + "'"; + + break; + case RESOURCE_NAME: + query+= " and $profile/Profile/Name/string() eq '" + fieldValue + "'"; + + break; + default: + break; + } + + return query+=" return $profile"; } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/catalogue/ApplicationProfileReaderForCatalogueResolver.java b/src/main/java/org/gcube/datatransfer/resolver/catalogue/ApplicationProfileReaderForCatalogueResolver.java index d3fd644..1099e39 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/catalogue/ApplicationProfileReaderForCatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/catalogue/ApplicationProfileReaderForCatalogueResolver.java @@ -1,20 +1,24 @@ +/* + * + */ package org.gcube.datatransfer.resolver.catalogue; import static org.gcube.resources.discovery.icclient.ICFactory.client; import java.io.StringReader; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.xml.parsers.DocumentBuilder; 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.datatransfer.resolver.applicationprofile.ApplicationProfile; import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileNotFoundException; import org.gcube.datatransfer.resolver.applicationprofile.GcubeQuery; +import org.gcube.datatransfer.resolver.applicationprofile.GcubeQuery.FIELD_TYPE; import org.gcube.datatransfer.resolver.applicationprofile.ScopeUtil; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.Query; @@ -24,37 +28,36 @@ import org.xml.sax.InputSource; /** - * The Class ApplicationProfileReader. + * The Class ApplicationProfileReaderForCatalogueResolver. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Sep 6, 2016 + * Dec 20, 2016 */ public class ApplicationProfileReaderForCatalogueResolver { - protected static final String RESOURCE_PROFILE_BODY_END_POINT_VRE_TEXT = "/Resource/Profile/Body/EndPoint/URL/text()"; - protected static final String RESOURCE_PROFILE_BODY_END_POINT_SCOPE_TEXT = "/Resource/Profile/Body/EndPoint/Scope/text()"; - protected static final String RESOURCE_PROFILE_BODY_TEXT = "/Resource/Profile/Body/text()"; - protected static final String RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT = "/Resource/Profile/Body/ThumbnailURL/text()"; - protected static final String RESOURCE_PROFILE_BODY_APP_ID_TEXT = "/Resource/Profile/Body/AppId/text()"; - protected static final String RESOURCE_PROFILE_DESCRIPTION_TEXT = "/Resource/Profile/Description/text()"; + public static final String SECONDARY_TYPE = "ApplicationProfile"; + public static final String RESOURCE_NAME = "Catalogue-Resolver"; protected static final String RESOURCE_PROFILE_NAME_TEXT = "/Resource/Profile/Name/text()"; - - protected static final String SECONDARY_TYPE = "ApplicationProfile"; - protected static final String RESOURCE_NAME = "Catalogue-Resolver"; + protected static final String RESOURCE_PROFILE_DESCRIPTION_TEXT = "/Resource/Profile/Description/text()"; + protected static final String RESOURCE_PROFILE_BODY_END_POINT_SCOPE_TEXT = "/Resource/Profile/Body/EndPoint/SCOPE/text()"; + protected static final String RESOURCE_PROFILE_BODY_END_POINT_VRE_NAME_TEXT = "/Resource/Profile/Body/EndPoint/VRE_NAME/text()"; private Logger logger = Logger.getLogger(ApplicationProfileReaderForCatalogueResolver.class); private String secondaryType; private String resourceName; private String scope; - private ApplicationProfile applicationProfile; private boolean useRootScope = false; + /** The hash vre name scope. + * Used by Catalogue Resolver for mapping VRE NAME with its SCOPE where to fetch the Data Catalogue Portlet so that resolve correctly URL of + * kind: http://[CATALOGUE_RESOLVER_SERVLET]/[VRE_NAME]/[entity_context value]/[entity_name value] + * */ + private Map hashVreNameScope = new HashMap(); /** * Instantiates a new application profile reader for catalogue resolver. * * @param scope the scope - * @param portletClassName the portlet class name * @param useRootScope the use root scope */ public ApplicationProfileReaderForCatalogueResolver(String scope, boolean useRootScope) { @@ -62,27 +65,15 @@ public class ApplicationProfileReaderForCatalogueResolver { this.secondaryType = SECONDARY_TYPE; this.resourceName = RESOURCE_NAME; this.useRootScope = useRootScope; - this.applicationProfile = readProfileFromInfrastrucure(); + this.readProfileFromInfrastrucure(); } /** - * Gets the application profile. - * - * @return the application profile + * Read profile from infrastrucure and fills the map {@link #hashVreNameScope} */ - public ApplicationProfile getApplicationProfile() { - return applicationProfile; - } + private void readProfileFromInfrastrucure() { - /** - * this method looks up the applicationProfile profile among the ones available in the infrastructure. - * - * @return the applicationProfile profile - */ - private ApplicationProfile readProfileFromInfrastrucure() { - - ApplicationProfile appProf = new ApplicationProfile(); - String queryString = GcubeQuery.getGcubeGenericQueryString(secondaryType, resourceName); + String queryString = GcubeQuery.getGcubeGenericResource(secondaryType, FIELD_TYPE.RESOURCE_NAME, resourceName); String originalScope = null; try { @@ -90,97 +81,32 @@ public class ApplicationProfileReaderForCatalogueResolver { originalScope = ScopeProvider.instance.get(); String discoveryScope = useRootScope?ScopeUtil.getInfrastructureNameFromScope(scope):scope; ScopeProvider.instance.set(discoveryScope); - logger.info("Trying to fetch ApplicationProfile in the infra scope: "+discoveryScope+", SecondaryType: " + secondaryType + ", AppId: " + resourceName); + logger.info("Trying to fetch ApplicationProfile in the scope: "+discoveryScope+", SecondaryType: " + secondaryType + ", ResourceName: " + resourceName); 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: " + resourceName +" is not registered in the infra scope: "+discoveryScope); + throw new ApplicationProfileNotFoundException("ApplicationProfile with SecondaryType: " + secondaryType + ", ResourceName: " + resourceName +" is not registered in the scope: "+discoveryScope); else { + try{ String elem = appProfile.get(0); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); XPathHelper helper = new XPathHelper(node); - - List currValue = null; - currValue = helper.evaluate(RESOURCE_PROFILE_NAME_TEXT); - if (currValue != null && currValue.size() > 0) { - appProf.setName(currValue.get(0)); + List scopes = helper.evaluate(RESOURCE_PROFILE_BODY_END_POINT_SCOPE_TEXT); + for (String scopeFound : scopes) { + List vreName = helper.evaluate("/Resource/Profile/Body/EndPoint[SCOPE='"+scopeFound.toString()+"']/VRE_NAME/text()"); + logger.info("For scope: "+scopeFound+", found VRE_NAME "+vreName); + hashVreNameScope.put(vreName.get(0), scopeFound); } - else throw new ApplicationProfileNotFoundException("Your applicationProfile NAME was not found in the profile"); - - currValue = helper.evaluate(RESOURCE_PROFILE_DESCRIPTION_TEXT); - if (currValue != null && currValue.size() > 0) { - appProf.setDescription(currValue.get(0)); + }catch(Exception e){ + throw new ApplicationProfileNotFoundException("Error during parsing application profile with resource name: "+resourceName + " in the scope: "+scope.toString()); } - else logger.warn("No Description exists for " + appProf.getName()); - - currValue = helper.evaluate(RESOURCE_PROFILE_BODY_APP_ID_TEXT); - if (currValue != null && currValue.size() > 0) { - appProf.setKey(currValue.get(0)); - } - else throw new ApplicationProfileNotFoundException("Your applicationProfile ID n was not found in the profile, consider adding element in "); - - currValue = helper.evaluate(RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT); - if (currValue != null && currValue.size() > 0) { - appProf.setImageUrl(currValue.get(0)); - } - else{ - logger.warn("Null or empty element in " + appProf.getName()); - } - - currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Scope/text()"); - - if (currValue != null && currValue.size() > 0) { - List scopes = currValue; - String currentScope = scopes.get(0); - - int slashCount = StringUtils.countMatches(currentScope, "/"); - - if(slashCount < 3){//CASE not VRE - set session scope - logger.info("Scope "+ scope.toString() + " is not a VRE"); - - List listSessionScope = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Sessionscope/text()"); //get session scope of i+1-mo scope - - if(listSessionScope!=null && listSessionScope.size()>0){ //If sessions scope exists - - logger.trace("setting session scope "+ listSessionScope.get(0)); - appProf.setScope(listSessionScope.get(0)); - } - else{ - logger.trace("session scope not exists setting scope "+ scope.toString()); - appProf.setScope(scope.toString()); - } - } - else{ //CASE IS A VRE - logger.info("Scope "+ scope.toString() + " is a VRE"); - appProf.setScope(scope.toString()); - - } - - //RETRIEVE URL - currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/URL/text()"); - - if (currValue != null && currValue.size() > 0) { - String url = currValue.get(0); - // System.out.println("URL "+url); - if(url!=null) - appProf.setUrl(url); - else - throw new ApplicationProfileNotFoundException("Your applicationProfile URL was not found in the profile for Scope: " + scope.toString()); - } - else throw new ApplicationProfileNotFoundException("Your applicationProfile URL was not found in the profile for Scope: " + scope.toString()); - - } - else throw new ApplicationProfileNotFoundException("Your applicationProfile with scope "+scope.toString()+" was not found in the profile, consider adding element in "); - - return appProf; } } 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); @@ -193,34 +119,44 @@ public class ApplicationProfileReaderForCatalogueResolver { } + + /** + * @return the hashVreNameScope + */ + public Map getHashVreNameScope() { + + return hashVreNameScope; + } + /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { + StringBuilder builder = new StringBuilder(); - builder.append("ApplicationProfileReader [secondaryType="); + builder.append("ApplicationProfileReaderForCatalogueResolver [logger="); + builder.append(logger); + builder.append(", secondaryType="); builder.append(secondaryType); - builder.append(", appId="); + builder.append(", resourceName="); builder.append(resourceName); builder.append(", scope="); builder.append(scope); - builder.append(", applicationProfile="); - builder.append(applicationProfile); + builder.append(", useRootScope="); + builder.append(useRootScope); + builder.append(", hashVreNameScope="); + builder.append(hashVreNameScope); builder.append("]"); return builder.toString(); } - /* - public static void main(String[] args) { - String portletClassName = "org.gcube.portlets.user.gisviewerapp.server.GisViewerAppServiceImpl"; + /*public static void main(String[] args) { String scope ="/gcube"; - String secondaryType = "ApplicationProfile"; - ApplicationProfileReader reader = new ApplicationProfileReader(scope, secondaryType, portletClassName); - + ApplicationProfileReaderForCatalogueResolver reader = new ApplicationProfileReaderForCatalogueResolver(scope, true); System.out.println(reader); - }*/ + } diff --git a/src/main/java/org/gcube/datatransfer/resolver/catalogue/CatalogueResolver.java b/src/main/java/org/gcube/datatransfer/resolver/catalogue/CatalogueResolver.java index 88a4403..15b1aba 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/catalogue/CatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/catalogue/CatalogueResolver.java @@ -51,6 +51,8 @@ public class CatalogueResolver extends HttpServlet{ /** The logger. */ private static final Logger logger = LoggerFactory.getLogger(CatalogueResolver.class); + private ApplicationProfileReaderForCatalogueResolver appPrCatResolver; + /** The scope to enc decr. */ private String scopeToEncDecr = null; @@ -77,10 +79,12 @@ public class CatalogueResolver extends HttpServlet{ return scopeToEncDecr; scopeToEncDecr = System.getenv(ENV_SCOPE); - logger.info("Reading Environment Variable "+ENV_SCOPE+" to get the scope for encrypt/descrypt"); + logger.info("Reading Environment Variable "+ENV_SCOPE+" to get the scope for encrypt/descrypt; I read scope: "+scopeToEncDecr); if(scopeToEncDecr == null || scopeToEncDecr.isEmpty()) throw new ServletException(CatalogueResolver.class.getName() +" cannot work without set the Environment Variable: "+ENV_SCOPE); + appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); + logger.info("Reosurce for Catalogue Resolver: "+appPrCatResolver); return scopeToEncDecr; } @@ -95,14 +99,14 @@ public class CatalogueResolver extends HttpServlet{ String clearCatalogueQueryLink = req.getParameter(DIRECT_CATALOGUE_LINK_PARAM); String encryptedCatalogueQueryLink = req.getParameter(ENC_CATALOGUE_LINK_PARAM); - boolean foundLink = false; + boolean foundClearLink = false; if(clearCatalogueQueryLink==null || clearCatalogueQueryLink.isEmpty()){ logger.info("Link is not a direct link to catalogue"); }else - foundLink = true; + foundClearLink = true; String queryStringToCat = ""; - if(foundLink){ + if(foundClearLink){ logger.info("Trying to resolve clear link to catalogue using query link: "+clearCatalogueQueryLink); queryStringToCat = clearCatalogueQueryLink; }else{ @@ -152,6 +156,20 @@ public class CatalogueResolver extends HttpServlet{ return; } + /** + * I'm replacing VRE_NAME passed into parameter GCUBE_SCOPE with the scope read from ApplicationProfileReaderForCatalogueResolver#RESOURCE_NAME + * that is the full scope + */ + if(foundClearLink){ + if(appPrCatResolver==null) + appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); + + String fullScope = appPrCatResolver.getHashVreNameScope().get(scope); + logger.debug("Read fullScope: "+fullScope + " for VRE_NAME: "+scope +" from Map and overring it"); + cer.addParameterToRequest(CatalogueRequestParameter.GCUBE_SCOPE.getKey(), fullScope); + scope = fullScope; + } + String ckanPorltetUrl = ""; try{ logger.info("Using scope "+scope+ " to search Ckan Portlet URL from IS");