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) * @author Andrea Manzi(CERN)
* updated by Francesco Mangiacrapa * updated by Francesco Mangiacrapa
* *
* *
*/ */
public class HttpResolver extends HttpServlet { public class HttpResolver extends HttpServlet {
protected static final String SMP_URI = "smp-uri"; protected static final String SMP_URI = "smp-uri";
protected static final String VALIDATION = "validation"; protected static final String VALIDATION = "validation";
protected static final String CONTENT_TYPE = "contentType"; 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 { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String uri =null; String uri =null;
String fileName =null; String fileName =null;
String contentType =null; String contentType =null;
@ -62,30 +62,30 @@ public class HttpResolver extends HttpServlet {
response.sendError(404); response.sendError(404);
return; return;
} }
fileName = request.getParameter(FILE_NAME); fileName = request.getParameter(FILE_NAME);
if (fileName == null || fileName.equals("")) { if (fileName == null || fileName.equals("")) {
logger.warn("fileName not found"); logger.warn("fileName not found");
fileName = null; fileName = null;
} }
contentType = request.getParameter(CONTENT_TYPE); contentType = request.getParameter(CONTENT_TYPE);
if (contentType == null || contentType.equals("")) { if (contentType == null || contentType.equals("")) {
logger.warn("contentType not found"); logger.warn("contentType not found");
contentType = null; contentType = null;
} }
String validation = request.getParameter(VALIDATION); String validation = request.getParameter(VALIDATION);
validatingURI = Boolean.parseBoolean(validation); validatingURI = Boolean.parseBoolean(validation);
logger.info("validation? "+validatingURI); logger.info("validation? "+validatingURI);
//we should not unescape the filename with spaces //we should not unescape the filename with spaces
logger.debug("uri = "+uri); logger.debug("uri = "+uri);
int index= uri.indexOf("?"); int index= uri.indexOf("?");
if ( index!= -1) if ( index!= -1)
{ {
@ -103,36 +103,39 @@ public class HttpResolver extends HttpServlet {
logger.debug("Not found char ?"); 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 paramenters, we just put it back
} }
logger.info("URI = "+ uri); logger.info("URI = "+ uri);
InputStream in = null; InputStream in = null;
try { try {
OutputStream out = response.getOutputStream(); OutputStream out = response.getOutputStream();
if (fileName != null) if (fileName != null){
response.addHeader("content-disposition", "attachment; filename=" +fileName); //The filename should be a quoted string. (According to Section 19.5.1 of RFC 2616)
else //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); response.addHeader("content-disposition", "attachment; filename="+ConstantsHttpResolver.DEFAULT_FILENAME_FROM_STORAGE_MANAGER);
if (contentType!= null) if (contentType!= null)
response.setContentType(contentType); response.setContentType(contentType);
else else
response.setContentType(ConstantsHttpResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN); response.setContentType(ConstantsHttpResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN);
URL url = new URL(null, uri, new URLStreamHandler() { URL url = new URL(null, uri, new URLStreamHandler() {
@Override @Override
protected URLConnection openConnection(URL u) throws IOException { protected URLConnection openConnection(URL u) throws IOException {
return new SMPURLConnection(u); return new SMPURLConnection(u);
} }
}); });
URLConnection uc = null; URLConnection uc = null;
try { try {
uc = ( URLConnection ) url.openConnection(); uc = url.openConnection();
in = uc.getInputStream(); in = uc.getInputStream();
} }
catch(Exception e){ catch(Exception e){
@ -140,7 +143,7 @@ public class HttpResolver extends HttpServlet {
logger.error("URLConnection Exception:", e); logger.error("URLConnection Exception:", e);
return; return;
} }
//CASE InputStream NULL //CASE InputStream NULL
if(in==null){ if(in==null){
logger.warn("Input stream is null, sending status error 404"); logger.warn("Input stream is null, sending status error 404");
@ -158,38 +161,38 @@ public class HttpResolver extends HttpServlet {
IOUtils.closeQuietly(in); IOUtils.closeQuietly(in);
response.setStatus(200); response.setStatus(200);
return; return;
} }
} }
/* /*
IOUtils.copy(in, out); IOUtils.copy(in, out);
out.flush(); out.flush();
out.close(); out.close();
in.close(); in.close();
*/ */
//CHANGED BY FRANCESCO M. //CHANGED BY FRANCESCO M.
try { try {
IOUtils.copy(in, out); IOUtils.copy(in, out);
} catch (IOException e){ } catch (IOException e){
logger.warn("IOException class name: "+e.getClass().getSimpleName()); logger.warn("IOException class name: "+e.getClass().getSimpleName());
if (e.getClass().getSimpleName().equals("ClientAbortException")) if (e.getClass().getSimpleName().equals("ClientAbortException"))
logger.warn("Skipping ClientAbortException: "+e.getMessage()); logger.warn("Skipping ClientAbortException: "+e.getMessage());
else else
throw e; //Sending Exceptions throw e; //Sending Exceptions
} catch (NullPointerException e) { } catch (NullPointerException e) {
logger.warn("NullPointerException during copy, skipping printStrackTrace"); logger.warn("NullPointerException during copy, skipping printStrackTrace");
sendErrorQuietly(response, 404); sendErrorQuietly(response, 404);
} finally { } finally {
IOUtils.closeQuietly(in); IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out); IOUtils.closeQuietly(out);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Exception:", e); logger.error("Exception:", e);
// response.sendError(404); // response.sendError(404);
@ -199,9 +202,9 @@ public class HttpResolver extends HttpServlet {
} }
} }
protected void sendErrorQuietly(HttpServletResponse response, int code){ protected void sendErrorQuietly(HttpServletResponse response, int code){
if(response!=null){ if(response!=null){
try { try {
response.sendError(code); response.sendError(code);
@ -211,11 +214,11 @@ public class HttpResolver extends HttpServlet {
} }
} }
} }
protected void doPost(HttpServletRequest request, protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
doGet(request,response); doGet(request,response);
} }
} }

View File

@ -3,7 +3,6 @@ package org.gcube.datatransfer.resolver.http;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -148,7 +147,9 @@ public class StorageIDResolver extends HttpServlet {
if(contentType==null || contentType.isEmpty()) if(contentType==null || contentType.isEmpty())
contentType = ConstantsHttpResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN; 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); response.setContentType(contentType);
//CASE VALIDATION //CASE VALIDATION