115 lines
3.9 KiB
Java
115 lines
3.9 KiB
Java
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);
|
|
}
|
|
|
|
}
|
|
|
|
}
|