2695: Uri Resolver: patch to fix old bug in HL renaming files

Task-Url: https://support.d4science.org/issues/2695

Fixed this issue

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@125572 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-03-17 10:49:07 +00:00
parent d09dce4ac1
commit dc5dc1a7b2
3 changed files with 89 additions and 19 deletions

View File

@ -23,8 +23,9 @@
(mime type) from Storage </Change>
</Changeset>
<Changeset component="org.gcube.data-transfer.uri-resolver.1-5-0"
date="2016-01-19">
date="2016-03-17">
<Change>[Feature #2008] Updated the method to resolve gCube Storage ID</Change>
<Change>[Feature #1925] Added class UriResolverRewriteFilter to filter the different public link types, see: #1959</Change>
<Change>[gCube - Support #2695] Uri Resolver: patch to fix old bug in HL renaming files</Change>
</Changeset>
</ReleaseNotes>

View File

@ -1,3 +1,5 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@ -16,6 +18,7 @@
<include>README</include>
<include>LICENSE</include>
<include>changelog.xml</include>
<include>profile.xml</include>
</includes>
<fileMode>755</fileMode>
<filtered>true</filtered>
@ -26,5 +29,6 @@
<source>target/${build.finalName}.${project.packaging}</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
</files>
</assembly>
</assembly>

View File

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