From 815858c4f3525136975d9a830632c814f006a08a Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Fri, 23 Nov 2018 14:45:33 +0000 Subject: [PATCH] starting work on Parthenos Resolver git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@174355 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../catalogue/ParthenosPostRequest.java | 17 ++ .../resolver/services/CatalogueResolver.java | 143 +++++++++-------- .../resolver/services/GeonetworkResolver.java | 91 +++-------- .../services/PartheosRegistryResolver.java | 146 ++++++++++++++++++ 4 files changed, 261 insertions(+), 136 deletions(-) create mode 100644 src/main/java/org/gcube/datatransfer/resolver/catalogue/ParthenosPostRequest.java create mode 100644 src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java diff --git a/src/main/java/org/gcube/datatransfer/resolver/catalogue/ParthenosPostRequest.java b/src/main/java/org/gcube/datatransfer/resolver/catalogue/ParthenosPostRequest.java new file mode 100644 index 0000000..c1d002f --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/catalogue/ParthenosPostRequest.java @@ -0,0 +1,17 @@ +/** + * + */ +package org.gcube.datatransfer.resolver.catalogue; + + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Nov 16, 2018 + * + * Binding the body of ParthenosPostRequest as JSON + */ +public class ParthenosPostRequest { + + String parthenosURL; +} 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 6ac4fc8..894cb40 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java @@ -10,11 +10,9 @@ import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datatransfer.resolver.caches.LoadingVREsScopeCache; @@ -30,64 +28,49 @@ import org.slf4j.LoggerFactory; import eu.trentorise.opendata.jackan.model.CkanDataset; +/** + * The Class CatalogueResolver. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Nov 16, 2018 + */ @Path("/") public class CatalogueResolver { private static Logger logger = LoggerFactory.getLogger(CatalogueResolver.class); private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver#CATALOGUE_Resolver"; - /** The scope to enc decr. */ - //private String scopeToEncDecr = null; - - //ApplicationProfileReaderForCatalogueResolver appPrCatResolver; - + /** + * Resolve catalogue. + * + * @param req the req + * @param entityName the entity name + * @param vreName the vre name + * @param entityContext the entity context + * @return the response + */ @GET @Path("{entityContext:ctlg(-(o|g|p|d))?}/{vreName}/{entityName}") - public Response resolveCatalogue(@PathParam("entityName") String entityName, @PathParam("vreName") String vreName, @PathParam("entityContext") String entityContext) { + public Response resolveCatalogue(@Context HttpServletRequest req, @PathParam("entityName") String entityName, @PathParam("vreName") String vreName, @PathParam("entityContext") String entityContext) { InnerMethodName.instance.set("resolveCataloguePublicLink"); logger.info(CatalogueResolver.class.getSimpleName()+" GET starts..."); try { - String entityContextValue = ResourceCatalogueCodes.valueOfCodeId(entityContext).getValue(); - - //ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(vreName, true); - - String fullScope = LoadingVREsScopeCache.getCache().get(vreName); - logger.info("Read fullScope: "+fullScope + " for VRE_NAME: "+vreName +" from cache created by: "+GetAllInfrastructureVREs.class.getSimpleName()); - - //String fullScope = appPrCatResolver.getHashVreNameScope().get(vreName); - - ScopeProvider.instance.set(fullScope); - GatewayCKANCatalogueReference ckanCatalogueReference = CkanCatalogueConfigurationsReader.loadCatalogueEndPoints(); - - logger.info("For scope "+fullScope+" loaded end points: "+ckanCatalogueReference); - - //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(); - String datasetName = entityName; - 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(); - } - } - - - String url = String.format("%s?path=/%s/%s",ckanPorltetUrl,entityContextValue, entityName); - - return Response.seeOther(new URL(url).toURI()).build(); + String itemCatalogueURL = getItemCatalogueURL(vreName, entityContext, entityName); + return Response.seeOther(new URL(itemCatalogueURL).toURI()).build(); }catch (Exception e) { logger.error("error resolving catalogue link",e); - throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR); + ExceptionManager.throwInternalErrorException(req, "Error occurred resolving catalogue link", this.getClass(), helpURI); + return null; } } + /** + * Post catalogue. + * + * @param req the req + * @param jsonRequest the json request + * @return the response + */ @POST @Path("catalogue") @Consumes(MediaType.APPLICATION_JSON) @@ -128,31 +111,63 @@ public class CatalogueResolver { //throw new WebApplicationException("Entity context is null/malformed", Status.BAD_REQUEST); } - String linkURL = String.format("%s/%s/%s/%s", serverUrl, rc.getId(), vreName, jsonRequest.getEntity_name()); - logger.info("Returining Catalogue URL: "+linkURL); - - //IT'S GOING TO UPDATE THE GENERIC RESOURCE IF IS NEEDED - /*final String fullscope = scope; - new Thread(){ - public void run() { - try { - boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver.validateEndPoint(UriResolverStartupListener.getRootContextScope(), 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); - } - }; - }.start();*/ - - return Response.ok(linkURL).header("Location", linkURL).build(); } + /** + * Gets the item catalogue url. + * + * @param vreName the vre name + * @param entityContext the entity context + * @param entityName the entity name + * @return the item catalogue url + * @throws Exception the exception + */ + protected static String getItemCatalogueURL(String vreName, String entityContext, String entityName) throws Exception{ + + try { + String entityContextValue = ResourceCatalogueCodes.valueOfCodeId(entityContext).getValue(); + + //ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(vreName, true); + + String fullScope = LoadingVREsScopeCache.getCache().get(vreName); + logger.info("Read fullScope: "+fullScope + " for VRE_NAME: "+vreName +" from cache created by: "+GetAllInfrastructureVREs.class.getSimpleName()); + + //String fullScope = appPrCatResolver.getHashVreNameScope().get(vreName); + + ScopeProvider.instance.set(fullScope); + GatewayCKANCatalogueReference ckanCatalogueReference = CkanCatalogueConfigurationsReader.loadCatalogueEndPoints(); + + logger.info("For scope "+fullScope+" loaded end points: "+ckanCatalogueReference); + + //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(); + String datasetName = entityName; + 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(); + } + } + + + return String.format("%s?path=/%s/%s",ckanPorltetUrl,entityContextValue, entityName); + }catch (Exception e) { + logger.error("error resolving catalogue link",e); + throw e; + } + + } + /** * Gets the server url. * diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java index 90f270c..7c2c36d 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java @@ -129,12 +129,12 @@ public class GeonetworkResolver { if(scope==null || scope.isEmpty()){ logger.error("Path Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'scope'", GeonetworkResolver.class, help); + ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), help); } if(mode==null || mode.isEmpty()){ logger.error("Path Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'mode'", GeonetworkResolver.class, help); + ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), help); } scope = ScopeUtil.normalizeScope(scope, "|"); @@ -144,12 +144,12 @@ public class GeonetworkResolver { }catch(Exception e){ List toPrint = Arrays.asList(MODE.values()); logger.error("The 'mode' parameter is wrong, Have you pass a valid parameter MODE like "+toPrint+"?"); - ExceptionManager.throwWrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, GeonetworkResolver.class, help); + ExceptionManager.throwWrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), help); } if(visibility==null){ logger.error("Path Parameter 'visibility' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'visibility'", GeonetworkResolver.class, help); + ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), help); } visibility = visibility.toUpperCase(); @@ -158,7 +158,7 @@ public class GeonetworkResolver { }catch (Exception e) { List toPrint = Arrays.asList(VISIBILITY.values()); logger.error("The 'visibility' parameter is wrong, Have you pass a valid parameter VISIBILITY like "+toPrint+"?"); - ExceptionManager.throwWrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, GeonetworkResolver.class, help); + ExceptionManager.throwWrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), help); } if(resetCache!=null && Boolean.parseBoolean(resetCache)){ @@ -203,23 +203,23 @@ public class GeonetworkResolver { return responseBuilder.build(); case HttpServletResponse.SC_FORBIDDEN: - ExceptionManager.throwForbiddenException(req, "You are not authorized to perform the request "+gnGetlURL, GeonetworkResolver.class, help); + ExceptionManager.throwForbiddenException(req, "You are not authorized to perform the request "+gnGetlURL, this.getClass(), help); break; default: - ExceptionManager.throwInternalErrorException(req, "Sorry, an error occurred performing the geonetwork request "+gnGetlURL+" with scope "+scope, GeonetworkResolver.class, help); + ExceptionManager.throwInternalErrorException(req, "Sorry, an error occurred performing the geonetwork request "+gnGetlURL+" with scope "+scope, this.getClass(), help); } } catch (Exception e) { logger.error("Exception:", e); String error = "Sorry, an error occurred on resolving geonetwork request with scope "+scope+". Please, contact support!"; - ExceptionManager.throwInternalErrorException(req, error, GeonetworkResolver.class, help); + ExceptionManager.throwInternalErrorException(req, error, this.getClass(), help); } //An error occurred try { - throw new InternalServerException(req, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR, "Error during perform GET operation to: "+gnGetlURL, GeonetworkResolver.class, new URI(help)); + throw new InternalServerException(req, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR, "Error during perform GET operation to: "+gnGetlURL, this.getClass(), new URI(help)); } catch (URISyntaxException e) { //silent @@ -260,12 +260,12 @@ public class GeonetworkResolver { if(scope==null || scope.isEmpty()){ logger.error("Path Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'scope'", GeonetworkResolver.class, help); + ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), help); } if(mode==null || mode.isEmpty()){ logger.error("Path Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'mode'", GeonetworkResolver.class, help); + ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), help); } scope = ScopeUtil.normalizeScope(scope, "|"); @@ -275,12 +275,12 @@ public class GeonetworkResolver { }catch(Exception e){ List toPrint = Arrays.asList(MODE.values()); logger.error("The 'mode' parameter is wrong, Have you pass a valid parameter MODE like "+toPrint+"?"); - ExceptionManager.throwWrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, GeonetworkResolver.class, help); + ExceptionManager.throwWrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), help); } if(visibility==null){ logger.error("Path Parameter 'visibility' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'visibility'", GeonetworkResolver.class, help); + ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), help); } visibility = visibility.toUpperCase(); @@ -289,7 +289,7 @@ public class GeonetworkResolver { }catch (Exception e) { List toPrint = Arrays.asList(VISIBILITY.values()); logger.error("The 'visibility' parameter is wrong, Have you pass a valid parameter VISIBILITY like "+toPrint+"?"); - ExceptionManager.throwWrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, GeonetworkResolver.class, help); + ExceptionManager.throwWrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), help); } //HOW TO PASS MORE THAN ONE? @@ -390,7 +390,7 @@ public class GeonetworkResolver { if(in==null){ logger.warn("Input stream returned is null, sending "+HttpServletResponse.SC_NOT_FOUND); - ExceptionManager.throwNotFoundException(req, "Input stream is null to the request "+gnCSWlURL+ " with body: "+byteArray.toString(), GeonetworkResolver.class, help); + ExceptionManager.throwNotFoundException(req, "Input stream is null to the request "+gnCSWlURL+ " with body: "+byteArray.toString(), this.getClass(), help); } try{ @@ -453,25 +453,25 @@ public class GeonetworkResolver { }catch(Exception e){ logger.error("Error on copy the response to send to client: ", e); - ExceptionManager.throwInternalErrorException(req, "Error on copy the response!", GeonetworkResolver.class, help); + ExceptionManager.throwInternalErrorException(req, "Error on copy the response!", this.getClass(), help); }finally{ IOUtils.closeQuietly(in); } } catch (IllegalArgumentException e){ logger.error("IllegalArgumentException:", e); - ExceptionManager.throwBadRequestException(req, "Illegal argument to carry out the request!", GeonetworkResolver.class, help); + ExceptionManager.throwBadRequestException(req, "Illegal argument to carry out the request!", this.getClass(), help); } catch (Exception e) { logger.error("Exception:", e); String error = "Sorry, an error occurred on resolving geonetwork request with scope "+scope+". Please, contact support!"; - ExceptionManager.throwInternalErrorException(req, error, GeonetworkResolver.class, help); + ExceptionManager.throwInternalErrorException(req, error, this.getClass(), help); } //An error occurred try { - throw new InternalServerException(req, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR, "Error during perform POST operation to: "+gnCSWlURL, GeonetworkResolver.class, new URI(help)); + throw new InternalServerException(req, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR, "Error during perform POST operation to: "+gnCSWlURL, this.getClass(), new URI(help)); } catch (URISyntaxException e) { //silent @@ -480,32 +480,6 @@ public class GeonetworkResolver { } -// /** -// * Purge remain from query string. -// * -// * @param remain_value the scope -// * @param queryString the query string -// * @return the string -// */ -// private static String purgeRemainFromQueryString(String remain_value, String queryString){ -//// SCOPE is: /gcube/devsec/devVRE -//// [INFO ] 2016-04-05 15:01:42,808 org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkResolver - -//// Query String is: scope=/gcube/devsec/devVRE&version=2.0.2&request=GetCapabilities&service=CSW -// int start = queryString.indexOf(PATH_PARAM_REMAINPATH+"="); -// -// if(start>=0){ -// int end = queryString.indexOf("&", start); -// -// if(end==-1 && queryString.length()==(PATH_PARAM_REMAINPATH+"="+remain_value).length()){ //SCOPE IS THE UNIQUE PARAMETER INTO QUETY STRING -// logger.debug("Scope is the unique parameter, returning empty query string"); -// return ""; -// }else if(end=0){ -// int end = queryString.indexOf("&", start); -// -// if(end==-1 && queryString.length()==(PATH_PARAM_SCOPE+"="+scope_value).length()){ //SCOPE IS THE UNIQUE PARAMETER INTO QUETY STRING -// logger.debug("Scope is the unique parameter, returning empty query string"); -// return ""; -// }else if(end