001: package org.mortbay.cometd;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.List;
006: import java.util.Map;
007:
008: import javax.servlet.http.HttpServletResponse;
009:
010: public class IFrameTransport extends AbstractTransport {
011: PrintWriter _writer;
012: boolean _initialized = false;
013:
014: public void setResponse(HttpServletResponse response)
015: throws IOException {
016: _initialized = false;
017: super .setResponse(response);
018: }
019:
020: private void init(Map reply) throws IOException {
021: if (_initialized)
022: return;
023: _initialized = true;
024:
025: String channel = (String) reply.get("channel");
026: if (!"/meta/connect".equals(channel)
027: && !"/meta/reconnect".equals(channel))
028: reply = null;
029:
030: getResponse().setContentType("text/html; charset=UTF-8");
031: _writer = getResponse().getWriter();
032:
033: _writer
034: .println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
035: _writer
036: .println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">");
037: _writer.println("<head>");
038: _writer
039: .println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></meta>");
040: _writer.println("<title>cometd: over jetty</title>");
041:
042: if (reply != null) {
043: _writer.write("<script type=\"text/javascript\">");
044: _writer.write("window.parent.cometd.deliver([");
045: _writer.write(JSON.toString(reply));
046: _writer.write("]);");
047: _writer.write("</script>");
048: }
049: _writer.println("</head>");
050: _writer
051: .println("<body onload=\"window.parent.cometd.tunnelCollapse();\">");
052: _writer.flush();
053: }
054:
055: public void send(Map reply) throws IOException {
056: if (!_initialized)
057: init(reply);
058: else {
059: _writer.write("<br /><script type=\"text/javascript\">");
060: _writer.write("window.parent.cometd.deliver([");
061: _writer.write(JSON.toString(reply));
062: _writer.write("]);");
063: _writer.write("</script><br/>");
064: for (int i = 0; i < 16; i++)
065: _writer
066: .write(" ");
067: _writer.write("<br/>");
068: _writer.flush();
069: }
070: }
071:
072: public void send(List replies) throws IOException {
073: if (replies == null)
074: return;
075:
076: int m = 0;
077:
078: if (!_initialized) {
079: if (replies.size() > 0)
080: init((Map) replies.get(m++));
081: else
082: init(null);
083: }
084:
085: if (replies.size() > m) {
086: _writer.write("<br /><script type=\"text/javascript\">");
087: _writer.write("window.parent.cometd.deliver([");
088:
089: for (int i = m; i < replies.size(); i++) {
090:
091: // encode((Map)replies.get(i));
092: // do multiple messages in one deliver
093: _writer.write(JSON.toString(replies.get(i)));
094: if (i != replies.size() - 1)
095: _writer.write(", ");
096: }
097: _writer.write("]);");
098: _writer.write("</script><br/>");
099: for (int i = 0; i < 16; i++)
100: _writer
101: .write(" ");
102: _writer.write("<br/>");
103: _writer.flush();
104: }
105: }
106:
107: public void complete() throws IOException {
108: // _writer.write("<script
109: // type=\"text/javascript\">window.parent.cometd.tunnelCollapse();");
110: // _writer.write("</script>");
111: _writer.write("</body>");
112: _writer.write("</html>");
113: _writer.flush();
114: }
115:
116: public boolean keepAlive() throws IOException {
117: return false;
118: }
119:
120: public void initTunnel(HttpServletResponse response)
121: throws IOException {
122: response.setContentType("text/html; charset=utf-8");
123: _writer = response.getWriter();
124: _writer
125: .println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
126: _writer
127: .println(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
128: _writer.println("");
129: _writer
130: .println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">");
131: _writer.println(" <head>");
132: _writer
133: .println(" <title>cometd: The Long Tail of Comet</title>");
134: _writer
135: .println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></meta>");
136: _writer.println(" <script type=\"text/javascript\">");
137: _writer
138: .println(" // window.parent.dojo.debug(\"tunnelInit\");");
139: _writer.println(" var noInit = false;");
140: _writer.println(" var domain = \"\";");
141: _writer.println(" function init(){");
142: _writer.println(" var sparams = document.location.search;");
143: _writer.println(" if(sparams.length >= 0){");
144: _writer.println(" if(sparams.charAt(0) == \"?\"){");
145: _writer.println(" sparams = sparams.substring(1);");
146: _writer.println(" }");
147: _writer
148: .println(" var ss = (sparams.indexOf(\"&\") >= 0) ? \"&\" : \"&\";");
149: _writer.println(" sparams = sparams.split(ss);");
150: _writer.println(" for(var x=0; x<sparams.length; x++){");
151: _writer.println(" var tp = sparams[x].split(\"=\");");
152: _writer
153: .println(" if(typeof window[tp[0]] != \"undefined\"){");
154: _writer
155: .println(" window[tp[0]] = ((tp[1]==\"true\")||(tp[1]==\"false\")) ? eval(tp[1]) : tp[1];");
156: _writer.println(" }");
157: _writer.println(" }");
158: _writer.println(" }");
159: _writer.println(" if(noInit){ return; }");
160: _writer.println(" /*");
161: _writer.println(" if(domain.length > 0){");
162: _writer.println(" document.domain = domain;");
163: _writer.println(" }");
164: _writer.println(" */");
165: _writer.println(" if(window.parent != window){");
166: _writer.println(" //Notify parent that we are loaded.");
167: _writer
168: .println(" window.parent.cometd.tunnelInit(window.location, document.domain);");
169: _writer.println(" }");
170: _writer.println(" }");
171: _writer.println(" </script>");
172: _writer.println(" </head>");
173: _writer
174: .println(" <body onload=\"try{ init(); }catch(e){ alert(e); }\">");
175: _writer.println(" <h4>cometd: The Long Tail of Comet</h4>");
176: _writer.println(" </body>");
177: _writer.println("</html>");
178:
179: }
180:
181: }
|