KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > servlets > DirectoryServlet


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.PrintWriter JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47 public class DirectoryServlet extends HttpServlet JavaDoc {
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 JavaDoc req, HttpServletResponse JavaDoc res)
74
75     throws IOException JavaDoc
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 JavaDoc 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 JavaDoc 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 JavaDoc) 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 JavaDoc servletPath;
117
118     if (cauchoReq != null)
119       servletPath = cauchoReq.getPageServletPath();
120     else if (isInclude)
121       servletPath = (String JavaDoc) 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 JavaDoc pathInfo;
129     if (cauchoReq != null)
130       pathInfo = cauchoReq.getPagePathInfo();
131     else if (isInclude)
132       pathInfo = (String JavaDoc) 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 JavaDoc relPath = cb.close();
140
141     String JavaDoc filename = getServletContext().getRealPath(relPath);
142     Path path = _context.lookupNative(filename);
143     
144     String JavaDoc rawpath = java.net.URLDecoder.decode(uri);
145
146     PrintWriter JavaDoc 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 JavaDoc 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 JavaDoc i = path.iterator();
170     while (i.hasNext()) {
171       String JavaDoc name = (String JavaDoc) i.next();
172
173       if (name.equalsIgnoreCase("web-inf") ||
174           name.equalsIgnoreCase("meta-inf"))
175         continue;
176
177       String JavaDoc 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