First release for #20993

task_20993
Francesco Mangiacrapa 3 years ago
parent c6adaba745
commit d9fff245ed

@ -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

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

@ -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);

@ -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();

Loading…
Cancel
Save