001: /*
002: * HomeDirHandler.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1998-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.9
024: * Created by suhler on 98/09/14
025: * Last modified by suhler on 00/12/11 13:26:57
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.io.File;
031: import sunlabs.brazil.server.FileHandler;
032: import sunlabs.brazil.server.Handler;
033: import sunlabs.brazil.server.Request;
034: import sunlabs.brazil.server.Server;
035: import java.io.IOException;
036:
037: /**
038: * Handler for converting ~username queries.
039: * When invoked upstream of the
040: * {@link FileHandler}
041: * This provides Unix user's with individual home pages.
042: *<p>
043: *Properties:
044: * <dl class=props>
045: * <dt>subdir <dd>Name of the directory in the user's home directory
046: * that represents the user's "doc root"
047: * <dt>home <dd>The mount-point for homre directories,
048: * defaults to "/home/".
049: * </dl>
050: * Url's of the form:
051: * <pre>/~[user]/stuff...</pre>
052: * are transformed into
053: * [home][user]/[subdir]/stuff....
054: * <p>
055: * Note: This functionallity has been mostly subsumed by the
056: * {@link UrlMapperHandler}.
057: *
058: * @author Stephen Uhler
059: * @version 1.9, 00/12/11
060: */
061:
062: public class HomeDirHandler implements Handler {
063: private String propsPrefix;
064: private String docDir;
065: private String home;
066:
067: /**
068: * Handler configuration property <b>subdir</b>.
069: * The name of the directory in the user's home directory to use as the
070: * document root for this request.
071: * The default is <code>public_html</code>.
072: */
073: static final String DIR = "subdir"; // subdirectory for finding pages
074: /**
075: * Handler configuration property <b>home</b>.
076: * The directory that user accounts live in.
077: * The current implementation doesn't consult the password file, but
078: * uses a static mapping from the userid to a directory.
079: * The default is /home.
080: * using the default settings, a url of the form:
081: * <code>/~user/foo.html</code> gets translated into the file:
082: * <code>/home/user/public_html/foo.html</code>.
083: */
084: static final String HOME = "home"; // where user's live - need both slashes
085:
086: /**
087: * Get and set the configuration parameters.
088: */
089:
090: public boolean init(Server server, String prefix) {
091: propsPrefix = prefix;
092: docDir = server.props.getProperty(propsPrefix + DIR,
093: "public_html");
094: home = server.props.getProperty(propsPrefix + HOME, "/home/");
095: return true;
096: }
097:
098: /**
099: * If this is a ~user request, modify the <code>root</code> and
100: * <code>url</code> properties of the request object.
101: */
102:
103: public boolean respond(Request request) throws IOException {
104: String user;
105: String url = "/";
106: if (request.url.startsWith("/~")) {
107: int index = request.url.indexOf("/", 2);
108: if (index < 0) {
109: request.redirect(request.url + "/", null);
110: return true;
111: } else {
112: user = request.url.substring(2, index);
113: url = request.url.substring(index);
114: }
115:
116: String root = home + user + "/" + docDir;
117: File file = new File(root);
118: if (file.isDirectory()) {
119: request.url = url;
120: request.props.put(FileHandler.ROOT, root);
121: request.log(Server.LOG_INFORMATIONAL, propsPrefix
122: + ".. mapping " + user + " to "
123: + request.props.get(FileHandler.ROOT)
124: + " url: " + request.url);
125: }
126: }
127: return false;
128: }
129: }
|