package com.ngMAT.appl.ezfile.servlets; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import com.ngMAT.Common.Log; import com.ngMAT.HTML.HTMLComponent; import com.ngMAT.HTML.HTMLTemplate; import com.ngMAT.HTML.ByteArrayComponent; import com.ngMAT.appl.genericservlet.GenericServlet; import com.ngMAT.appl.genericservlet.GenericServletConfig; import com.ngMAT.appl.genericservlet.GenericServletContext; public class EZFileServlet extends GenericServlet { public boolean isReadyToGo(GenericServletContext context) { // This method is derived from class com.ngMAT.appl.genericservlet.GenericServlet // to do: code goes here return true; } public HTMLComponent process(GenericServletContext context) { // This method is derived from class com.ngMAT.appl.genericservlet.GenericServlet // to do: code goes here EZFileServletConfig config = (EZFileServletConfig)context.getConfig(); // String uri = context.getRequestURI(); String uri = config.path_handler.getPath (context); if (isDir (config, context, uri)) { if (! uri.endsWith (config.path_separator)) uri = uri + config.path_separator; uri = uri + config.default_filename; context.sendRedirect (uri); return null; } if (isHTML (config, context, uri)) { try { HTMLTemplate output = context.getTemplate (config.document_root + uri); return output; } catch (Throwable t) { getLog().println (t.getMessage()); try { context.sendError (HttpServletResponse.SC_NOT_FOUND); } catch (IOException e2) { // エラーを送れなかったらあきらめる(^^ゞ } return config.not_available_template; } } else { try { ByteArrayComponent bac = new ByteArrayComponent(); FileInputStream fis = new FileInputStream ( config.TemplateFolder + config.path_separator + config.document_root + uri); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 64]; int len; while ((len = fis.read (buffer)) >= 0) { baos.write (buffer, 0, len); } fis.close(); bac.setMimeType (findMimeType (config, context, uri)); bac.setBuffer (baos.toByteArray()); baos.close(); return bac; } catch (java.io.FileNotFoundException e) { try { context.sendError (HttpServletResponse.SC_NOT_FOUND); } catch (IOException e2) { // エラーを送れなかったらあきらめる(^^ゞ } return null; } catch (java.io.IOException e) { throw new com.ngMAT.Common.LowLevelException (e); } } } public boolean isHTML (EZFileServletConfig config, GenericServletContext context, String path) { boolean res = false; path = path.toLowerCase(); for (int i = 0; i < config.html_extentions.size(); i++) { if (path.endsWith ((String)config.html_extentions.elementAt (i))) { res = true; break; } } return res; } public boolean isTextfile (EZFileServletConfig config, GenericServletContext context, String path) { int x = path.lastIndexOf ("."); if (x < 0) return false; return config.textfile_extentions.indexOf ("," + path.substring (x) + ",") >= 0; } public boolean isDir (EZFileServletConfig config, GenericServletContext context, String path) { if (path.endsWith (config.path_separator)) return true; File f = new File (config.document_root + path); return f.isDirectory(); } private String findMimeType (EZFileServletConfig config, GenericServletContext context, String path) { int x = path.lastIndexOf ("."); if (x >= 0) { String res = config.mime_types.getProperty (path.substring (x + 1)); if (res != null) { if (isTextfile (config, context, path)) { res += "; charset=" + config.getServletCharSet(); } return res; } } return config.mime_types.getProperty ("*"); } }