View Javadoc

1   // ========================================================================
2   // Copyright 2006 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.mortbay.cometd.ext;
16  
17  import java.util.TimeZone;
18  
19  import org.cometd.Client;
20  import org.cometd.Extension;
21  import org.cometd.Message;
22  import org.mortbay.cometd.AbstractBayeux;
23  import org.mortbay.util.DateCache;
24  
25  public class TimestampExtension implements Extension
26  {
27      private final DateCache _dateCache;
28  
29      public TimestampExtension()
30      {
31          _dateCache=new DateCache();
32          _dateCache.setTimeZone(TimeZone.getTimeZone("UTC"));
33      }
34  
35      public TimestampExtension(String format)
36      {
37          _dateCache=new DateCache(format);
38          _dateCache.setTimeZone(TimeZone.getTimeZone("UTC"));
39      }
40  
41      public TimestampExtension(String format, TimeZone tz)
42      {
43          _dateCache=new DateCache(format);
44          _dateCache.setTimeZone(tz);
45      }
46  
47      public Message rcv(Client from, Message message)
48      {
49          return message;
50      }
51  
52      public Message rcvMeta(Client from, Message message)
53      {
54          return message;
55      }
56  
57      public Message send(Client from, Message message)
58      {
59          message.put(AbstractBayeux.TIMESTAMP_FIELD,_dateCache.format(System.currentTimeMillis()));
60          return message;
61      }
62  
63      public Message sendMeta(Client from, Message message)
64      {
65          message.put(AbstractBayeux.TIMESTAMP_FIELD,_dateCache.format(System.currentTimeMillis()));
66          return message;
67      }
68  
69  }