diff --git a/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java b/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java index 84cb81b..bead527 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java @@ -24,13 +24,16 @@ import org.slf4j.LoggerFactory; /** * * @author Andrea Manzi(CERN) + * updated by Francesco Mangiacrapa + * * */ public class HttpResolver extends HttpServlet { - String uri =null; - String fileName =null; - String contentType =null; + protected static final String SMP_URI = "smp-uri"; + 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; @@ -44,14 +47,15 @@ public class HttpResolver extends HttpServlet { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { - - - logger.info("The http session id is: " + request.getSession().getId()); - logger.debug("Original URI = "+ uri); + String uri =null; + String fileName =null; + String contentType =null; + boolean validatingURI = false; - - uri = request.getParameter("smp-uri"); +// logger.info("The http session id is: " + request.getSession().getId()); + + uri = request.getParameter(SMP_URI); if (uri == null || uri.equals("")) { logger.debug("URI not found"); @@ -59,7 +63,7 @@ public class HttpResolver extends HttpServlet { return; } - fileName = request.getParameter("fileName"); + fileName = request.getParameter(FILE_NAME); if (fileName == null || fileName.equals("")) { @@ -67,13 +71,17 @@ public class HttpResolver extends HttpServlet { fileName = null; } - contentType = request.getParameter("contentType"); + contentType = request.getParameter(CONTENT_TYPE); if (contentType == null || contentType.equals("")) { logger.debug("contentType not found"); contentType = null; } + String validation = request.getParameter(VALIDATION); + validatingURI = Boolean.parseBoolean(validation); + logger.debug("validation? "+validatingURI); + //we should not unescape the filename with spaces int index= uri.indexOf("?"); @@ -123,8 +131,27 @@ public class HttpResolver extends HttpServlet { logger.error("URLConnection 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; + } + } + /* IOUtils.copy(in, out); @@ -133,8 +160,6 @@ public class HttpResolver extends HttpServlet { in.close(); */ - if(in!=null) - //CHANGED BY FRANCESCO M. try { @@ -148,7 +173,7 @@ public class HttpResolver extends HttpServlet { throw e; //Sending Exceptions } catch (NullPointerException e) { - logger.warn("NullPointerException: input stream is null, skipping printStrackTrace"); + logger.warn("NullPointerException during copy, skipping printStrackTrace"); sendErrorQuietly(response, 404); } finally { @@ -171,6 +196,7 @@ public class HttpResolver extends HttpServlet { if(response!=null){ try { response.sendError(code); + logger.info("Response sent error: "+code); } catch (IOException ioe) { // ignore }