fileName bug fix according to section 19.5.1 of RFC 2616

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@122565 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-01-27 14:13:34 +00:00
parent 98e540a756
commit 49cae260dd
2 changed files with 44 additions and 40 deletions

View File

@ -22,14 +22,14 @@ import org.slf4j.LoggerFactory;
/**
*
*
* @author Andrea Manzi(CERN)
* updated by Francesco Mangiacrapa
*
*
*
*/
public class HttpResolver extends HttpServlet {
protected static final String SMP_URI = "smp-uri";
protected static final String VALIDATION = "validation";
protected static final String CONTENT_TYPE = "contentType";
@ -47,7 +47,7 @@ public class HttpResolver extends HttpServlet {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String uri =null;
String fileName =null;
String contentType =null;
@ -62,30 +62,30 @@ public class HttpResolver extends HttpServlet {
response.sendError(404);
return;
}
fileName = request.getParameter(FILE_NAME);
if (fileName == null || fileName.equals("")) {
logger.warn("fileName not found");
fileName = null;
}
contentType = request.getParameter(CONTENT_TYPE);
if (contentType == null || contentType.equals("")) {
logger.warn("contentType 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("uri = "+uri);
int index= uri.indexOf("?");
if ( index!= -1)
{
@ -103,36 +103,39 @@ public class HttpResolver extends HttpServlet {
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
}
logger.info("URI = "+ uri);
InputStream in = null;
try {
OutputStream out = response.getOutputStream();
if (fileName != null)
response.addHeader("content-disposition", "attachment; filename=" +fileName);
else
if (fileName != null){
//The filename should be a quoted string. (According to Section 19.5.1 of RFC 2616)
//http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1
response.setHeader(ConstantsHttpResolver.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
// response.addHeader("content-disposition", "attachment; filename=" +fileName);
}else
response.addHeader("content-disposition", "attachment; filename="+ConstantsHttpResolver.DEFAULT_FILENAME_FROM_STORAGE_MANAGER);
if (contentType!= null)
response.setContentType(contentType);
else
else
response.setContentType(ConstantsHttpResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN);
URL url = new URL(null, uri, new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
return new SMPURLConnection(u);
}
});
URLConnection uc = null;
try {
uc = ( URLConnection ) url.openConnection();
uc = url.openConnection();
in = uc.getInputStream();
}
catch(Exception e){
@ -140,7 +143,7 @@ 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");
@ -158,38 +161,38 @@ public class HttpResolver extends HttpServlet {
IOUtils.closeQuietly(in);
response.setStatus(200);
return;
}
}
}
/*
IOUtils.copy(in, out);
out.flush();
out.close();
in.close();
*/
//CHANGED BY FRANCESCO M.
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);
// response.sendError(404);
@ -199,9 +202,9 @@ public class HttpResolver extends HttpServlet {
}
}
protected void sendErrorQuietly(HttpServletResponse response, int code){
if(response!=null){
try {
response.sendError(code);
@ -211,11 +214,11 @@ public class HttpResolver extends HttpServlet {
}
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException {
doGet(request,response);
}
}

View File

@ -3,7 +3,6 @@ package org.gcube.datatransfer.resolver.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@ -148,7 +147,9 @@ public class StorageIDResolver extends HttpServlet {
if(contentType==null || contentType.isEmpty())
contentType = ConstantsHttpResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN;
response.addHeader(ConstantsHttpResolver.CONTENT_DISPOSITION, "attachment; filename=" +URLEncoder.encode(fileName, "UTF-8"));
//The filename should be a quoted string. (According to Section 19.5.1 of RFC 2616)
//http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1
response.setHeader(ConstantsHttpResolver.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
response.setContentType(contentType);
//CASE VALIDATION