moved to 2.5.0-SNAPSHOT. Starting activity #20993

This commit is contained in:
Francesco Mangiacrapa 2021-03-30 18:21:49 +02:00
parent 7a28d43768
commit 2c3538caf4
4 changed files with 202 additions and 1 deletions

View File

@ -4,6 +4,13 @@
All notable changes to this project will be documented in this file. 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). 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 ## [v2-4-1] - 2021-01-13
**Bug Fixes** **Bug Fixes**

View File

@ -9,7 +9,7 @@
</parent> </parent>
<groupId>org.gcube.data.transfer</groupId> <groupId>org.gcube.data.transfer</groupId>
<artifactId>uri-resolver</artifactId> <artifactId>uri-resolver</artifactId>
<version>2.4.1</version> <version>2.5.0-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<description>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.</description> <description>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.</description>

View File

@ -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<ServiceEndpoint> 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<ServiceEndpoint> 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<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> 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<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> 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<AccessPoint> theAccessPoints = se.profile().accessPoints().asCollection();
for (AccessPoint accessPoint : theAccessPoints) {
Collection<Property> 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();
}
}
}
}

View File

@ -5,4 +5,5 @@
<description>URIResolver RESTful</description> <description>URIResolver RESTful</description>
<include>/analytics/create/*</include> <include>/analytics/create/*</include>
<include>/knime/create/*</include> <include>/knime/create/*</include>
<include>/wekeo/gettoken/*</include>
</application> </application>