diff --git a/src/main/java/org/gcube/datatransfer/resolver/http/StorageIDResolver.java b/src/main/java/org/gcube/datatransfer/resolver/http/StorageIDResolver.java
new file mode 100644
index 0000000..44bd091
--- /dev/null
+++ b/src/main/java/org/gcube/datatransfer/resolver/http/StorageIDResolver.java
@@ -0,0 +1,195 @@
+package org.gcube.datatransfer.resolver.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.io.IOUtils;
+import org.gcube.contentmanagement.blobstorage.service.IClient;
+import org.gcube.contentmanager.storageclient.model.protocol.smp.Handler;
+import org.gcube.contentmanager.storageclient.wrapper.AccessType;
+import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+
+
+/**
+ * The Class StorageIDResolver.
+ *
+ * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
+ * Apr 28, 2015
+ */
+public class StorageIDResolver extends HttpServlet {
+
+ protected static final String SMP_ID = "smp-id";
+ protected static final String VALIDATION = "validation";
+ protected static final String CONTENT_TYPE = "contentType";
+ protected static final String FILE_NAME = "fileName";
+
+ private static final long serialVersionUID = 1L;
+
+ /** The logger. */
+ private static final Logger logger = LoggerFactory.getLogger(StorageIDResolver.class);
+
+ /* (non-Javadoc)
+ * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
+ */
+ public void init(ServletConfig conf) throws ServletException {
+ Handler.activateProtocol();
+ super.init(conf);
+
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+ */
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+
+ String smpID =null;
+ String fileName =null;
+ String contentType =null;
+ boolean validatingURI = false;
+
+// logger.info("The http session id is: " + request.getSession().getId());
+
+ smpID = request.getParameter(SMP_ID);
+
+ if (smpID == null || smpID.equals("")) {
+ logger.warn(SMP_ID+" not found");
+ response.sendError(404);
+ return;
+ }
+
+ fileName = request.getParameter(FILE_NAME);
+
+
+ if (fileName == null || fileName.equals("")) {
+ logger.warn(FILE_NAME+" not found");
+ fileName = null;
+ }
+
+ contentType = request.getParameter(CONTENT_TYPE);
+
+ if (contentType == null || contentType.equals("")) {
+ logger.warn(CONTENT_TYPE+" not found");
+ contentType = null;
+ }
+
+ String validation = request.getParameter(VALIDATION);
+ validatingURI = Boolean.parseBoolean(validation);
+ logger.info("validation? "+validatingURI);
+
+ //we should not unescape the filename with spaces
+
+ logger.debug(SMP_ID+" = "+ smpID);
+ InputStream in = null;
+ try {
+
+ OutputStream out = response.getOutputStream();
+
+ if (fileName != null)
+ response.addHeader("content-disposition", "attachment; filename=" +fileName);
+ else
+ response.addHeader("content-disposition", "attachment; filename=fromStorageManager");
+
+ if (contentType!= null)
+ response.setContentType(contentType);
+ else
+ response.setContentType("unknown/unknown");
+
+ try{
+ StorageClient client = new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), StorageIDResolver.class.getName(), AccessType.PUBLIC);
+ IClient icClient = client.getClient();
+ in=icClient.get().RFileAsInputStream(smpID);
+ }
+ catch (Exception e) {
+ response.sendError(404);
+ logger.error("Storage Client Exception:", e);
+ return;
+ }
+
+ //CASE InputStream NULL
+ if(in==null){
+ logger.warn("Input stream is null, sending status error 404");
+ sendErrorQuietly(response, 404);
+ return;
+ }
+
+ //CASE VALIDATION
+ if(validatingURI){
+ byte[] bytes = new byte[1]; //1B
+ int c = in.read(bytes);
+ logger.info(c+" byte read from InputStream");
+ if(c>0){
+ logger.info("at least 1 byte read, returning status 200");
+ IOUtils.closeQuietly(in);
+ response.setStatus(200);
+ return;
+ }
+ }
+
+ try {
+
+ IOUtils.copy(in, out);
+
+ } catch (IOException e){
+ logger.warn("IOException class name: "+e.getClass().getSimpleName());
+ if (e.getClass().getSimpleName().equals("ClientAbortException"))
+ logger.warn("Skipping ClientAbortException: "+e.getMessage());
+ else
+ throw e; //Sending Exceptions
+
+ } catch (NullPointerException e) {
+ logger.warn("NullPointerException during copy, skipping printStrackTrace");
+ sendErrorQuietly(response, 404);
+
+ } finally {
+ IOUtils.closeQuietly(in);
+ IOUtils.closeQuietly(out);
+ }
+
+ } catch (Exception e) {
+ logger.error("Exception:", e);
+ IOUtils.closeQuietly(in);
+ sendErrorQuietly(response, 404);
+ return;
+ }
+
+ }
+
+ /**
+ * Send error quietly.
+ *
+ * @param response the response
+ * @param code the code
+ */
+ protected void sendErrorQuietly(HttpServletResponse response, int code){
+
+ if(response!=null){
+ try {
+ response.sendError(code);
+ logger.info("Response sent error: "+code);
+ } catch (IOException ioe) {
+ // ignore
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+ */
+ protected void doPost(HttpServletRequest request,
+ HttpServletResponse response) throws IOException {
+ doGet(request,response);
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml
index 15679f5..bc6bfe6 100644
--- a/src/main/webapp/WEB-INF/web.xml
+++ b/src/main/webapp/WEB-INF/web.xml
@@ -10,7 +10,14 @@
org.gcube.datatransfer.resolver.http.HttpResolver
true
-
+
+
+ id
+ id
+ org.gcube.datatransfer.resolver.http.StorageIDResolver
+ true
+
+
gisResolver
gisResolver
@@ -27,6 +34,11 @@
gisResolver
/gis
+
+
+ id
+ /id
+
diff --git a/src/test/java/HttpRequestUtil.java b/src/test/java/HttpRequestUtil.java
new file mode 100644
index 0000000..29559b7
--- /dev/null
+++ b/src/test/java/HttpRequestUtil.java
@@ -0,0 +1,84 @@
+
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.SocketTimeoutException;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.log4j.Logger;
+
+
+/**
+ * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
+ * @Apr 26, 2013
+ *
+ */
+public class HttpRequestUtil {
+
+ private static final int CONNECTION_TIMEOUT = 1000;
+ public static Logger logger = Logger.getLogger(HttpRequestUtil.class);
+
+ public static boolean urlExists(String urlConn) throws Exception {
+
+
+ if(urlConn==null || urlConn.isEmpty())
+ return false;
+
+ URL url;
+
+ try {
+ url = new URL(urlConn);
+
+ URLConnection connection = url.openConnection();
+ connection.setConnectTimeout(CONNECTION_TIMEOUT);
+ connection.setReadTimeout(CONNECTION_TIMEOUT+CONNECTION_TIMEOUT);
+
+ logger.trace("open connection on: " + url);
+
+ // Cast to a HttpURLConnection
+ if (connection instanceof HttpURLConnection) {
+ HttpURLConnection httpConnection = (HttpURLConnection) connection;
+
+ httpConnection.setRequestMethod("GET");
+
+ int code = httpConnection.getResponseCode();
+
+ httpConnection.disconnect();
+
+ if (code == 200) {
+ logger.trace("status code is "+code+" - on url connection: "+urlConn);
+ return true;
+ }else
+ logger.warn("status code is "+code+" - on url connection: "+urlConn);
+
+// logger.trace("result: "+result);
+
+ } else {
+ logger.error("error - not a http request!");
+ }
+
+ return false;
+
+ } catch (SocketTimeoutException e) {
+ logger.error("Error SocketTimeoutException with url " +urlConn, e);
+ return true;
+ } catch (MalformedURLException e) {
+ logger.error("Error MalformedURLException with url " +urlConn, e);
+ throw new Exception("Error MalformedURLException");
+ } catch (IOException e) {
+ logger.error("Error IOException with url " +urlConn, e);
+ throw new Exception("Error IOException");
+ }catch (Exception e) {
+ logger.error("Error Exception with url " +urlConn, e);
+ throw new Exception("Error Exception");
+ }
+ }
+
+
+
+ public static void main(String[] args) throws Exception {
+ System.out.println(HttpRequestUtil.urlExists("http://geoserver2.d4science.research-infrastructures.eu/geoserver/wms"));
+ }
+}
diff --git a/src/test/java/StorageIDResolverTest.java b/src/test/java/StorageIDResolverTest.java
new file mode 100644
index 0000000..b38f4df
--- /dev/null
+++ b/src/test/java/StorageIDResolverTest.java
@@ -0,0 +1,125 @@
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ *
+ */
+
+/**
+ * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
+ * @Nov 12, 2014
+ *
+ */
+public class StorageIDResolverTest {
+
+ public static String hostname = "http://dev.d4science.org/uri-resolver/smp?";
+
+ // CASE IS NULL
+// public static final String uri = hostname+"fileName=HCAF_2050&contentType=text%2Fcsv&smp-uri=smp%3A%2F%2FShare%2Fcd8cb73f-feb6-4072-864c-3bb57f81ad56%2FHCAF+2050%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D";
+// public static String filename = "HCAF_2050";
+// public static String ext = "csv";
+
+ // CASE OK
+// public static final String uri = hostname+"fileName=gattino02.jpg&contentType=audio%2Fmpeg&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2Fgattino02.jpg%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D";
+// public static String filename ="gattino02";
+// public static String ext = "jpg";
+
+
+// ANOTHER CASE OK
+// public static final String uri = hostname+"fileName=org.gcube.portlets-user.messages-0.4.0-0.notifica&contentType=application%2Fx-gtar&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2FSharedFolder1%2Forg.gcube.portlets-user.messages-0.4.0-0.notifica%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D";
+// public static String filename ="org.gcube.portlets-user.messages-0.4.0-0.notifica";
+// public static String ext = "war";
+
+
+ //SIZE 0
+ public static final String uri = hostname+"fileName=Untitled_Document&contentType=application%2Foctet-stream&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2FUntitled+Document%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D";
+ public static String filename ="Untitled_Document";
+ public static String ext = "txt";
+
+ public static void main(String[] args) {
+
+ startTest();
+ }
+
+ public static void startTest() {
+
+ InputStream inputStream = null;
+ FileOutputStream outputStream = null;
+
+ String url = uri;
+
+ System.out.println(url);
+
+ try {
+
+ try {
+ System.out.println("Validating..");
+ boolean isValid = HttpRequestUtil.urlExists(url+"&validation=true");
+
+ if(!isValid){
+ System.out.println("url not valid, return!");
+ return;
+ }
+
+ System.out.println("URL is valid, continue..");
+
+
+ // DOWNLOAD URL
+ URLConnection connection = new URL(url).openConnection();
+ // InputStream response = connection.getInputStream();
+ // read this file into InputStream
+ inputStream = connection.getInputStream();
+
+ System.out.println(" Total file size to read (in bytes) : "
+ + inputStream.available());
+
+ // write the inputStream to a FileOutputStream
+ outputStream = new FileOutputStream(new File(
+ "/home/francesco-mangiacrapa/Desktop/UriResolverDownloads/"
+ + filename + "." + ext));
+
+ int read = 0;
+ byte[] bytes = new byte[1024];
+
+ while ((read = inputStream.read(bytes)) != -1) {
+ outputStream.write(bytes, 0, read);
+ }
+
+ outputStream.flush();
+
+ System.out.println("DOWNLOAD COMPLETED!");
+
+ } catch (IOException e) {
+ e.printStackTrace();
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ } finally {
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (outputStream != null) {
+ try {
+ // outputStream.flush();
+ outputStream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+ }
+
+ }
+
+}
diff --git a/src/test/java/UriResolverTest.java b/src/test/java/UriResolverTest.java
new file mode 100644
index 0000000..760664d
--- /dev/null
+++ b/src/test/java/UriResolverTest.java
@@ -0,0 +1,125 @@
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ *
+ */
+
+/**
+ * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
+ * @Nov 12, 2014
+ *
+ */
+public class UriResolverTest {
+
+ public static String hostname = "http://dev.d4science.org/uri-resolver/smp?";
+
+ // CASE IS NULL
+// public static final String uri = hostname+"fileName=HCAF_2050&contentType=text%2Fcsv&smp-uri=smp%3A%2F%2FShare%2Fcd8cb73f-feb6-4072-864c-3bb57f81ad56%2FHCAF+2050%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D";
+// public static String filename = "HCAF_2050";
+// public static String ext = "csv";
+
+ // CASE OK
+// public static final String uri = hostname+"fileName=gattino02.jpg&contentType=audio%2Fmpeg&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2Fgattino02.jpg%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D";
+// public static String filename ="gattino02";
+// public static String ext = "jpg";
+
+
+// ANOTHER CASE OK
+// public static final String uri = hostname+"fileName=org.gcube.portlets-user.messages-0.4.0-0.notifica&contentType=application%2Fx-gtar&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2FSharedFolder1%2Forg.gcube.portlets-user.messages-0.4.0-0.notifica%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D";
+// public static String filename ="org.gcube.portlets-user.messages-0.4.0-0.notifica";
+// public static String ext = "war";
+
+
+ //SIZE 0
+ public static final String uri = hostname+"fileName=Untitled_Document&contentType=application%2Foctet-stream&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2FUntitled+Document%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D";
+ public static String filename ="Untitled_Document";
+ public static String ext = "txt";
+
+ public static void main(String[] args) {
+
+ startTest();
+ }
+
+ public static void startTest() {
+
+ InputStream inputStream = null;
+ FileOutputStream outputStream = null;
+
+ String url = uri;
+
+ System.out.println(url);
+
+ try {
+
+ try {
+ System.out.println("Validating..");
+ boolean isValid = HttpRequestUtil.urlExists(url+"&validation=true");
+
+ if(!isValid){
+ System.out.println("url not valid, return!");
+ return;
+ }
+
+ System.out.println("URL is valid, continue..");
+
+
+ // DOWNLOAD URL
+ URLConnection connection = new URL(url).openConnection();
+ // InputStream response = connection.getInputStream();
+ // read this file into InputStream
+ inputStream = connection.getInputStream();
+
+ System.out.println(" Total file size to read (in bytes) : "
+ + inputStream.available());
+
+ // write the inputStream to a FileOutputStream
+ outputStream = new FileOutputStream(new File(
+ "/home/francesco-mangiacrapa/Desktop/UriResolverDownloads/"
+ + filename + "." + ext));
+
+ int read = 0;
+ byte[] bytes = new byte[1024];
+
+ while ((read = inputStream.read(bytes)) != -1) {
+ outputStream.write(bytes, 0, read);
+ }
+
+ outputStream.flush();
+
+ System.out.println("DOWNLOAD COMPLETED!");
+
+ } catch (IOException e) {
+ e.printStackTrace();
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ } finally {
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (outputStream != null) {
+ try {
+ // outputStream.flush();
+ outputStream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+ }
+
+ }
+
+}