/** * */ 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; import org.apache.commons.io.IOUtils; import org.apache.http.HttpStatus; import org.gcube.datatransfer.resolver.util.HTTPCallsUtils; import org.junit.Test; 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. * * @throws Exception the exception */ //@Test public void testStorageHub() throws Exception{ String storageHubId = "2019b881-083b-444a-81be-8abee4b91020"; String url = String.format("%s/shub/%s",URI_RESOLVER_SERVICE_ENDPOINT,storageHubId); 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(); 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. * * @throws Exception the exception */ //@Test public void testStorageID() throws Exception{ String storageID = "clZ2YmxTYytETzVLaHkwMjM3TmVETTFMb256YVRMS3lHbWJQNStIS0N6Yz0"; String url = String.format("%s/%s",URI_RESOLVER_SERVICE_ENDPOINT,storageID); 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()); 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. * * @throws Exception the exception */ //@Test public void testStorageIDdoHEAD() throws Exception{ String storageID = "clZ2YmxTYytETzVLaHkwMjM3TmVETTFMb256YVRMS3lHbWJQNStIS0N6Yz0"; String url = String.format("%s/%s",URI_RESOLVER_SERVICE_ENDPOINT,storageID); 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; } } /** * Storage hub test. * * @throws Exception the exception */ //@Test public void testCatalogueResolver() throws Exception{ 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; } } /** * Storage hub test. * * @throws Exception the exception */ //@Test public void testCatalogueResolverCreatePublicItemURL() throws Exception{ String entityName = "sarda-sarda"; //String entityContext = "product"; String entityContext = "dataset"; String scope = "/gcube/devsec/devVRE"; String url = String.format("%s/%s",URI_RESOLVER_SERVICE_ENDPOINT,"catalogue"); 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; } } /** * Storage hub test. * * @throws Exception the exception */ @Test public void testCatalogueResolverCreatePubrivatItemURL() throws Exception{ String entityName = "dynamic_reporting"; //String entityContext = "product"; String entityContext = "dataset"; String scope = "/gcube/devsec/devVRE"; String url = String.format("%s/%s",URI_RESOLVER_SERVICE_ENDPOINT,"catalogue"); 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; } } //@Test /** * Test gis resolver. * * @throws Exception the exception */ //@Test 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; } } /** * 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(); } /** * Prints the headers. * * @param map the map */ public static void printHeaders(Map> map){ System.out.println("Printing Response Header..."); for (Map.Entry> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue()); } } /** * Gets the filename. * * @param map the map * @return the filename */ public static String getFilename(Map> map){ String fileName = "filename"; List contentDispValue = map.get("content-disposition"); if(contentDispValue==null) return fileName; 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; } }