KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > handler > IQLastActivityHandler


1 package org.jivesoftware.messenger.handler;
2
3 import org.dom4j.Element;
4 import org.jivesoftware.messenger.IQHandlerInfo;
5 import org.jivesoftware.messenger.PresenceManager;
6 import org.jivesoftware.messenger.XMPPServer;
7 import org.jivesoftware.messenger.auth.UnauthorizedException;
8 import org.jivesoftware.messenger.disco.ServerFeaturesProvider;
9 import org.jivesoftware.messenger.roster.RosterItem;
10 import org.jivesoftware.messenger.user.User;
11 import org.jivesoftware.messenger.user.UserManager;
12 import org.jivesoftware.messenger.user.UserNotFoundException;
13 import org.xmpp.packet.IQ;
14 import org.xmpp.packet.PacketError;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 /**
20  * Implements the TYPE_IQ jabber:iq:last protocol (last activity). Allows users to find out
21  * the number of seconds another user has been offline. This information is only available to
22  * those users that already subscribed to the users presence. Otherwhise, a <tt>forbidden</tt>
23  * error will be returned.
24  *
25  * @author Gaston Dombiak
26  */

27 public class IQLastActivityHandler extends IQHandler implements ServerFeaturesProvider {
28
29     private IQHandlerInfo info;
30     private PresenceManager presenceManager;
31
32     public IQLastActivityHandler() {
33         super("XMPP Last Activity Handler");
34         info = new IQHandlerInfo("query", "jabber:iq:last");
35     }
36
37     public IQ handleIQ(IQ packet) throws UnauthorizedException {
38         IQ reply = IQ.createResultIQ(packet);
39         Element lastActivity = reply.setChildElement("query", "jabber:iq:last");
40         String JavaDoc sender = packet.getFrom().getNode();
41         String JavaDoc username = packet.getTo().getNode();
42
43         // Check if any of the usernames is null
44
if (sender == null || username == null) {
45             reply.setError(PacketError.Condition.forbidden);
46             return reply;
47         }
48
49         try {
50             User user = UserManager.getInstance().getUser(username);
51             RosterItem item = user.getRoster().getRosterItem(packet.getFrom());
52             // Check that the user requesting this information is subscribed to the user's presence
53
if (item.getSubStatus() == RosterItem.SUB_FROM ||
54                     item.getSubStatus() == RosterItem.SUB_BOTH) {
55                 if (sessionManager.getSessions(username).isEmpty()) {
56                     // The user is offline so answer the user's "last available time and the
57
// status message of the last unavailable presence received from the user"
58
long lastActivityTime = presenceManager.getLastActivity(user);
59                     lastActivity.addAttribute("seconds", String.valueOf(lastActivityTime));
60                     String JavaDoc lastStatus = presenceManager.getLastPresenceStatus(user);
61                     if (lastStatus != null && lastStatus.length() > 0) {
62                         lastActivity.setText(lastStatus);
63                     }
64                 }
65                 else {
66                     // The user is online so answer seconds=0
67
lastActivity.addAttribute("seconds", "0");
68                 }
69             }
70             else {
71                 reply.setError(PacketError.Condition.forbidden);
72             }
73         }
74         catch (UserNotFoundException e) {
75             reply.setError(PacketError.Condition.forbidden);
76         }
77         return reply;
78     }
79
80     public IQHandlerInfo getInfo() {
81         return info;
82     }
83
84     public Iterator JavaDoc getFeatures() {
85         ArrayList JavaDoc features = new ArrayList JavaDoc();
86         features.add("jabber:iq:last");
87         return features.iterator();
88     }
89
90     public void initialize(XMPPServer server) {
91         super.initialize(server);
92         presenceManager = server.getPresenceManager();
93     }
94 }
Popular Tags