From 406886b6b69b9e32ebe332821e47ef91ae7dbf20 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Wed, 1 Feb 2017 14:47:11 +0000 Subject: [PATCH] Completed Task #6492 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@141977 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 2 +- .../resolver/ResourceCatalogueCodes.java | 126 ++++++++++++++ .../resolver/UriResolverRewriteFilter.java | 155 +++++++++++------- .../resolver/catalogue/CatalogueResolver.java | 15 +- src/test/java/GeonetworkQueryTest.java | 10 +- 5 files changed, 242 insertions(+), 66 deletions(-) create mode 100644 src/main/java/org/gcube/datatransfer/resolver/ResourceCatalogueCodes.java diff --git a/pom.xml b/pom.xml index 468fcfe..0c2cf19 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.data.transfer uri-resolver - 1.9.0-SNAPSHOT + 1.10.0-SNAPSHOT war The URI Resolver is an HTTP URI resolver implemented as an HTTP servlet which gives access trough HTTP to different protocols URIs. diff --git a/src/main/java/org/gcube/datatransfer/resolver/ResourceCatalogueCodes.java b/src/main/java/org/gcube/datatransfer/resolver/ResourceCatalogueCodes.java new file mode 100644 index 0000000..1bda1a9 --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/ResourceCatalogueCodes.java @@ -0,0 +1,126 @@ +/** + * + */ +package org.gcube.datatransfer.resolver; + +import java.util.ArrayList; +import java.util.List; + + +/** + * The Enum ResourceCatalogueCodes. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jan 31, 2017 + * + * see wiki page: https://wiki.gcube-system.org/gcube/URI_Resolver#CATALOGUE_Resolver + */ +public enum ResourceCatalogueCodes { + + CTLG("ctlg","product", "Catalogue Product/Dataset"), + CTLGP("ctlg-p","product", "Catalogue Product"), + CTLGD("ctlg-d","dataset", "Catalogue Dataset"), + CTLGO("ctlg-o","organization", "Catalogue Organization"), + CTLGG("ctlg-g","group", "Catalogue Group"); + + private String id; //the code id + private String value; //the code value + private String description; + + /** + * Instantiates a new resource catalogue codes. + * + * @param id the id + * @param value the value + * @param description the description + */ + private ResourceCatalogueCodes(String id, String value, String description) { + this.id = id; + this.value = value; + this.description = description; + } + + /** + * Gets the id. + * + * @return the id + */ + public String getId() { + + return id; + } + + + /** + * Gets the value. + * + * @return the value + */ + public String getValue() { + + return value; + } + + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + + return description; + } + + + /** + * Codes. + * + * @return the list + */ + public static List codes(){ + + List codes = new ArrayList(ResourceCatalogueCodes.values().length); + for (ResourceCatalogueCodes value : ResourceCatalogueCodes.values()) { + codes.add(value.getId()); + } + + return codes; + } + + + /** + * Value of code id. + * + * @param id the id + * @return the resource catalogue codes + */ + public static ResourceCatalogueCodes valueOfCodeId(String id){ + if(id==null || id.isEmpty()) + return null; + + for (ResourceCatalogueCodes value : ResourceCatalogueCodes.values()) { + if(value.id.compareTo(id)==0) + return value; + } + return null; + } + + + /** + * Value of code value. + * + * @param codeValue the code value + * @return the resource catalogue codes + */ + public static ResourceCatalogueCodes valueOfCodeValue(String codeValue){ + if(codeValue==null || codeValue.isEmpty()) + return null; + + for (ResourceCatalogueCodes rcc : ResourceCatalogueCodes.values()) { + if(rcc.value.compareTo(codeValue)==0) + return rcc; + } + return null; + } +} diff --git a/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java b/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java index 5d0aae2..5765c38 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/UriResolverRewriteFilter.java @@ -4,6 +4,8 @@ package org.gcube.datatransfer.resolver; import java.io.IOException; +import java.util.HashSet; +import java.util.Set; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -49,6 +51,8 @@ public class UriResolverRewriteFilter implements Filter{ protected static final Logger logger = LoggerFactory.getLogger(UriResolverRewriteFilter.class); private FilterConfig config; + private static final Set resourceCataloguesCodes = new HashSet(ResourceCatalogueCodes.codes()); + //private ApplicationProfileReaderForCatalogueResolver appPrfCatResolver = new ApplicationProfileReaderForCatalogueResolver(scope, useRootScope) /** @@ -79,80 +83,115 @@ public class UriResolverRewriteFilter implements Filter{ MultiReadHttpServletRequest multiReadRequest = new MultiReadHttpServletRequest((HttpServletRequest) req); String requestURI = multiReadRequest.getRequestURI(); String queryString = multiReadRequest.getQueryString(); - logger.debug("Request URI: " + requestURI + ", QueryString: " +queryString+ ", Servlet path: "+multiReadRequest.getServletPath()); + String servletPath = multiReadRequest.getServletPath(); + logger.debug("Request URI: " + requestURI + ", QueryString: " +queryString+ ", Servlet path: "+servletPath); //IS A REQUEST FOR GEONETWORK AUTHENTICATION? (CKAN HARVESTING?) - if(multiReadRequest.getServletPath().startsWith(SERVLET_GEONETWORK) || multiReadRequest.getServletPath().startsWith(SERVLET_URI_RESOLVER+SERVLET_GEONETWORK)){ - logger.debug("is geonetwork request"); - GeonetworkRequestDecoder grd = new GeonetworkRequestDecoder(multiReadRequest.getServletPath(), queryString); + if(servletPath.startsWith(SERVLET_GEONETWORK) || servletPath.startsWith(SERVLET_URI_RESOLVER+SERVLET_GEONETWORK)){ + logger.debug("It is a request to geonetwork"); + GeonetworkRequestDecoder grd = new GeonetworkRequestDecoder(servletPath, queryString); logger.debug("forward to: "+grd.getNewURI()); multiReadRequest.getRequestDispatcher(grd.getNewURI()).forward(multiReadRequest, response); - }else if(multiReadRequest.getServletPath().startsWith(SERVLET_CATALOGUE) || multiReadRequest.getServletPath().startsWith(SERVLET_URI_RESOLVER+SERVLET_CATALOGUE)){ - logger.debug("is a catalogue request"); + }else{ + logger.debug("checking if it is a request to catologue..."); + boolean isCatalogueServletReference = servletPath.startsWith(SERVLET_CATALOGUE); - //int startIndex = requestURI.indexOf(SERVLET_CATALOGUE))+SERVLET_CATALOGUE.length(); - //String vreName = requestURI.substring(beginIndex) - HttpServletRequest request = (HttpServletRequest) req; - logger.debug("method is: "+request.getMethod()); - - if(request.getMethod().compareTo("POST")==0){ - logger.debug("is POST request..."); - logger.debug("forward to: " + SERVLET_CATALOGUE); - multiReadRequest.getRequestDispatcher(SERVLET_CATALOGUE).forward(multiReadRequest, response); - } - else{ - logger.debug("is GET request..."); - String[] pathSplit = multiReadRequest.getServletPath().split(PATH_SEPARATOR); - 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() +"="+pathSplit[2]; - String eC = CatalogueRequestParameter.ENTITY_CONTEXT.getKey() +"="+pathSplit[3]; - logger.debug("found context name: "+eC); - String eN = CatalogueRequestParameter.ENTITY_NAME.getKey() +"="+pathSplit[4]; - logger.debug("found entity name: "+eN); - String encodedQuery = UrlEncoderUtil.encodeQuery(gcubeScope,eC,eN); - newURI+= "?" + PARAMETER_DIRECT_CATALOGUE_LINK + "=" + encodedQuery; - }else{ - logger.info("Resolving an encrypted URL to catalogue..."); - int lastSlash = requestURI.lastIndexOf(PATH_SEPARATOR); - String toCatalogueLink = requestURI.substring(lastSlash + 1, requestURI.length()); - newURI+= "?" + PARAMETER_ENC_CATALOGUE_LINK + "=" + toCatalogueLink; + String idCodeCatalogue = null; + boolean isIdCodeCatalogue = false; + String splittedServletPath[] = servletPath.split(PATH_SEPARATOR); + if(!isCatalogueServletReference){ + idCodeCatalogue = splittedServletPath[1]; + logger.trace("checking if the code: "+idCodeCatalogue+" belongs to a catalogue code"); + isIdCodeCatalogue = resourceCataloguesCodes.contains(idCodeCatalogue); + logger.debug("\tIs it a catalogue code? "+isIdCodeCatalogue); } - logger.debug("forward to: " + newURI); - multiReadRequest.getRequestDispatcher(newURI).forward(multiReadRequest, response); - } + if(isCatalogueServletReference || isIdCodeCatalogue){ - //chain.doFilter(multiReadRequest, response); - }else{ - //IS WORKSPACE REQUEST? - if (queryString == null) { // IS A /XXXXX - logger.debug("QueryString is null, is It a new SMP public uri by ID?"); - int lastSlash = requestURI.lastIndexOf(PATH_SEPARATOR); - if (lastSlash + 1 == requestURI.length()) { - logger.debug("'/' is last index, doFilter Request"); - // req.getRequestDispatcher("/").forward(req, res); - chain.doFilter(multiReadRequest, response); + //int startIndex = requestURI.indexOf(SERVLET_CATALOGUE))+SERVLET_CATALOGUE.length(); + //String vreName = requestURI.substring(beginIndex) + HttpServletRequest request = (HttpServletRequest) req; + logger.trace("method is: "+request.getMethod()); + + if(request.getMethod().compareToIgnoreCase("POST")==0){ + logger.debug("Managing as Catalogue POST request.."); + logger.debug("forward to: " + SERVLET_CATALOGUE); + multiReadRequest.getRequestDispatcher(SERVLET_CATALOGUE).forward(multiReadRequest, response); + }else{ + logger.debug("Managing as Catalogue GET request.."); + String newURI = SERVLET_CATALOGUE; + //String[] pathSplit = newServletPath.split(PATH_SEPARATOR); + if(isIdCodeCatalogue){ + //TO BUILD A COMPLETE URL +// logger.debug("is a catalogue code, rebuilding complete url..."); +// ResourceCatalogueCodes rcc = ResourceCatalogueCodes.valueOfId(idCodeCatalogue); +// newServletPath = SERVLET_CATALOGUE+splittedServletPath[2]+PATH_SEPARATOR+rcc.getValue()+PATH_SEPARATOR+splittedServletPath[3]; +// logger.debug("rebuilded complete url: "+newServletPath); + logger.debug("is a catalogue code, resolving a short url in clear ..."); + ResourceCatalogueCodes rcc = ResourceCatalogueCodes.valueOfCodeId(idCodeCatalogue); + logger.debug("found VRE name: "+splittedServletPath[2]); + String gcubeScope = CatalogueRequestParameter.GCUBE_SCOPE.getKey() +"="+splittedServletPath[2]; + String eC = CatalogueRequestParameter.ENTITY_CONTEXT.getKey() +"="+rcc.getValue(); + logger.debug("found id code: "+idCodeCatalogue+", resolving it with context name: "+eC); + String eN = CatalogueRequestParameter.ENTITY_NAME.getKey() +"="+splittedServletPath[3]; + logger.debug("found entity name: "+eN); + String encodedQuery = UrlEncoderUtil.encodeQuery(gcubeScope,eC,eN); + newURI+= "?" + PARAMETER_DIRECT_CATALOGUE_LINK + "=" + encodedQuery; + }else + if(splittedServletPath.length==5){ + logger.info("resolving a complete URL in clear..."); + logger.debug("found VRE name: "+splittedServletPath[2]); + String gcubeScope = CatalogueRequestParameter.GCUBE_SCOPE.getKey() +"="+splittedServletPath[2]; + String eC = CatalogueRequestParameter.ENTITY_CONTEXT.getKey() +"="+splittedServletPath[3]; + logger.debug("found context name: "+eC); + String eN = CatalogueRequestParameter.ENTITY_NAME.getKey() +"="+splittedServletPath[4]; + logger.debug("found entity name: "+eN); + String encodedQuery = UrlEncoderUtil.encodeQuery(gcubeScope,eC,eN); + newURI+= "?" + PARAMETER_DIRECT_CATALOGUE_LINK + "=" + encodedQuery; + }else{ + + logger.info("Resolving an encrypted URL to catalogue..."); + int lastSlash = requestURI.lastIndexOf(PATH_SEPARATOR); + String toCatalogueLink = requestURI.substring(lastSlash + 1, requestURI.length()); + newURI+= "?" + PARAMETER_ENC_CATALOGUE_LINK + "=" + toCatalogueLink; + } + + logger.debug("forward to: " + newURI); + multiReadRequest.getRequestDispatcher(newURI).forward(multiReadRequest, response); + } + + //chain.doFilter(multiReadRequest, response); + }else{ + logger.debug("It is a request to workspace/storage"); + //LAST CASE, IS WORKSPACE REQUEST? + if (queryString == null) { // IS A /XXXXX + logger.debug("QueryString is null, is It a new SMP public uri by ID?"); + int lastSlash = requestURI.lastIndexOf(PATH_SEPARATOR); + if (lastSlash + 1 == requestURI.length()) { + logger.debug("'/' is last index, doFilter Request"); + // req.getRequestDispatcher("/").forward(req, res); + chain.doFilter(multiReadRequest, response); + } + else { + String toStorageID = requestURI.substring(lastSlash + 1, requestURI.length()); + // String newURI = requestURI.replace(toReplace, + // SERVLET_RESOLVER_BY_ID+"?"+SMP_ID+"="+toReplace); + String newURI = SERVLET_STORAGE_ID + "?" + PARAMETER_SMP_ID + "=" + toStorageID; + logger.debug("forward to: " + newURI); + multiReadRequest.getRequestDispatcher(newURI).forward(multiReadRequest, response); + } } else { - String toStorageID = requestURI.substring(lastSlash + 1, requestURI.length()); - // String newURI = requestURI.replace(toReplace, - // SERVLET_RESOLVER_BY_ID+"?"+SMP_ID+"="+toReplace); - String newURI = SERVLET_STORAGE_ID + "?" + PARAMETER_SMP_ID + "=" + toStorageID; - logger.debug("forward to: " + newURI); - multiReadRequest.getRequestDispatcher(newURI).forward(multiReadRequest, response); + logger.debug("is NOT a SMP public uri by ID, doFilter Request"); + chain.doFilter(multiReadRequest, response); } } - else { - logger.debug("is NOT a SMP public uri by ID, doFilter Request"); - chain.doFilter(multiReadRequest, response); - } + } } + /** * Gets the scope. * 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 2443c8e..66cb0a0 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/catalogue/CatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/catalogue/CatalogueResolver.java @@ -17,6 +17,7 @@ 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.json.JSONArray; import org.json.JSONException; @@ -264,7 +265,7 @@ public class CatalogueResolver extends HttpServlet{ } String buildLink = getServerURL(req); - buildLink += req.getRequestURI(); + //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()); @@ -281,8 +282,16 @@ public class CatalogueResolver extends HttpServlet{ logger.info("Clear URL is: "+bClearURL); if(bClearURL){ - String lastContext = scope.substring(scope.lastIndexOf("/")+1, scope.length()); - buildLink+=PATH_SEPARATOR+lastContext+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_CONTEXT.getKey())+PATH_SEPARATOR+cer.getValueOfParameter(CatalogueRequestParameter.ENTITY_NAME.getKey()); + 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); }else{ diff --git a/src/test/java/GeonetworkQueryTest.java b/src/test/java/GeonetworkQueryTest.java index 11b049d..69b4265 100644 --- a/src/test/java/GeonetworkQueryTest.java +++ b/src/test/java/GeonetworkQueryTest.java @@ -10,6 +10,7 @@ import org.gcube.spatial.data.geonetwork.LoginLevel; import org.gcube.spatial.data.geonetwork.configuration.Configuration; import org.gcube.spatial.data.geonetwork.model.Account; import org.gcube.spatial.data.geonetwork.model.Account.Type; +import org.junit.Test; /** * @@ -22,20 +23,21 @@ public class GeonetworkQueryTest { //private String[] scopes = {"/gcube/devNext/NextNext"}; - private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab"}; + //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab"}; - private LoginLevel loginLevel = LoginLevel.ADMIN; + private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"}; + + private LoginLevel loginLevel = LoginLevel.CKAN; private Type accountType = Type.SCOPE; - //@Test + @Test public void getCount() throws Exception{ try{ for(String scope:scopesProd){ ScopeProvider.instance.set(scope); GeoNetworkPublisher reader=GeoNetwork.get(); - Configuration config = reader.getConfiguration(); Account account=config.getScopeConfiguration().getAccounts().get(accountType);