1 37 package net.sourceforge.cruisecontrol.servlet; 38 39 import javax.servlet.ServletConfig ; 40 import javax.servlet.ServletContext ; 41 import javax.servlet.ServletException ; 42 import javax.servlet.ServletOutputStream ; 43 import javax.servlet.http.HttpServlet ; 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.http.HttpServletResponse ; 46 import java.io.BufferedInputStream ; 47 import java.io.BufferedOutputStream ; 48 import java.io.File ; 49 import java.io.FileInputStream ; 50 import java.io.InputStream ; 51 import java.io.IOException ; 52 import java.io.OutputStream ; 53 import java.io.Writer ; 54 import java.util.StringTokenizer ; 55 import java.util.List ; 56 import java.util.Collections ; 57 import java.util.ArrayList ; 58 import java.util.Arrays ; 59 import java.util.Date ; 60 61 public class FileServlet extends HttpServlet { 62 63 private File rootDir; 64 private List indexFiles; 65 66 public File getRootDir() { 67 return rootDir; 68 } 69 70 public void init(ServletConfig servletconfig) throws ServletException { 71 super.init(servletconfig); 72 rootDir = getRootDir(servletconfig); 73 indexFiles = getIndexFiles(servletconfig); 74 } 75 76 protected File getRootDir(ServletConfig servletconfig) throws ServletException { 77 String root = servletconfig.getInitParameter("rootDir"); 78 File rootDirectory = getDirectoryFromName(root); 79 if (rootDirectory == null) { 80 rootDirectory = getLogDir(servletconfig); 81 if (rootDirectory == null) { 82 String message = "ArtifactServlet not configured correctly in web.xml.\n" 83 + "Either rootDir or logDir must point to existing directory.\n" 84 + "rootDir is currently set to <" + root + "> " 85 + "while logDir is <" + getLogDirParameter(servletconfig) + ">"; 86 throw new ServletException (message); 87 } 88 } 89 90 return rootDirectory; 91 } 92 93 protected String getLogDirParameter(ServletConfig servletconfig) throws ServletException { 94 ServletContext context = servletconfig.getServletContext(); 95 return context.getInitParameter("logDir"); 96 } 97 protected File getLogDir(ServletConfig servletconfig) throws ServletException { 98 String logDir = getLogDirParameter(servletconfig); 99 return getDirectoryFromName(logDir); 100 } 101 102 103 List getIndexFiles(ServletConfig servletconfig) { 104 ServletContext context = servletconfig.getServletContext(); 105 String logDir = context.getInitParameter("fileServlet.welcomeFiles"); 106 List indexes = Collections.EMPTY_LIST; 107 if (logDir != null) { 108 StringTokenizer tokenizer = new StringTokenizer (logDir); 109 indexes = new ArrayList (); 110 while (tokenizer.hasMoreTokens()) { 111 String indexFile = ((String ) tokenizer.nextElement()); 112 if (!"".equals(indexFile)) { 115 indexes.add(indexFile); 116 } 117 } 118 } 119 return indexes; 120 } 121 122 private static File getDirectoryFromName(String dir) { 123 File rootDirectory; 124 if (dir == null) { 125 return null; 126 } 127 rootDirectory = new File (dir); 128 if (!rootDirectory.exists() || rootDirectory.isFile()) { 129 return null; 130 } 131 return rootDirectory; 132 } 133 134 public void service(HttpServletRequest request, HttpServletResponse response) 135 throws ServletException , IOException { 136 WebFile file = getSubWebFile(request.getPathInfo()); 137 138 if (file.isDir()) { 139 if (!request.getPathInfo().endsWith("/")) { 141 response.sendRedirect(response.encodeRedirectURL(request.getRequestURI() + '/')); 142 return; 143 } 144 String index = getIndexFile(file); 145 if (index != null) { 146 file = getSubWebFile(request.getPathInfo() + index); 147 } 148 } 149 150 if (file.isFile()) { 151 String filename = file.getName(); 152 String mimeType; 153 if (request.getParameter("mimetype") != null) { 154 mimeType = request.getParameter("mimetype"); 155 } else { 156 mimeType = getMimeType(filename); 157 } 158 Date date = new Date (file.getFile().lastModified()); 159 response.addDateHeader("Last-Modified", date.getTime()); 160 response.setContentType(mimeType); 161 file.write(response.getOutputStream()); 162 return; 163 } 164 165 response.setContentType("text/html"); 166 Writer writer = response.getWriter(); 167 writer.write("<html>"); 168 writer.write("<body>"); 169 writer.write("<h1>" + file + "</h1>"); 170 if (file.isDir()) { 171 printDirs(request, file, writer); 172 } else { 173 response.setStatus(HttpServletResponse.SC_NOT_FOUND); 174 writer.write("<h1>Invalid File or Directory</h1>"); 175 } 176 writer.write("</body>"); 177 writer.write("</html>"); 178 } 179 180 protected String getMimeType(String filename) { 181 String mimeType = getServletContext().getMimeType(filename); 182 if (mimeType == null) { 183 mimeType = getDefaultMimeType(); 184 } 185 return mimeType; 186 } 187 188 protected String getDefaultMimeType() { 189 return "text/plain"; 190 } 191 192 197 private String getIndexFile(WebFile dir) { 198 if (!dir.isDir()) { 199 throw new IllegalArgumentException (dir + " is not a directory"); 200 } 201 for (int i = 0; i < indexFiles.size(); i++) { 202 final File file = new File (dir.getFile(), (String ) indexFiles.get(i)); 203 if (file.exists() && file.isFile()) { 205 return (String ) indexFiles.get(i); 206 } 207 } 208 return null; 209 } 210 211 218 void printDirs(HttpServletRequest request, WebFile file, Writer writer) 219 throws IOException { 220 String [] files = file.list(); 221 writer.write("<ul>"); 222 for (int i = 0; i < files.length; i++) { 223 final String requestURI = request.getRequestURI(); 224 int jsessionidIdx = requestURI.indexOf(";jsessionid"); 225 String shortRequestURI; 226 String jsessionid; 227 if (jsessionidIdx >= 0) { 228 shortRequestURI = requestURI.substring(0, jsessionidIdx); 229 jsessionid = requestURI.substring(jsessionidIdx); 230 } else { 231 shortRequestURI = requestURI; 232 jsessionid = ""; 233 } 234 235 final String subFilePath = request.getPathInfo() + '/' + files[i]; 236 WebFile sub = getSubWebFile(subFilePath); 237 writer.write( 238 "<li><a HREF=\"" 239 + shortRequestURI 240 + "/" 241 + files[i] 242 + jsessionid 243 + "\">" 244 + files[i] 245 + (sub.isDir() ? "/" : "") 246 + "</a></li>"); 247 } 248 writer.write("</ul>"); 249 } 250 251 protected WebFile getSubWebFile(final String subFilePath) { 252 return new WebFile(rootDir, subFilePath); 253 } 254 255 } 256 257 class WebFile { 258 259 private final File file; 260 261 public WebFile(File logfile) { 262 file = logfile; 263 } 264 265 public WebFile(File root, String path) { 266 file = WebFile.parsePath(root, path); 267 } 268 269 public String getName() { 270 return file.getName(); 271 } 272 273 public boolean isDir() { 274 return file.isDirectory(); 275 } 276 277 protected InputStream getInputStream() throws IOException { 278 return new FileInputStream (file); 279 } 280 281 public void write(ServletOutputStream stream) throws IOException { 282 InputStream input = new BufferedInputStream (getInputStream()); 283 OutputStream output = new BufferedOutputStream (stream); 284 try { 285 int i; 286 while ((i = input.read()) != -1) { 287 output.write(i); 288 } 289 } finally { 290 input.close(); 291 output.flush(); 292 } 293 } 294 295 public boolean isFile() { 296 return file.isFile(); 297 } 298 299 private static File parsePath(File rootDir, String string) { 300 if (string == null || string.trim().length() == 0 || string.equals("/")) { 301 return rootDir; 302 } 303 String filename = string.replace('/', File.separatorChar); 304 filename = filename.replace('\\', File.separatorChar); 305 return new File (rootDir, filename); 306 } 307 308 public String [] list() { 309 String [] files = file.list(); 310 if (files == null) { 311 files = new String [0]; 312 } else { 313 Arrays.sort(files); 314 } 315 return files; 316 } 317 318 public String toString() { 319 return file.toString(); 320 } 321 322 public File getFile() { 323 return file; 324 } 325 } 326 | Popular Tags |