001: /*
002: * CopyContentFilter.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): cstevens, suhler.
022: *
023: * Version: 1.5
024: * Created by suhler on 00/02/16
025: * Last modified by suhler on 00/12/11 13:26:15
026: */
027:
028: package sunlabs.brazil.filter;
029:
030: import sunlabs.brazil.server.FileHandler;
031: import sunlabs.brazil.server.Request;
032: import sunlabs.brazil.server.Server;
033: import sunlabs.brazil.util.http.MimeHeaders;
034:
035: import java.io.File;
036: import java.io.FileOutputStream;
037: import java.io.IOException;
038:
039: /**
040: * Filter to save content (of an entire site) to a disk file.
041: * This is used to "steal" other sites. It is expected to be used
042: * in conjunction with a {@link sunlabs.brazil.handler.GenericProxyHandler}.
043: *
044: * Only files that don't already exist on the local file system are
045: * saved.
046: *
047: * Properties:
048: *<dl class=props>
049: *<dt>directoryName <dd>The root in the file system to save the content in
050: *</dl>
051: *
052: * @author Stephen Uhler
053: * @version %V% CopyContentFilter.java
054: */
055:
056: public class CopyContentFilter implements Filter {
057: static final String DIR = "directoryName";
058: String prefix; // the properties prefix
059: File dir; // root directory to steal stuff in
060:
061: public boolean init(Server server, String prefix) {
062: String name = server.props.getProperty(prefix + DIR);
063: this .prefix = prefix;
064: if (name.startsWith("/")) {
065: dir = new File(name);
066: } else {
067: String root = server.props.getProperty("root", ".");
068: dir = new File(root, name);
069: }
070: if (dir.mkdir() || dir.isDirectory()) {
071: return true;
072: } else {
073: server.log(Server.LOG_WARNING, prefix, "Can't write into: "
074: + dir);
075: return false;
076: }
077: }
078:
079: /**
080: * This is the request object before the content was fetched
081: */
082:
083: public boolean respond(Request request) {
084: return false;
085: }
086:
087: /**
088: * Watch every document that passes by. If the HTTP rerun code is
089: * "200", plan to save the content on the local file system.
090: */
091:
092: public boolean shouldFilter(Request request, MimeHeaders headers) {
093: return (headers.get("status").indexOf("200") >= 0);
094: }
095:
096: /**
097: * Grab the contents, and save as a file (if file doesn't already exist).
098: * The URL is mapped into a pathname starting from <code>directoryName
099: * </code>.
100: */
101:
102: public byte[] filter(Request request, MimeHeaders headers,
103: byte[] content) {
104: File file = new File(dir, FileHandler.urlToPath(request.url));
105: if (!file.exists()) {
106: try {
107: (new File(file.getParent())).mkdirs();
108: FileOutputStream out = new FileOutputStream(file);
109: out.write(content);
110: out.close();
111: request.log(Server.LOG_DIAGNOSTIC, prefix, "Saved "
112: + content.length + " bytes to: " + file);
113: } catch (IOException e) {
114: request.log(Server.LOG_WARNING, prefix, " can't write "
115: + file + ": " + e);
116: }
117: }
118: return content;
119: }
120: }
|