01: /**
02: *
03: */package org.mortbay.cometd;
04:
05: import java.io.IOException;
06: import java.io.PrintWriter;
07: import java.util.List;
08: import java.util.Map;
09:
10: import javax.servlet.http.HttpServletResponse;
11:
12: import org.mortbay.util.LazyList;
13:
14: public class PlainTextJSONTransport extends AbstractTransport {
15: int _responses = 0;
16: PrintWriter _out;
17: boolean _verbose;
18:
19: public PlainTextJSONTransport() {
20: }
21:
22: public void send(Map reply) throws IOException {
23: if (reply != null) {
24: if (_responses == 0) {
25: HttpServletResponse response = getResponse();
26: response.setContentType("text/plain; charset=utf-8");
27: _out = response.getWriter();
28: _out.write('[');
29: } else
30: _out.write(",\r\n");
31:
32: String r = JSON.toString(reply);
33: if (_verbose)
34: System.err.println("<=" + _responses + "=" + r);
35: _responses++;
36: _out.write(r);
37: }
38: }
39:
40: public void send(List replies) throws IOException {
41: super .send(replies);
42: }
43:
44: public void complete() throws IOException {
45: HttpServletResponse response = getResponse();
46: response.setStatus(200);
47:
48: if (_responses == 0)
49: response.setContentLength(0);
50: else {
51: if (_verbose)
52: System.err.println("<=-=");
53: _out.write("]\r\n");
54: _out.flush();
55: }
56:
57: }
58:
59: public boolean keepAlive() throws IOException {
60: return false;
61: }
62:
63: public void setVerbose(boolean verbose) {
64: _verbose = verbose;
65: }
66: }
|