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