uri-resolver/src/test/java/org/gcube/datatransfer/test/WekeoResolverTest.java

130 lines
4.1 KiB
Java
Raw Normal View History

2021-03-31 18:02:54 +02:00
package org.gcube.datatransfer.test;
import java.io.InputStream;
2021-04-01 17:15:03 +02:00
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
2021-03-31 18:02:54 +02:00
2021-04-01 17:15:03 +02:00
import org.apache.commons.codec.binary.Base64;
2021-03-31 18:02:54 +02:00
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
2021-04-01 17:15:03 +02:00
import org.gcube.common.scope.api.ScopeProvider;
2021-03-31 18:02:54 +02:00
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";
2021-04-01 17:15:03 +02:00
2021-04-07 12:19:51 +02:00
//@Test
2021-04-01 17:15:03 +02:00
public void testWekeo() throws Exception{
StringBuilder wekeoResponse = new StringBuilder();
2021-03-31 18:02:54 +02:00
try {
2021-04-01 17:15:03 +02:00
logger.info(" test starts...");
2021-03-31 18:02:54 +02:00
2021-04-01 17:15:03 +02:00
ScopeProvider.instance.set(scope);
2021-03-31 18:02:54 +02:00
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()) {
2021-04-01 17:15:03 +02:00
String msg = String.format("Found the username '%s' and the address '%s' to perform the request",
2021-03-31 18:02:54 +02:00
wekeoUsername, wekeoAddress);
logger.debug(msg);
}
2021-04-01 17:15:03 +02:00
logger.info("The pwd is: "+wekeoPwd);
2021-03-31 18:02:54 +02:00
// decrypting the pwd
if (wekeoPwd != null) {
2021-04-01 17:15:03 +02:00
wekeoPwd = StringEncrypter.getEncrypter().decrypt(wekeoPwd);
logger.info("Decrypted pwd registered into Access Point '" + wekeoAccessPoint.name() + "' is: "
+ wekeoPwd.substring(0,wekeoPwd.length()/2)+"...");
2021-03-31 18:02:54 +02:00
}
if (wekeoUsername != null && wekeoPwd != null & wekeoAddress != null) {
2021-04-01 17:15:03 +02:00
HttpURLConnection connection = null;
2021-03-31 18:02:54 +02:00
try {
2021-04-01 17:15:03 +02:00
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);
InputStream content = (InputStream) connection.getInputStream();
InputStreamReader 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);
2021-03-31 18:02:54 +02:00
}
2021-04-01 17:15:03 +02:00
wekeoResponse.append(sb.toString());
//System.out.println(wekeoResponse);
2021-03-31 18:02:54 +02:00
} catch (Exception e) {
2021-04-01 17:15:03 +02:00
logger.error(e.getMessage(), e);
2021-03-31 18:02:54 +02:00
String error = String.format("Error on performing request to %s", wekeoAddress);
throw new Exception(error);
2021-04-01 17:15:03 +02:00
} finally {
try {
if (connection != null) {
connection.disconnect();
}
}catch (Exception e) {
}
2021-03-31 18:02:54 +02:00
}
} 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
2021-04-01 17:15:03 +02:00
if (wekeoResponse.length() == 0) {
2021-03-31 18:02:54 +02:00
String error = String
2021-04-01 17:15:03 +02:00
.format("Sorry an error occured on getting the access token from Wekeo. Please, retry the request");
2021-03-31 18:02:54 +02:00
throw new Exception(error);
}
2021-04-01 17:15:03 +02:00
logger.info("returning: \n"+wekeoResponse.toString());
2021-03-31 18:02:54 +02:00
} catch (Exception e) {
2021-04-01 17:15:03 +02:00
e.printStackTrace();
2021-03-31 18:02:54 +02:00
logger.error(e.getMessage(), e);
}
}
}