Fix ContentDisposition management.

This commit is contained in:
Christian P. MOMON 2013-07-01 01:55:10 +02:00
parent 653d2bedca
commit cc7639aeb9

View file

@ -29,7 +29,13 @@ import fr.devinsy.util.StringList;
*/
public class SimpleServletDispatcher extends HttpServlet
{
public enum ContentDispositionType
{
ATTACHMENT, INLINE
}
private static final long serialVersionUID = -3471226305721330069L;
static private Logger logger = LoggerFactory.getLogger(ServletDispatcher.class);
static final private Pattern SHORT_REWRITED_URL_CLASS = Pattern.compile("^([^-]+)-.+\\.xhtml$");
@ -110,13 +116,20 @@ public class SimpleServletDispatcher extends HttpServlet
* path = request.getPathInfo();
* }
*/
String path = request.getRequestURI();
// String path = request.getRequestURI();
String path = request.getPathInfo();
if ((!path.endsWith("/")) && (!path.endsWith(".xhtml")) && (!path.contains("-/")))
{
// path = getServletContext().getRealPath("/") +
// request.getRequestURI();
path = getServletContext().getRealPath("/") + request.getPathInfo();
logger.info("path1=" + getServletContext().getRealPath("/"));
if (!new File(path).exists())
{
path = getServletContext().getRealPath("/") + this.webclassesRootPath + "/" + request.getPathInfo();
}
logger.info("path2=" + getServletContext().getRealPath("/") + this.webclassesRootPath);
returnInlineFile(response, new File(path), getServletContext().getMimeType(path));
logger.info("File returned directly [" + path + "] with mimetype [" + getServletContext().getMimeType(path) + "].");
@ -403,17 +416,18 @@ public class SimpleServletDispatcher extends HttpServlet
*/
static public void returnAttachmentFile(final HttpServletResponse response, final File file, final String mimeType) throws IOException
{
returnFile(response, file, mimeType, true);
returnFile(response, file, mimeType, ContentDispositionType.ATTACHMENT);
}
/**
*
*/
static public void returnFile(final HttpServletResponse response, final File file, final String mimeType, final boolean isAttachment) throws IOException
static public void returnFile(final HttpServletResponse response, final File file, final String mimeType, final ContentDispositionType contentDisposition) throws IOException
{
if ((file == null) || (!file.exists()))
{
logger.warn("File not found [" + file.getAbsolutePath() + "]");
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
else
@ -428,7 +442,16 @@ public class SimpleServletDispatcher extends HttpServlet
response.reset();
response.setContentType(mimeType);
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
String contentDispositionToken;
if (contentDisposition == ContentDispositionType.ATTACHMENT)
{
contentDispositionToken = "attachment";
}
else
{
contentDispositionToken = "inline";
}
response.setHeader("Content-Disposition", contentDispositionToken + "; filename=\"" + file.getName() + "\"");
response.flushBuffer();
ServletOutputStream out = response.getOutputStream();
@ -476,7 +499,7 @@ public class SimpleServletDispatcher extends HttpServlet
*/
static public void returnInlineFile(final HttpServletResponse response, final File file, final String mimeType) throws IOException
{
returnFile(response, file, mimeType, false);
returnFile(response, file, mimeType, ContentDispositionType.INLINE);
}
/**