001: /*
002: * ProxyPropertiesHandler.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.17
024: * Created by suhler on 98/09/18
025: * Last modified by suhler on 00/12/11 13:28:37
026: */
027:
028: package sunlabs.brazil.handler;
029:
030: import java.io.ByteArrayInputStream;
031: import java.io.IOException;
032: import sunlabs.brazil.server.Handler;
033: import sunlabs.brazil.server.Request;
034: import sunlabs.brazil.server.Server;
035: import sunlabs.brazil.util.http.MimeHeaders;
036: import java.util.Enumeration;
037: import java.util.Properties;
038:
039: /**
040: * Obtain properties format content from remote websites, and
041: * add it to the current request properties.
042: * Many of the handlers
043: * are designed to produce side effects, by inserting values into the
044: * request properties (see {@link PropertiesHandler}).
045: * If they are instead configured to produce the properties
046: * in java properties format, then this handler
047: * will read their output, and place the result in the request object on
048: * their behalf. This capability allows certain handlers to be run on
049: * other web sites, yet behave as if they are in the handler chain.
050: *
051: * The following request properties are used:
052: * <dl class=props>
053: * <dt>type <dd> The document type for files to process as
054: * java properties (defaults to text/plain)
055: * <dt>prepend <dd> The prefix that should be prepended to each property
056: * before it is inserted into the request properties
057: * <dt>url <dd> The url that should be used to fetch the remote content.
058: * If not specified, the curent url is used instead.
059: * <br>NOTE: This capability should be generalized
060: * </dl>
061: *
062: * @author Stephen Uhler
063: * @version 1.17, 00/12/11
064: */
065:
066: public class ProxyPropertiesHandler extends GenericProxyHandler
067: implements Handler {
068:
069: String type; // document type for filtering
070: String prepend = null; // prepend all properties with this
071: String mapUrl;
072:
073: public boolean init(Server server, String prefix) {
074: type = server.props.getProperty(prefix + "type", "text/plain");
075: server.log(Server.LOG_DIAGNOSTIC, prefix, "processing type: "
076: + type);
077: return super .init(server, prefix);
078: }
079:
080: public boolean respond(Request request) throws IOException {
081: request.log(Server.LOG_DIAGNOSTIC, prefix,
082: "Calling ProxyProps handler respond");
083: prepend = request.props.getProperty(prefix + "prepend");
084: mapUrl = request.props.getProperty(prefix + "url");
085: boolean result;
086: if (mapUrl != null) {
087: String save = request.url;
088: request.url = mapUrl;
089: request.log(Server.LOG_DIAGNOSTIC, prefix + "Mapping "
090: + save + " -> " + mapUrl);
091: result = super .respond(request);
092: request.url = save;
093: } else {
094: result = super .respond(request);
095: }
096: request.log(Server.LOG_DIAGNOSTIC, prefix, "PRoxy done: "
097: + result);
098: return result;
099: }
100:
101: /**
102: * See if the content needs to be filtered
103: * Return "true" if "modifyContent" should be called
104: * @param headers mime headers for data to proxy
105: */
106:
107: protected boolean shouldFilter(MimeHeaders headers) {
108: String header = headers.get("Content-Type");
109: return (header != null && header.equals(type));
110: }
111:
112: public byte[] modifyContent(Request request, byte[] content) {
113:
114: /*
115: * Get snarf into byte input array, then do a url.props.load on it.
116: * If its not a properties object, just return it.
117: * How do we know??
118: */
119:
120: ByteArrayInputStream in = new ByteArrayInputStream(content);
121: Properties p;
122: if (prepend != null) {
123: p = new Properties();
124: } else {
125: p = request.props;
126: }
127: try {
128: p.load(in);
129: request.log(Server.LOG_INFORMATIONAL, prefix +
130: ".. Got remote properties");
131: } catch (java.io.IOException e) {
132: request.props.put("Error",e.toString());
133: }
134:
135: /* add the prefix, if any (yuk!) */
136:
137: if (prepend != null) {
138: Properties to = request.props;
139: request.log(Server.LOG_INFORMATIONAL, prefix +
140: ".. prepending: " + prepend);
141: Enumeration enum = p.propertyNames();
142: while(enum.hasMoreElements()) {
143: String key = (String) enum.nextElement();
144: to.put(prepend + key, p.getProperty(key));
145: }
146: p = null;
147: }
148: try {
149: in.close();
150: } catch (java.io.IOException e) {
151: request.log(Server.LOG_WARNING, prefix +
152: ".. Close failed!! reading props: " + e);
153: }
154: return null;
155: }
156: }
|