From 2c3538caf4d16e88c4c2b4ffd5b7fe2e0f228a68 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 30 Mar 2021 18:21:49 +0200 Subject: [PATCH 01/17] 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 From c3f0a7663f5900aad6b624a53be38cdd31f26660 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 31 Mar 2021 18:02:54 +0200 Subject: [PATCH 02/17] in progress on WekeoResolver --- .classpath | 1 - .../resolver/services/WekeoResolver.java | 126 +++++++++++------- .../resolver/util/HttpRequestUtil.java | 10 -- .../datatransfer/test/WekeoResolverTest.java | 114 ++++++++++++++++ src/test/resources/.gitignore | 1 + 5 files changed, 191 insertions(+), 61 deletions(-) create mode 100644 src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java diff --git a/.classpath b/.classpath index 27073a0..045159b 100644 --- a/.classpath +++ b/.classpath @@ -29,6 +29,5 @@ - diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java index bcb369c..53290b1 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -3,6 +3,8 @@ 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.io.BufferedInputStream; +import java.io.InputStream; import java.util.Collection; import java.util.List; @@ -13,6 +15,15 @@ import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.HttpClientBuilder; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.resources.gcore.ServiceEndpoint; @@ -58,7 +69,7 @@ public class WekeoResolver { public Response getToken(@Context HttpServletRequest req) throws WebApplicationException{ logger.info(this.getClass().getSimpleName()+" getToken starts..."); - String wekeoToken = ""; + String wekeoToken = null; try { @@ -73,15 +84,64 @@ public class WekeoResolver { throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); } - List endPoints = getConfigurationFromIS(); + AccessPoint wekeoAccessPoint = readWekeoServiceEndpoint(req, scope); - 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); + if(wekeoAccessPoint!=null) { + + String wekeoUsername = wekeoAccessPoint.username(); + String wekeoAddress = wekeoAccessPoint.address(); + String wekeoPwd = wekeoAccessPoint.password(); + + //printing the access point found + if(logger.isDebugEnabled()) { + String msg = String.format("Found the username %s and the address %s to perform the request", wekeoUsername, wekeoAddress); + logger.debug(msg); + } + + //decrypting the pwd + if(wekeoPwd!=null){ + wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoAccessPoint.password()); + logger.info("Returning decrypted pwd registered into "+RUNTIME_WKEO_RESOURCE_NAME +" SE: "+wekeoPwd.substring(0,wekeoPwd.length()/2)+"...."); + } + + if(wekeoUsername!=null && wekeoPwd!=null & wekeoAddress!=null) { + + try { + //performing the HTTP request with Basic Authentication + CredentialsProvider provider = new BasicCredentialsProvider(); + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(wekeoUsername, wekeoPwd); + provider.setCredentials(AuthScope.ANY, credentials); + + HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); + logger.info("calling the URL and performing basic authentication to: "+wekeoAddress); + HttpResponse response = client.execute(new HttpGet(wekeoAddress)); + int statusCode = response.getStatusLine().getStatusCode(); + logger.info("the response stus code is: "+statusCode); + if(statusCode == 200) { + InputStream is = response.getEntity().getContent(); + BufferedInputStream bif = new BufferedInputStream(is); + wekeoToken = bif.toString(); + logger.info("got the wekeo token: "+wekeoToken.substring(0,wekeoToken.length()/2)+"...."); + }else { + String error = String.format("The request to %s returned status code %d",wekeoAddress,statusCode); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + }catch (Exception e) { + String error = String.format("Error on performing request to %s",wekeoAddress); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + + }else { + String error = String.format("I cannot read the configurations (adress, username,password) from %s in the scope %s",RUNTIME_WKEO_RESOURCE_NAME,scope); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } } - String wekeoEndPoint = readWekeoServiceEndpoint(req, scope); - + //to be sure + if(wekeoToken==null) { + String error = String.format("Sorry an rrror occured on getting the wekeo token. Please, retry the request"); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } return Response.ok(wekeoToken).build(); }catch (Exception e) { @@ -92,24 +152,6 @@ public class WekeoResolver { } - /** - * 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} * @@ -117,10 +159,9 @@ public class WekeoResolver { * @param scope the scope * @return the string */ - private static String readWekeoServiceEndpoint(HttpServletRequest req, String scope){ + public static AccessPoint readWekeoServiceEndpoint(HttpServletRequest req, String scope){ String callerScope = null; - String gCubeAppToken = null; try{ callerScope = ScopeProvider.instance.get(); ScopeProvider.instance.set(scope); @@ -141,37 +182,22 @@ public class WekeoResolver { throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI); } - + String wekeoUsername = null; + String wekeoPwd = null; + 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; -// } + wekeoUsername = accessPoint.username(); + wekeoPwd = accessPoint.password(); + if(wekeoUsername!=null && wekeoPwd!=null) { + logger.info("returning the access point with name: "+accessPoint.name()); + return accessPoint; } - - 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); diff --git a/src/main/java/org/gcube/datatransfer/resolver/util/HttpRequestUtil.java b/src/main/java/org/gcube/datatransfer/resolver/util/HttpRequestUtil.java index 936431c..a652d6a 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/util/HttpRequestUtil.java +++ b/src/main/java/org/gcube/datatransfer/resolver/util/HttpRequestUtil.java @@ -120,14 +120,4 @@ public class HttpRequestUtil { return false; } - - /** - * The main method. - * - * @param args the arguments - * @throws Exception the exception - */ - public static void main(String[] args) throws Exception { - System.out.println(HttpRequestUtil.urlExists("http://geoserver2.d4science.research-infrastructures.eu/geoserver/wms", true)); - } } diff --git a/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java b/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java new file mode 100644 index 0000000..468e5a4 --- /dev/null +++ b/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java @@ -0,0 +1,114 @@ +package org.gcube.datatransfer.test; +import java.io.BufferedInputStream; +import java.io.InputStream; + +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.HttpClientBuilder; +import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.datatransfer.resolver.services.WekeoResolver; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class WekeoResolverTest. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Mar 31, 2021 + */ +public class WekeoResolverTest { + + private static Logger logger = LoggerFactory.getLogger(WekeoResolverTest.class); + + private final static String RUNTIME_WKEO_RESOURCE_NAME = "WekeoDataBroker"; + + public static final String scope = "/gcube/devsec/devVRE"; + + public static void main(String[] args) { + + try { + + logger.info("starts..."); + String wekeoToken = null; + + AccessPoint wekeoAccessPoint = WekeoResolver.readWekeoServiceEndpoint(null, scope); + + if (wekeoAccessPoint != null) { + + String wekeoUsername = wekeoAccessPoint.username(); + String wekeoAddress = wekeoAccessPoint.address(); + String wekeoPwd = wekeoAccessPoint.password(); + + // printing the access point found + if (logger.isDebugEnabled()) { + String msg = String.format("Found the username %s and the address %s to perform the request", + wekeoUsername, wekeoAddress); + logger.debug(msg); + } + + // decrypting the pwd + if (wekeoPwd != null) { + wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoAccessPoint.password()); + logger.info("Returning decrypted pwd registered into " + RUNTIME_WKEO_RESOURCE_NAME + " SE: " + + wekeoPwd.substring(0, wekeoPwd.length() / 2) + "...."); + } + + if (wekeoUsername != null && wekeoPwd != null & wekeoAddress != null) { + + try { + // performing the HTTP request with Basic Authentication + CredentialsProvider provider = new BasicCredentialsProvider(); + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(wekeoUsername, + wekeoPwd); + provider.setCredentials(AuthScope.ANY, credentials); + + HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); + logger.info("calling the URL and performing basic authentication to: " + wekeoAddress); + HttpResponse response = client.execute(new HttpGet(wekeoAddress)); + int statusCode = response.getStatusLine().getStatusCode(); + logger.info("the response stus code is: " + statusCode); + if (statusCode == 200) { + InputStream is = response.getEntity().getContent(); + BufferedInputStream bif = new BufferedInputStream(is); + wekeoToken = bif.toString(); + logger.info("got the wekeo token: " + wekeoToken.substring(0, wekeoToken.length() / 2) + + "...."); + } else { + String error = String.format("The request to %s returned status code %d", wekeoAddress, + statusCode); + throw new Exception(error); + } + } catch (Exception e) { + String error = String.format("Error on performing request to %s", wekeoAddress); + throw new Exception(error); + } + + } else { + String error = String.format( + "I cannot read the configurations (adress, username,password) from %s in the scope %s", + RUNTIME_WKEO_RESOURCE_NAME, scope); + throw new Exception(error); + } + } + + // to be sure + if (wekeoToken == null) { + String error = String + .format("Sorry an rrror occured on getting the wekeo token. Please, retry the request"); + throw new Exception(error); + } + + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + + } + +} diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore index c04ed84..91f56a4 100644 --- a/src/test/resources/.gitignore +++ b/src/test/resources/.gitignore @@ -11,3 +11,4 @@ /gcube.gcubekey /preprod.gcubekey /pred4s.gcubekey +/log4j.properties From c6adaba745da0f40772e23cdc1fa7eba940e7d70 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 1 Apr 2021 17:15:03 +0200 Subject: [PATCH 03/17] completed WekeoResolverTest --- pom.xml | 20 +- .../resolver/gis/MetadataConverter.java | 19 -- .../resolver/services/WekeoResolver.java | 4 +- src/test/java/GeonetworkQueryTest.java | 231 ----------------- .../test}/CatalogueNameExtractor.java | 1 + .../test}/CatalogueResolverTest.java | 1 + .../datatransfer/test}/DetachedVREsTest.java | 1 + .../test}/GcoreEndpointReader.java | 1 + .../test}/GeonetworkMefServiceTest.java | 1 + .../test/GeonetworkQueryTest.java | 232 ++++++++++++++++++ .../test}/GeonetworkRequestDecoderTest.java | 1 + .../test}/GeonetworkResolverTest.java | 1 + .../GetAllInfrastructureScopesFromIS.java | 1 + .../datatransfer/test}/GisResolverTest.java | 1 + .../datatransfer/test}/HttpRequestUtil.java | 1 + .../test}/StorageIDResolverTest.java | 1 + .../datatransfer/test}/UriResolverTest.java | 1 + .../datatransfer/test/WekeoResolverTest.java | 100 +++++--- 18 files changed, 329 insertions(+), 289 deletions(-) delete mode 100644 src/test/java/GeonetworkQueryTest.java rename src/test/java/{ => org/gcube/datatransfer/test}/CatalogueNameExtractor.java (94%) rename src/test/java/{ => org/gcube/datatransfer/test}/CatalogueResolverTest.java (97%) rename src/test/java/{ => org/gcube/datatransfer/test}/DetachedVREsTest.java (97%) rename src/test/java/{ => org/gcube/datatransfer/test}/GcoreEndpointReader.java (98%) rename src/test/java/{ => org/gcube/datatransfer/test}/GeonetworkMefServiceTest.java (97%) create mode 100644 src/test/java/org/gcube/datatransfer/test/GeonetworkQueryTest.java rename src/test/java/{ => org/gcube/datatransfer/test}/GeonetworkRequestDecoderTest.java (96%) rename src/test/java/{ => org/gcube/datatransfer/test}/GeonetworkResolverTest.java (99%) rename src/test/java/{ => org/gcube/datatransfer/test}/GetAllInfrastructureScopesFromIS.java (99%) rename src/test/java/{ => org/gcube/datatransfer/test}/GisResolverTest.java (98%) rename src/test/java/{ => org/gcube/datatransfer/test}/HttpRequestUtil.java (98%) rename src/test/java/{ => org/gcube/datatransfer/test}/StorageIDResolverTest.java (99%) rename src/test/java/{ => org/gcube/datatransfer/test}/UriResolverTest.java (99%) diff --git a/pom.xml b/pom.xml index c88758c..71f5112 100644 --- a/pom.xml +++ b/pom.xml @@ -137,7 +137,6 @@ compile - javax.enterprise @@ -170,6 +169,11 @@ compile + + org.projectlombok + lombok + 1.14.8 + @@ -180,13 +184,25 @@ compile + + + + + + + + + + + + + commons-lang commons-lang 2.6 - org.w3c diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/MetadataConverter.java b/src/main/java/org/gcube/datatransfer/resolver/gis/MetadataConverter.java index a61188d..b3cb4d6 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/MetadataConverter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/MetadataConverter.java @@ -189,23 +189,4 @@ public class MetadataConverter { return gisLI; } - - /* - public static void main(String[] args) throws Exception { - -// String geoserver = "http://www.fao.org/figis/a/wms/?service=WMS&version=1.1.0&request=GetMap&layers=area:FAO_AREAS&styles=Species_prob, puppa&bbox=-180.0,-88.0,180.0,90.0000000694&width=667&height=330&srs=EPSG:4326&format=image%2Fpng"; -// System.out.println(MetadataConverter.getGeoserverBaseUri(geoserver)); - - String user ="admin"; - String pwd = "admin"; - boolean authenticate = true; -// String uuid ="177e1c3c-4a22-4ad9-b015-bfc443d16cb8"; - String uuid ="fao-species-map-bep"; -// String uuid ="fao-species-map-bon"; //FAO - String geoNetworkUrl ="http://geoserver-dev2.d4science-ii.research-infrastructures.eu/geonetwork"; - GeonetworkInstance geonetowrkInstance = new GeonetworkInstance(geoNetworkUrl, user, pwd, authenticate); - String onLineResource = getWMSOnLineResource(geonetowrkInstance, uuid); - System.out.println(onLineResource); - - }*/ } diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java index 53290b1..fded230 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -94,7 +94,7 @@ public class WekeoResolver { //printing the access point found if(logger.isDebugEnabled()) { - String msg = String.format("Found the username %s and the address %s to perform the request", wekeoUsername, wekeoAddress); + String msg = String.format("Found the username ' %s' and the address '%s' to perform the request", wekeoUsername, wekeoAddress); logger.debug(msg); } @@ -214,6 +214,4 @@ public class WekeoResolver { } } - - } diff --git a/src/test/java/GeonetworkQueryTest.java b/src/test/java/GeonetworkQueryTest.java deleted file mode 100644 index 90518b0..0000000 --- a/src/test/java/GeonetworkQueryTest.java +++ /dev/null @@ -1,231 +0,0 @@ -import org.gcube.common.encryption.StringEncrypter; -import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.spatial.data.geonetwork.GeoNetwork; -import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher; -import org.gcube.spatial.data.geonetwork.GeoNetworkReader; -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.opengis.metadata.Metadata; - -import it.geosolutions.geonetwork.util.GNSearchRequest; -import it.geosolutions.geonetwork.util.GNSearchResponse; - -/** - * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Aug 31, 2016 - */ -public class GeonetworkQueryTest { - - private static final int MAX = 10; - - private static final String UUID = "8a878105-ef06-4b1f-843f-120fc525b22b"; - - //private String[] scopesProd = {"/gcube/devsec/devVRE"}; - - //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"}; - - //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/fisheriesandecosystematmii"}; - - //private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research"}; - - //private String[] scopesProd = {"/d4science.research-infrastructures.eu"}; - - //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/FAO_TunaAtlas"}; - - //private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research/Blue-Datathon"}; - private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab"}; - - private LoginLevel loginLevel = LoginLevel.CKAN; - - private Type accountType = Type.SCOPE; - - private String textToSearch = "geo_fea"; - - //@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); - - //System.out.println("User: "+account.getUser()+", Pwd: "+account.getPassword()); - System.out.println("Admin: "+config.getAdminAccount().getUser()+", Pwd: "+config.getAdminAccount().getPassword()); - - try{ - String decryptedPassword = StringEncrypter.getEncrypter().decrypt(account.getPassword()); - System.out.println("Decrypted Password: "+decryptedPassword); - }catch(Exception e){ - System.out.println("ignoring exception during pwd decrypting"); - } - - - // req.addParam("keyword", "Thredds"); - final GNSearchRequest req=new GNSearchRequest(); -// req.addParam(GNSearchRequest.Param.any,"Thredds"); - GNSearchResponse resp = reader.query(req); - int publicCount=resp.getCount(); - reader.login(loginLevel); - int totalCount=reader.query(req).getCount(); - System.out.println("SCOPE "+scope+" found "+totalCount+" (public : "+publicCount+", private :"+(totalCount-publicCount)+")"); - if(totalCount==0) - return; - /*try{ - int last = totalCount>MAX?totalCount:MAX; - for(int i=0; iMAX?totalCount:MAX; -// -// for(int i=0; iMAX?totalCount:MAX; +// for(int i=0; iMAX?totalCount:MAX; +//// +//// for(int i=0; i 0) { + sb.append(charArray, 0, numCharsRead); } + wekeoResponse.append(sb.toString()); + //System.out.println(wekeoResponse); + } catch (Exception e) { + logger.error(e.getMessage(), e); String error = String.format("Error on performing request to %s", wekeoAddress); throw new Exception(error); + } finally { + try { + if (connection != null) { + connection.disconnect(); + } + }catch (Exception e) { + + } } } else { @@ -99,16 +130,17 @@ public class WekeoResolverTest { } // to be sure - if (wekeoToken == null) { + if (wekeoResponse.length() == 0) { String error = String - .format("Sorry an rrror occured on getting the wekeo token. Please, retry the request"); + .format("Sorry an error occured on getting the access token from Wekeo. Please, retry the request"); throw new Exception(error); } + + logger.info("returning: \n"+wekeoResponse.toString()); } catch (Exception e) { + e.printStackTrace(); logger.error(e.getMessage(), e); } - } - } From d9fff245ed660f1298aa3adf66559b3b597375af Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 7 Apr 2021 12:19:51 +0200 Subject: [PATCH 04/17] First release for #20993 --- CHANGELOG.md | 2 +- pom.xml | 39 ++--- .../resolver/services/WekeoResolver.java | 138 ++++++++++-------- .../datatransfer/test/WekeoResolverTest.java | 19 +-- 4 files changed, 104 insertions(+), 94 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5de1e93..21d16ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm **New features** -[Task #19942] Supported new resource "Wekeo Interface" - gettoken. +[Task #20993] Supported new resource "Wekeo Interface" - gettoken. ## [v2-4-1] - 2021-01-13 diff --git a/pom.xml b/pom.xml index 71f5112..d3b3f49 100644 --- a/pom.xml +++ b/pom.xml @@ -92,14 +92,17 @@ - - org.gcube.common - gxHTTP - compile - + + + + + - + + com.google.guava + guava + 18.0 + org.gcube.core @@ -184,18 +187,18 @@ compile - - - - - - + + + + + + - - - - - + + + + + commons-lang diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java index fded230..21080be 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -3,8 +3,10 @@ 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.io.BufferedInputStream; import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.Collection; import java.util.List; @@ -15,20 +17,11 @@ import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.commons.codec.binary.Base64; 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; @@ -84,66 +77,97 @@ public class WekeoResolver { throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); } - AccessPoint wekeoAccessPoint = readWekeoServiceEndpoint(req, scope); - - if(wekeoAccessPoint!=null) { - + StringBuilder wekeoResponse = new StringBuilder(); + AccessPoint wekeoAccessPoint = WekeoResolver.readWekeoServiceEndpoint(req, scope); + + if (wekeoAccessPoint != null) { + String wekeoUsername = wekeoAccessPoint.username(); String wekeoAddress = wekeoAccessPoint.address(); String wekeoPwd = wekeoAccessPoint.password(); - - //printing the access point found - if(logger.isDebugEnabled()) { - String msg = String.format("Found the username ' %s' and the address '%s' to perform the request", wekeoUsername, wekeoAddress); + + // printing the access point found + if (logger.isDebugEnabled()) { + String msg = String.format("Found the username '%s' and the address '%s' to perform the request", + wekeoUsername, wekeoAddress); logger.debug(msg); } - - //decrypting the pwd - if(wekeoPwd!=null){ - wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoAccessPoint.password()); - logger.info("Returning decrypted pwd registered into "+RUNTIME_WKEO_RESOURCE_NAME +" SE: "+wekeoPwd.substring(0,wekeoPwd.length()/2)+"...."); + + logger.info("The pwd is: "+wekeoPwd); + // decrypting the pwd + if (wekeoPwd != null) { + wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoPwd); + logger.info("Decrypted pwd registered into Access Point '" + wekeoAccessPoint.name() + "' is: " + + wekeoPwd.substring(0,wekeoPwd.length()/2)+"..."); } - - if(wekeoUsername!=null && wekeoPwd!=null & wekeoAddress!=null) { - + + if (wekeoUsername != null && wekeoPwd != null & wekeoAddress != null) { + HttpURLConnection connection = null; + InputStream content = null; + InputStreamReader in = null; try { - //performing the HTTP request with Basic Authentication - CredentialsProvider provider = new BasicCredentialsProvider(); - UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(wekeoUsername, wekeoPwd); - provider.setCredentials(AuthScope.ANY, credentials); + + String authString = wekeoUsername + ":" + wekeoPwd; + byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); + String authStringEnc = new String(authEncBytes); + logger.debug("Base64 encoded auth string: " + authStringEnc); + + logger.info("Performing the request to: "+wekeoAddress); + URL url = new URL(wekeoAddress); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setDoOutput(true); + connection.setRequestProperty("Authorization", "Basic " + authStringEnc); - HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); - logger.info("calling the URL and performing basic authentication to: "+wekeoAddress); - HttpResponse response = client.execute(new HttpGet(wekeoAddress)); - int statusCode = response.getStatusLine().getStatusCode(); - logger.info("the response stus code is: "+statusCode); - if(statusCode == 200) { - InputStream is = response.getEntity().getContent(); - BufferedInputStream bif = new BufferedInputStream(is); - wekeoToken = bif.toString(); - logger.info("got the wekeo token: "+wekeoToken.substring(0,wekeoToken.length()/2)+"...."); - }else { - String error = String.format("The request to %s returned status code %d",wekeoAddress,statusCode); - throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + content = (InputStream) connection.getInputStream(); + in = new InputStreamReader(content); + + logger.info("the response code is: "+connection.getResponseCode()); + + int numCharsRead; + char[] charArray = new char[1024]; + StringBuffer sb = new StringBuffer(); + logger.debug("reading the response..."); + while ((numCharsRead = in.read(charArray)) > 0) { + sb.append(charArray, 0, numCharsRead); + } + wekeoResponse.append(sb.toString()); + //System.out.println(wekeoResponse); + + } catch (Exception e) { + logger.error(e.getMessage(), e); + String error = String.format("Error on performing request to %s", wekeoAddress); + throw new Exception(error); + } finally { + try { + if (content!= null && in != null) { + in.close(); + content.close(); + } + }catch (Exception e) { + //silent } - }catch (Exception e) { - String error = String.format("Error on performing request to %s",wekeoAddress); - throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } - - }else { - String error = String.format("I cannot read the configurations (adress, username,password) from %s in the scope %s",RUNTIME_WKEO_RESOURCE_NAME,scope); - throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + + } else { + String error = String.format( + "I cannot read the configurations (adress, username,password) from %s in the scope %s", + RUNTIME_WKEO_RESOURCE_NAME, scope); + throw new Exception(error); } } - - //to be sure - if(wekeoToken==null) { - String error = String.format("Sorry an rrror occured on getting the wekeo token. Please, retry the request"); - throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + + // to be sure + if (wekeoResponse.length() == 0) { + String error = String + .format("Sorry an error occured on getting the access token from Wekeo. Please, retry the request"); + throw new Exception(error); } - return Response.ok(wekeoToken).build(); + String theResponse = wekeoResponse.toString(); + logger.info("returning: \n"+theResponse); + + return Response.ok(theResponse).build(); }catch (Exception e) { //ALREADY MANAGED AS WebApplicationException logger.error("Exception:", e); diff --git a/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java b/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java index efb2bfe..f7780f1 100644 --- a/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java +++ b/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java @@ -1,31 +1,14 @@ package org.gcube.datatransfer.test; -import java.io.BufferedInputStream; -import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.apache.commons.codec.binary.Base64; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datatransfer.resolver.services.WekeoResolver; -import org.json.JSONObject; -import org.json.simple.parser.JSONParser; -import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,7 +28,7 @@ public class WekeoResolverTest { public static final String scope = "/gcube/devsec/devVRE"; - @Test + //@Test public void testWekeo() throws Exception{ StringBuilder wekeoResponse = new StringBuilder(); From c2ff9db3a60ed604731164b5fdbdb0eceb7b3977 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 7 Apr 2021 12:26:01 +0200 Subject: [PATCH 05/17] removed unused code --- .../org/gcube/datatransfer/resolver/services/WekeoResolver.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java index 21080be..37f845b 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -62,7 +62,6 @@ public class WekeoResolver { public Response getToken(@Context HttpServletRequest req) throws WebApplicationException{ logger.info(this.getClass().getSimpleName()+" getToken starts..."); - String wekeoToken = null; try { From 93434b1f0cf2088d48f48f4a2cefb493935b4193 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 7 Apr 2021 15:04:17 +0200 Subject: [PATCH 06/17] Added as POST and Consume JSON --- .../datatransfer/resolver/services/WekeoResolver.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java index 37f845b..fd72bfa 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -11,10 +11,14 @@ import java.util.Collection; import java.util.List; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; +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 org.apache.commons.codec.binary.Base64; @@ -30,7 +34,7 @@ import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -// TODO: Auto-generated Javadoc + /** * The Class WekeoResolver. * @@ -57,8 +61,10 @@ public class WekeoResolver { * @return the token * @throws WebApplicationException the web application exception */ - @GET + @POST @Path("/gettoken") + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.APPLICATION_JSON) public Response getToken(@Context HttpServletRequest req) throws WebApplicationException{ logger.info(this.getClass().getSimpleName()+" getToken starts..."); From 78784473f6a89b08ce5c31d3f8a6ec92087347fa Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 7 Apr 2021 16:55:12 +0200 Subject: [PATCH 07/17] changed the WekeoResolver --- .../resolver/services/WekeoResolver.java | 21 +++++- src/main/webapp/WEB-INF/jsp/cr7huevos.jsp | 22 ------- .../datatransfer/test/StorageHubTest.java | 64 +++++++++++++++++++ src/test/java/rest/TestResolvers.java | 10 ++- 4 files changed, 92 insertions(+), 25 deletions(-) delete mode 100644 src/main/webapp/WEB-INF/jsp/cr7huevos.jsp create mode 100644 src/test/java/org/gcube/datatransfer/test/StorageHubTest.java diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java index fd72bfa..654b3ab 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -29,6 +29,7 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; 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.datatransfer.resolver.util.Util; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.slf4j.Logger; @@ -61,9 +62,8 @@ public class WekeoResolver { * @return the token * @throws WebApplicationException the web application exception */ - @POST + @GET @Path("/gettoken") - @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.APPLICATION_JSON) public Response getToken(@Context HttpServletRequest req) throws WebApplicationException{ @@ -180,6 +180,23 @@ public class WekeoResolver { } } + @GET + @Path("") + @Produces(MediaType.TEXT_HTML) + public Response getIndex(@Context HttpServletRequest req) { + logger.info(this.getClass().getSimpleName()+" getIndex starts..."); + + StringBuilder wekeoResponse = new StringBuilder(); + wekeoResponse.append("

Welcome to Wekeo Resolver

"); + wekeoResponse.append("
See documentation at: "+helpURI +"
"); + String wekeoGetTokenURL = String.format("%s/%s", Util.getServerURL(req), "wekeo/gettoken"); + wekeoResponse.append("
Go to gettoken request: "+wekeoGetTokenURL +"
"); + + String theResponse = wekeoResponse.toString(); + logger.info("debug: \n"+theResponse); + return Response.ok(theResponse).build(); + } + /** * Reads the wekeo endpoint information from IS. {The SE name is: @link WekeoResolver#RUNTIME_WKEO_RESOURCE_NAME} diff --git a/src/main/webapp/WEB-INF/jsp/cr7huevos.jsp b/src/main/webapp/WEB-INF/jsp/cr7huevos.jsp deleted file mode 100644 index 3c2d1cc..0000000 --- a/src/main/webapp/WEB-INF/jsp/cr7huevos.jsp +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - -
- -
- - \ No newline at end of file diff --git a/src/test/java/org/gcube/datatransfer/test/StorageHubTest.java b/src/test/java/org/gcube/datatransfer/test/StorageHubTest.java new file mode 100644 index 0000000..7e146c2 --- /dev/null +++ b/src/test/java/org/gcube/datatransfer/test/StorageHubTest.java @@ -0,0 +1,64 @@ +package org.gcube.datatransfer.test; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.storagehub.client.StreamDescriptor; +import org.gcube.common.storagehub.client.plugins.AbstractPlugin; +import org.gcube.common.storagehub.client.proxies.ItemManagerClient; +import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader; +import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference; +import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit; +import org.gcube.datatransfer.resolver.services.CatalogueResolver; +import org.gcube.datatransfer.resolver.services.StorageHubResolver; +import org.gcube.datatransfer.resolver.services.error.ExceptionManager; +import org.gcube.datatransfer.resolver.shub.StorageHubMetadataResponseBuilder; +import org.junit.Before; +import org.junit.Test; + +import com.itextpdf.text.log.SysoCounter; + + +/** + * The Class CatalogueResolverTest. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * + * May 13, 2019 + */ +public class StorageHubTest { + + private static String entityName = "using_e-infrastructures_for_biodiversity_conservation"; + private static String entityContext = "ctlg"; + private static String vreName = "BlueBridgeProject"; + + private String rootContextScope = "/d4science.research-infrastructures.eu"; + private String authorizationToken = "ea16e0fa-722a-4589-83b0-0a731d2d4039-843339462"; + + //@Before + public void init() { + + UriResolverSmartGearManagerInit.setRootContextScope(rootContextScope); + } + + //@Test + public void testStreamDescriptorInfo() { + System.out.println("testStreamDescriptorInfo starts..."); + ScopeProvider.instance.set(rootContextScope); + SecurityTokenProvider.instance.set(authorizationToken); + String storageHubId = "E_NGJIUEYvU09sNG1YY0R2VGIyaStWdGhDSW9sSjRNdDRkdVI2RHRGb1BZMVBaVFlzMG1mOU5QUEtFM1hQeE9kbw=="; + try{ + + ItemManagerClient client = AbstractPlugin.item().build(); + StreamDescriptor descriptor = client.resolvePublicLink(storageHubId); + System.out.println("Descriptor: "+descriptor); + + }catch(Exception e){ + e.printStackTrace(); + } + } + + + +} diff --git a/src/test/java/rest/TestResolvers.java b/src/test/java/rest/TestResolvers.java index baf0ea1..2b510cb 100644 --- a/src/test/java/rest/TestResolvers.java +++ b/src/test/java/rest/TestResolvers.java @@ -15,10 +15,19 @@ import java.nio.file.StandardCopyOption; import java.util.List; import java.util.Map; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; + import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.apache.http.HttpStatus; +import org.gcube.common.storagehub.client.StreamDescriptor; +import org.gcube.common.storagehub.client.plugins.AbstractPlugin; +import org.gcube.common.storagehub.client.proxies.ItemManagerClient; import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader; +import org.gcube.datatransfer.resolver.services.StorageHubResolver; +import org.gcube.datatransfer.resolver.services.error.ExceptionManager; +import org.gcube.datatransfer.resolver.shub.StorageHubMetadataResponseBuilder; import org.gcube.datatransfer.resolver.util.HTTPCallsUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -81,7 +90,6 @@ public class TestResolvers { } } - /** * Storage hub test. * From 6038760a39be34a6a5202fd7aa39e31b92e2012e Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 8 Apr 2021 10:25:48 +0200 Subject: [PATCH 08/17] #21093 changed status code from 204 to 200 --- CHANGELOG.md | 10 ++-- .../resolver/services/StorageHubResolver.java | 2 +- .../resolver/services/WekeoResolver.java | 40 +++++++++------ .../StorageHubMetadataResponseBuilder.java | 4 +- src/main/webapp/WEB-INF/jsp/wekeo.jsp | 51 +++++++++++++++++++ 5 files changed, 86 insertions(+), 21 deletions(-) create mode 100644 src/main/webapp/WEB-INF/jsp/wekeo.jsp diff --git a/CHANGELOG.md b/CHANGELOG.md index 21d16ad..aea3bc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,18 +4,22 @@ 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 +## [v2-5-0-SNAPSHOT] - 2021-04-08 **New features** -[Task #20993] Supported new resource "Wekeo Interface" - gettoken. +[#20993] Supported new resource "Wekeo Interface" - gettoken. + +**Bug fixes** + +[#21093] StorageHubResolver HEAD request does not support Content-Length ## [v2-4-1] - 2021-01-13 **Bug Fixes** -[Task #19942] Fixing master build fails +[#19942] Fixing master build fails ## [v2-4-0] [r4-24-0]- 2020-06-18 diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java index 481168f..da5711c 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java @@ -71,7 +71,7 @@ public class StorageHubResolver { ItemManagerClient client = AbstractPlugin.item().build(); StreamDescriptor descriptor = client.resolvePublicLink(id); - ResponseBuilder response = Response.noContent(); + ResponseBuilder response = Response.ok(); response = new StorageHubMetadataResponseBuilder(req, response).fillMetadata(descriptor, id, CONTENT_DISPOSITION_VALUE.attachment); return response.build(); diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java index 654b3ab..1b35465 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/WekeoResolver.java @@ -3,6 +3,8 @@ 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.io.File; +import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; @@ -181,23 +183,29 @@ public class WekeoResolver { } @GET + @Produces({MediaType.TEXT_HTML}) @Path("") - @Produces(MediaType.TEXT_HTML) - public Response getIndex(@Context HttpServletRequest req) { - logger.info(this.getClass().getSimpleName()+" getIndex starts..."); - - StringBuilder wekeoResponse = new StringBuilder(); - wekeoResponse.append("

Welcome to Wekeo Resolver

"); - wekeoResponse.append("
See documentation at: "+helpURI +"
"); - String wekeoGetTokenURL = String.format("%s/%s", Util.getServerURL(req), "wekeo/gettoken"); - wekeoResponse.append("
Go to gettoken request: "+wekeoGetTokenURL +"
"); - - String theResponse = wekeoResponse.toString(); - logger.info("debug: \n"+theResponse); - return Response.ok(theResponse).build(); + public InputStream index(@Context HttpServletRequest req) throws WebApplicationException{ + + String indexFile = "/WEB-INF/jsp/wekeo.jsp"; + + try{ + logger.info(UriResolverIndex.class.getSimpleName() +" called"); + String realPath = req.getServletContext().getRealPath(indexFile); + return new FileInputStream(new File(realPath)); + }catch (Exception e) { + + if(!(e instanceof WebApplicationException)){ + //UNEXPECTED EXCEPTION managing it as WebApplicationException + String error = "Index.jsp not found. Please, contact the support!"; + throw ExceptionManager.internalErrorException(req, error, this.getClass(), null); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; + } } - /** * Reads the wekeo endpoint information from IS. {The SE name is: @link WekeoResolver#RUNTIME_WKEO_RESOURCE_NAME} * @@ -225,7 +233,7 @@ public class WekeoResolver { 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); + throw ExceptionManager.internalErrorException(req, errorMessage, WekeoResolver.class, helpURI); } String wekeoUsername = null; @@ -247,7 +255,7 @@ public class WekeoResolver { }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); + throw ExceptionManager.internalErrorException(req, errorMessage, WekeoResolver.class, helpURI); }finally{ if(callerScope!=null){ diff --git a/src/main/java/org/gcube/datatransfer/resolver/shub/StorageHubMetadataResponseBuilder.java b/src/main/java/org/gcube/datatransfer/resolver/shub/StorageHubMetadataResponseBuilder.java index c05d2a4..da67910 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/shub/StorageHubMetadataResponseBuilder.java +++ b/src/main/java/org/gcube/datatransfer/resolver/shub/StorageHubMetadataResponseBuilder.java @@ -58,8 +58,10 @@ public class StorageHubMetadataResponseBuilder { responseBuilder.header("Content-Location", contentLocation); //Managing "Content-Type" - if (streamDescriptor.getContentType()!= null && !streamDescriptor.getContentType().isEmpty()) + if (streamDescriptor.getContentType()!= null && !streamDescriptor.getContentType().isEmpty()) { + responseBuilder.header("Content-Type", streamDescriptor.getContentType()); + } //Managing "Content-Lenght" if(streamDescriptor.getContentLenght()>0) { diff --git a/src/main/webapp/WEB-INF/jsp/wekeo.jsp b/src/main/webapp/WEB-INF/jsp/wekeo.jsp new file mode 100644 index 0000000..f8b628f --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/wekeo.jsp @@ -0,0 +1,51 @@ + + + + + + + +
+ +
The Wekeo Resolver
+
Available Operations: +
    +
  • getToken at <%=request.getRequestURL()%>/gettoken +
  • +
+
+

+ See wiki page at gCube Wiki Wekeo + Resolver +

+
+ + \ No newline at end of file From 4c414651677c6d91f4ec774ee4c8cca72431a3ed Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 8 Apr 2021 10:29:12 +0200 Subject: [PATCH 09/17] added wekeo.jsp --- src/main/webapp/WEB-INF/jsp/wekeo.jsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/webapp/WEB-INF/jsp/wekeo.jsp b/src/main/webapp/WEB-INF/jsp/wekeo.jsp index f8b628f..b73f34f 100644 --- a/src/main/webapp/WEB-INF/jsp/wekeo.jsp +++ b/src/main/webapp/WEB-INF/jsp/wekeo.jsp @@ -37,7 +37,7 @@ body {
The Wekeo Resolver
Available Operations:
    -
  • getToken at <%=request.getRequestURL()%>/gettoken +
  • getToken at
From 4d2f9fc94ae71a0c221ce1a487d395f5c63dc330 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 8 Apr 2021 10:58:16 +0200 Subject: [PATCH 10/17] improved the wekeo.jsp --- src/main/webapp/WEB-INF/jsp/wekeo.jsp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/webapp/WEB-INF/jsp/wekeo.jsp b/src/main/webapp/WEB-INF/jsp/wekeo.jsp index b73f34f..dd8939b 100644 --- a/src/main/webapp/WEB-INF/jsp/wekeo.jsp +++ b/src/main/webapp/WEB-INF/jsp/wekeo.jsp @@ -27,6 +27,18 @@ body { font-size: 22px; font-weight: bold; } + +.myListOperations { + margin-top: 20px; + margin-bottom: 40px; + color: #333; +} + +.uri-footer { + padding: 20px 20px; + font-size: 14px; +} + @@ -35,17 +47,17 @@ body {
The Wekeo Resolver
-
Available Operations: +
Available Operations:
    -
  • getToken at +
  • authentication operator gettoken (gCube AuthN Required)
-

+

\ No newline at end of file From 5d719646d1291c502fc74253e307a432d0c0e998 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 8 Apr 2021 11:56:58 +0200 Subject: [PATCH 11/17] fixed the jsp --- src/main/webapp/WEB-INF/jsp/wekeo.jsp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/webapp/WEB-INF/jsp/wekeo.jsp b/src/main/webapp/WEB-INF/jsp/wekeo.jsp index dd8939b..20ffc56 100644 --- a/src/main/webapp/WEB-INF/jsp/wekeo.jsp +++ b/src/main/webapp/WEB-INF/jsp/wekeo.jsp @@ -48,10 +48,9 @@ body { src="https://www.d4science.org/image/layout_set_logo?img_id=12630" />
The Wekeo Resolver
Available Operations: -
    -
  • authentication operator gettoken (gCube AuthN Required) -
  • -
+

+ # authentication operator gettoken (gCube AuthN Required) +