Easier convention for dispatch. Return directly file which are not classes. Add methods to return file directly.

This commit is contained in:
Christian P. MOMON 2010-08-07 01:18:43 +02:00
parent 33451be5ac
commit f9a13c2b45

150
src/fr/devinsy/kiss4web/SimpleServletDispatcher.java Executable file → Normal file
View file

@ -7,11 +7,11 @@
package fr.devinsy.kiss4web; package fr.devinsy.kiss4web;
import java.io.*; import java.io.*;
import java.util.Enumeration;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.http.*; import javax.servlet.http.*;
import fr.devinsy.hico.Hico;
import fr.devinsy.util.StringConcatenator; 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); static protected org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (ServletDispatcher.class);
protected String webclassesRootPath; protected String webclassesRootPath;
/** /**
* *
*/ */
@ -192,9 +191,84 @@ public class SimpleServletDispatcher extends HttpServlet
return(result); 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 public void doIt (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ {
logger.info ("=================================================="); logger.info ("==================================================");
@ -206,7 +280,9 @@ public class SimpleServletDispatcher extends HttpServlet
logger.info ("getRequestURL=[" + request.getRequestURL () + "]"); logger.info ("getRequestURL=[" + request.getRequestURL () + "]");
logger.info ("getServletPath=[" + request.getServletPath () + "]"); logger.info ("getServletPath=[" + request.getServletPath () + "]");
// //
/* In past, possibility to use the servlet path was enable. It is too complexe, not kiss mind.
String path; String path;
if (request.getPathInfo() == null) if (request.getPathInfo() == null)
{ {
@ -218,42 +294,54 @@ public class SimpleServletDispatcher extends HttpServlet
// web.xml url-pattern = /* // web.xml url-pattern = /*
path = request.getPathInfo(); path = request.getPathInfo();
} }
*/
String path = request.getRequestURI();
String className = pathInfoToClassName (path, this.webclassesRootPath); if ((!path.endsWith("/")) && (!path.endsWith(".xhtml")))
logger.info ("className=" + className);
HttpServlet servlet = instanciateServlet (className);
//servlet.getServletContext().setAttribute(arg0, arg1);
if (servlet == null)
{ {
response.setContentType ("text/html"); path = Hico.instance().webRootPath() + request.getRequestURI();
PrintWriter out = response.getWriter();
out.println ("<html><head></head><body>"); returnInlineFile(response, new File(path), getServletContext().getMimeType(path));
out.println ("Unknow page."); logger.info("File returned directly [" + path + "] with mimetype [" + getServletContext().getMimeType(path) + "].");
out.println ("</body></html>");
out.close();
}
else if (isAuthorized(request, response))
{
servlet.service(request, response);
} }
else else
{ {
/* String className = pathInfoToClassName (path, this.webclassesRootPath);
* TODO logger.info ("className=[" + className + "]");
//
response.setContentType ("text/html");
PrintWriter out = response.getWriter();
out.println ("<html><head></head><body>"); HttpServlet servlet = instanciateServlet (className);
out.println ("Not authorized page.");
out.println ("</body></html>"); //servlet.getServletContext().setAttribute(arg0, arg1);
*/
if (servlet == null)
{
response.setContentType ("text/html");
PrintWriter out = response.getWriter();
out.println ("<html><head></head><body>");
out.println ("Unknow page.");
out.println ("</body></html>");
out.close();
}
else if (isAuthorized(request, response))
{
servlet.init(this.getServletConfig());
servlet.service(request, response);
}
else
{
/*
* TODO
//
response.setContentType ("text/html");
PrintWriter out = response.getWriter();
out.println ("<html><head></head><body>");
out.println ("Not authorized page.");
out.println ("</body></html>");
*/
}
} }
} }