added new resolver (servlet): Storage ID Resolver

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@114484 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2015-04-28 14:43:38 +00:00
parent b306232ea0
commit 24e2e1bc2d
5 changed files with 542 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -10,7 +10,14 @@
<servlet-class>org.gcube.datatransfer.resolver.http.HttpResolver</servlet-class>
<load-on-startup>true</load-on-startup>
</servlet>
<servlet>
<servlet-name>id</servlet-name>
<display-name>id</display-name>
<servlet-class>org.gcube.datatransfer.resolver.http.StorageIDResolver</servlet-class>
<load-on-startup>true</load-on-startup>
</servlet>
<servlet>
<servlet-name>gisResolver</servlet-name>
<display-name>gisResolver</display-name>
@ -27,6 +34,11 @@
<servlet-name>gisResolver</servlet-name>
<url-pattern>/gis</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>id</servlet-name>
<url-pattern>/id</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -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"));
}
}

View File

@ -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();
}
}
}
}
}

View File

@ -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();
}
}
}
}
}