259 lines
7.2 KiB
Java
259 lines
7.2 KiB
Java
/**
|
|
*
|
|
*/
|
|
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.http.HttpStatus;
|
|
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
|
|
*/
|
|
//@Test
|
|
public void storageHubTest() 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
|
|
*/
|
|
//@Test
|
|
public void storageIDTest() 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
|
|
*/
|
|
//@Test
|
|
public void catalogueResolverTest() 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
|
|
// */
|
|
// @Test
|
|
// public void createCatalogueURLTest() 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;
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
|
|
public static String getFilename(Map<String, List<String>> map){
|
|
|
|
List<String> contentDispValue = map.get("content-disposition");
|
|
String fileName = "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;
|
|
}
|
|
}
|