diff --git a/distro/changelog.xml b/distro/changelog.xml index eac5dd5..e9f68ea 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -23,8 +23,9 @@ (mime type) from Storage + date="2016-03-17"> [Feature #2008] Updated the method to resolve gCube Storage ID [Feature #1925] Added class UriResolverRewriteFilter to filter the different public link types, see: #1959 + [gCube - Support #2695] Uri Resolver: patch to fix old bug in HL renaming files \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml index dc46ade..12bfdfb 100644 --- a/distro/descriptor.xml +++ b/distro/descriptor.xml @@ -1,3 +1,5 @@ + + README LICENSE changelog.xml + profile.xml 755 true @@ -26,5 +29,6 @@ target/${build.finalName}.${project.packaging} /${artifactId} + - \ No newline at end of file + 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 a06568e..6899425 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/http/HttpResolver.java @@ -22,11 +22,10 @@ import org.slf4j.LoggerFactory; /** + * The Class HttpResolver. * * @author Andrea Manzi(CERN) * updated by Francesco Mangiacrapa - * - * */ public class HttpResolver extends HttpServlet { @@ -34,18 +33,25 @@ public class HttpResolver extends HttpServlet { protected static final String VALIDATION = "validation"; protected static final String CONTENT_TYPE = "contentType"; protected static final String FILE_NAME = "fileName"; + protected static final String SMP_PATH_SEPARATOR = "/"; private static final long serialVersionUID = 1L; /** The logger. */ private static final Logger logger = LoggerFactory.getLogger(HttpResolver.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 uri =null; @@ -87,21 +93,31 @@ public class HttpResolver extends HttpServlet { logger.debug("uri = "+uri); int index= uri.indexOf("?"); - if ( index!= -1) - { + + if (index!= -1){ logger.debug("Found char ?"); String firsPart = uri.substring(0, index); + + //PATCH TO FIX #2695 + try { + firsPart = validateItemName(firsPart, fileName); + logger.trace("valid smp path is: "+firsPart); + } + catch (Exception e) { + logger.warn("An error occurred during check right filename into SMP PATH"); + } + String secondPart= uri.substring( index+1); logger.debug("firsPart not chagend: "+firsPart); logger.debug("replacing in smp-uri char space with char + ..."); //FIXED BY FRANCESCO M. - secondPart = secondPart.replace(" ","+");//the char + is removed when the servlet is doing unescaping of the query paramenters, we just put it back + secondPart = secondPart.replace(" ","+");//the char + is removed when the servlet is doing unescaping of the query parameters, we just put it back logger.debug("new secondPart: "+secondPart); uri= firsPart+"?"+secondPart; - } - else { + + }else { logger.debug("Not found char ?"); - uri = uri.replace(" ","+");//the char + is removed when the servlet is doing unescaping of the query paramenters, we just put it back + uri = uri.replace(" ","+");//the char + is removed when the servlet is doing unescaping of the query parameters, we just put it back } logger.info("URI = "+ uri); @@ -164,14 +180,6 @@ public class HttpResolver extends HttpServlet { } } - /* - IOUtils.copy(in, out); - - out.flush(); - out.close(); - in.close(); - */ - //CHANGED BY FRANCESCO M. try { @@ -195,7 +203,6 @@ public class HttpResolver extends HttpServlet { } catch (Exception e) { logger.error("Exception:", e); -// response.sendError(404); IOUtils.closeQuietly(in); sendErrorQuietly(response, 404); return; @@ -203,6 +210,43 @@ public class HttpResolver extends HttpServlet { } + /** + * Validate item name. + * Since the right filename is in the URI (fileName=COL_Mammalia_taxa.taf.gz), if SMP path does't contain it the right file is overwritten into SMP PATH + * + * @param smpPath the smp path + * @param fileName the file name + * @return the string + * @throws Exception the exception + */ + protected static String validateItemName(String smpPath, String fileName) throws Exception{ + logger.debug("Checking right filename into SMP path.."); + + if(smpPath==null) + throw new Exception("Invalid smp path: "+smpPath); + + int lastSp = smpPath.lastIndexOf(SMP_PATH_SEPARATOR); + if(lastSp<0) + throw new Exception(SMP_PATH_SEPARATOR + " not found in "+smpPath); + + String smpItemName = smpPath.substring(lastSp+1, smpPath.length()); + + if(smpItemName.compareTo(fileName)!=0){ + logger.info("SMP PATH contains a different filename, overrinding with "+fileName); + return smpPath.substring(0, lastSp+1)+fileName; + } + + logger.info("SMP PATH contains same filename, returning"); + return smpPath; + + } + + /** + * Send error quietly. + * + * @param response the response + * @param code the code + */ protected void sendErrorQuietly(HttpServletResponse response, int code){ if(response!=null){ @@ -215,10 +259,31 @@ public class HttpResolver extends HttpServlet { } } + /* (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); } + /** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + + String fileName = "COL_taxa.taf.gz"; + String smpPath = "smp://Share/89971b8f-a993-4e7b-9a95-8d774cb68a99/Work+Packages/WP+6+-+Virtual+Research+Environments+Deployment+and+Operation/T6.2+Resources+and+Tools/COMET-Species-Matching-Engine/YASMEEN/1.2.0/Data/BiOnymTAF/COL_taxa.taf.gz"; + try { + System.out.println(validateItemName(smpPath, fileName)); + } + catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } \ No newline at end of file