Easier convention for dispatch. Return directly file which are not classes. Add methods to return file directly.
This commit is contained in:
parent
33451be5ac
commit
f9a13c2b45
1 changed files with 122 additions and 34 deletions
96
src/fr/devinsy/kiss4web/SimpleServletDispatcher.java
Executable file → Normal file
96
src/fr/devinsy/kiss4web/SimpleServletDispatcher.java
Executable file → Normal file
|
@ -7,11 +7,11 @@
|
|||
package fr.devinsy.kiss4web;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
import fr.devinsy.hico.Hico;
|
||||
import fr.devinsy.util.StringConcatenator;
|
||||
|
||||
|
||||
|
@ -24,7 +24,6 @@ public class SimpleServletDispatcher extends HttpServlet
|
|||
static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (ServletDispatcher.class);
|
||||
protected String webclassesRootPath;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
@ -192,9 +191,84 @@ public class SimpleServletDispatcher extends HttpServlet
|
|||
return(result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static public void returnInlineFile(HttpServletResponse response, File file, String mimeType) throws IOException
|
||||
{
|
||||
returnFile(response, file, mimeType, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static public void returnAttachmentFile(HttpServletResponse response, File file, String mimeType) throws IOException
|
||||
{
|
||||
returnFile(response, file, mimeType, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static public void returnFile(HttpServletResponse response, File file, String mimeType, boolean isAttachment) throws IOException
|
||||
{
|
||||
|
||||
if ((file == null) || (!file.exists()))
|
||||
{
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
response.setContentType("application/" + extension);
|
||||
response.setContentLength((int) data.length);
|
||||
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
|
||||
response.flushBuffer();
|
||||
*/
|
||||
response.reset();
|
||||
response.setContentType(mimeType);
|
||||
response.setContentLength((int) file.length());
|
||||
response.setHeader("Content-Disposition","inline; filename=\"" + file.getName()+ "\"");
|
||||
response.flushBuffer();
|
||||
|
||||
ServletOutputStream out = response.getOutputStream();
|
||||
|
||||
FileInputStream in = null;
|
||||
try // Only for the in.
|
||||
{
|
||||
byte[] buffer = new byte[256*1024];
|
||||
|
||||
in = new FileInputStream(file);
|
||||
|
||||
while (in.read(buffer) >= 0)
|
||||
{
|
||||
out.write(buffer);
|
||||
}
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
response.sendError(HttpServletResponse.SC_PARTIAL_CONTENT);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public void doIt (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
logger.info ("==================================================");
|
||||
|
@ -206,7 +280,9 @@ public class SimpleServletDispatcher extends HttpServlet
|
|||
logger.info ("getRequestURL=[" + request.getRequestURL () + "]");
|
||||
logger.info ("getServletPath=[" + request.getServletPath () + "]");
|
||||
|
||||
|
||||
//
|
||||
/* In past, possibility to use the servlet path was enable. It is too complexe, not kiss mind.
|
||||
String path;
|
||||
if (request.getPathInfo() == null)
|
||||
{
|
||||
|
@ -218,13 +294,23 @@ public class SimpleServletDispatcher extends HttpServlet
|
|||
// web.xml url-pattern = /*
|
||||
path = request.getPathInfo();
|
||||
}
|
||||
*/
|
||||
String path = request.getRequestURI();
|
||||
|
||||
if ((!path.endsWith("/")) && (!path.endsWith(".xhtml")))
|
||||
{
|
||||
path = Hico.instance().webRootPath() + request.getRequestURI();
|
||||
|
||||
returnInlineFile(response, new File(path), getServletContext().getMimeType(path));
|
||||
logger.info("File returned directly [" + path + "] with mimetype [" + getServletContext().getMimeType(path) + "].");
|
||||
}
|
||||
else
|
||||
{
|
||||
String className = pathInfoToClassName (path, this.webclassesRootPath);
|
||||
logger.info ("className=" + className);
|
||||
logger.info ("className=[" + className + "]");
|
||||
|
||||
HttpServlet servlet = instanciateServlet (className);
|
||||
|
||||
|
||||
//servlet.getServletContext().setAttribute(arg0, arg1);
|
||||
|
||||
if (servlet == null)
|
||||
|
@ -240,6 +326,7 @@ public class SimpleServletDispatcher extends HttpServlet
|
|||
}
|
||||
else if (isAuthorized(request, response))
|
||||
{
|
||||
servlet.init(this.getServletConfig());
|
||||
servlet.service(request, response);
|
||||
}
|
||||
else
|
||||
|
@ -256,6 +343,7 @@ public class SimpleServletDispatcher extends HttpServlet
|
|||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue