001: /*
002: * JunkBusterHandler.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: cstevens.
018: * Portions created by cstevens are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): cstevens, suhler.
022: *
023: * Version: 1.6
024: * Created by cstevens on 00/04/03
025: * Last modified by suhler on 00/12/11 13:31:40
026: */
027:
028: package sunlabs.brazil.proxy;
029:
030: import sunlabs.brazil.handler.ResourceHandler;
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 sunlabs.brazil.util.http.HttpInputStream;
036: import sunlabs.brazil.util.regexp.Regexp;
037:
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.util.Properties;
041:
042: /**
043: * Remove <i>junk</i> images from web pages.
044: * This approach is to take all requests for images that look like ads and
045: * instead return a dummy bitmap.
046: * <p>
047: * Other approaches to removing ads are to filter the HTML returned and
048: * (1) remove the ads altogether or (2) change the href in the ads to point
049: * to a different bitmap. The advantage of option (2) is that all ads can be
050: * changed to point to the same bitmap, increasing the caching performance
051: * of the browser.
052: * <p>
053: * Properties:
054: * <dl class=props>
055: * <dt>image <dd>The file to contain the replacement image.
056: * <dt>host <dd>The regular expression matching url's to reject.
057: * If the expression starts with a '@', it interpreted as a
058: * file name (minus the @) that contains a new-line separated
059: * list of regular exporessions. See
060: * {@link sunlabs.brazil.util.regexp.Regexp} for more information on
061: * regular expressions.
062: * </dl>
063: */
064: public class JunkBusterHandler implements Handler {
065: private static final String IMAGE = "image";
066: private static final String HOSTS = "hosts";
067:
068: byte[] image;
069: String type;
070:
071: String prefix;
072:
073: String urlPattern;
074: Regexp hostPattern;
075:
076: public boolean init(Server server, String prefix) {
077: this .prefix = prefix;
078:
079: Properties props = server.props;
080:
081: String name = props.getProperty(prefix + IMAGE, "");
082: String suffix = name.substring(name.lastIndexOf('.'));
083: type = props.getProperty(FileHandler.MIME + suffix);
084: if (type == null) {
085: server.log(Server.LOG_DIAGNOSTIC, prefix,
086: "unknown image file MIME type: " + suffix);
087: return false;
088: }
089: try {
090: image = ResourceHandler.getResourceBytes(props, prefix,
091: name);
092: } catch (Exception e) {
093: server.log(Server.LOG_DIAGNOSTIC, prefix,
094: "error reading image file");
095: return false;
096: }
097:
098: try {
099: String hosts = props.getProperty(prefix + HOSTS);
100: if (hosts.charAt(0) == '@') {
101: hostPattern = loadUrls(props, prefix, hosts
102: .substring(1));
103: } else {
104: hostPattern = new Regexp(hosts);
105: }
106: } catch (Exception e) {
107: server.log(Server.LOG_DIAGNOSTIC, prefix,
108: "error in hosts: " + e.getMessage());
109: }
110: return true;
111: }
112:
113: public boolean respond(Request request) throws IOException {
114: if ((hostPattern != null)
115: && hostPattern.match(request.url, (int[]) null)) {
116: request.log(Server.LOG_DIAGNOSTIC, prefix, request.url);
117: return sendReplacementImage(request);
118: }
119: return false;
120: }
121:
122: public Regexp loadUrls(Properties props, String prefix, String file) {
123: StringBuffer sb = new StringBuffer();
124: try {
125: HttpInputStream in = new HttpInputStream(ResourceHandler
126: .getResourceStream(props, prefix, file));
127:
128: String line;
129: while ((line = in.readLine()) != null) {
130: line = line.trim();
131: if ((line.length() == 0) || (line.charAt(0) == '#')) {
132: continue;
133: }
134: sb.append(line).append('|');
135: }
136: sb.setLength(sb.length() - 1);
137: return new Regexp(sb.toString());
138: } catch (Exception e) {
139: return null;
140: }
141: }
142:
143: public boolean sendReplacementImage(Request request)
144: throws IOException {
145: request.sendResponse(image, type);
146: return true;
147: }
148: }
|