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