001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.servlets;
031:
032: import com.caucho.i18n.CharacterEncoding;
033: import com.caucho.server.connection.CauchoRequest;
034: import com.caucho.server.webapp.WebApp;
035: import com.caucho.server.util.CauchoSystem;
036: import com.caucho.util.CharBuffer;
037: import com.caucho.util.URLUtil;
038: import com.caucho.vfs.Path;
039: import com.caucho.vfs.Vfs;
040:
041: import javax.servlet.http.HttpServlet;
042: import javax.servlet.http.HttpServletRequest;
043: import javax.servlet.http.HttpServletResponse;
044: import java.io.IOException;
045: import java.io.PrintWriter;
046: import java.util.Iterator;
047:
048: public class DirectoryServlet extends HttpServlet {
049: WebApp _app;
050: Path _context;
051: private boolean _enable = true;
052:
053: public DirectoryServlet(Path context) {
054: _context = context;
055: }
056:
057: public DirectoryServlet() {
058: this (Vfs.lookup());
059: }
060:
061: public void setEnable(boolean enable) {
062: _enable = enable;
063: }
064:
065: public void init() {
066: _app = (WebApp) getServletContext();
067: _context = _app.getAppDir();
068: }
069:
070: public void doGet(HttpServletRequest req, HttpServletResponse res)
071:
072: throws IOException {
073: if (!_enable) {
074: res.sendError(404);
075: return;
076: }
077:
078: CauchoRequest cauchoReq = null;
079:
080: if (req instanceof CauchoRequest)
081: cauchoReq = (CauchoRequest) req;
082:
083: String uri = req.getRequestURI();
084: boolean redirect = false;
085:
086: String encoding = CharacterEncoding.getLocalEncoding();
087: if (encoding == null)
088: res.setContentType("text/html");
089: else
090: res.setContentType("text/html; charset=" + encoding);
091:
092: boolean isInclude = false;
093:
094: if (cauchoReq != null) {
095: uri = cauchoReq.getPageURI();
096: isInclude = !uri.equals(cauchoReq.getRequestURI());
097: } else {
098: uri = (String) req
099: .getAttribute("javax.servlet.include.request_uri");
100: if (uri != null)
101: isInclude = true;
102: else
103: uri = req.getRequestURI();
104: }
105:
106: StringBuilder cb = new StringBuilder();
107: String servletPath;
108:
109: if (cauchoReq != null)
110: servletPath = cauchoReq.getPageServletPath();
111: else if (isInclude)
112: servletPath = (String) req
113: .getAttribute("javax.servlet.include.servlet_path");
114: else
115: servletPath = req.getServletPath();
116:
117: if (servletPath != null)
118: cb.append(servletPath);
119:
120: String pathInfo;
121: if (cauchoReq != null)
122: pathInfo = cauchoReq.getPagePathInfo();
123: else if (isInclude)
124: pathInfo = (String) req
125: .getAttribute("javax.servlet.include.path_info");
126: else
127: pathInfo = req.getPathInfo();
128:
129: if (pathInfo != null)
130: cb.append(pathInfo);
131:
132: String relPath = cb.toString();
133: String filename = getServletContext().getRealPath(relPath);
134: Path path = _context.lookupNative(filename);
135:
136: if (CauchoSystem.isWindows() && path.isWindowsInsecure()) {
137: res.sendError(HttpServletResponse.SC_NOT_FOUND);
138: return;
139: }
140:
141: if (uri.length() > 0 && uri.charAt(uri.length() - 1) != '/') {
142: res.sendRedirect(uri + "/");
143: return;
144: }
145:
146: String rawpath = java.net.URLDecoder.decode(uri);
147:
148: PrintWriter pw = res.getWriter();
149:
150: if (rawpath.length() == 0 || rawpath.charAt(0) != '/')
151: rawpath = "/" + rawpath;
152:
153: boolean endsSlash = rawpath.charAt(rawpath.length() - 1) == '/';
154: String tail = "";
155: if (!endsSlash) {
156: int p = rawpath.lastIndexOf('/');
157: tail = rawpath.substring(p + 1) + "/";
158: rawpath = rawpath + "/";
159: }
160:
161: pw.println("<html>");
162: pw.println("<head>");
163: pw.println("<title>Directory of " + rawpath + "</title>");
164: pw.println("</head>");
165: pw.println("<body>");
166:
167: pw.println("<h1>Directory of " + rawpath + "</h1>");
168:
169: pw.println("<ul>");
170:
171: Iterator i = path.iterator();
172: while (i.hasNext()) {
173: String name = (String) i.next();
174:
175: if (name.equalsIgnoreCase("web-inf")
176: || name.equalsIgnoreCase("meta-inf"))
177: continue;
178:
179: String enc = URLUtil.encodeURL(tail + name);
180:
181: pw.println("<li><a href='" + enc + "'>" + name + "</a>");
182: }
183: pw.println("</ul>");
184: pw.println("</body>");
185: pw.println("</html>");
186: pw.close();
187: }
188: }
|