001: /*
002: * DirectoryHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1999-2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.13
024: * Created by suhler on 99/03/29
025: * Last modified by suhler on 00/12/11 20:23:36
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import sunlabs.brazil.util.http.HttpUtil;
031:
032: import sunlabs.brazil.server.Handler;
033: import sunlabs.brazil.server.Request;
034: import sunlabs.brazil.server.Server;
035: import sunlabs.brazil.server.FileHandler;
036: import java.io.IOException;
037: import java.io.File;
038: import java.util.Vector;
039: import sunlabs.brazil.util.Sort;
040:
041: /**
042: * Handler for directory lists.
043: * This is a bare-bones handler for providing directory listings
044: * for web servers.
045: * It is designed to be placed after the
046: * {@link sunlabs.brazil.server.FileHandler}. If no index file is found,
047: * Then a simple directory listing will be produced. Only files whose
048: * extensions are in the mime properties will be listed.
049: * <br>
050: * NOTE: This handler is obsolete, as it provides no control over the
051: * format of the directory listing.
052: * Use the {@link sunlabs.brazil.template.DirectoryTemplate} instead.
053: * <p>
054: * Configuration properties used:
055: * <dl class=props>
056: * <dt>prefix <dd>An initial prefix for urls that may provide listings
057: * <dt>DirectoryName <dd>This property is set by the
058: * {@link sunlabs.brazil.server.FileHandler} if the
059: * URL it was passed resolves to a directory, but no
060: * index file (e.g. index.html) was found.
061: * <dt>setProps <dd>If present, no content is returned. Instead,
062: * The properties "Directories" and "Files" are
063: * set in the request properties, so the format of
064: * the output may be generated dynamically.
065: * [Note: This feature is deprecated, use the
066: * {@link sunlabs.brazil.template.DirectoryTemplate}
067: * instead].
068: * <dt>delim <dd>The delimeter separating the file names.
069: * Defaults to a single space.
070: * <dt>mime.xxx <dd>Only documents ending in ".xxx" are considered.
071: * more than on mime.xxx parameters may be specified.
072: * </dl>
073: *
074: * @author Stephen Uhler
075: * @version 1.13, 00/12/11
076: */
077:
078: public class DirectoryHandler implements Handler {
079: private Server server;
080: private String propsPrefix;
081: private String urlPrefix;
082: private static final String PREFIX = "prefix"; // url prefix
083:
084: /**
085: * Get the url prefix for this handler.
086: */
087:
088: public boolean init(Server server, String prefix) {
089: this .server = server;
090: propsPrefix = prefix;
091: urlPrefix = server.props.getProperty(propsPrefix + PREFIX, "/");
092: return true;
093: }
094:
095: /**
096: * Display files in a directory, after being rejected by the
097: * FileHandler. The output is very simple.
098: */
099:
100: public boolean respond(Request request) throws IOException {
101: String directory = request.props.getProperty("DirectoryName");
102: if (directory == null || !request.url.startsWith(urlPrefix)) {
103: return false;
104: }
105:
106: String[] list = (new File(directory)).list();
107: Vector files = new Vector();
108: Vector dirs = new Vector();
109:
110: if (list != null) {
111: for (int i = 0; i < list.length; i++) {
112: String name = list[i];
113: int index = name.lastIndexOf(".");
114: if ((new File(directory, name)).isDirectory()) {
115: dirs.addElement(name);
116: } else if (index > 0
117: && server.props.containsKey(FileHandler.MIME
118: + name.substring(index))) {
119: files.addElement(name);
120: }
121: }
122: }
123:
124: /*
125: * Just set properties. We should be more flexible here. This
126: * doesn't work with embedded spaces for now!
127: */
128:
129: if (request.props.getProperty(propsPrefix + "setProps") != null) {
130: String delim = request.props.getProperty(propsPrefix
131: + "delim", " ");
132: StringBuffer dir = new StringBuffer();
133: for (int i = 0; i < dirs.size(); i++) {
134: if (i > 0) {
135: dir.append(delim);
136: }
137: dir.append((String) dirs.elementAt(i));
138: }
139: StringBuffer file = new StringBuffer();
140: for (int i = 0; i < files.size(); i++) {
141: if (i > 0) {
142: file.append(delim);
143: }
144: file.append((String) files.elementAt(i));
145: }
146: request.props.put("Directories", dir.toString());
147: request.props.put("Files", file.toString());
148: return false;
149: }
150:
151: /*
152: * simple output for now
153: */
154:
155: StringBuffer result = new StringBuffer();
156: result.append("<title>Directory Listing</title>\n");
157: result.append("<h1>Directory Listing</h1>\n");
158: result.append("<a href=..><b>parent directory</b></a>\n");
159: if (dirs.size() > 0) {
160: Sort.qsort(dirs);
161: result.append("<h2>Directories</h2>\n");
162: list(dirs, result);
163: }
164: if (files.size() > 0) {
165: Sort.qsort(files);
166: result.append("<h2>Files</h2>\n");
167: list(files, result);
168: }
169: request.sendResponse(result.toString());
170: return true;
171: }
172:
173: /*
174: * List the elements of the directory in a row
175: */
176:
177: private void list(Vector v, StringBuffer result) {
178: for (int i = 0; i < v.size(); i++) {
179: String name = (String) v.elementAt(i);
180: result.append("<a href=\"" + name + " \">"
181: + HttpUtil.htmlEncode(name) + "</a><br>\n");
182: }
183: }
184: }
|