001: /*
002: * NotFoundHandler.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.9
024: * Created by suhler on 99/03/29
025: * Last modified by suhler on 00/12/11 13:27:46
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import sunlabs.brazil.server.Handler;
031: import sunlabs.brazil.server.Request;
032: import sunlabs.brazil.server.Server;
033: import sunlabs.brazil.server.FileHandler;
034: import java.io.File;
035: import java.io.IOException;
036:
037: /**
038: * Handler for returning "file not found" errors back to the client.
039: * Look for the file "NotFound.html" in the current directory, and return it
040: * if it exists. Otherwise, return the "NotFound.html" file in the document
041: * root directory. If neither can be found, then punt, and let someone else
042: * deal with it.
043: * <p>
044: * Configuration parameters understood by this handler
045: * <dl class=props>
046: * <dt>root <dd>The location of the document root for locating the
047: * default "not found" file (also looks using prefix of "").
048: * <dt>prefix <dd>The default url prefix for urls. Defaults to "/".
049: * <dt>fileName <dd>The name of the file to send for missing files.
050: * Defaults to "notfound.html"
051: * <dt>type <dd>The file type, defaults to text/html
052: * </dl>
053: *
054: * @author Stephen Uhler
055: * @version 1.9, 00/12/11
056: */
057:
058: public class NotFoundHandler implements Handler {
059: static final String PREFIX = "prefix"; // URL prefix
060: static final String NAME = "fileName"; // name of the not-found file
061: static final String TYPE = "type"; // type of the not-found file (should be looked up from the suffix)
062:
063: String urlPrefix; // required URL prefix
064: String fileName; // name of "not found" file
065: String type; // file type
066: File rootFile; // root not-found file
067:
068: /**
069: * Extract the handler properties.
070: * Get the URL prefix and default "missing" file name.
071: */
072:
073: public boolean init(Server server, String prefix) {
074: urlPrefix = server.props.getProperty(prefix + PREFIX, "/");
075: fileName = server.props.getProperty(prefix + NAME,
076: "notfound.html");
077: type = server.props.getProperty(prefix + TYPE, "text/html");
078: String root = server.props.getProperty(prefix
079: + FileHandler.ROOT, server.props.getProperty(
080: FileHandler.ROOT, "."));
081: rootFile = new File(root, fileName);
082: server.log(Server.LOG_DIAGNOSTIC, prefix, "looking for: "
083: + rootFile);
084: if (!rootFile.isFile()) {
085: server.log(Server.LOG_WARNING, prefix, "Can't find file: "
086: + rootFile);
087: }
088: return true;
089: }
090:
091: /**
092: * Look for and deliver the "not found" file
093: * Look in the current directory first, then in the doc root.
094: * Only files whose suffixes have valid mime types are delivered.
095: */
096:
097: public boolean respond(Request request) throws IOException {
098: if (!request.url.startsWith(urlPrefix)) {
099: return false;
100: }
101:
102: String missing = request.props.getProperty("fileName");
103: if (missing == null) {
104: request.log(Server.LOG_DIAGNOSTIC,
105: "No missing file found!!");
106: return false;
107: }
108: File name = new File((new File(missing)).getParent(), fileName);
109: if (name.canRead() && name.isFile()) {
110: FileHandler.sendFile(request, name, 404, type);
111: request
112: .log(Server.LOG_DIAGNOSTIC,
113: "sending not-found file");
114: } else if (rootFile.canRead() && rootFile.isFile()) {
115: FileHandler.sendFile(request, rootFile, 404, type);
116: request.log(Server.LOG_DIAGNOSTIC,
117: "sending Root not-found file");
118: } else {
119: return false;
120: }
121: return true;
122: }
123: }
|