001: /*
002: * PlainFilter.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.9
024: * Created by suhler on 99/07/29
025: * Last modified by suhler on 00/12/11 13:25:51
026: */
027:
028: package sunlabs.brazil.filter;
029:
030: import java.util.StringTokenizer;
031: import sunlabs.brazil.server.Request;
032: import sunlabs.brazil.server.Server;
033: import sunlabs.brazil.util.http.MimeHeaders;
034:
035: /**
036: * Filter to turn text/plain into html. This allows plain text to
037: * be processed by other filters that only deal with html.
038: * <p>
039: * The following server properties are used:
040: * <dl class=props>
041: * <dt>template <dd> The string to use as an html template. The string
042: * should contain a single "%", which is replaced by the
043: * text/plain content.
044: * </dl>
045: *
046: * @author Stephen Uhler
047: * @version %V% PlainFilter.java
048: */
049:
050: public class PlainFilter implements Filter {
051: String template1; // html wrapper template (prefix)
052: String template2; // html wrapper template (postfix)
053:
054: public boolean init(Server server, String prefix) {
055: String template = server.props.getProperty(prefix + "template",
056: "<title>text document</title><body bgcolor=white><pre>"
057: + "%</pre></body>");
058: int index = template.indexOf("%");
059: if (index > 0) {
060: template1 = template.substring(0, index);
061: template2 = template.substring(index + 1);
062: return true;
063: } else {
064: return false;
065: }
066: }
067:
068: /**
069: * This is the request object before the content was fetched
070: */
071:
072: public boolean respond(Request request) {
073: return false;
074: }
075:
076: /**
077: * Only filter text/plain documents
078: */
079:
080: public boolean shouldFilter(Request request, MimeHeaders headers) {
081: String type = headers.get("content-type");
082: return (type != null && type.startsWith("text/plain"));
083: }
084:
085: /**
086: * Wrap html around text/plain, converting it to html.
087: * Change the content-type to text/html.
088: */
089:
090: public byte[] filter(Request request, MimeHeaders headers,
091: byte[] content) {
092: String result = template1 + htmlIfy(new String(content))
093: + template2;
094: headers.put("content-type", "text/html");
095: return result.getBytes();
096: }
097:
098: /**
099: * protect html special characters
100: * Protect the characters: "&" "<" and ">".
101: * @param text The plain text to protect
102: */
103:
104: private static String htmlIfy(String text) {
105: StringBuffer result = new StringBuffer();
106: StringTokenizer st = new StringTokenizer(text, "&<>", true);
107: while (st.hasMoreTokens()) {
108: String s = st.nextToken();
109: if (s.equals("&")) {
110: result.append("&");
111: } else if (s.equals("<")) {
112: result.append("<");
113: } else if (s.equals("?")) {
114: result.append(">");
115: } else {
116: result.append(s);
117: }
118: }
119: return result.toString();
120: }
121: }
|