KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > plugin > PresencePlugin


1 /**
2  * $RCSfile: PresencePlugin.java,v $
3  * $Revision: 1.4 $
4  * $Date: 2005/07/28 22:19:16 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.plugin;
13
14 import org.jivesoftware.messenger.container.Plugin;
15 import org.jivesoftware.messenger.container.PluginManager;
16 import org.jivesoftware.messenger.user.UserManager;
17 import org.jivesoftware.messenger.user.UserNotFoundException;
18 import org.jivesoftware.messenger.user.User;
19 import org.jivesoftware.messenger.PresenceManager;
20 import org.jivesoftware.messenger.XMPPServer;
21 import org.jivesoftware.util.JiveGlobals;
22 import org.xmpp.packet.Presence;
23 import org.xmpp.packet.JID;
24
25 import java.io.File JavaDoc;
26
27 /**
28  * Plugin that includes a servlet that provides information about the presence type of the
29  * users in the server. For security reasons, the XMPP spec does not allow anyone to see
30  * the presence of any user. Only the users that are subscribed to the presence of other
31  * users may see their presences.<p>
32  *
33  * However, in order to make the servlet more useful it is possible to configure this plugin
34  * so that anyone or only the users that are subscribed to a user presence may see the presence
35  * of other users.<p>
36  *
37  * Currently, the servlet provides information about user presences in two formats. In XML format
38  * or using images.
39  *
40  * @author Gaston Dombiak
41  */

42 public class PresencePlugin implements Plugin {
43
44     private UserManager userManager;
45     private PresenceManager presenceManager;
46     private String JavaDoc hostname;
47
48     public void initializePlugin(PluginManager manager, File JavaDoc pluginDirectory) {
49         XMPPServer server = XMPPServer.getInstance();
50         userManager = server.getUserManager();
51         presenceManager = server.getPresenceManager();
52         hostname = server.getServerInfo().getName();
53     }
54
55     public void destroyPlugin() {
56         userManager = null;
57         presenceManager = null;
58     }
59
60     /**
61      * Returns true if anyone is allowed to see the presence of other users. False means that
62      * only the users that are subscribed to a user presence will be able to get information
63      * about the user. By default, presence information is not publicly available.
64      *
65      * @return true if anyone is allowed to see the presence of other users.
66      */

67     public boolean isPresencePublic() {
68         return JiveGlobals.getBooleanProperty("plugin.presence.public", false);
69     }
70
71     /**
72      * Sets if anyone is allowed to see the presence of other users. A false value means that
73      * only the users that are subscribed to a user presence will be able to get information
74      * about the user. By default, presence information is not publicly available.
75      *
76      * @param presencesPublic if anyone is allowed to see the presence of other users.
77      */

78     public void setPresencePublic(boolean presencesPublic) {
79         JiveGlobals.setProperty("plugin.presence.public", presencesPublic ? "true" : "false");
80     }
81
82     /**
83      * Returns the presence of the requested user or <tt>null</tt> if the user is offline. If
84      * presences are not public then the user presence will be returned if and only if the sender
85      * of the request is subscribed to the user presence.
86      *
87      * @param sender the bare JID of the user making the request.
88      * @param jid the bare JID of the entity whose presence is being probed.
89      * @return the presence of the requested user.
90      * @throws UserNotFoundException If presences are not public and the sender is null or the
91      * sender cannot probe the presence of the requested user. Or if the requested user
92      * does not exist in the local server.
93      */

94     public Presence getPresence(String JavaDoc sender, String JavaDoc jid) throws UserNotFoundException {
95         JID targetJID = new JID(jid);
96         // Check that the sender is not requesting information of a remote server entity
97
if (targetJID.getDomain() == null || XMPPServer.getInstance().isRemote(targetJID)) {
98             throw new UserNotFoundException("Domain does not matches local server domain");
99         }
100         if (!hostname.equals(targetJID.getDomain())) {
101             // Sender is requesting information about component presence
102
// TODO Implement this
103
throw new UserNotFoundException("Presence of components not supported yet!");
104         }
105         if (targetJID.getNode() == null) {
106             // Sender is requesting presence information of an anonymous user
107
throw new UserNotFoundException("Username is null");
108         }
109         if (!isPresencePublic()) {
110             if (sender == null) {
111                 throw new UserNotFoundException("Sender is null");
112             }
113             else if (!presenceManager.canProbePresence(new JID(sender), targetJID.getNode())) {
114                 throw new UserNotFoundException("Sender is not allowed to probe this user");
115             }
116         }
117         User user = userManager.getUser(targetJID.getNode());
118         return presenceManager.getPresence(user);
119     }
120 }
121
Popular Tags