View Javadoc

1   // ========================================================================
2   // Copyright 2007-2008 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // Licensed under the Apache License, Version 2.0 (the "License");
5   // you may not use this file except in compliance with the License.
6   // You may obtain a copy of the License at 
7   // http://www.apache.org/licenses/LICENSE-2.0
8   // Unless required by applicable law or agreed to in writing, software
9   // distributed under the License is distributed on an "AS IS" BASIS,
10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  // See the License for the specific language governing permissions and
12  // limitations under the License.
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              // subscribe("/**","monitorVerbose");
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          public void monitorVerbose(Client client, Message message)
99          {
100             System.err.println(message);
101             try 
102             {
103                 Thread.sleep(5000);
104             }
105             catch(Exception e)
106             {
107                 Log.warn(e);
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 }