From f9a13c2b454c25d564a0c87751d10da05108d503 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sat, 7 Aug 2010 01:18:43 +0200 Subject: [PATCH] Easier convention for dispatch. Return directly file which are not classes. Add methods to return file directly. --- .../kiss4web/SimpleServletDispatcher.java | 156 ++++++++++++++---- 1 file changed, 122 insertions(+), 34 deletions(-) mode change 100755 => 100644 src/fr/devinsy/kiss4web/SimpleServletDispatcher.java diff --git a/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java b/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java old mode 100755 new mode 100644 index 5c5288e..5df2161 --- a/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java +++ b/src/fr/devinsy/kiss4web/SimpleServletDispatcher.java @@ -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; - /** * */ @@ -126,7 +125,7 @@ public class SimpleServletDispatcher extends HttpServlet { result = prefix + "." + pathInfoToClassName(pathInfo); } - + // return (result); } @@ -191,8 +190,83 @@ 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 @@ -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,42 +294,54 @@ public class SimpleServletDispatcher extends HttpServlet // web.xml url-pattern = /* path = request.getPathInfo(); } - - String className = pathInfoToClassName (path, this.webclassesRootPath); - logger.info ("className=" + className); + */ + String path = request.getRequestURI(); - HttpServlet servlet = instanciateServlet (className); - - - //servlet.getServletContext().setAttribute(arg0, arg1); - - if (servlet == null) + if ((!path.endsWith("/")) && (!path.endsWith(".xhtml"))) { - response.setContentType ("text/html"); - PrintWriter out = response.getWriter(); - - out.println (""); - out.println ("Unknow page."); - out.println (""); - - out.close(); - } - else if (isAuthorized(request, response)) - { - servlet.service(request, response); + 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 { - /* - * TODO - // - response.setContentType ("text/html"); - PrintWriter out = response.getWriter(); - - out.println (""); - out.println ("Not authorized page."); - out.println (""); - */ + String className = pathInfoToClassName (path, this.webclassesRootPath); + logger.info ("className=[" + className + "]"); + + HttpServlet servlet = instanciateServlet (className); + + //servlet.getServletContext().setAttribute(arg0, arg1); + + if (servlet == null) + { + response.setContentType ("text/html"); + PrintWriter out = response.getWriter(); + + out.println (""); + out.println ("Unknow page."); + out.println (""); + + 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 (""); + out.println ("Not authorized page."); + out.println (""); + */ + } } }