KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > core > BrowserAdapter


1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2
// Distributable under LGPL license. See terms of license at gnu.org.
3

4 package nl.justobjects.pushlet.core;
5
6 import nl.justobjects.pushlet.util.Log;
7
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Generic implementation of ClientAdapter for browser clients.
15  *
16  * @version $Id: BrowserAdapter.java,v 1.3 2005/02/28 12:45:59 justb Exp $
17  * @author Just van den Broecke - Just Objects &copy;
18  */

19 public class BrowserAdapter implements ClientAdapter, Protocol {
20
21     public static final String JavaDoc START_DOCUMENT =
22             "<html><head><meta http-equiv=\"Pragma\" content=\"no-cache\"><meta http-equiv=\"Expires\" content=\"Tue, 31 Dec 1997 23:59:59 GMT\"></head>"
23             + "<body>"
24             + "\n<script language=\"JavaScript\"> var url=\" \"; \nfunction refresh() { document.location.href=url; }</script>";
25     public static final String JavaDoc END_DOCUMENT = "</body></html>";
26
27     private PrintWriter JavaDoc servletOut;
28     private HttpServletResponse JavaDoc servletRsp;
29     private int bytesSent;
30
31     /** Constructor. */
32     public BrowserAdapter(HttpServletResponse JavaDoc aServletResponse) {
33         servletRsp = aServletResponse;
34     }
35
36     /** Generic init. */
37     public void start() throws IOException JavaDoc {
38         // Keep servlet request/response objects until page ends in stop()
39
// Content type as HTML
40
servletRsp.setStatus(HttpServletResponse.SC_OK);
41         servletRsp.setContentType("text/html");
42
43         // http://www.junlu.com/msg/45902.html
44
// Log.debug("bufsize=" + aRsp.getBufferSize());
45
servletOut = servletRsp.getWriter();
46         send(START_DOCUMENT);
47     }
48
49     /** Push Event to client. */
50     public void push(Event anEvent) throws IOException JavaDoc {
51         Log.debug("BCA event=" + anEvent.toXML());
52
53         // Check if we should refresh
54
if (anEvent.getEventType().equals(Protocol.E_REFRESH)) {
55             // Append refresh and tail of HTML document
56
// Construct the JS callback line to be sent as last line of doc.
57
// This will refresh the request using the unique id to determine
58
// the subscriber instance on the server. The client will wait for
59
// a number of milliseconds.
60
long refreshWaitMillis = Long.parseLong(anEvent.getField(P_WAIT));
61
62             // Create servlet request for requesting next events (refresh)
63
String JavaDoc url = anEvent.getField(P_URL);
64             String JavaDoc jsRefreshTrigger = "\n<script language=\"JavaScript\">url=\"" + url + "\";\n setTimeout(\"refresh()\", " + refreshWaitMillis + ");\n</script>";
65
66
67             send(jsRefreshTrigger + END_DOCUMENT);
68         } else {
69             send(event2JavaScript(anEvent));
70         }
71     }
72
73     /** End HTML page in client browser. */
74     public void stop() {
75         // To be garbage collected if adapter remains active
76
servletOut = null;
77     }
78
79     /** Send any string to browser. */
80     protected void send(String JavaDoc s) throws IOException JavaDoc {
81         // Send string to browser.
82
// Log.debug("Adapter: sending: " + s);
83
if (servletOut == null) {
84             throw new IOException JavaDoc("Client adapter was stopped");
85         }
86
87         servletOut.print(s);
88
89         servletOut.flush();
90
91         // Note: this doesn't seem to have effect
92
// in Tomcat 4/5 if the client already disconnected.
93
servletRsp.flushBuffer();
94
95         bytesSent += s.length();
96         Log.debug("bytesSent= " + bytesSent);
97         // Log.debug("BCA sent event: " + s);
98
}
99
100     /** Converts the Java Event to a JavaScript function call in browser page. */
101     protected String JavaDoc event2JavaScript(Event event) throws IOException JavaDoc {
102
103         // Convert the event to a comma-separated string.
104
String JavaDoc jsArgs = "";
105         for (Iterator JavaDoc iter = event.getFieldNames(); iter.hasNext();) {
106             String JavaDoc name = (String JavaDoc) iter.next();
107             String JavaDoc value = event.getField(name);
108             String JavaDoc nextArgument = (jsArgs.equals("") ? "" : ",") + "'" + name + "'" + ", \"" + value + "\"";
109             jsArgs += nextArgument;
110         }
111
112         // Construct and return the function call */
113
return "<script language=\"JavaScript\">parent.push(" + jsArgs + ");</script>";
114     }
115
116 }
117
118 /*
119  * $Log: BrowserAdapter.java,v $
120  * Revision 1.3 2005/02/28 12:45:59 justb
121  * introduced Command class
122  *
123  * Revision 1.2 2005/02/21 11:50:44 justb
124  * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
125  *
126  * Revision 1.1 2005/02/18 10:07:23 justb
127  * many renamings of classes (make names compact)
128  *
129  * Revision 1.12 2005/02/15 13:30:23 justb
130  * changes for Tomcat buffering (now working in tc4 and 5.0)
131  *
132  * Revision 1.11 2005/01/24 22:45:58 justb
133  * getting safari to work
134  *
135  * Revision 1.10 2005/01/18 16:46:27 justb
136  * buffer size setting ignored by tomcat workings
137  *
138  * Revision 1.9 2004/10/24 12:58:18 justb
139  * revised client and test classes for new protocol
140  *
141  * Revision 1.8 2004/09/20 22:01:38 justb
142  * more changes for new protocol
143  *
144  * Revision 1.7 2004/09/03 22:35:37 justb
145  * Almost complete rewrite, just checking in now
146  *
147  * Revision 1.6 2004/08/15 16:00:15 justb
148  * enhancements to pull mode
149  *
150  * Revision 1.5 2004/08/13 23:36:05 justb
151  * rewrite of Pullet into Pushlet "pull" mode
152  *
153  * Revision 1.4 2003/08/15 08:37:40 justb
154  * fix/add Copyright+LGPL file headers and footers
155  *
156  * Revision 1.3 2003/08/12 09:57:05 justb
157  * replaced all print statements to Log.*() calls
158  *
159  * Revision 1.2 2003/05/18 16:15:07 justb
160  * support for XML encoded Events
161  *
162  * Revision 1.1.1.1 2002/09/24 21:02:30 justb
163  * import to sourceforge
164  *
165  * Revision 1.1.1.1 2002/09/20 22:48:17 justb
166  * import to SF
167  *
168  * Revision 1.1.1.1 2002/09/20 14:19:02 justb
169  * first import into SF
170  *
171  * Revision 1.5 2002/04/15 20:42:41 just
172  * reformatting and renaming GuardedQueue to EventQueue
173  *
174  * Revision 1.4 2000/12/27 22:39:35 just
175  * no message
176  *
177  * Revision 1.3 2000/10/30 14:15:47 just
178  * no message
179  *
180  *
181  */

182
183
Popular Tags