2018-10-31 11:52:04 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package rest;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.apache.commons.io.FilenameUtils;
|
2018-11-07 12:27:45 +01:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2018-10-31 11:52:04 +01:00
|
|
|
import org.apache.http.HttpStatus;
|
2018-12-12 15:38:44 +01:00
|
|
|
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
|
2018-11-06 17:09:38 +01:00
|
|
|
import org.gcube.datatransfer.resolver.util.HTTPCallsUtils;
|
2018-10-31 11:52:04 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class TestResolvers.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* Oct 31, 2018
|
|
|
|
*/
|
|
|
|
public class TestResolvers {
|
|
|
|
|
|
|
|
|
|
|
|
public static final String URI_RESOLVER_SERVICE_ENDPOINT = "https://data1-d.d4science.net";
|
|
|
|
|
|
|
|
public static final Logger logger = LoggerFactory.getLogger(TestResolvers.class);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage hub test.
|
2018-11-08 15:33:24 +01:00
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
2018-10-31 11:52:04 +01:00
|
|
|
*/
|
2018-12-05 17:04:56 +01:00
|
|
|
//@Test
|
2018-11-08 15:33:24 +01:00
|
|
|
public void testStorageHub() throws Exception{
|
2018-10-31 11:52:04 +01:00
|
|
|
|
2018-12-28 15:02:22 +01:00
|
|
|
String storageHubId = "E_RHdYOUxBSGJ3dU1ESjEyMXNuc2ZMRm5HbXh0d1ZqaWJ4MHBaN0lOZ0dBR1dCQlVhWnVIS0hrdmN2VDhkTXk0UA==";
|
2018-10-31 11:52:04 +01:00
|
|
|
String url = String.format("%s/shub/%s",URI_RESOLVER_SERVICE_ENDPOINT,storageHubId);
|
2018-10-31 12:01:52 +01:00
|
|
|
logger.info("Request to URL: "+url);
|
2018-10-31 11:52:04 +01:00
|
|
|
URL toURL;
|
|
|
|
int status;
|
|
|
|
try {
|
|
|
|
toURL = new URL(url);
|
|
|
|
HttpURLConnection con = (HttpURLConnection) toURL.openConnection();
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
con.connect();
|
|
|
|
status = con.getResponseCode();
|
2018-12-28 15:02:22 +01:00
|
|
|
System.out.println("header fields: "+con.getHeaderFields());
|
2018-10-31 11:52:04 +01:00
|
|
|
String fileName = getFilename(con.getHeaderFields());
|
|
|
|
System.out.println("Response status is: "+status);
|
|
|
|
if(status==HttpStatus.SC_OK){
|
|
|
|
Path target = Files.createTempFile(FilenameUtils.getBaseName(fileName), "."+FilenameUtils.getExtension(fileName));
|
|
|
|
try (InputStream in = con.getInputStream()) {
|
|
|
|
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
}
|
|
|
|
System.out.println("File downloaded at: "+target.toAbsolutePath());
|
|
|
|
}else{
|
|
|
|
System.out.println("\nNo file downoladed");
|
|
|
|
System.out.println("Response: \n"+getContentReponse(con.getInputStream()));
|
|
|
|
}
|
|
|
|
//con.setRequestProperty("Content-Type", "application/json");
|
|
|
|
//con.setConnectTimeout(5000);
|
|
|
|
//con.setReadTimeout(5000)
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
logger.error("Error: ",e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Storage hub test.
|
2018-11-08 15:33:24 +01:00
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
2018-10-31 11:52:04 +01:00
|
|
|
*/
|
2018-12-07 11:28:37 +01:00
|
|
|
//@Test
|
2018-11-08 15:33:24 +01:00
|
|
|
public void testStorageID() throws Exception{
|
2018-10-31 11:52:04 +01:00
|
|
|
|
|
|
|
String storageID = "clZ2YmxTYytETzVLaHkwMjM3TmVETTFMb256YVRMS3lHbWJQNStIS0N6Yz0";
|
2018-12-07 10:41:18 +01:00
|
|
|
String url = String.format("%s/storage/%s",URI_RESOLVER_SERVICE_ENDPOINT,storageID);
|
2018-10-31 12:01:52 +01:00
|
|
|
logger.info("Request to URL: "+url);
|
2018-10-31 11:52:04 +01:00
|
|
|
URL toURL;
|
|
|
|
int status;
|
|
|
|
try {
|
|
|
|
toURL = new URL(url);
|
|
|
|
HttpURLConnection con = (HttpURLConnection) toURL.openConnection();
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
con.connect();
|
|
|
|
status = con.getResponseCode();
|
|
|
|
//printHeaders(con.getHeaderFields());
|
|
|
|
String fileName = getFilename(con.getHeaderFields());
|
|
|
|
System.out.println("Response status is: "+status);
|
|
|
|
if(status==HttpStatus.SC_OK){
|
|
|
|
Path target = Files.createTempFile(FilenameUtils.getBaseName(fileName), "."+FilenameUtils.getExtension(fileName));
|
|
|
|
try (InputStream in = con.getInputStream()) {
|
|
|
|
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
}
|
|
|
|
System.out.println("File downloaded at: "+target.toAbsolutePath());
|
|
|
|
}else{
|
|
|
|
System.out.println("\nNo file downoladed");
|
|
|
|
System.out.println("Response: \n"+getContentReponse(con.getInputStream()));
|
|
|
|
}
|
|
|
|
//con.setRequestProperty("Content-Type", "application/json");
|
|
|
|
//con.setConnectTimeout(5000);
|
|
|
|
//con.setReadTimeout(5000)
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
logger.error("Error: ",e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:27:45 +01:00
|
|
|
/**
|
|
|
|
* Storage hub test.
|
2018-11-08 15:33:24 +01:00
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
2018-11-07 12:27:45 +01:00
|
|
|
*/
|
2018-12-07 11:28:37 +01:00
|
|
|
//@Test
|
2018-11-08 15:33:24 +01:00
|
|
|
public void testStorageIDdoHEAD() throws Exception{
|
2018-11-07 12:27:45 +01:00
|
|
|
|
|
|
|
String storageID = "clZ2YmxTYytETzVLaHkwMjM3TmVETTFMb256YVRMS3lHbWJQNStIS0N6Yz0";
|
2018-12-07 10:41:18 +01:00
|
|
|
String url = String.format("%s/storage/%s",URI_RESOLVER_SERVICE_ENDPOINT,storageID);
|
2018-11-07 12:27:45 +01:00
|
|
|
logger.info("Request to URL: "+url);
|
|
|
|
URL toURL;
|
|
|
|
int status;
|
|
|
|
try {
|
|
|
|
toURL = new URL(url);
|
|
|
|
HttpURLConnection con = (HttpURLConnection) toURL.openConnection();
|
|
|
|
con.setRequestMethod("HEAD");
|
|
|
|
con.connect();
|
|
|
|
status = con.getResponseCode();
|
|
|
|
//printHeaders(con.getHeaderFields());
|
|
|
|
System.out.println("Response status is: "+status);
|
|
|
|
if(status==HttpStatus.SC_OK){
|
|
|
|
System.out.println("\nFile to URL: "+url +" is reachable via doHEAD");
|
|
|
|
System.out.println(IOUtils.toString(con.getInputStream()));
|
|
|
|
}else{
|
|
|
|
System.err.println("\nNo file reachable at: "+url);
|
|
|
|
}
|
|
|
|
//con.setRequestProperty("Content-Type", "application/json");
|
|
|
|
//con.setConnectTimeout(5000);
|
|
|
|
//con.setReadTimeout(5000)
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
logger.error("Error: ",e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:01:52 +01:00
|
|
|
/**
|
|
|
|
* Storage hub test.
|
2018-11-08 15:33:24 +01:00
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
2018-10-31 12:01:52 +01:00
|
|
|
*/
|
2018-12-28 15:04:45 +01:00
|
|
|
//@Test
|
2018-11-08 15:33:24 +01:00
|
|
|
public void testCatalogueResolver() throws Exception{
|
2018-10-31 12:01:52 +01:00
|
|
|
|
|
|
|
String entityName = "sarda-sarda";
|
|
|
|
String entityContext = "ctlg";
|
|
|
|
String vreName = "devVRE";
|
|
|
|
|
|
|
|
String url = String.format("%s/%s/%s/%s",URI_RESOLVER_SERVICE_ENDPOINT,entityContext, vreName, entityName);
|
|
|
|
logger.info("Request to URL: "+url);
|
|
|
|
URL toURL;
|
|
|
|
int status;
|
|
|
|
try {
|
|
|
|
toURL = new URL(url);
|
|
|
|
HttpURLConnection con = (HttpURLConnection) toURL.openConnection();
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
con.connect();
|
|
|
|
status = con.getResponseCode();
|
|
|
|
System.out.println("Response status is: "+status);
|
|
|
|
if(status==HttpStatus.SC_OK){
|
|
|
|
System.out.println("Response: \n"+getContentReponse(con.getInputStream()));
|
|
|
|
}else{
|
|
|
|
System.out.println("\nError on resolving the Catalogue URL: "+toURL);
|
|
|
|
System.out.println("Response: \n"+getContentReponse(con.getInputStream()));
|
|
|
|
}
|
|
|
|
//con.setRequestProperty("Content-Type", "application/json");
|
|
|
|
//con.setConnectTimeout(5000);
|
|
|
|
//con.setReadTimeout(5000)
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
logger.error("Error: ",e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:09:38 +01:00
|
|
|
/**
|
|
|
|
* Storage hub test.
|
2018-11-08 15:33:24 +01:00
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
2018-11-06 17:09:38 +01:00
|
|
|
*/
|
2018-11-14 17:00:22 +01:00
|
|
|
//@Test
|
2018-11-08 15:33:24 +01:00
|
|
|
public void testCatalogueResolverCreatePublicItemURL() throws Exception{
|
2018-11-06 17:09:38 +01:00
|
|
|
|
|
|
|
String entityName = "sarda-sarda";
|
2018-11-06 17:16:40 +01:00
|
|
|
//String entityContext = "product";
|
|
|
|
String entityContext = "dataset";
|
2019-02-19 15:18:19 +01:00
|
|
|
String scope = "/gcube/devsec/devVRE";
|
2018-11-06 17:09:38 +01:00
|
|
|
|
2019-02-19 15:18:19 +01:00
|
|
|
String url = String.format("%s/%s",URI_RESOLVER_SERVICE_ENDPOINT,"ctlg");
|
2018-11-07 12:27:45 +01:00
|
|
|
logger.info("POST Request to URL: "+url);
|
|
|
|
try {
|
|
|
|
|
|
|
|
HTTPCallsUtils httCaller = new HTTPCallsUtils(null, null);
|
|
|
|
String jsonString =
|
|
|
|
"{" +
|
|
|
|
"\"gcube_scope\":\""+scope+"\"," +
|
|
|
|
"\"entity_context\":\""+entityContext+"\"," +
|
|
|
|
"\"entity_name\":\""+entityName+"\"" +
|
|
|
|
"}";
|
|
|
|
System.out.println("Sending JSON: "+jsonString);
|
|
|
|
// JSONObject json = new JSONObject();
|
|
|
|
// json.append("gcube_scope", scope);
|
|
|
|
// json.append("entity_context", entityContext);
|
|
|
|
// json.append("entity_name", entityName);
|
|
|
|
// System.out.println("Sending json object: "+json.toString());
|
|
|
|
InputStream response = httCaller.post(url, jsonString, "application/json");
|
|
|
|
System.out.println("Response: \n"+getContentReponse(response));
|
|
|
|
//con.setRequestProperty("Content-Type", "application/json");
|
|
|
|
//con.setConnectTimeout(5000);
|
|
|
|
//con.setReadTimeout(5000)
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
logger.error("Error: ",e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:04:45 +01:00
|
|
|
|
2018-11-07 12:27:45 +01:00
|
|
|
/**
|
2018-12-28 15:04:45 +01:00
|
|
|
* Test catalogue resolver create private item url.
|
2018-11-08 15:33:24 +01:00
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
2018-11-07 12:27:45 +01:00
|
|
|
*/
|
2018-12-28 15:04:45 +01:00
|
|
|
//@Test
|
|
|
|
public void testCatalogueResolverCreatePrivateItemURL() throws Exception{
|
2018-11-07 12:27:45 +01:00
|
|
|
|
|
|
|
String entityName = "dynamic_reporting";
|
|
|
|
//String entityContext = "product";
|
|
|
|
String entityContext = "dataset";
|
|
|
|
String scope = "/gcube/devsec/devVRE";
|
|
|
|
|
2018-11-06 17:09:38 +01:00
|
|
|
String url = String.format("%s/%s",URI_RESOLVER_SERVICE_ENDPOINT,"catalogue");
|
|
|
|
logger.info("POST Request to URL: "+url);
|
|
|
|
try {
|
2018-10-31 11:52:04 +01:00
|
|
|
|
2018-11-06 17:09:38 +01:00
|
|
|
HTTPCallsUtils httCaller = new HTTPCallsUtils(null, null);
|
|
|
|
String jsonString =
|
|
|
|
"{" +
|
|
|
|
"\"gcube_scope\":\""+scope+"\"," +
|
|
|
|
"\"entity_context\":\""+entityContext+"\"," +
|
|
|
|
"\"entity_name\":\""+entityName+"\"" +
|
|
|
|
"}";
|
|
|
|
System.out.println("Sending JSON: "+jsonString);
|
|
|
|
// JSONObject json = new JSONObject();
|
|
|
|
// json.append("gcube_scope", scope);
|
|
|
|
// json.append("entity_context", entityContext);
|
|
|
|
// json.append("entity_name", entityName);
|
|
|
|
// System.out.println("Sending json object: "+json.toString());
|
|
|
|
InputStream response = httCaller.post(url, jsonString, "application/json");
|
|
|
|
System.out.println("Response: \n"+getContentReponse(response));
|
|
|
|
//con.setRequestProperty("Content-Type", "application/json");
|
|
|
|
//con.setConnectTimeout(5000);
|
|
|
|
//con.setReadTimeout(5000)
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
logger.error("Error: ",e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2018-11-08 15:33:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//@Test
|
|
|
|
/**
|
|
|
|
* Test gis resolver.
|
|
|
|
*
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2018-11-09 12:05:53 +01:00
|
|
|
//@Test
|
2018-11-08 15:33:24 +01:00
|
|
|
public void testGisResolver() throws Exception{
|
|
|
|
|
|
|
|
String gisUUID = "55c19a1f-214b-4f81-9220-fba09fcfa91f";
|
|
|
|
String scope = "/gcube/devsec/devVRE";
|
|
|
|
|
|
|
|
String queryString = "gis-UUID="+gisUUID +"&scope="+scope;
|
|
|
|
String url = String.format("%s/gis?%s",URI_RESOLVER_SERVICE_ENDPOINT,queryString);
|
|
|
|
|
|
|
|
logger.info("Request to URL: "+url);
|
|
|
|
URL toURL;
|
|
|
|
int status;
|
|
|
|
try {
|
|
|
|
toURL = new URL(url);
|
|
|
|
HttpURLConnection con = (HttpURLConnection) toURL.openConnection();
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
con.connect();
|
|
|
|
status = con.getResponseCode();
|
|
|
|
//printHeaders(con.getHeaderFields());
|
|
|
|
System.out.println("Response status is: "+status);
|
|
|
|
if(status==HttpStatus.SC_SEE_OTHER){
|
|
|
|
System.out.println("\nResponse to URL: "+url);
|
|
|
|
System.out.println(IOUtils.toString(con.getInputStream()));
|
|
|
|
}else{
|
|
|
|
System.err.println("\nNo file reachable at: "+url);
|
|
|
|
}
|
|
|
|
//con.setRequestProperty("Content-Type", "application/json");
|
|
|
|
//con.setConnectTimeout(5000);
|
|
|
|
//con.setReadTimeout(5000)
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
logger.error("Error: ",e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-31 11:52:04 +01:00
|
|
|
/**
|
|
|
|
* Gets the content reponse.
|
|
|
|
*
|
|
|
|
* @param is the is
|
|
|
|
* @return the content reponse
|
|
|
|
*/
|
|
|
|
public static String getContentReponse(InputStream is){
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(is));
|
|
|
|
String inputLine;
|
|
|
|
StringBuffer content = new StringBuffer();
|
|
|
|
try {
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
|
|
|
content.append(inputLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}finally{
|
|
|
|
if(in!=null)
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
//silent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return content.toString();
|
|
|
|
}
|
|
|
|
|
2018-11-08 15:33:24 +01:00
|
|
|
/**
|
|
|
|
* Prints the headers.
|
|
|
|
*
|
|
|
|
* @param map the map
|
|
|
|
*/
|
2018-10-31 11:52:04 +01:00
|
|
|
public static void printHeaders(Map<String, List<String>> map){
|
|
|
|
|
|
|
|
System.out.println("Printing Response Header...");
|
|
|
|
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
|
|
|
|
System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-08 15:33:24 +01:00
|
|
|
/**
|
|
|
|
* Gets the filename.
|
|
|
|
*
|
|
|
|
* @param map the map
|
|
|
|
* @return the filename
|
|
|
|
*/
|
2018-10-31 11:52:04 +01:00
|
|
|
public static String getFilename(Map<String, List<String>> map){
|
|
|
|
|
|
|
|
String fileName = "filename";
|
2018-12-28 15:02:22 +01:00
|
|
|
List<String> contentDispValue = map.get("Content-Disposition");
|
2018-11-08 15:33:24 +01:00
|
|
|
if(contentDispValue==null)
|
|
|
|
return fileName;
|
|
|
|
|
2018-10-31 11:52:04 +01:00
|
|
|
System.out.println("Printing content-disposition from Response Header...");
|
|
|
|
for (String value : contentDispValue) {
|
|
|
|
System.out.println(value);
|
|
|
|
int start = value.indexOf("\"");
|
|
|
|
if(start>=0){
|
|
|
|
int end = value.lastIndexOf("\"");
|
|
|
|
fileName = value.substring(start+1,end).trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Filename is: "+fileName);
|
|
|
|
return fileName;
|
|
|
|
}
|
2018-12-12 15:38:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static final String ORG_GCUBE_PORTLETS_USER_DATAMINERMANAGER_SERVER_DATA_MINER_MANAGER_SERVICE_IMPL =
|
|
|
|
"org.gcube.portlets.user.dataminermanager.server.DataMinerManagerServiceImpl";
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static final String APPLICATION_PROFILE = "ApplicationProfile";
|
2018-12-28 15:04:45 +01:00
|
|
|
|
2018-12-12 15:38:44 +01:00
|
|
|
/**
|
2018-12-28 15:04:45 +01:00
|
|
|
* The main method.
|
2018-12-12 15:38:44 +01:00
|
|
|
*
|
2018-12-28 15:04:45 +01:00
|
|
|
* @param args the arguments
|
2018-12-12 15:38:44 +01:00
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
ApplicationProfileReader reader = new ApplicationProfileReader("/gcube/preprod/preVRE", APPLICATION_PROFILE, ORG_GCUBE_PORTLETS_USER_DATAMINERMANAGER_SERVER_DATA_MINER_MANAGER_SERVICE_IMPL, false);
|
|
|
|
System.out.println(reader);
|
|
|
|
}
|
2018-10-31 11:52:04 +01:00
|
|
|
}
|