task_20993 #1

Merged
francesco.mangiacrapa merged 17 commits from task_20993 into master 2021-04-09 11:11:49 +02:00
4 changed files with 104 additions and 94 deletions
Showing only changes of commit d9fff245ed - Show all commits

View File

@ -8,7 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
**New features** **New features**
[Task #19942] Supported new resource "Wekeo Interface" - gettoken. [Task #20993] Supported new resource "Wekeo Interface" - gettoken.
## [v2-4-1] - 2021-01-13 ## [v2-4-1] - 2021-01-13

39
pom.xml
View File

@ -92,14 +92,17 @@
</dependency> </dependency>
<!-- TODO REMOVE THIS IMPORT --> <!-- TODO REMOVE THIS IMPORT -->
<dependency> <!-- <dependency> -->
<groupId>org.gcube.common</groupId> <!-- <groupId>org.gcube.common</groupId> -->
<artifactId>gxHTTP</artifactId> <!-- <artifactId>gxHTTP</artifactId> -->
<scope>compile</scope> <!-- <scope>compile</scope> -->
</dependency> <!-- </dependency> -->
<!-- //TO BE ADDED THE VERSION 18.0 <dependency> <groupId>com.google.guava</groupId> <dependency>
<artifactId>guava</artifactId> </dependency> --> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
@ -184,18 +187,18 @@
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- NEEDED TO compile 'geonetwork' --> <!-- REMOVE THIS ON RELESE, IT IS NEEDED TO compile 'geonetwork' -->
<!-- <dependency> --> <!-- <dependency> -->
<!-- <groupId>org.opengis</groupId> --> <!-- <groupId>org.opengis</groupId> -->
<!-- <artifactId>geoapi</artifactId> --> <!-- <artifactId>geoapi</artifactId> -->
<!-- <version>3.0.0</version> --> <!-- <version>3.0.0</version> -->
<!-- </dependency> --> <!-- </dependency> -->
<!-- <dependency> --> <!-- <dependency> -->
<!-- <groupId>org.geotoolkit</groupId> --> <!-- <groupId>org.geotoolkit</groupId> -->
<!-- <artifactId>geotk-xml-base</artifactId> --> <!-- <artifactId>geotk-xml-base</artifactId> -->
<!-- <version>3.20-geoapi-3.0</version> --> <!-- <version>3.20-geoapi-3.0</version> -->
<!-- </dependency> --> <!-- </dependency> -->
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>

View File

@ -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.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.BufferedInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -15,20 +17,11 @@ import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.apache.http.HttpResponse; import org.apache.commons.codec.binary.Base64;
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.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; 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.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.requesthandler.RequestHandler; import org.gcube.datatransfer.resolver.requesthandler.RequestHandler;
import org.gcube.datatransfer.resolver.services.error.ExceptionManager; 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); throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI);
} }
AccessPoint wekeoAccessPoint = readWekeoServiceEndpoint(req, scope); StringBuilder wekeoResponse = new StringBuilder();
AccessPoint wekeoAccessPoint = WekeoResolver.readWekeoServiceEndpoint(req, scope);
if(wekeoAccessPoint!=null) {
if (wekeoAccessPoint != null) {
String wekeoUsername = wekeoAccessPoint.username(); String wekeoUsername = wekeoAccessPoint.username();
String wekeoAddress = wekeoAccessPoint.address(); String wekeoAddress = wekeoAccessPoint.address();
String wekeoPwd = wekeoAccessPoint.password(); String wekeoPwd = wekeoAccessPoint.password();
//printing the access point found // printing the access point found
if(logger.isDebugEnabled()) { 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); logger.debug(msg);
} }
//decrypting the pwd logger.info("The pwd is: "+wekeoPwd);
if(wekeoPwd!=null){ // decrypting the pwd
wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoAccessPoint.password()); if (wekeoPwd != null) {
logger.info("Returning decrypted pwd registered into "+RUNTIME_WKEO_RESOURCE_NAME +" SE: "+wekeoPwd.substring(0,wekeoPwd.length()/2)+"...."); 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 { try {
//performing the HTTP request with Basic Authentication
CredentialsProvider provider = new BasicCredentialsProvider(); String authString = wekeoUsername + ":" + wekeoPwd;
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(wekeoUsername, wekeoPwd); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
provider.setCredentials(AuthScope.ANY, credentials); 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(); content = (InputStream) connection.getInputStream();
logger.info("calling the URL and performing basic authentication to: "+wekeoAddress); in = new InputStreamReader(content);
HttpResponse response = client.execute(new HttpGet(wekeoAddress));
int statusCode = response.getStatusLine().getStatusCode(); logger.info("the response code is: "+connection.getResponseCode());
logger.info("the response stus code is: "+statusCode);
if(statusCode == 200) { int numCharsRead;
InputStream is = response.getEntity().getContent(); char[] charArray = new char[1024];
BufferedInputStream bif = new BufferedInputStream(is); StringBuffer sb = new StringBuffer();
wekeoToken = bif.toString(); logger.debug("reading the response...");
logger.info("got the wekeo token: "+wekeoToken.substring(0,wekeoToken.length()/2)+"...."); while ((numCharsRead = in.read(charArray)) > 0) {
}else { sb.append(charArray, 0, numCharsRead);
String error = String.format("The request to %s returned status code %d",wekeoAddress,statusCode); }
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); 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 { } else {
String error = String.format("I cannot read the configurations (adress, username,password) from %s in the scope %s",RUNTIME_WKEO_RESOURCE_NAME,scope); String error = String.format(
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); "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 // 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"); String error = String
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); .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) { }catch (Exception e) {
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e); logger.error("Exception:", e);

View File

@ -1,31 +1,14 @@
package org.gcube.datatransfer.test; package org.gcube.datatransfer.test;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import org.apache.commons.codec.binary.Base64; 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.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.services.WekeoResolver; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -45,7 +28,7 @@ public class WekeoResolverTest {
public static final String scope = "/gcube/devsec/devVRE"; public static final String scope = "/gcube/devsec/devVRE";
@Test //@Test
public void testWekeo() throws Exception{ public void testWekeo() throws Exception{
StringBuilder wekeoResponse = new StringBuilder(); StringBuilder wekeoResponse = new StringBuilder();