1 52 53 package com.go.teaservlet.util; 54 55 import java.io.IOException ; 56 import java.io.FileNotFoundException ; 57 import java.io.InputStream ; 58 import java.io.File ; 59 import java.io.FilenameFilter ; 60 import java.io.BufferedInputStream ; 61 import java.io.FileInputStream ; 62 import java.io.OutputStream ; 63 import java.util.Iterator ; 64 import java.util.Vector ; 65 import javax.servlet.*; 66 import javax.servlet.http.*; 67 68 78 public class TemplateServerServlet extends HttpServlet { 79 File mTemplateRoot; 80 ServletConfig mConfig; 81 82 public void init(ServletConfig conf) { 83 mConfig = conf; 84 mTemplateRoot = new File (conf.getInitParameter("template.root")); 85 } 86 87 public void doPost(HttpServletRequest req,HttpServletResponse res) { 88 doGet(req,res); 89 } 90 91 96 public void doGet(HttpServletRequest req,HttpServletResponse res) { 97 try { 98 getTemplateData(res.getOutputStream(), 99 req.getPathInfo()); 100 } 101 catch (IOException ioe) { 102 ioe.printStackTrace(); 103 } 104 } 105 106 public void getTemplateData(OutputStream out,String path) { 107 try { 108 File templateFile; 109 if (path != null) { 110 templateFile = new File (mTemplateRoot,path); 111 112 } 113 else { 114 templateFile = mTemplateRoot; 115 } 116 if (templateFile != null) { 117 if (templateFile.isFile()) { 118 InputStream fis = new BufferedInputStream ( 119 new FileInputStream (templateFile)); 120 for(int nextChar = -1;(nextChar = fis.read()) >= 0;) { 121 out.write(nextChar); 122 } 123 fis.close(); 124 } 125 else if (templateFile.isDirectory()) { 126 Vector tempVec = new Vector (); 127 File [] dirlist = templateFile.listFiles(); 128 for (int j=0;j<dirlist.length;j++) { 129 listTemplates(dirlist[j],tempVec,"/"); 130 } 131 Iterator tempIt = tempVec.iterator(); 132 while (tempIt.hasNext()) { 133 out.write((byte[])tempIt.next()); 134 } 135 } 136 else { 137 mConfig.getServletContext().log(path 138 + " doesn't map to an existing template or directory."); 139 } 140 } 141 } 142 catch (Exception e) { 143 e.printStackTrace(); 144 } 145 } 146 147 public void listTemplates(File root,Vector storage,String path) throws IOException { 148 if (root.isDirectory()) { 149 File [] dirlist = root.listFiles(); 150 for (int j=0;j<dirlist.length;j++) { 151 listTemplates(dirlist[j],storage, 152 path + root.getName() + "/"); 153 } 154 } 155 else if (root.isFile()) { 156 String templateName = root.getName(); 157 if (templateName.endsWith(".tea")) { 158 storage.add(("|" + path + templateName.substring(0,templateName.length()-4)+ "|" 159 + Long.toString(root.lastModified())).getBytes()); 160 } 161 } 162 } 163 } 164 | Popular Tags |