/*
* PlainFilter.java
*
* Brazil project web application Framework,
* export version: 1.1
* Copyright (c) 1999-2000 Sun Microsystems, Inc.
*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License Version
* 1.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is included as the file "license.terms",
* and also available at http://www.sun.com/
*
* The Original Code is from:
* Brazil project web application Framework release 1.1.
* The Initial Developer of the Original Code is: suhler.
* Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
* All Rights Reserved.
*
* Contributor(s): suhler.
*
* Version: 1.9
* Created by suhler on 99/07/29
* Last modified by suhler on 00/12/11 13:25:51
*/
package sunlabs.brazil.filter;
import java.util.StringTokenizer;
import sunlabs.brazil.server.Request;
import sunlabs.brazil.server.Server;
import sunlabs.brazil.util.http.MimeHeaders;
/**
* Filter to turn text/plain into html. This allows plain text to
* be processed by other filters that only deal with html.
* <p>
* The following server properties are used:
* <dl class=props>
* <dt>template <dd> The string to use as an html template. The string
* should contain a single "%", which is replaced by the
* text/plain content.
* </dl>
*
* @author Stephen Uhler
* @version %V% PlainFilter.java
*/
public class PlainFilter implements Filter {
String template1; // html wrapper template (prefix)
String template2; // html wrapper template (postfix)
public boolean
init(Server server, String prefix) {
String template = server.props.getProperty(prefix + "template",
"<title>text document</title><body bgcolor=white><pre>" +
"%</pre></body>");
int index = template.indexOf("%");
if (index > 0) {
template1 = template.substring(0,index);
template2 = template.substring(index+1);
return true;
} else {
return false;
}
}
/**
* This is the request object before the content was fetched
*/
public boolean respond(Request request) {
return false;
}
/**
* Only filter text/plain documents
*/
public boolean
shouldFilter(Request request, MimeHeaders headers) {
String type = headers.get("content-type");
return (type != null && type.startsWith("text/plain"));
}
/**
* Wrap html around text/plain, converting it to html.
* Change the content-type to text/html.
*/
public byte[]
filter(Request request, MimeHeaders headers, byte[] content) {
String result = template1 + htmlIfy(new String(content)) +
template2;
headers.put("content-type", "text/html");
return result.getBytes();
}
/**
* protect html special characters
* Protect the characters: "&" "<" and ">".
* @param text The plain text to protect
*/
private static String
htmlIfy(String text) {
StringBuffer result = new StringBuffer();
StringTokenizer st = new StringTokenizer(text, "&<>", true);
while(st.hasMoreTokens()) {
String s = st.nextToken();
if (s.equals("&")) {
result.append("&");
} else if (s.equals("<")) {
result.append("<");
} else if (s.equals("?")) {
result.append(">");
} else {
result.append(s);
}
}
return result.toString();
}
}
|