1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.cometd.demo;
16
17 import java.io.IOException;
18 import javax.servlet.GenericServlet;
19 import javax.servlet.ServletException;
20 import javax.servlet.ServletRequest;
21 import javax.servlet.ServletResponse;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.cometd.Bayeux;
25 import org.cometd.Client;
26 import org.cometd.Message;
27 import org.mortbay.cometd.BayeuxService;
28 import org.mortbay.cometd.ext.AcknowledgedMessagesExtension;
29 import org.mortbay.cometd.ext.TimesyncExtension;
30 import org.mortbay.log.Log;
31
32 public class CometdDemoServlet extends GenericServlet
33 {
34 public CometdDemoServlet()
35 {
36 }
37
38
39 @Override
40 public void init() throws ServletException
41 {
42 super.init();
43 Bayeux bayeux=(Bayeux)getServletContext().getAttribute(Bayeux.ATTRIBUTE);
44 new EchoRPC(bayeux);
45 new Monitor(bayeux);
46 new ChatService(bayeux);
47 bayeux.addExtension(new TimesyncExtension());
48 bayeux.addExtension(new AcknowledgedMessagesExtension());
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 }
64
65 public static class EchoRPC extends BayeuxService
66 {
67 public EchoRPC(Bayeux bayeux)
68 {
69 super(bayeux,"echo");
70 subscribe("/service/echo","doEcho");
71 }
72
73 public Object doEcho(Client client, Object data)
74 {
75 Log.info("ECHO from "+client+" "+data);
76 return data;
77 }
78 }
79
80 public static class Monitor extends BayeuxService
81 {
82 public Monitor(Bayeux bayeux)
83 {
84 super(bayeux,"monitor");
85 subscribe("/meta/subscribe","monitorSubscribe");
86 subscribe("/meta/unsubscribe","monitorUnsubscribe");
87 subscribe("/meta/*","monitorMeta");
88
89 }
90
91 public void monitorSubscribe(Client client, Message message)
92 {
93 Log.info("Subscribe from "+client+" for "+message.get(Bayeux.SUBSCRIPTION_FIELD));
94 }
95
96 public void monitorUnsubscribe(Client client, Message message)
97 {
98 Log.info("Unsubscribe from "+client+" for "+message.get(Bayeux.SUBSCRIPTION_FIELD));
99 }
100
101 public void monitorMeta(Client client, Message message)
102 {
103 if (Log.isDebugEnabled())
104 Log.debug(message.toString());
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 }
122
123 @Override
124 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
125 {
126 ((HttpServletResponse)res).sendError(503);
127 }
128 }