From 2c3538caf4d16e88c4c2b4ffd5b7fe2e0f228a68 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 30 Mar 2021 18:21:49 +0200 Subject: [PATCH] moved to 2.5.0-SNAPSHOT. Starting activity #20993 --- CHANGELOG.md | 7 + pom.xml | 2 +- .../resolver/services/WekeoResolver.java | 193 ++++++++++++++++++ src/main/webapp/WEB-INF/gcube-app.xml | 1 + 4 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java diff --git a/CHANGELOG.md b/CHANGELOG.md index c4df5c5..5de1e93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v2-5-0-SNAPSHOT] - 2021-30-03 + +**New features** + +[Task #19942] Supported new resource "Wekeo Interface" - gettoken. + + ## [v2-4-1] - 2021-01-13 **Bug Fixes** diff --git a/pom.xml b/pom.xml index 62eeb9c..c88758c 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ org.gcube.data.transfer uri-resolver - 2.4.1 + 2.5.0-SNAPSHOT war The URI Resolver is an HTTP URI resolver implemented as an REST service which gives access trough HTTP to different gcube Resolvers and gCube Applications. diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java new file mode 100644 index 0000000..bcb369c --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -0,0 +1,193 @@ +package org.gcube.datatransfer.resolver.services; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.Collection; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.datatransfer.resolver.requesthandler.RequestHandler; +import org.gcube.datatransfer.resolver.services.error.ExceptionManager; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +// TODO: Auto-generated Javadoc +/** + * The Class WekeoResolver. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Mar 30, 2021 + */ +@Path("wekeo") +public class WekeoResolver { + + private static Logger logger = LoggerFactory.getLogger(WekeoResolver.class); + + private final static String RUNTIME_WKEO_RESOURCE_NAME = "WekeoDataBroker"; + private final static String CATEGORY_WEKEO_TYPE = "OnlineService"; + + private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver#Wekeo_Resolver"; + + + + /** + * Gets the token. + * + * @param req the req + * @return the token + * @throws WebApplicationException the web application exception + */ + @GET + @Path("/gettoken") + public Response getToken(@Context HttpServletRequest req) throws WebApplicationException{ + + logger.info(this.getClass().getSimpleName()+" getToken starts..."); + String wekeoToken = ""; + + try { + + String contextToken = SecurityTokenProvider.instance.get(); + String scope = ScopeProvider.instance.get(); + logger.info("ScopeProvider has scope: "+scope); + + String appToken = req.getServletContext().getInitParameter(RequestHandler.ROOT_APP_TOKEN); + + if(contextToken.compareTo(appToken)==0){ + logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); + throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); + } + + List endPoints = getConfigurationFromIS(); + + if(endPoints==null || endPoints.size()==0) { + String error = String.format("Missing the RR with Name '%s' and Category '%s' in the scope '%s'. Please contact the support.",RUNTIME_WKEO_RESOURCE_NAME,CATEGORY_WEKEO_TYPE,scope); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + + String wekeoEndPoint = readWekeoServiceEndpoint(req, scope); + + + return Response.ok(wekeoToken).build(); + }catch (Exception e) { + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; + } + } + + + /** + * Retrieve the wekeo endpoint information from IS. + * + * @return list of endpoints for ckan database + * @throws Exception the exception + */ + private static List getConfigurationFromIS() throws Exception{ + + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_WKEO_RESOURCE_NAME +"'"); + query.addCondition("$resource/Profile/Cateogory/Name/text() eq '"+ CATEGORY_WEKEO_TYPE +"'"); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List toReturn = client.submit(query); + return toReturn; + + } + + + /** + * Reads the wekeo endpoint information from IS. {The SE name is: @link WekeoResolver#RUNTIME_WKEO_RESOURCE_NAME} + * + * @param req the req + * @param scope the scope + * @return the string + */ + private static String readWekeoServiceEndpoint(HttpServletRequest req, String scope){ + + String callerScope = null; + String gCubeAppToken = null; + try{ + callerScope = ScopeProvider.instance.get(); + ScopeProvider.instance.set(scope); + logger.info("Searching SE "+RUNTIME_WKEO_RESOURCE_NAME+" configurations in the scope: "+ScopeProvider.instance.get()); + + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_WKEO_RESOURCE_NAME +"'"); + query.addCondition("$resource/Profile/Category/text() eq '"+ CATEGORY_WEKEO_TYPE +"'"); + + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List toReturn = client.submit(query); + + logger.info("The query returned "+toReturn.size()+ " ServiceEndpoint/s"); + + if(toReturn.size()==0){ + String errorMessage = String.format("Missing the RR with Name '%s' and Category '%s' in the scope '%s'. Please contact the support.",RUNTIME_WKEO_RESOURCE_NAME,CATEGORY_WEKEO_TYPE,ScopeProvider.instance.get()); + logger.error(errorMessage); + throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI); + } + + + ServiceEndpoint se = toReturn.get(0); + Collection theAccessPoints = se.profile().accessPoints().asCollection(); + for (AccessPoint accessPoint : theAccessPoints) { + Collection properties = accessPoint.properties().asCollection(); + for (Property property : properties) { +// if(property.name().equalsIgnoreCase(GCUBE_TOKEN)){ +// logger.info("gcube-token as property was found, returning it"); +// gCubeAppToken = property.value(); +// break; +// } + } + + if(gCubeAppToken!=null) + break; + } + + if(gCubeAppToken!=null){ + String decryptedPassword = StringEncrypter.getEncrypter().decrypt(gCubeAppToken); + logger.info("Returning decrypted Application Token registered into "+RUNTIME_WKEO_RESOURCE_NAME +" SE: "+decryptedPassword.substring(0,decryptedPassword.length()/2)+"...."); + return decryptedPassword; + } + + return null; + +// +// String errorMessage = "No "+GCUBE_TOKEN+" as Property saved in the "+RUNTIME_WKEO_RESOURCE_NAME+" SE registered in the scope: "+ScopeProvider.instance.get(); +// logger.error(errorMessage); +// throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI); + + + }catch(Exception e){ + String errorMessage = "Error occurred on reading the "+RUNTIME_WKEO_RESOURCE_NAME+" SE registered in the scope: "+ScopeProvider.instance.get(); + logger.error(errorMessage, e); + throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI); + + }finally{ + if(callerScope!=null){ + logger.info("Setting to the callerScope scope: "+callerScope); + ScopeProvider.instance.set(callerScope); + }else{ + logger.info("Reset scope"); + ScopeProvider.instance.reset(); + } + } + + } + + +} diff --git a/src/main/webapp/WEB-INF/gcube-app.xml b/src/main/webapp/WEB-INF/gcube-app.xml index 2a9b9cc..dd44721 100644 --- a/src/main/webapp/WEB-INF/gcube-app.xml +++ b/src/main/webapp/WEB-INF/gcube-app.xml @@ -5,4 +5,5 @@ URIResolver RESTful /analytics/create/* /knime/create/* + /wekeo/gettoken/* \ No newline at end of file