001: /*
002: * ReplaceFilter.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): suhler.
022: *
023: * Version: 1.5
024: * Created by suhler on 99/09/01
025: * Last modified by suhler on 00/12/11 13:26:06
026: */
027:
028: package sunlabs.brazil.filter;
029:
030: import sunlabs.brazil.server.Request;
031: import sunlabs.brazil.server.Server;
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.IOException;
035: import sunlabs.brazil.util.http.MimeHeaders;
036:
037: /**
038: * Filter to replace current content with a static form, or template.
039: * This should be called
040: * the TemplateFiler, but that name's already taken. The content is
041: * replaced by the template lock-stock-and-barrel. Typically, an upstream
042: * filter has extracted the relevent parts of the content, and a down-stream
043: * filter will combine it with the template.
044: * The filename to use for the template
045: * is computed at each request, so it may be modified dynamically.
046: *
047: * The following server properties are used:
048: * <dl class=props>
049: * <dt>type <dd> Text subtype of content to filter. Defaults to "html"
050: * <dt>debug <dd> If set, the template is re-read each time. Otherwise
051: * a cached copy is used.
052: * <dt>fileName <dd> Name of the file to use as the form or template.
053: * <dt>root <dd> he document root used to find the template file.
054: * </dl>
055: *
056: * @author Stephen Uhler
057: * @version %V% ReplaceFilter.java
058: */
059:
060: public class ReplaceFilter implements Filter {
061: static final String FILE = "fileName";
062: String type; // the text sub-type to replace (defaults to html)
063: String prefix; // the properties prefix
064: byte[] data; // cache of the last template
065: String path; // cache of last file name;
066: boolean debug; // don't cache file
067:
068: public boolean init(Server server, String prefix) {
069: type = server.props.getProperty(prefix + "type", "html");
070: debug = server.props.getProperty(prefix + "debug") != null;
071: this .prefix = prefix;
072: path = null;
073: return true;
074: }
075:
076: /**
077: * This is the request object before the content was fetched
078: */
079:
080: public boolean respond(Request request) {
081: return false;
082: }
083:
084: /**
085: * Only replace text documents
086: */
087:
088: public boolean shouldFilter(Request request, MimeHeaders headers) {
089: String type = headers.get("content-type");
090: return (type != null && type.startsWith("text/"));
091: }
092:
093: /**
094: * Grab the template file name, Read in the file, and
095: * deliver it as content.
096: */
097:
098: public byte[] filter(Request request, MimeHeaders headers,
099: byte[] content) {
100: if (!(headers.get("content-type")).startsWith("text/" + type)) {
101: return content;
102: }
103: String name = request.props.getProperty(prefix + "fileName");
104: if (name == null) {
105: request.log(Server.LOG_WARNING,
106: "No fileName property found for " + prefix);
107: return content;
108: }
109:
110: if (!debug && path != null && name.equals(path)) {
111: request.log(Server.LOG_DIAGNOSTIC,
112: "using cached content for: " + name);
113: return data;
114: }
115:
116: File file;
117: if (name.startsWith("/")) {
118: file = new File(name);
119: } else {
120: String root = request.props.getProperty("root", ".");
121: file = new File(root, name);
122: }
123: try {
124: FileInputStream in = new FileInputStream(file);
125: data = new byte[(int) file.length()];
126: in.read(data);
127: in.close();
128: path = name; // cache for next use
129: return data;
130: } catch (IOException e) {
131: request.log(Server.LOG_WARNING, prefix + " can't get "
132: + file + ": " + e);
133: return content;
134: }
135: }
136: }
|