- From: Dave Makower <davemak@pencom.com>
- Date: Fri, 8 Nov 1996 17:14:02 -0500
- To: www-jigsaw@w3.org
Below, you will find some source code (significantly simplified)
from a project I'm working on. I need to have a method, other
than get(), which returns an InputStream (sometimes created by
an HtmlGenerator, but not always) that is converted into a Reply
by get().
The problem is that when I put this Resource() into Jigsaw,
it stalls before the output is completed. That is, the entire
page content gets transmitted, but the connection is not closed.
If I hit the browser's STOP button, then I get "Transfer
Interrupted!"
I think that there may be something wrong with the method
HtmlGenerator.getInputStream(), or with Reply.setStream()...
either that, or perhaps I am using them incorrectly.
Try it and see what I mean.
Any suggestions?
//------------------- StreamResource.java -------------------
// Copyright (c) 1996 Pencom Systems, Inc.
import w3c.jigsaw.html.*;
import w3c.www.http.*;
import java.io.*;
public class StreamResource extends FilteredResource {
public StreamResource() {
super();
}
public InputStream makeStream() {
StringBuffer contentBuf = new StringBuffer();
contentBuf.append("What I am doing here may seem a little strange.\n");
contentBuf.append("In fact, the real version reads its data from an external source.\n");
contentBuf.append("Then it reads it in a little at a time, ");
contentBuf.append("appending it to a StringBuffer.\n");
contentBuf.append("For reasons particular to the project I'm working on,\n");
contentBuf.append("I need to have this method that returns an InputStream,");
contentBuf.append("rather than having the get() method create an HTML generator.");
contentBuf.append("Still, one would think this method should work, right?");
contentBuf.append("I'd rather use Jigsaw's HTML generator than take care of all\n");
contentBuf.append("of the details myself.\n\n");
contentBuf.append("Note that this content might contain HTML tags, like\n");
contentBuf.append("<HTML> or <TABLE>, so it needs to be escaped.\n\n");
String contentString = contentBuf.toString();
// Emit preliminary HTML
HtmlGenerator gen = new HtmlGenerator("Who cares what the title is?");
gen.emitBODYTag(false);
gen.append("<BODY BGCOLOR=\"#FFFFFF\">\n");
gen.append("<H1 ALIGN=\"CENTER\">\n");
gen.append("Why doesn't this work?");
gen.append("</H1>\n");
gen.append("<HR>\n");
gen.append("<PRE>\n");
// Emit the content
gen.appendAndEscape(contentString);
// Emit tail HTML
gen.append("</PRE>\n");
gen.append("<HR>\n");
gen.append("</BODY>\n");
// gen.close(); // redundant
// Return an InputStream from the HTML generator
return gen.getInputStream();
}
public Reply get(Request request) {
Reply reply = request.makeReply(HTTP.OK);
InputStream content = null;
// Present the content
content = makeStream();
// Put the content in the Reply object
if (content != null) {
reply.setStream(content);
} else {
reply.setStatus(HTTP.NO_CONTENT);
HtmlGenerator gen = new HtmlGenerator("Not Found");
gen.append("<H1 ALIGN=\"CENTER\">\n");
gen.append("404 - Not Found\n");
gen.append("</H1>\n");
gen.append("<HR>\n");
gen.append("The resource" + getURL() + "has no content.\n");
gen.append("<P>");
if (request != null) {
gen.append("Referring URL: ");
gen.append("<A HREF=\"" + request.getReferer() + "\">");
gen.append(request.getReferer());
gen.append("</A>\n");
gen.append("<P>");
}
gen.close();
reply.setStream(gen);
}
return reply;
}
}
Received on Friday, 8 November 1996 17:12:47 UTC