From 04d897ee7c30d61c2d8eecac6fcaeecf9ab13b5d Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Mon, 5 Nov 2018 14:45:31 +0000 Subject: [PATCH] completed caches and refactored git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@173959 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../CatalogueApplicationProfilesCache.java | 107 ++ ...atalogueApplicationProfilesGuavaCache.java | 60 - ...va => GeoExplorerApplicationURLCache.java} | 16 +- ...ache.java => GeonetworkInstanceCache.java} | 15 +- ...java => GisViewerApplicationURLCache.java} | 15 +- .../resolver/catalogue/CatalogueResolver.java | 1092 ++++++++--------- .../listeners/UriResolverStartupListener.java | 61 +- .../resolver/scope/ScopeUtil.java | 33 +- .../resolver/services/CatalogueResolver.java | 24 +- .../resolver/services/GisResolver.java | 24 +- 10 files changed, 760 insertions(+), 687 deletions(-) create mode 100644 src/main/java/org/gcube/datatransfer/resolver/caches/CatalogueApplicationProfilesCache.java delete mode 100644 src/main/java/org/gcube/datatransfer/resolver/caches/CatalogueApplicationProfilesGuavaCache.java rename src/main/java/org/gcube/datatransfer/resolver/caches/{GeoExplorerApplicationURLGuavaCache.java => GeoExplorerApplicationURLCache.java} (83%) rename src/main/java/org/gcube/datatransfer/resolver/caches/{GeonetworkInstanceGuavaCache.java => GeonetworkInstanceCache.java} (86%) rename src/main/java/org/gcube/datatransfer/resolver/caches/{GisViewerApplicationURLGuavaCache.java => GisViewerApplicationURLCache.java} (84%) diff --git a/src/main/java/org/gcube/datatransfer/resolver/caches/CatalogueApplicationProfilesCache.java b/src/main/java/org/gcube/datatransfer/resolver/caches/CatalogueApplicationProfilesCache.java new file mode 100644 index 0000000..152fb10 --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/caches/CatalogueApplicationProfilesCache.java @@ -0,0 +1,107 @@ +/** + * + */ +package org.gcube.datatransfer.resolver.caches; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import org.gcube.datatransfer.resolver.catalogue.resource.ApplicationProfileReaderForCatalogueResolver; +import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; + + + +/** + * The Class CatalogueApplicationProfilesCache. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Nov 5, 2018 + */ +public class CatalogueApplicationProfilesCache { + + private static Logger logger = LoggerFactory.getLogger(CatalogueApplicationProfilesCache.class); + private static LoadingCache catalogueApplicationProfiles; + + static{ + + CacheLoader loader = new CacheLoader(){ + + @Override + public String load(String vreName) + throws Exception { + + logger.info(CatalogueApplicationProfilesCache.class.getSimpleName() +" loaded"); + return loadApplicationProfiles(vreName); + } + + }; + + RemovalListener removalListener = new RemovalListener() { + + @Override + public void onRemoval(RemovalNotification arg0) { + + logger.info(CatalogueApplicationProfilesCache.class.getSimpleName() +" cache expired"); + + } + }; + + catalogueApplicationProfiles = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite( + 1, TimeUnit.HOURS).removalListener(removalListener). + build(loader); + + //PRE-POPULATE CACHE + ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(UriResolverStartupListener.getContextScope(), true); + catalogueApplicationProfiles.asMap().putAll(appPrCatResolver.getHashVreNameScope()); + + logger.info(CatalogueApplicationProfilesCache.class.getSimpleName() +" instancied "+catalogueApplicationProfiles); + logger.info("Pre-Loaded cache is: "+catalogueApplicationProfiles.toString()); + + } + + /** + * Gets the cache. + * + * @return the cache + */ + public static LoadingCache getCache(){ + return catalogueApplicationProfiles; + } + + + + /** + * Load application profiles. + * + * @param vreName the vre name + * @return the string + */ + public static String loadApplicationProfiles(String vreName){ + + String fullScope = null; + try { + + fullScope = catalogueApplicationProfiles.get(vreName); + } + catch (ExecutionException e) { + logger.warn("ExecutionException: ",e); + } + + if(fullScope==null){ + logger.debug("FullScope is null for VRE_NAME: "+vreName+" into Application Profile: "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME+", reading profile again"); + ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(UriResolverStartupListener.getContextScope(), true); + catalogueApplicationProfiles.asMap().putAll(appPrCatResolver.getHashVreNameScope()); + } + + return fullScope; + } + +} diff --git a/src/main/java/org/gcube/datatransfer/resolver/caches/CatalogueApplicationProfilesGuavaCache.java b/src/main/java/org/gcube/datatransfer/resolver/caches/CatalogueApplicationProfilesGuavaCache.java deleted file mode 100644 index 8111608..0000000 --- a/src/main/java/org/gcube/datatransfer/resolver/caches/CatalogueApplicationProfilesGuavaCache.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * - */ -package org.gcube.datatransfer.resolver.caches; - -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; - - -/** - * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Nov 5, 2018 - */ -public class CatalogueApplicationProfilesGuavaCache { - - private static Logger logger = LoggerFactory.getLogger(CatalogueApplicationProfilesGuavaCache.class); - private static LoadingCache catalogueApplicationProfiles; - - static{ - - CacheLoader loader = new CacheLoader(){ - - @Override - public String load(String arg0) - throws Exception { - - logger.info(CatalogueApplicationProfilesGuavaCache.class.getSimpleName() +" loaded"); - //ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); - return null; - } - - }; - - RemovalListener removalListener = new RemovalListener() { - - @Override - public void onRemoval(RemovalNotification arg0) { - - logger.info(CatalogueApplicationProfilesGuavaCache.class.getSimpleName() +" cache expired"); - - } - }; - - catalogueApplicationProfiles = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite( - 1, TimeUnit.HOURS).removalListener(removalListener). - build(loader); - - - - } -} diff --git a/src/main/java/org/gcube/datatransfer/resolver/caches/GeoExplorerApplicationURLGuavaCache.java b/src/main/java/org/gcube/datatransfer/resolver/caches/GeoExplorerApplicationURLCache.java similarity index 83% rename from src/main/java/org/gcube/datatransfer/resolver/caches/GeoExplorerApplicationURLGuavaCache.java rename to src/main/java/org/gcube/datatransfer/resolver/caches/GeoExplorerApplicationURLCache.java index 9403163..4311aad 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/caches/GeoExplorerApplicationURLGuavaCache.java +++ b/src/main/java/org/gcube/datatransfer/resolver/caches/GeoExplorerApplicationURLCache.java @@ -18,15 +18,16 @@ import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalNotification; + /** - * The Class GeoExplorerApplicationHostnameGuavaCache. + * The Class GeoExplorerApplicationURLCache. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) - * Nov 2, 2018 + * Nov 5, 2018 */ -public class GeoExplorerApplicationURLGuavaCache { +public class GeoExplorerApplicationURLCache { - private static Logger logger = LoggerFactory.getLogger(GeoExplorerApplicationURLGuavaCache.class); + private static Logger logger = LoggerFactory.getLogger(GeoExplorerApplicationURLCache.class); //A cache (Scope, GeoExplorer-URL) private static LoadingCache geoExplorerApplicationURLCache; @@ -37,14 +38,14 @@ public class GeoExplorerApplicationURLGuavaCache { @Override public String load(String scope) throws Exception { - logger.info(GeoExplorerApplicationURLGuavaCache.class.getSimpleName() +" loaded"); + logger.info(GeoExplorerApplicationURLCache.class.getSimpleName() +" loaded"); return loadGeoExplorerApplicationURL(scope); } }; RemovalListener removalListener = new RemovalListener() { public void onRemoval(RemovalNotification removal) { - logger.info(GeoExplorerApplicationURLGuavaCache.class.getSimpleName() +" cache expired"); + logger.info(GeoExplorerApplicationURLCache.class.getSimpleName() +" cache expired"); } }; @@ -53,7 +54,7 @@ public class GeoExplorerApplicationURLGuavaCache { 1, TimeUnit.DAYS).removalListener(removalListener). build(loader); - logger.info(GeoExplorerApplicationURLGuavaCache.class.getSimpleName() +" instancied"); + logger.info(GeoExplorerApplicationURLCache.class.getSimpleName() +" instancied"); } /** @@ -83,6 +84,5 @@ public class GeoExplorerApplicationURLGuavaCache { String url = reader.getApplicationProfile().getUrl(); logger.info("With scope "+scope+" loaded the GeoExplorer Application URL "+url); return url; - } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/caches/GeonetworkInstanceGuavaCache.java b/src/main/java/org/gcube/datatransfer/resolver/caches/GeonetworkInstanceCache.java similarity index 86% rename from src/main/java/org/gcube/datatransfer/resolver/caches/GeonetworkInstanceGuavaCache.java rename to src/main/java/org/gcube/datatransfer/resolver/caches/GeonetworkInstanceCache.java index b88b5d0..25c5440 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/caches/GeonetworkInstanceGuavaCache.java +++ b/src/main/java/org/gcube/datatransfer/resolver/caches/GeonetworkInstanceCache.java @@ -20,15 +20,16 @@ import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalNotification; + /** - * The Class GeoentworkInstanceGuavaCache. + * The Class GeonetworkInstanceCache. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) - * Nov 2, 2018 + * Nov 5, 2018 */ -public class GeonetworkInstanceGuavaCache { +public class GeonetworkInstanceCache { - private static Logger logger = LoggerFactory.getLogger(GeonetworkInstanceGuavaCache.class); + private static Logger logger = LoggerFactory.getLogger(GeonetworkInstanceCache.class); //A cache (Scope, GeonetworkInstance) private static LoadingCache geonetworkInstancesCache; @@ -39,14 +40,14 @@ public class GeonetworkInstanceGuavaCache { @Override public GeonetworkInstance load(String scope) throws Exception { - logger.info(GeonetworkInstanceGuavaCache.class.getSimpleName() +" loaded"); + logger.info(GeonetworkInstanceCache.class.getSimpleName() +" loaded"); return loadGeonetworkInstance(scope); } }; RemovalListener removalListener = new RemovalListener() { public void onRemoval(RemovalNotification removal) { - logger.info(GeonetworkInstanceGuavaCache.class.getSimpleName() +" cache expired"); + logger.info(GeonetworkInstanceCache.class.getSimpleName() +" cache expired"); } }; @@ -55,7 +56,7 @@ public class GeonetworkInstanceGuavaCache { 1, TimeUnit.DAYS).removalListener(removalListener). build(loader); - logger.info(GeonetworkInstanceGuavaCache.class.getSimpleName() +" instancied"); + logger.info(GeonetworkInstanceCache.class.getSimpleName() +" instancied"); } diff --git a/src/main/java/org/gcube/datatransfer/resolver/caches/GisViewerApplicationURLGuavaCache.java b/src/main/java/org/gcube/datatransfer/resolver/caches/GisViewerApplicationURLCache.java similarity index 84% rename from src/main/java/org/gcube/datatransfer/resolver/caches/GisViewerApplicationURLGuavaCache.java rename to src/main/java/org/gcube/datatransfer/resolver/caches/GisViewerApplicationURLCache.java index 6464cf6..743e5d0 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/caches/GisViewerApplicationURLGuavaCache.java +++ b/src/main/java/org/gcube/datatransfer/resolver/caches/GisViewerApplicationURLCache.java @@ -17,15 +17,16 @@ import com.google.common.cache.LoadingCache; import com.google.common.cache.RemovalListener; import com.google.common.cache.RemovalNotification; + /** - * The Class GisViewerApplicationHostnameGuavaCache. + * The Class GisViewerApplicationURLCache. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) - * Nov 2, 2018 + * Nov 5, 2018 */ -public class GisViewerApplicationURLGuavaCache { +public class GisViewerApplicationURLCache { - private static Logger logger = LoggerFactory.getLogger(GisViewerApplicationURLGuavaCache.class); + private static Logger logger = LoggerFactory.getLogger(GisViewerApplicationURLCache.class); //A cache (Scope, GisViewerApplication-URL) private static LoadingCache gisViewerApplicationURLCache; @@ -36,14 +37,14 @@ public class GisViewerApplicationURLGuavaCache { @Override public String load(String scope) throws Exception { - logger.info(GisViewerApplicationURLGuavaCache.class.getSimpleName() +" loaded"); + logger.info(GisViewerApplicationURLCache.class.getSimpleName() +" loaded"); return loadGisViewerApplicationURL(scope); } }; RemovalListener removalListener = new RemovalListener() { public void onRemoval(RemovalNotification removal) { - logger.info(GisViewerApplicationURLGuavaCache.class.getSimpleName() +" cache expired"); + logger.info(GisViewerApplicationURLCache.class.getSimpleName() +" cache expired"); } }; @@ -52,7 +53,7 @@ public class GisViewerApplicationURLGuavaCache { 1, TimeUnit.DAYS).removalListener(removalListener). build(loader); - logger.info(GisViewerApplicationURLGuavaCache.class.getSimpleName() +" instancied"); + logger.info(GisViewerApplicationURLCache.class.getSimpleName() +" instancied"); } 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 fa9ecbb..61b99c7 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/catalogue/CatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/catalogue/CatalogueResolver.java @@ -1,546 +1,546 @@ -/** - * - */ -package org.gcube.datatransfer.resolver.catalogue; - -import java.io.IOException; -import java.io.StringReader; -import java.io.UnsupportedEncodingException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.codec.binary.Base64; -import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.io.IOUtils; -import org.gcube.common.encryption.StringEncrypter; -import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.datatransfer.resolver.ResourceCatalogueCodes; -import org.gcube.datatransfer.resolver.UriResolverRewriteFilter; -import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileNotFoundException; -import org.gcube.datatransfer.resolver.catalogue.resource.ApplicationProfileReaderForCatalogueResolver; -import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader; -import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference; -import org.gcube.datatransfer.resolver.catalogue.resource.UpdateApplicationProfileCatalogueResolver; -import org.gcube.datatransfer.resolver.scope.ScopeUtil; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import eu.trentorise.opendata.jackan.model.CkanDataset; - - -/** - * The Class GisResolver. - * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Jan 7, 2016 - */ -public class CatalogueResolver extends HttpServlet{ - - private static final long serialVersionUID = -8273405286016095823L; - - private static final String TEXT_PALIN_CHARSET_UTF_8 = "text/plain;charset=UTF-8"; - public static final String UTF_8 = "UTF-8"; - - public static final String ENC_CATALOGUE_LINK_PARAM = UriResolverRewriteFilter.PARAMETER_ENC_CATALOGUE_LINK; - - public static final String DIRECT_CATALOGUE_LINK_PARAM = UriResolverRewriteFilter.PARAMETER_DIRECT_CATALOGUE_LINK; - - private static final String PATH_SEPARATOR = "/"; - - public static final String PARAMETER_PATH = "path"; - - public static final String ENV_SCOPE = "SCOPE"; //Environment Variable - - /** The logger. */ - private static final Logger logger = LoggerFactory.getLogger(CatalogueResolver.class); - - private ApplicationProfileReaderForCatalogueResolver appPrCatResolver; - - - /** The scope to enc decr. */ - private String scopeToEncDecr = null; - - - /* (non-Javadoc) - * @see javax.servlet.GenericServlet#init() - */ - @Override - public void init() throws ServletException { - initScopeFromEnv(); - } - - - /** - * Inits the scope from env. - * - * @return the string - * @throws ServletException the servlet exception - */ - private String initScopeFromEnv() throws ServletException{ - - if(scopeToEncDecr!=null) - return scopeToEncDecr; - - try{ - scopeToEncDecr = ScopeUtil.getScopeFromEnvironment(); - }catch(ServletException e){ - logger.error("I cannot encrypt/descrypt: ", e); - throw new ServletException(CatalogueResolver.class.getName() +" cannot work without set the Environment Variable: "+ENV_SCOPE); - } - - appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); - logger.info("Resource read for CatalogueResolver: "+appPrCatResolver); - return scopeToEncDecr; - } - - /* (non-Javadoc) - * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) - */ - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - String originalScope = ScopeProvider.instance.get(); - String clearCatalogueQueryLink = req.getParameter(DIRECT_CATALOGUE_LINK_PARAM); - String encryptedCatalogueQueryLink = req.getParameter(ENC_CATALOGUE_LINK_PARAM); - logger.debug("Read original scope from ScopeProvider: "+originalScope); - - boolean foundClearLink = false; - if(clearCatalogueQueryLink==null || clearCatalogueQueryLink.isEmpty()){ - logger.info("Link is not a direct link to catalogue"); - }else - foundClearLink = true; - - String queryStringToCat = ""; - if(foundClearLink){ - logger.info("Trying to resolve clear link to catalogue using query link: "+clearCatalogueQueryLink); - queryStringToCat = clearCatalogueQueryLink; - }else{ - logger.info("Trying to resolve encripted link to catalogue using query link: "+encryptedCatalogueQueryLink); - if(encryptedCatalogueQueryLink==null || encryptedCatalogueQueryLink.isEmpty()){ - logger.error("Data Catalogue Link is malformed, set "+ENC_CATALOGUE_LINK_PARAM+" parameter"); - sendError(resp, HttpStatus.SC_BAD_REQUEST, "Data Catalogue Link is malformed, either you must set "+ENC_CATALOGUE_LINK_PARAM+" parameter or "+DIRECT_CATALOGUE_LINK_PARAM+ " parameter"); - return; - } - String base64DecodedId = ""; - - try { - - base64DecodedId = base64DecodeString(encryptedCatalogueQueryLink); - logger.info("Base 64 decoded Data Catalogue Link: "+base64DecodedId +", now decrypting..."); - - if(scopeToEncDecr==null) - initScopeFromEnv(); - - ScopeProvider.instance.set(scopeToEncDecr); - queryStringToCat = StringEncrypter.getEncrypter().decrypt(base64DecodedId); - logger.info("Decrypted Data Catalogue Link: "+queryStringToCat); - - }catch (Exception e) { - logger.error("An error occurred during decrypting data catalogue link: "+base64DecodedId+", using the scope: "+scopeToEncDecr, e); - sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "The system cannot decrypt the Catalogue Link"); - return; - }finally{ - - if(originalScope!=null){ - ScopeProvider.instance.set(originalScope); - logger.info("scope provider set to orginal scope: "+originalScope); - }else{ - ScopeProvider.instance.reset(); - logger.info("scope provider reset"); - } - - } - } - - CatalogueEntityRequest cer = new CatalogueEntityRequest(); - for (CatalogueRequestParameter parameter : CatalogueRequestParameter.values()) { - String value = getValueOfParameter(parameter.getKey(), queryStringToCat); - cer.addParameterToRequest(parameter.getKey(), value); - } - - if(cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey()).compareToIgnoreCase("product")==0){ - logger.debug("Read "+CatalogueRequestParameter.ENTITY_CONTEXT.getKey() + " value: 'product' replacing with 'dataset'"); - cer.addParameterToRequest(CatalogueRequestParameter.ENTITY_CONTEXT.getKey(), "dataset"); - } - - logger.debug("Read parameters: "+cer.toString()); - - String scope = cer.getValueOfParameter(CatalogueRequestParameter.GCUBE_SCOPE.getKey()); - if(scope==null || scope.isEmpty()){ - logger.error("An error occurred during resolving data catalogue link: the scope to search CKan Portlet is null or empty"); - sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "The system cannot resolve the Catalogue Link, the scope is null or empty"); - 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); - - if(fullScope==null){ - logger.debug("FullScope is null for VRE_NAME: "+scope+" into Application Profile: "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME+", reading profile again"); - appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); - fullScope = appPrCatResolver.getHashVreNameScope().get(scope); - } - - logger.debug("Read fullScope: "+fullScope + " for VRE_NAME: "+scope +" into Application Profile "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME); - cer.addParameterToRequest(CatalogueRequestParameter.GCUBE_SCOPE.getKey(), fullScope); - scope = fullScope; - } - - String ckanPorltetUrl = ""; - try{ - logger.info("Setting scope "+scope+ " to search Ckan Portlet URL from IS"); - ScopeProvider.instance.set(scope); - GatewayCKANCatalogueReference ckanCatalogueReference = CkanCatalogueConfigurationsReader.loadCatalogueEndPoints(); - - //IS THE PRODUCT PLUBLIC OR PRIVATE? - //USING ACCESS TO PUBLIC PORTLET IF THE ITEM IS PUBLIC, OTHERWISE ACCESS TO PRIVATE PORTLET - ckanPorltetUrl = ckanCatalogueReference.getPrivatePortletURL(); - String datasetName = cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); - if(ckanCatalogueReference.getCkanURL()!=null){ - try{ - CkanDataset dataset = CkanCatalogueConfigurationsReader.getDataset(datasetName, ckanCatalogueReference.getCkanURL()); - if(dataset!=null){ - ckanPorltetUrl = ckanCatalogueReference.getPublicPortletURL(); - logger.info("The dataset "+datasetName+" is a public item using public access to CKAN portlet: "+ckanPorltetUrl); - } - }catch(Exception e){ - logger.warn("Error on checking if dataset: "+datasetName+" is private or not", e); - ckanPorltetUrl = ckanCatalogueReference.getPublicPortletURL(); - } - } - - - if(ckanPorltetUrl == null || ckanPorltetUrl.isEmpty()){ - sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during discovery the resource: "+CkanCatalogueConfigurationsReader.APPLICATION_PROFILE_NAME+" in the scope: "+scope+", try again later"); - return; - } - - - }catch(Exception e){ - logger.error("Error during discovery the resource: "+CkanCatalogueConfigurationsReader.APPLICATION_PROFILE_NAME+" in the scope: "+scope, e); - sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during discovery the resource: "+CkanCatalogueConfigurationsReader.APPLICATION_PROFILE_NAME+" in the scope: "+scope+", try again later"); - return; - - }finally{ - - if(originalScope!=null){ - ScopeProvider.instance.set(originalScope); - logger.info("scope provider set to orginal scope: "+originalScope); - }else{ - ScopeProvider.instance.reset(); - logger.info("scope provider reset"); - } - - } - - //UrlEncoderUtil.encodeQuery(cer.getParameters()); - String buildPath = PARAMETER_PATH +"="; - buildPath+= PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey()) + PATH_SEPARATOR; - buildPath+=cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); - - - String finalUrl = ckanPorltetUrl+"?"+buildPath; - logger.info("Builded final URL: "+finalUrl); - resp.sendRedirect(resp.encodeRedirectURL(finalUrl)); - } - - - /* (non-Javadoc) - * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) - */ - @Override - protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - String originalScope = ScopeProvider.instance.get(); - final CatalogueEntityRequest cer = new CatalogueEntityRequest(); - try{ - String jsonRequest = IOUtils.toString(req.getInputStream()); - logger.debug("Read json request: "+jsonRequest); - JSONObject inputJson = new JSONObject(jsonRequest); - - for (CatalogueRequestParameter parameter : CatalogueRequestParameter.values()) { - - try{ - - switch (parameter) { - case QUERY_STRING: - //TODO must be implemented - JSONArray queryString = inputJson.getJSONArray(parameter.getKey()); - break; - default: - String value = inputJson.getString(parameter.getKey()); - logger.debug("Read value: "+value+", for parameter: "+parameter.getKey()); - cer.addParameterToRequest(parameter.getKey(), value); - break; - } - - }catch(Exception e){ - String error = ""; - try { - - if(parameter.isMandatory()){ - error = parameter.getKey() +" not found"; - sendError(resp, HttpStatus.SC_BAD_REQUEST, error); - return; - } - else - logger.debug("Not Mandatory parameter: "+parameter.getKey()+", not found, continuing..."); - - }catch (IOException e1) { - //silent - } - } - - } - }catch(JSONException e){ - try { - logger.error("Json passed is malformed: ", e); - sendError(resp, HttpStatus.SC_BAD_REQUEST, "Json passed is malformed"); - } - catch (IOException e1) { - //silent - } - return; - } - - try{ - //CHECK IF INPUT SCOPE IS VALID - String scope = cer.getValueOfParameter(CatalogueRequestParameter.GCUBE_SCOPE.getKey()); - if(!scope.startsWith("/")){ - logger.info("Scope not start with char '/' adding it"); - scope+="/"+scope; - cer.addParameterToRequest(CatalogueRequestParameter.GCUBE_SCOPE.getKey(), scope); - } - - String buildLink = getServerURL(req); - //buildLink += req.getRequestURI(); //AVOIDNG TO ADD THE SERVLET NAME: 'catalogue', THERE IS THE URL REWRITE USED FOR 'uri-resolver' - - String clearURL = cer.getValueOfParameter(CatalogueRequestParameter.CLEAR_URL.getKey()); - - boolean bClearURL = false; - try{ - bClearURL = Boolean.parseBoolean(clearURL); - }catch(Exception e){ - //silent - } - - resp.setContentType(TEXT_PALIN_CHARSET_UTF_8); - resp.setCharacterEncoding(UTF_8); - - logger.info("Clear URL is: "+bClearURL); - if(bClearURL){ - - final String vreName = scope.substring(scope.lastIndexOf("/")+1, scope.length()); - //buildLink+=PATH_SEPARATOR+vreName+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey())+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); - String econtext = cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey()); - ResourceCatalogueCodes rc = ResourceCatalogueCodes.valueOfCodeValue(econtext); - if(rc==null){ - logger.error("Entity context is null/malformed"); - sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error during generating Data Catalogue Link, the entity context passed is not recognized. Is it malformed?"); - return; - } - buildLink += PATH_SEPARATOR+rc.getId()+PATH_SEPARATOR+vreName+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); - logger.info("Writing Decoded Catalogue Link: "+buildLink); - - //IT'S GOING TO UPDATE THE GENERIC RESOURCE IF IS NEEDED - new Thread(){ - public void run() { - try { - String fullScope = cer.getValueOfParameter(CatalogueRequestParameter.GCUBE_SCOPE.getKey()); - boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver.validateEndPoint(scopeToEncDecr, vreName, fullScope); - - if(endPointUpdated) - appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); - - } - catch (ApplicationProfileNotFoundException e) { - logger.error("Error during validating Application Profile", e); - } - }; - }.start(); - - }else{ - //ADDING THE SERVLET NAME - buildLink += req.getRequestURI(); - - cer.removeParameterToRequest(CatalogueRequestParameter.CLEAR_URL.getKey()); - logger.info("Using scope "+scopeToEncDecr+ " from env to get encrypt key"); - ScopeProvider.instance.set(scopeToEncDecr); - //String query = UrlEncoderUtil.encodeQuery(cer.getParameters()); - String query = ""; - for (String key : cer.getParameters().keySet()) { - query+=key+"="+ cer.getParameters().get(key) +"&"; - } - query = UrlEncoderUtil.removeLastChar(query); - logger.info("Builded query string: "+query); - String encriptedQuery = StringEncrypter.getEncrypter().encrypt(query); - logger.info("Encrypted query: "+encriptedQuery); - String encodedQuery = base64EncodeStringURLSafe(encriptedQuery); - logger.info("Catalogue Query Link: "+encodedQuery); - buildLink+=PATH_SEPARATOR+encodedQuery; - logger.info("Writing Encoded Catalogue Link: "+buildLink); - } - - resp.getWriter().write(buildLink); - - }catch(Exception e){ - try { - logger.error("An internal error is occurred: ", e); - sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during generating Data Catalogue Link, try again later"); - return; - } - catch (IOException e1) { - //silent - } - }finally{ - - if(originalScope!=null){ - ScopeProvider.instance.set(originalScope); - logger.info("scope provider set to orginal scope: "+originalScope); - }else{ - ScopeProvider.instance.reset(); - logger.info("scope provider reset"); - } - } - - } - - /** - * Send error. - * - * @param response the response - * @param status the status - * @param message the message - * @throws IOException Signals that an I/O exception has occurred. - */ - protected static void sendError(HttpServletResponse response, int status, String message) throws IOException{ - - logger.error("error message: "+message); - logger.info("writing response..."); - - if(response==null) - return; - - response.setStatus(status); - StringReader sr = new StringReader(message); - IOUtils.copy(sr, response.getOutputStream()); - logger.info("response writed"); - response.flushBuffer(); - } - - - /** - * Gets the server url. - * - * @param req the req - * @return the server url - */ - public String getServerURL(HttpServletRequest req) { - - String scheme = req.getScheme(); // http - String serverName = req.getServerName(); // hostname.com - int serverPort = req.getServerPort(); // 80 - //String contextPath = req.getContextPath(); // /mywebapp - - // Reconstruct original requesting URL - StringBuffer url = new StringBuffer(); - url.append(scheme).append("://").append(serverName); - - if (serverPort != 80 && serverPort != 443) - url.append(":").append(serverPort); - -// if(contextPath!=null) -// url.append(":").append(contextPath); - - String uToS = url.toString(); - logger.debug("returning servlet context URL: "+uToS); - return uToS; - } - - - /** - * Gets the value of parameter in the passed query string, null otherwise - * - * @param parameter the parameter - * @param httpQueryString the http query string - * @return the value of parameter if exists, null otherwise - */ - public static String getValueOfParameter(String parameter, String httpQueryString) { -// logger.trace("finding: "+wmsParam +" into "+url); - int index = httpQueryString.toLowerCase().indexOf(parameter.toLowerCase()+"="); //ADDING CHAR "=" IN TAIL TO BE SECURE IT IS A PARAMETER -// logger.trace("start index of "+wmsParam+ " is: "+index); - String value = ""; - if(index > -1){ - - int start = index + parameter.length()+1; //add +1 for char '=' - String sub = httpQueryString.substring(start, httpQueryString.length()); - int indexOfSeparator = sub.indexOf("&"); - int end = indexOfSeparator!=-1?indexOfSeparator:sub.length(); - value = sub.substring(0, end); - }else - return null; - -// logger.trace("return value: "+value); - return value; - } - - /** - * Base64 encode string url safe. - * - * @param s the s - * @return the string - */ - public static String base64EncodeStringURLSafe(String s) { - - try { - return Base64.encodeBase64URLSafeString(s.getBytes(UTF_8)); - } - catch (UnsupportedEncodingException e) { - logger.error("Failed to decode the String", e); - logger.error("Returning input string: " + s); - return s; - } - } - - /** - * Base64 decode string. - * - * @param s the s - * @return the string - */ - public static String base64DecodeString(String s) { - - try { - return new String(Base64.decodeBase64(s.getBytes(UTF_8))); - } - catch (UnsupportedEncodingException e) { - logger.error("Failed to decode the String", e); - logger.error("Returning input string: " + s); - return s; - } - } - - public static void main(String[] args) { - - try{ - String scope = "/d4science.research-infrastructures.eu"; - ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scope, true); - logger.info("Reosurce for Catalogue Resolver: "+appPrCatResolver); - }catch(Exception e){ - e.printStackTrace(); - } - } -} +///** +// * +// */ +//package org.gcube.datatransfer.resolver.catalogue; +// +//import java.io.IOException; +//import java.io.StringReader; +//import java.io.UnsupportedEncodingException; +// +//import javax.servlet.ServletException; +//import javax.servlet.http.HttpServlet; +//import javax.servlet.http.HttpServletRequest; +//import javax.servlet.http.HttpServletResponse; +// +//import org.apache.commons.codec.binary.Base64; +//import org.apache.commons.httpclient.HttpStatus; +//import org.apache.commons.io.IOUtils; +//import org.gcube.common.encryption.StringEncrypter; +//import org.gcube.common.scope.api.ScopeProvider; +//import org.gcube.datatransfer.resolver.ResourceCatalogueCodes; +//import org.gcube.datatransfer.resolver.UriResolverRewriteFilter; +//import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileNotFoundException; +//import org.gcube.datatransfer.resolver.catalogue.resource.ApplicationProfileReaderForCatalogueResolver; +//import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader; +//import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference; +//import org.gcube.datatransfer.resolver.catalogue.resource.UpdateApplicationProfileCatalogueResolver; +//import org.gcube.datatransfer.resolver.scope.ScopeUtil; +//import org.json.JSONArray; +//import org.json.JSONException; +//import org.json.JSONObject; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; +// +//import eu.trentorise.opendata.jackan.model.CkanDataset; +// +// +///** +// * The Class GisResolver. +// * +// * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it +// * Jan 7, 2016 +// */ +//public class CatalogueResolver extends HttpServlet{ +// +// private static final long serialVersionUID = -8273405286016095823L; +// +// private static final String TEXT_PALIN_CHARSET_UTF_8 = "text/plain;charset=UTF-8"; +// public static final String UTF_8 = "UTF-8"; +// +// public static final String ENC_CATALOGUE_LINK_PARAM = UriResolverRewriteFilter.PARAMETER_ENC_CATALOGUE_LINK; +// +// public static final String DIRECT_CATALOGUE_LINK_PARAM = UriResolverRewriteFilter.PARAMETER_DIRECT_CATALOGUE_LINK; +// +// private static final String PATH_SEPARATOR = "/"; +// +// public static final String PARAMETER_PATH = "path"; +// +// public static final String ENV_SCOPE = "SCOPE"; //Environment Variable +// +// /** The logger. */ +// private static final Logger logger = LoggerFactory.getLogger(CatalogueResolver.class); +// +// private ApplicationProfileReaderForCatalogueResolver appPrCatResolver; +// +// +// /** The scope to enc decr. */ +// private String scopeToEncDecr = null; +// +// +// /* (non-Javadoc) +// * @see javax.servlet.GenericServlet#init() +// */ +// @Override +// public void init() throws ServletException { +// initScopeFromEnv(); +// } +// +// +// /** +// * Inits the scope from env. +// * +// * @return the string +// * @throws ServletException the servlet exception +// */ +// private String initScopeFromEnv() throws ServletException{ +// +// if(scopeToEncDecr!=null) +// return scopeToEncDecr; +// +// try{ +// scopeToEncDecr = ScopeUtil.getScopeFromEnvironment(); +// }catch(ServletException e){ +// logger.error("I cannot encrypt/descrypt: ", e); +// throw new ServletException(CatalogueResolver.class.getName() +" cannot work without set the Environment Variable: "+ENV_SCOPE); +// } +// +// appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); +// logger.info("Resource read for CatalogueResolver: "+appPrCatResolver); +// return scopeToEncDecr; +// } +// +// /* (non-Javadoc) +// * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) +// */ +// @Override +// protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { +// +// String originalScope = ScopeProvider.instance.get(); +// String clearCatalogueQueryLink = req.getParameter(DIRECT_CATALOGUE_LINK_PARAM); +// String encryptedCatalogueQueryLink = req.getParameter(ENC_CATALOGUE_LINK_PARAM); +// logger.debug("Read original scope from ScopeProvider: "+originalScope); +// +// boolean foundClearLink = false; +// if(clearCatalogueQueryLink==null || clearCatalogueQueryLink.isEmpty()){ +// logger.info("Link is not a direct link to catalogue"); +// }else +// foundClearLink = true; +// +// String queryStringToCat = ""; +// if(foundClearLink){ +// logger.info("Trying to resolve clear link to catalogue using query link: "+clearCatalogueQueryLink); +// queryStringToCat = clearCatalogueQueryLink; +// }else{ +// logger.info("Trying to resolve encripted link to catalogue using query link: "+encryptedCatalogueQueryLink); +// if(encryptedCatalogueQueryLink==null || encryptedCatalogueQueryLink.isEmpty()){ +// logger.error("Data Catalogue Link is malformed, set "+ENC_CATALOGUE_LINK_PARAM+" parameter"); +// sendError(resp, HttpStatus.SC_BAD_REQUEST, "Data Catalogue Link is malformed, either you must set "+ENC_CATALOGUE_LINK_PARAM+" parameter or "+DIRECT_CATALOGUE_LINK_PARAM+ " parameter"); +// return; +// } +// String base64DecodedId = ""; +// +// try { +// +// base64DecodedId = base64DecodeString(encryptedCatalogueQueryLink); +// logger.info("Base 64 decoded Data Catalogue Link: "+base64DecodedId +", now decrypting..."); +// +// if(scopeToEncDecr==null) +// initScopeFromEnv(); +// +// ScopeProvider.instance.set(scopeToEncDecr); +// queryStringToCat = StringEncrypter.getEncrypter().decrypt(base64DecodedId); +// logger.info("Decrypted Data Catalogue Link: "+queryStringToCat); +// +// }catch (Exception e) { +// logger.error("An error occurred during decrypting data catalogue link: "+base64DecodedId+", using the scope: "+scopeToEncDecr, e); +// sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "The system cannot decrypt the Catalogue Link"); +// return; +// }finally{ +// +// if(originalScope!=null){ +// ScopeProvider.instance.set(originalScope); +// logger.info("scope provider set to orginal scope: "+originalScope); +// }else{ +// ScopeProvider.instance.reset(); +// logger.info("scope provider reset"); +// } +// +// } +// } +// +// CatalogueEntityRequest cer = new CatalogueEntityRequest(); +// for (CatalogueRequestParameter parameter : CatalogueRequestParameter.values()) { +// String value = getValueOfParameter(parameter.getKey(), queryStringToCat); +// cer.addParameterToRequest(parameter.getKey(), value); +// } +// +// if(cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey()).compareToIgnoreCase("product")==0){ +// logger.debug("Read "+CatalogueRequestParameter.ENTITY_CONTEXT.getKey() + " value: 'product' replacing with 'dataset'"); +// cer.addParameterToRequest(CatalogueRequestParameter.ENTITY_CONTEXT.getKey(), "dataset"); +// } +// +// logger.debug("Read parameters: "+cer.toString()); +// +// String scope = cer.getValueOfParameter(CatalogueRequestParameter.GCUBE_SCOPE.getKey()); +// if(scope==null || scope.isEmpty()){ +// logger.error("An error occurred during resolving data catalogue link: the scope to search CKan Portlet is null or empty"); +// sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "The system cannot resolve the Catalogue Link, the scope is null or empty"); +// 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); +// +// if(fullScope==null){ +// logger.debug("FullScope is null for VRE_NAME: "+scope+" into Application Profile: "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME+", reading profile again"); +// appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); +// fullScope = appPrCatResolver.getHashVreNameScope().get(scope); +// } +// +// logger.debug("Read fullScope: "+fullScope + " for VRE_NAME: "+scope +" into Application Profile "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME); +// cer.addParameterToRequest(CatalogueRequestParameter.GCUBE_SCOPE.getKey(), fullScope); +// scope = fullScope; +// } +// +// String ckanPorltetUrl = ""; +// try{ +// logger.info("Setting scope "+scope+ " to search Ckan Portlet URL from IS"); +// ScopeProvider.instance.set(scope); +// GatewayCKANCatalogueReference ckanCatalogueReference = CkanCatalogueConfigurationsReader.loadCatalogueEndPoints(); +// +// //IS THE PRODUCT PLUBLIC OR PRIVATE? +// //USING ACCESS TO PUBLIC PORTLET IF THE ITEM IS PUBLIC, OTHERWISE ACCESS TO PRIVATE PORTLET +// ckanPorltetUrl = ckanCatalogueReference.getPrivatePortletURL(); +// String datasetName = cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); +// if(ckanCatalogueReference.getCkanURL()!=null){ +// try{ +// CkanDataset dataset = CkanCatalogueConfigurationsReader.getDataset(datasetName, ckanCatalogueReference.getCkanURL()); +// if(dataset!=null){ +// ckanPorltetUrl = ckanCatalogueReference.getPublicPortletURL(); +// logger.info("The dataset "+datasetName+" is a public item using public access to CKAN portlet: "+ckanPorltetUrl); +// } +// }catch(Exception e){ +// logger.warn("Error on checking if dataset: "+datasetName+" is private or not", e); +// ckanPorltetUrl = ckanCatalogueReference.getPublicPortletURL(); +// } +// } +// +// +// if(ckanPorltetUrl == null || ckanPorltetUrl.isEmpty()){ +// sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during discovery the resource: "+CkanCatalogueConfigurationsReader.APPLICATION_PROFILE_NAME+" in the scope: "+scope+", try again later"); +// return; +// } +// +// +// }catch(Exception e){ +// logger.error("Error during discovery the resource: "+CkanCatalogueConfigurationsReader.APPLICATION_PROFILE_NAME+" in the scope: "+scope, e); +// sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during discovery the resource: "+CkanCatalogueConfigurationsReader.APPLICATION_PROFILE_NAME+" in the scope: "+scope+", try again later"); +// return; +// +// }finally{ +// +// if(originalScope!=null){ +// ScopeProvider.instance.set(originalScope); +// logger.info("scope provider set to orginal scope: "+originalScope); +// }else{ +// ScopeProvider.instance.reset(); +// logger.info("scope provider reset"); +// } +// +// } +// +// //UrlEncoderUtil.encodeQuery(cer.getParameters()); +// String buildPath = PARAMETER_PATH +"="; +// buildPath+= PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey()) + PATH_SEPARATOR; +// buildPath+=cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); +// +// +// String finalUrl = ckanPorltetUrl+"?"+buildPath; +// logger.info("Builded final URL: "+finalUrl); +// resp.sendRedirect(resp.encodeRedirectURL(finalUrl)); +// } +// +// +// /* (non-Javadoc) +// * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) +// */ +// @Override +// protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { +// +// String originalScope = ScopeProvider.instance.get(); +// final CatalogueEntityRequest cer = new CatalogueEntityRequest(); +// try{ +// String jsonRequest = IOUtils.toString(req.getInputStream()); +// logger.debug("Read json request: "+jsonRequest); +// JSONObject inputJson = new JSONObject(jsonRequest); +// +// for (CatalogueRequestParameter parameter : CatalogueRequestParameter.values()) { +// +// try{ +// +// switch (parameter) { +// case QUERY_STRING: +// //TODO must be implemented +// JSONArray queryString = inputJson.getJSONArray(parameter.getKey()); +// break; +// default: +// String value = inputJson.getString(parameter.getKey()); +// logger.debug("Read value: "+value+", for parameter: "+parameter.getKey()); +// cer.addParameterToRequest(parameter.getKey(), value); +// break; +// } +// +// }catch(Exception e){ +// String error = ""; +// try { +// +// if(parameter.isMandatory()){ +// error = parameter.getKey() +" not found"; +// sendError(resp, HttpStatus.SC_BAD_REQUEST, error); +// return; +// } +// else +// logger.debug("Not Mandatory parameter: "+parameter.getKey()+", not found, continuing..."); +// +// }catch (IOException e1) { +// //silent +// } +// } +// +// } +// }catch(JSONException e){ +// try { +// logger.error("Json passed is malformed: ", e); +// sendError(resp, HttpStatus.SC_BAD_REQUEST, "Json passed is malformed"); +// } +// catch (IOException e1) { +// //silent +// } +// return; +// } +// +// try{ +// //CHECK IF INPUT SCOPE IS VALID +// String scope = cer.getValueOfParameter(CatalogueRequestParameter.GCUBE_SCOPE.getKey()); +// if(!scope.startsWith("/")){ +// logger.info("Scope not start with char '/' adding it"); +// scope+="/"+scope; +// cer.addParameterToRequest(CatalogueRequestParameter.GCUBE_SCOPE.getKey(), scope); +// } +// +// String buildLink = getServerURL(req); +// //buildLink += req.getRequestURI(); //AVOIDNG TO ADD THE SERVLET NAME: 'catalogue', THERE IS THE URL REWRITE USED FOR 'uri-resolver' +// +// String clearURL = cer.getValueOfParameter(CatalogueRequestParameter.CLEAR_URL.getKey()); +// +// boolean bClearURL = false; +// try{ +// bClearURL = Boolean.parseBoolean(clearURL); +// }catch(Exception e){ +// //silent +// } +// +// resp.setContentType(TEXT_PALIN_CHARSET_UTF_8); +// resp.setCharacterEncoding(UTF_8); +// +// logger.info("Clear URL is: "+bClearURL); +// if(bClearURL){ +// +// final String vreName = scope.substring(scope.lastIndexOf("/")+1, scope.length()); +// //buildLink+=PATH_SEPARATOR+vreName+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey())+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); +// String econtext = cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey()); +// ResourceCatalogueCodes rc = ResourceCatalogueCodes.valueOfCodeValue(econtext); +// if(rc==null){ +// logger.error("Entity context is null/malformed"); +// sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error during generating Data Catalogue Link, the entity context passed is not recognized. Is it malformed?"); +// return; +// } +// buildLink += PATH_SEPARATOR+rc.getId()+PATH_SEPARATOR+vreName+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); +// logger.info("Writing Decoded Catalogue Link: "+buildLink); +// +// //IT'S GOING TO UPDATE THE GENERIC RESOURCE IF IS NEEDED +// new Thread(){ +// public void run() { +// try { +// String fullScope = cer.getValueOfParameter(CatalogueRequestParameter.GCUBE_SCOPE.getKey()); +// boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver.validateEndPoint(scopeToEncDecr, vreName, fullScope); +// +// if(endPointUpdated) +// appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true); +// +// } +// catch (ApplicationProfileNotFoundException e) { +// logger.error("Error during validating Application Profile", e); +// } +// }; +// }.start(); +// +// }else{ +// //ADDING THE SERVLET NAME +// buildLink += req.getRequestURI(); +// +// cer.removeParameterToRequest(CatalogueRequestParameter.CLEAR_URL.getKey()); +// logger.info("Using scope "+scopeToEncDecr+ " from env to get encrypt key"); +// ScopeProvider.instance.set(scopeToEncDecr); +// //String query = UrlEncoderUtil.encodeQuery(cer.getParameters()); +// String query = ""; +// for (String key : cer.getParameters().keySet()) { +// query+=key+"="+ cer.getParameters().get(key) +"&"; +// } +// query = UrlEncoderUtil.removeLastChar(query); +// logger.info("Builded query string: "+query); +// String encriptedQuery = StringEncrypter.getEncrypter().encrypt(query); +// logger.info("Encrypted query: "+encriptedQuery); +// String encodedQuery = base64EncodeStringURLSafe(encriptedQuery); +// logger.info("Catalogue Query Link: "+encodedQuery); +// buildLink+=PATH_SEPARATOR+encodedQuery; +// logger.info("Writing Encoded Catalogue Link: "+buildLink); +// } +// +// resp.getWriter().write(buildLink); +// +// }catch(Exception e){ +// try { +// logger.error("An internal error is occurred: ", e); +// sendError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR, "An error occurred during generating Data Catalogue Link, try again later"); +// return; +// } +// catch (IOException e1) { +// //silent +// } +// }finally{ +// +// if(originalScope!=null){ +// ScopeProvider.instance.set(originalScope); +// logger.info("scope provider set to orginal scope: "+originalScope); +// }else{ +// ScopeProvider.instance.reset(); +// logger.info("scope provider reset"); +// } +// } +// +// } +// +// /** +// * Send error. +// * +// * @param response the response +// * @param status the status +// * @param message the message +// * @throws IOException Signals that an I/O exception has occurred. +// */ +// protected static void sendError(HttpServletResponse response, int status, String message) throws IOException{ +// +// logger.error("error message: "+message); +// logger.info("writing response..."); +// +// if(response==null) +// return; +// +// response.setStatus(status); +// StringReader sr = new StringReader(message); +// IOUtils.copy(sr, response.getOutputStream()); +// logger.info("response writed"); +// response.flushBuffer(); +// } +// +// +// /** +// * Gets the server url. +// * +// * @param req the req +// * @return the server url +// */ +// public String getServerURL(HttpServletRequest req) { +// +// String scheme = req.getScheme(); // http +// String serverName = req.getServerName(); // hostname.com +// int serverPort = req.getServerPort(); // 80 +// //String contextPath = req.getContextPath(); // /mywebapp +// +// // Reconstruct original requesting URL +// StringBuffer url = new StringBuffer(); +// url.append(scheme).append("://").append(serverName); +// +// if (serverPort != 80 && serverPort != 443) +// url.append(":").append(serverPort); +// +//// if(contextPath!=null) +//// url.append(":").append(contextPath); +// +// String uToS = url.toString(); +// logger.debug("returning servlet context URL: "+uToS); +// return uToS; +// } +// +// +// /** +// * Gets the value of parameter in the passed query string, null otherwise +// * +// * @param parameter the parameter +// * @param httpQueryString the http query string +// * @return the value of parameter if exists, null otherwise +// */ +// public static String getValueOfParameter(String parameter, String httpQueryString) { +//// logger.trace("finding: "+wmsParam +" into "+url); +// int index = httpQueryString.toLowerCase().indexOf(parameter.toLowerCase()+"="); //ADDING CHAR "=" IN TAIL TO BE SECURE IT IS A PARAMETER +//// logger.trace("start index of "+wmsParam+ " is: "+index); +// String value = ""; +// if(index > -1){ +// +// int start = index + parameter.length()+1; //add +1 for char '=' +// String sub = httpQueryString.substring(start, httpQueryString.length()); +// int indexOfSeparator = sub.indexOf("&"); +// int end = indexOfSeparator!=-1?indexOfSeparator:sub.length(); +// value = sub.substring(0, end); +// }else +// return null; +// +//// logger.trace("return value: "+value); +// return value; +// } +// +// /** +// * Base64 encode string url safe. +// * +// * @param s the s +// * @return the string +// */ +// public static String base64EncodeStringURLSafe(String s) { +// +// try { +// return Base64.encodeBase64URLSafeString(s.getBytes(UTF_8)); +// } +// catch (UnsupportedEncodingException e) { +// logger.error("Failed to decode the String", e); +// logger.error("Returning input string: " + s); +// return s; +// } +// } +// +// /** +// * Base64 decode string. +// * +// * @param s the s +// * @return the string +// */ +// public static String base64DecodeString(String s) { +// +// try { +// return new String(Base64.decodeBase64(s.getBytes(UTF_8))); +// } +// catch (UnsupportedEncodingException e) { +// logger.error("Failed to decode the String", e); +// logger.error("Returning input string: " + s); +// return s; +// } +// } +// +// public static void main(String[] args) { +// +// try{ +// String scope = "/d4science.research-infrastructures.eu"; +// ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scope, true); +// logger.info("Reosurce for Catalogue Resolver: "+appPrCatResolver); +// }catch(Exception e){ +// e.printStackTrace(); +// } +// } +//} diff --git a/src/main/java/org/gcube/datatransfer/resolver/listeners/UriResolverStartupListener.java b/src/main/java/org/gcube/datatransfer/resolver/listeners/UriResolverStartupListener.java index 9218be0..4b3091e 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/listeners/UriResolverStartupListener.java +++ b/src/main/java/org/gcube/datatransfer/resolver/listeners/UriResolverStartupListener.java @@ -12,10 +12,11 @@ import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; +import javax.servlet.ServletException; import javax.servlet.annotation.WebListener; -import org.gcube.datatransfer.resolver.caches.GeonetworkInstanceGuavaCache; -import org.gcube.datatransfer.resolver.caches.GisViewerApplicationURLGuavaCache; +import org.gcube.datatransfer.resolver.caches.GeonetworkInstanceCache; +import org.gcube.datatransfer.resolver.caches.GisViewerApplicationURLCache; import org.gcube.datatransfer.resolver.gis.property.ApplicationProfileGenericResourceReader; import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException; import org.slf4j.Logger; @@ -45,6 +46,11 @@ public class UriResolverStartupListener implements ServletContextListener { protected static final String SECONDARY_TYPE = "SECONDARY_TYPE"; protected static final String APP_ID = "APP_ID"; + public static final String ENV_SCOPE = "SCOPE"; //Environment Variable + + + private static String contextScope = null; + private static ApplicationProfileGenericResourceReader gisViewerProfile; private static ApplicationProfileGenericResourceReader geoExplorerProfile; @@ -53,11 +59,24 @@ public class UriResolverStartupListener implements ServletContextListener { */ @Override public void contextInitialized(ServletContextEvent event) { - logger.info("Context initialized!"); + try { + + contextScope = loadScopeFromEnvironment(); + } + catch (ServletException e) { + // + logger.error(e.getMessage(), e); + } + gisViewerProfile = loadApplicationProfile(event.getServletContext(), GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES); geoExplorerProfile = loadApplicationProfile(event.getServletContext(), GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES); - new GeonetworkInstanceGuavaCache(); - new GisViewerApplicationURLGuavaCache(); + new GeonetworkInstanceCache(); + new GisViewerApplicationURLCache(); + + logger.info("Context initialized with: "); + logger.info("Scope: "+contextScope); + logger.info("GisViewerProfile: "+gisViewerProfile); + logger.info("GeoExplorerProfile: "+geoExplorerProfile); } /* (non-Javadoc) @@ -94,6 +113,26 @@ public class UriResolverStartupListener implements ServletContextListener { } + /** + * Load scope from environment. + * + * @return the scope read from Environment Variable + * @throws ServletException the servlet exception + */ + public static String loadScopeFromEnvironment() throws ServletException{ + + logger.info("Reading Environment Variable "+ENV_SCOPE); + String scopeFromEnv = System.getenv(ENV_SCOPE); + + if(scopeFromEnv == null || scopeFromEnv.isEmpty()) + throw new ServletException(UriResolverStartupListener.class.getName() +" cannot read scope from Environment Variable: "+ENV_SCOPE+", It is null or empty"); + + logger.info("Read scope: "+scopeFromEnv+" from Environment Variable: "+ENV_SCOPE); + return scopeFromEnv; + } + + + /** * Gets the gis viewer profile. @@ -118,4 +157,16 @@ public class UriResolverStartupListener implements ServletContextListener { + /** + * Gets the context scope. + * + * @return the contextScope + */ + public static String getContextScope() { + + return contextScope; + } + + + } \ No newline at end of file diff --git a/src/main/java/org/gcube/datatransfer/resolver/scope/ScopeUtil.java b/src/main/java/org/gcube/datatransfer/resolver/scope/ScopeUtil.java index cfb8dc5..d009074 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/scope/ScopeUtil.java +++ b/src/main/java/org/gcube/datatransfer/resolver/scope/ScopeUtil.java @@ -3,46 +3,17 @@ */ package org.gcube.datatransfer.resolver.scope; -import javax.servlet.ServletException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - /** * The Class ScopeUtil. * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Mar 22, 2017 + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Nov 5, 2018 */ public class ScopeUtil { - public static final String ENV_SCOPE = "SCOPE"; //Environment Variable - - private static final Logger logger = LoggerFactory.getLogger(ScopeUtil.class); - public static final String SCOPE_SEPARATOR = "|"; - - /** - * Gets the scope from environment. - * - * @return the scope from environment - * @throws ServletException the servlet exception - */ - public static String getScopeFromEnvironment() throws ServletException{ - - logger.info("Reading Environment Variable "+ENV_SCOPE); - String scopeFromEnv = System.getenv(ENV_SCOPE); - - if(scopeFromEnv == null || scopeFromEnv.isEmpty()) - throw new ServletException(ScopeUtil.class.getName() +" cannot read scope from Environment Variable: "+ENV_SCOPE+", It is null or empty"); - - logger.info("Read scope: "+scopeFromEnv+" from Environment Variable: "+ENV_SCOPE); - return scopeFromEnv; - } - /** * Normalize scope. * Add the '/' as prefix and remove all '|' diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java index 725bc74..e52082f 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java @@ -18,11 +18,13 @@ import javax.ws.rs.core.Response.Status; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datatransfer.resolver.ResourceCatalogueCodes; import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileNotFoundException; +import org.gcube.datatransfer.resolver.caches.CatalogueApplicationProfilesCache; import org.gcube.datatransfer.resolver.catalogue.CatalogueRequest; import org.gcube.datatransfer.resolver.catalogue.resource.ApplicationProfileReaderForCatalogueResolver; import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader; import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference; import org.gcube.datatransfer.resolver.catalogue.resource.UpdateApplicationProfileCatalogueResolver; +import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener; import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,12 +36,10 @@ public class CatalogueResolver { private static Logger logger = LoggerFactory.getLogger(CatalogueResolver.class); - public static final String ENV_SCOPE = "SCOPE"; //Environment Variable - /** The scope to enc decr. */ - private String scopeToEncDecr = null; + //private String scopeToEncDecr = null; - ApplicationProfileReaderForCatalogueResolver appPrCatResolver; + //ApplicationProfileReaderForCatalogueResolver appPrCatResolver; @GET @Path("{entityContext:ctlg(-(o|g|p|d))?}/{vreName}/{entityName}") @@ -49,15 +49,16 @@ public class CatalogueResolver { try { String entityContextValue = ResourceCatalogueCodes.valueOfCodeId(entityContext).getValue(); - ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(vreName, true); + //ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(vreName, true); - String fullScope = appPrCatResolver.getHashVreNameScope().get(vreName); + String fullScope = CatalogueApplicationProfilesCache.getCache().get(vreName); + logger.debug("Read fullScope: "+fullScope + " for VRE_NAME: "+vreName +" into Application Profile "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME); + + //String fullScope = appPrCatResolver.getHashVreNameScope().get(vreName); ScopeProvider.instance.set(fullScope); GatewayCKANCatalogueReference ckanCatalogueReference = CkanCatalogueConfigurationsReader.loadCatalogueEndPoints(); - logger.debug("Read fullScope: "+fullScope + " for VRE_NAME: "+vreName +" into Application Profile "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME); - //IS THE PRODUCT PLUBLIC OR PRIVATE? //USING ACCESS TO PUBLIC PORTLET IF THE ITEM IS PUBLIC, OTHERWISE ACCESS TO PRIVATE PORTLET String ckanPorltetUrl = ckanCatalogueReference.getPrivatePortletURL(); @@ -121,9 +122,10 @@ public class CatalogueResolver { new Thread(){ public void run() { try { - boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver.validateEndPoint(scopeToEncDecr, vreName, fullscope); - if(endPointUpdated) - appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(fullscope, true); + boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver.validateEndPoint(UriResolverStartupListener.getContextScope(), vreName, fullscope); + logger.info("Is the Application profile for Catalogue Resolver updated? "+endPointUpdated); +// if(endPointUpdated) +// appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(fullscope, true); } catch (ApplicationProfileNotFoundException e) { logger.error("Error during validating Application Profile", e); diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java index 4f3da29..c6518c5 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java @@ -15,9 +15,9 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.datatransfer.resolver.caches.GeoExplorerApplicationURLGuavaCache; -import org.gcube.datatransfer.resolver.caches.GeonetworkInstanceGuavaCache; -import org.gcube.datatransfer.resolver.caches.GisViewerApplicationURLGuavaCache; +import org.gcube.datatransfer.resolver.caches.GeoExplorerApplicationURLCache; +import org.gcube.datatransfer.resolver.caches.GeonetworkInstanceCache; +import org.gcube.datatransfer.resolver.caches.GisViewerApplicationURLCache; import org.gcube.datatransfer.resolver.gis.GeonetworkInstance; import org.gcube.datatransfer.resolver.gis.MetadataConverter; import org.gcube.datatransfer.resolver.gis.entity.GisLayerItem; @@ -155,11 +155,11 @@ public class GisResolver { protected String getGisViewerApplicationURL(String scope) throws Exception{ logger.info("Tentative of recovering gis viewer application hostname from cache for scope: "+scope); - String gisViewerAppHostname = GisViewerApplicationURLGuavaCache.getCache().get(scope); + String gisViewerAppHostname = GisViewerApplicationURLCache.getCache().get(scope); if(gisViewerAppHostname==null){ logger.info("Gis viewer application hostname is null, reading from application profile.."); - String url = GisViewerApplicationURLGuavaCache.loadGisViewerApplicationURL(scope); - GisViewerApplicationURLGuavaCache.getCache().put(scope, url); + String url = GisViewerApplicationURLCache.loadGisViewerApplicationURL(scope); + GisViewerApplicationURLCache.getCache().put(scope, url); logger.info("Updated GisViewerApplication cache! Scope "+scope+" linking "+url); return url; }else @@ -203,13 +203,13 @@ public class GisResolver { protected GeonetworkInstance getCachedGeonetworkInstance(String scope) throws Exception{ logger.info("Attempt to get the GeonetworkInstance from cache by scope: "+scope); - GeonetworkInstance geonInstance = GeonetworkInstanceGuavaCache.getCache().get(scope); + GeonetworkInstance geonInstance = GeonetworkInstanceCache.getCache().get(scope); if(geonInstance==null){ logger.info("GeonetworkInstance is null in cache, reading from library..."); try { - geonInstance = GeonetworkInstanceGuavaCache.loadGeonetworkInstance(scope); - GeonetworkInstanceGuavaCache.getCache().put(scope, geonInstance); + geonInstance = GeonetworkInstanceCache.loadGeonetworkInstance(scope); + GeonetworkInstanceCache.getCache().put(scope, geonInstance); logger.info("Updated GeonetworkInstance cache! Scope "+scope+" linking "+geonInstance); } catch (Exception e) { logger.error("An error occurred on getting GeonetworkInstance for scope: "+scope, e); @@ -234,11 +234,11 @@ public class GisResolver { protected String getGeoExplorerApplicationURL(String scope) throws Exception{ logger.info("Tentative of recovering geo explorer application hostname from cache for scope: "+scope); - String geoExplorerApplicationHostname = GeoExplorerApplicationURLGuavaCache.getCache().get(scope); + String geoExplorerApplicationHostname = GeoExplorerApplicationURLCache.getCache().get(scope); if(geoExplorerApplicationHostname==null){ logger.info("GeoExplorer application hostname is null, reading from application profile.."); - String url = GeoExplorerApplicationURLGuavaCache.loadGeoExplorerApplicationURL(scope); - GeoExplorerApplicationURLGuavaCache.getCache().put(scope, url); + String url = GeoExplorerApplicationURLCache.loadGeoExplorerApplicationURL(scope); + GeoExplorerApplicationURLCache.getCache().put(scope, url); logger.info("Updated GeoExplorerApplication cache! Scope "+scope+" linking "+url); return url; }else