001: /*
002: * DefaultFileHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 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): suhler.
022: *
023: * Version: 1.2
024: * Created by suhler on 00/08/16
025: * Last modified by suhler on 00/12/11 13:29:00
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.io.File;
031: import java.io.IOException;
032: import java.util.StringTokenizer;
033: import sunlabs.brazil.server.FileHandler;
034: import sunlabs.brazil.server.Handler;
035: import sunlabs.brazil.server.Request;
036: import sunlabs.brazil.server.Server;
037:
038: /**
039: * Handler for appending a url ending with '/' into the appropriate
040: * url based on a default file in the file system.
041: * <p>
042: * The following request properties are used:
043: * <dl class=props>
044: * <dt>defaults<dd>The names of the default files to search for in
045: * the directory implied by the URL.
046: * The first one that exists will
047: * cause its name to be appended to the URL. Defaults
048: * to "index.html".
049: * <dt>root<dd>The document root to look for files. If none is found with our
050: * prefix, then "root" is examined. Defaults to ".".
051: * <dt>DirectoryName<dd>This property is set if the URL represents a valid
052: * directory in the document root.
053: * <dt>fileName<dd>This property is set to the name of the default file,
054: * if one was found.
055: * </dl>
056: *
057: * @author Stephen Uhler
058: * @version %V% 00/12/11
059: */
060:
061: public class DefaultFileHandler implements Handler {
062: static final String DEFAULTS = "defaults"; // list of default files
063: static final String ROOT = "root"; // document root
064: String prefix; // our properties prefix
065:
066: /**
067: * Remember our profix in the properties table.
068: */
069:
070: public boolean init(Server server, String prefix) {
071: this .prefix = prefix;
072: return true;
073: }
074:
075: /**
076: * If the url ends with a "/" look around in the corrosponding directory
077: * to find a suitable default file, and then change the url.
078: *
079: * @returns Always returns false.
080: */
081:
082: public boolean respond(Request request) throws IOException {
083: if (!request.url.startsWith("/") || !request.url.endsWith("/")) {
084: return false;
085: }
086: String root = request.props.getProperty(prefix + ROOT,
087: request.props.getProperty(ROOT, "."));
088: String dir = root + FileHandler.urlToPath(request.url);
089: request.log(Server.LOG_DIAGNOSTIC, prefix,
090: "Checking directory: " + dir);
091: File base = new File(dir);
092:
093: if (!base.isDirectory()) {
094: request.log(Server.LOG_DIAGNOSTIC, prefix,
095: " Not a directory");
096: return false;
097: }
098: request.props.put("DirectoryName", base.getPath());
099:
100: StringTokenizer st = new StringTokenizer(request.props
101: .getProperty(prefix + DEFAULTS, "index.html"));
102: while (st.hasMoreTokens()) {
103: String token = st.nextToken();
104: File name = new File(base, token);
105: request.log(Server.LOG_DIAGNOSTIC, prefix, name + " ?");
106: if (name.isFile()) {
107: request.url += token;
108: request.props.put("fileName", name.getPath());
109: request.log(Server.LOG_DIAGNOSTIC, prefix, "new url: "
110: + request.url);
111: break;
112: }
113: }
114: return false;
115: }
116: }
|