1 29 30 package com.caucho.servlets; 31 32 import com.caucho.i18n.CharacterEncoding; 33 import com.caucho.server.connection.CauchoRequest; 34 import com.caucho.server.webapp.Application; 35 import com.caucho.util.CharBuffer; 36 import com.caucho.util.URLUtil; 37 import com.caucho.vfs.Path; 38 import com.caucho.vfs.Vfs; 39 40 import javax.servlet.http.HttpServlet ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.io.IOException ; 44 import java.io.PrintWriter ; 45 import java.util.Iterator ; 46 47 public class DirectoryServlet extends HttpServlet { 48 Application _app; 49 Path _context; 50 private boolean _enable = true; 51 52 public DirectoryServlet(Path context) 53 { 54 _context = context; 55 } 56 57 public DirectoryServlet() 58 { 59 this(Vfs.lookup()); 60 } 61 62 public void setEnable(boolean enable) 63 { 64 _enable = enable; 65 } 66 67 public void init() 68 { 69 _app = (Application) getServletContext(); 70 _context = _app.getAppDir(); 71 } 72 73 public void doGet(HttpServletRequest req, HttpServletResponse res) 74 75 throws IOException 76 { 77 if (! _enable) { 78 res.sendError(404); 79 return; 80 } 81 82 CauchoRequest cauchoReq = null; 83 84 if (req instanceof CauchoRequest) 85 cauchoReq = (CauchoRequest) req; 86 87 String uri = req.getRequestURI(); 88 boolean redirect = false; 89 90 if (uri.length() > 0 && uri.charAt(uri.length() - 1) != '/') { 91 res.sendRedirect(uri + "/"); 92 return; 93 } 94 95 String encoding = CharacterEncoding.getLocalEncoding(); 96 if (encoding == null) 97 res.setContentType("text/html"); 98 else 99 res.setContentType("text/html; charset=" + encoding); 100 101 boolean isInclude = false; 102 103 if (cauchoReq != null) { 104 uri = cauchoReq.getPageURI(); 105 isInclude = ! uri.equals(cauchoReq.getRequestURI()); 106 } 107 else { 108 uri = (String ) req.getAttribute("javax.servlet.include.request_uri"); 109 if (uri != null) 110 isInclude = true; 111 else 112 uri = req.getRequestURI(); 113 } 114 115 CharBuffer cb = CharBuffer.allocate(); 116 String servletPath; 117 118 if (cauchoReq != null) 119 servletPath = cauchoReq.getPageServletPath(); 120 else if (isInclude) 121 servletPath = (String ) req.getAttribute("javax.servlet.include.servlet_path"); 122 else 123 servletPath = req.getServletPath(); 124 125 if (servletPath != null) 126 cb.append(servletPath); 127 128 String pathInfo; 129 if (cauchoReq != null) 130 pathInfo = cauchoReq.getPagePathInfo(); 131 else if (isInclude) 132 pathInfo = (String ) req.getAttribute("javax.servlet.include.path_info"); 133 else 134 pathInfo = req.getPathInfo(); 135 136 if (pathInfo != null) 137 cb.append(pathInfo); 138 139 String relPath = cb.close(); 140 141 String filename = getServletContext().getRealPath(relPath); 142 Path path = _context.lookupNative(filename); 143 144 String rawpath = java.net.URLDecoder.decode(uri); 145 146 PrintWriter pw = res.getWriter(); 147 148 if (rawpath.length() == 0 || rawpath.charAt(0) != '/') 149 rawpath = "/" + rawpath; 150 151 boolean endsSlash = rawpath.charAt(rawpath.length() - 1) == '/'; 152 String tail = ""; 153 if (! endsSlash) { 154 int p = rawpath.lastIndexOf('/'); 155 tail = rawpath.substring(p + 1) + "/"; 156 rawpath = rawpath + "/"; 157 } 158 159 pw.println("<html>"); 160 pw.println("<head>"); 161 pw.println("<title>Directory of " + rawpath + "</title>"); 162 pw.println("</head>"); 163 pw.println("<body>"); 164 165 pw.println("<h1>Directory of " + rawpath + "</h1>"); 166 167 pw.println("<ul>"); 168 169 Iterator i = path.iterator(); 170 while (i.hasNext()) { 171 String name = (String ) i.next(); 172 173 if (name.equalsIgnoreCase("web-inf") || 174 name.equalsIgnoreCase("meta-inf")) 175 continue; 176 177 String enc = URLUtil.encodeURL(tail + name); 178 179 pw.println("<li><a HREF='" + enc + "'>" + name + "</a>"); 180 } 181 pw.println("</ul>"); 182 pw.println("</body>"); 183 pw.println("</html>"); 184 pw.close(); 185 } 186 } 187 | Popular Tags |