KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > PresenceRouter


1 /**
2  * $RCSfile: PresenceRouter.java,v $
3  * $Revision: 1.7 $
4  * $Date: 2005/05/24 19:51:46 $
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;
13
14 import org.xmpp.packet.Presence;
15 import org.xmpp.packet.JID;
16 import org.xmpp.packet.PacketError;
17 import org.jivesoftware.messenger.handler.PresenceUpdateHandler;
18 import org.jivesoftware.messenger.handler.PresenceSubscribeHandler;
19 import org.jivesoftware.messenger.auth.UnauthorizedException;
20 import org.jivesoftware.messenger.container.BasicModule;
21 import org.jivesoftware.util.Log;
22 import org.jivesoftware.util.LocaleUtils;
23
24 /**
25  * <p>Route presence packets throughout the server.</p>
26  * <p>Routing is based on the recipient and sender addresses. The typical
27  * packet will often be routed twice, once from the sender to some internal
28  * server component for handling or processing, and then back to the router
29  * to be delivered to it's final destination.</p>
30  *
31  * @author Iain Shigeoka
32  */

33 public class PresenceRouter extends BasicModule {
34
35     private RoutingTable routingTable;
36     private PresenceUpdateHandler updateHandler;
37     private PresenceSubscribeHandler subscribeHandler;
38     private PresenceManager presenceManager;
39     private SessionManager sessionManager;
40
41     /**
42      * Constructs a presence router.
43      */

44     public PresenceRouter() {
45         super("XMPP Presence Router");
46     }
47
48     /**
49      * Routes presence packets.
50      *
51      * @param packet the packet to route.
52      * @throws NullPointerException if the packet is null.
53      */

54     public void route(Presence packet) {
55         if (packet == null) {
56             throw new NullPointerException JavaDoc();
57         }
58         Session session = sessionManager.getSession(packet.getFrom());
59         if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
60             handle(packet);
61         }
62         else {
63             packet.setTo(session.getAddress());
64             packet.setFrom((JID)null);
65             packet.setError(PacketError.Condition.not_authorized);
66             try {
67                 session.process(packet);
68             }
69             catch (UnauthorizedException ue) {
70                 Log.error(ue);
71             }
72         }
73     }
74
75     private void handle(Presence packet) {
76         JID recipientJID = packet.getTo();
77         try {
78             Presence.Type type = packet.getType();
79             // Presence updates (null is 'available')
80
if (type == null || Presence.Type.unavailable == type) {
81                 // check for local server target
82
if (recipientJID == null
83                         || recipientJID.getDomain() == null
84                         || "".equals(recipientJID.getDomain())
85                         || (recipientJID.getNode() == null && recipientJID.getResource() == null)) {
86
87                     updateHandler.process(packet);
88                 }
89                 else {
90                     // The user sent a directed presence to an entity
91
ChannelHandler handler = routingTable.getRoute(recipientJID);
92                     handler.process(packet);
93                     // Notify the PresenceUpdateHandler of the directed presence
94
updateHandler.directedPresenceSent(packet, handler, recipientJID.toString());
95                 }
96
97             }
98             else if (Presence.Type.subscribe == type // presence subscriptions
99
|| Presence.Type.unsubscribe == type
100                     || Presence.Type.subscribed == type
101                     || Presence.Type.unsubscribed == type)
102             {
103                 subscribeHandler.process(packet);
104             }
105             else if (Presence.Type.probe == type) {
106                 // Handle a presence probe sent by a remote server
107
presenceManager.handleProbe(packet);
108             }
109             else {
110                 // It's an unknown or ERROR type, just deliver it because there's nothing else to do with it
111
routingTable.getRoute(recipientJID).process(packet);
112             }
113
114         }
115         catch (NoSuchRouteException e) {
116             // Do nothing, presence to unreachable routes are dropped
117
}
118         catch (Exception JavaDoc e) {
119             Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
120             Session session = sessionManager.getSession(packet.getFrom());
121             if (session != null) {
122                 Connection conn = session.getConnection();
123                 if (conn != null) {
124                     conn.close();
125                 }
126             }
127         }
128     }
129
130     public void initialize(XMPPServer server) {
131         super.initialize(server);
132         routingTable = server.getRoutingTable();
133         updateHandler = server.getPresenceUpdateHandler();
134         subscribeHandler = server.getPresenceSubscribeHandler();
135         presenceManager = server.getPresenceManager();
136         sessionManager = server.getSessionManager();
137     }
138 }
139
Popular Tags