1 2 24 25 package org.enhydra.servlet.servlets; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletContext ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletOutputStream ; 37 import javax.servlet.http.HttpServlet ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 41 import com.lutris.http.MimeType; 42 import com.lutris.logging.LogChannel; 43 import com.lutris.logging.Logger; 44 import com.lutris.logging.StandardLogger; 45 48 49 59 60 public class FileServerServlet 61 extends HttpServlet { 62 63 66 private static final int buffer_size = 1024*1; 67 68 71 private final static String NOT_FOUND_MSG = 72 "<TITLE>Not Found</TITLE><H1>Not Found</H1> " + 73 "The requested object does not exist on this Enhydra Server. " + 74 "The link you followed is either outdated, inaccurate, " + 75 "or the server has been instructed not to let you have it."; 76 77 80 private final static String ACCESS_DENIED_MSG = 81 "<TITLE>Access Denied</TITLE><H1>Access Denied</H1> " + 82 "Access is not allowed to the requested file. "; 83 84 87 private final static String SC_MOVED_PERMANENTLY_MSG = 88 "<TITLE>Moved Permanently</TITLE><H1>Moved Permanently</H1> " + 89 "Resouce has been moved"; 90 91 94 private String defaultPage = null; 95 96 99 private LogChannel logChannel; 100 101 105 private StandardLogger standardLogger; 106 107 111 private boolean needToConfigureLogChannel = false; 112 113 114 117 private ServletContext myContext; 118 119 123 private Path docRoot = null; 124 125 128 private long numServed = 0; 129 130 133 private MimeType mimetypes = new MimeType(); 134 135 136 139 private String cgiType = null; 140 141 144 public FileServerServlet() { 145 super(); 146 if (Logger.getCentralLogger() == null) { 147 standardLogger = new StandardLogger(true); 148 needToConfigureLogChannel = true; 149 } 150 logChannel = Logger.getCentralLogger().getChannel("Enhydra"); 151 } 152 153 154 161 public void init(ServletConfig config) 162 throws ServletException { 163 super.init(config); 164 myContext = config.getServletContext(); 165 166 docRoot = new Path(myContext.getRealPath("")); 168 169 defaultPage = config.getInitParameter("index"); 171 if (defaultPage == null) { 172 defaultPage = "index.html"; 173 } 174 175 cgiType = config.getInitParameter("cgi"); 177 178 logInfo("Starting File Server Servlet with docRoot " + docRoot.getFilePath()); 187 } 188 189 198 public void doGet (HttpServletRequest req, HttpServletResponse res) 199 throws ServletException , IOException { 200 201 206 207 String pathInfo = req.getPathInfo(); 208 String path = docRoot.appendFilePath(pathInfo); 209 File file = new File (path); 210 if (isAuth(file.getPath())) { 211 if (cgiType != null) { 213 String scriptPath = getScriptPath(path); 214 if (scriptPath != null) { 215 String scriptPathInfo = getScriptPathInfo(path, scriptPath); 217 String scriptName = getScriptName(docRoot.getUrlPath(), scriptPath); 218 CgiProcessor cgi = new CgiProcessor(); 219 cgi.processCgiRequest(req, res, scriptPath, scriptPathInfo, scriptName); 220 } else { 221 processFileRequest(file, req, res); 223 } 224 } else { 225 processFileRequest(file, req, res); 227 } 228 } else { 229 res.sendError(res.SC_NOT_FOUND, NOT_FOUND_MSG); 231 logInfo("FileServerServlet: File " + file + " not Found."); 232 } 233 } 234 235 238 private String getScriptName(String docRoot, String scriptPath) { 239 240 return scriptPath.substring(docRoot.length() + 1); 241 } 242 243 246 private String getScriptPathInfo(String path, String scriptPath) { 247 248 int index = path.indexOf(scriptPath); 249 if (index == -1) { 250 return ""; 251 } 252 return path.substring(index + scriptPath.length()); 253 } 254 255 258 private String getScriptPath(String path) { 259 260 String cgi = "." + cgiType; 261 int index = path.indexOf(cgi); 262 if (index == -1) { 263 return null; 264 } 265 return path.substring(0, index + cgi.length()); 266 } 267 268 269 272 private void processFileRequest(File file, HttpServletRequest req, HttpServletResponse res) throws IOException { 273 274 if (file.exists()) { 275 if (file.isDirectory()) { 277 processDirectory(req, res); 279 } else { 280 sendPage(file, res); 282 } 283 } else { 284 res.sendError(res.SC_NOT_FOUND, NOT_FOUND_MSG); 286 logInfo("FileServerServlet: File " + file + " not Found."); 287 } 288 } 289 290 293 private void processDirectory(HttpServletRequest req, HttpServletResponse res) 294 throws IOException { 295 296 String pathInfo = req.getPathInfo(); 297 if (pathInfo == null) { 298 pathInfo = "/"; 299 } 300 301 Path path = new Path(docRoot.getUrlPath(), pathInfo); 302 if (pathInfo.endsWith("/")) { 303 String indexPath = path.appendFilePath(defaultPage); 304 File indexFile = new File (indexPath); 305 if (indexFile.exists()) { 306 sendPage(indexFile, res); 308 } else { 309 File dir = new File (path.getFilePath()); 311 listDirectory(dir, req, res); 312 } 313 } else { 314 sendDirectoryRedirect(req, res); 315 } 316 } 317 318 321 private void sendDirectoryRedirect(HttpServletRequest req, HttpServletResponse res) 322 throws IOException { 323 324 String location = req.getRequestURI() + "/"; 325 res.setHeader("Location", location); 326 res.sendError(res.SC_MOVED_PERMANENTLY, SC_MOVED_PERMANENTLY_MSG); 327 } 328 329 335 String getUrl(HttpServletRequest request) { 336 337 String host = request.getScheme() + "://"; 338 host += request.getServerName(); 339 int port; 340 if ((port = request.getServerPort()) != 80) { 341 host += ":" + port; 342 } 343 return request.getRequestURI(); 344 } 345 346 356 private void listDirectory(File root, HttpServletRequest req, HttpServletResponse res) 357 throws IOException { 358 359 res.setContentType("text/html"); 360 ServletOutputStream out = res.getOutputStream(); 361 362 String vPath = (req.getPathInfo() == null ? "/" : req.getPathInfo()); 363 String vParent = (vPath.equals("/") ? "" : "../" ); 364 out.println("<html>"); 365 out.println("<head><title>Index of " + vPath + "</title></head>"); 366 out.println("<body>"); 367 out.println("<h1>Index of " + vPath + "</h1>\n<hr>"); 368 out.println("<pre>"); 369 out.println("<a HREF=\"" + vParent + "\">Parent Directory</a>"); 370 371 String href = null; 372 String files[] = root.list(); 373 String path = root.getPath(); 374 for (int i = 0; i < files.length; i++) { 375 File f = new File (path + "/" + files[i]); 376 if (f.isDirectory()) { 377 href = "<a HREF=\"" + files[i] + "/\">" + files[i] + "</a>"; 378 } else { 379 href = "<a HREF=\"" + files[i] + "\">" + files[i] + "</a>"; 380 } 381 out.println(href); 382 } 383 out.println("</pre>"); 384 out.println("</body>"); 385 out.println("</html>"); 386 } 387 388 396 private boolean isAuth(String path) { 397 398 if (path.indexOf("..") == -1) { 399 return true; 400 } 401 return false; 402 } 403 404 408 private void sendPage(File file, HttpServletResponse res) 409 throws IOException { 410 411 String filetype = mimetypes.getType(file.getName()); 412 res.setContentType(filetype); 413 Long len = new Long (file.length()); 414 res.setHeader("Content-Length", len.toString()); 415 ServletOutputStream out = res.getOutputStream(); 416 FileInputStream in = new FileInputStream (file); 417 writeFile(out, in); 418 in.close(); 419 numServed++; 420 } 421 422 431 public void doPost (HttpServletRequest req, HttpServletResponse res) 432 throws ServletException , IOException { 433 res.sendError(res.SC_NOT_IMPLEMENTED); 434 logInfo("FileServerServlet: Access Denied."); 435 } 436 437 443 public String getServletInfo() { 444 return "A FileServerServlet, with doc root " + docRoot.getFilePath(); 445 } 446 447 450 public String toHtml() { 451 return "Serving files from the directory " + docRoot.getFilePath() + ".<BR>\n" + 452 numServed + " files served."; 453 } 454 455 464 private void writeFile(OutputStream out, InputStream in) 465 throws IOException { 466 467 byte buffer[] = new byte[buffer_size]; 468 while (true) { 469 int n = in.read(buffer); 470 if (n == -1) break; 471 out.write(buffer, 0, n); 472 } 473 } 474 475 483 private void logInfo(String msg) { 484 if (logChannel != null) 486 logChannel.write(Logger.INFO, msg); 489 else 491 myContext.log(msg); 492 } 493 494 503 private void logError(String err, Exception e) { 504 if (logChannel != null) 506 logChannel.write(Logger.ERROR, err, e); 509 else 511 myContext.log(e, err); 512 } 513 } 514 | Popular Tags |