KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: MessageRouter.java,v $
3  * $Revision: 1.6 $
4  * $Date: 2005/06/26 17:48:13 $
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.jivesoftware.messenger.auth.UnauthorizedException;
15 import org.jivesoftware.messenger.container.BasicModule;
16 import org.jivesoftware.util.JiveGlobals;
17 import org.jivesoftware.util.Log;
18 import org.xmpp.packet.JID;
19 import org.xmpp.packet.Message;
20 import org.xmpp.packet.PacketError;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 /**
25  * <p>Route message 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 MessageRouter extends BasicModule {
34
35     private OfflineMessageStrategy messageStrategy;
36     private RoutingTable routingTable;
37     private SessionManager sessionManager;
38
39     private String JavaDoc serverName;
40
41     /**
42      * Constructs a message router.
43      */

44     public MessageRouter() {
45         super("XMPP Message Router");
46     }
47
48     /**
49      * <p>Performs the actual packet routing.</p>
50      * <p>You routing is considered 'quick' and implementations may not take
51      * excessive amounts of time to complete the routing. If routing will take
52      * a long amount of time, the actual routing should be done in another thread
53      * so this method returns quickly.</p>
54      * <h2>Warning</h2>
55      * <p>Be careful to enforce concurrency DbC of concurrent by synchronizing
56      * any accesses to class resources.</p>
57      *
58      * @param packet The packet to route
59      * @throws NullPointerException If the packet is null
60      */

61     public void route(Message packet) {
62         if (packet == null) {
63             throw new NullPointerException JavaDoc();
64         }
65         Session session = sessionManager.getSession(packet.getFrom());
66         if (session == null
67                 || session.getStatus() == Session.STATUS_AUTHENTICATED)
68         {
69             JID recipientJID = packet.getTo();
70
71             // If the message was sent to the server hostname then forward the message to
72
// a configurable set of JID's (probably admin users)
73
if (recipientJID.getNode() == null && recipientJID.getResource() == null &&
74                     serverName.equals(recipientJID.getDomain())) {
75                 sendMessageToAdmins(packet);
76                 return;
77             }
78
79             try {
80                 routingTable.getBestRoute(recipientJID).process(packet);
81             }
82             catch (Exception JavaDoc e) {
83                 try {
84                     messageStrategy.storeOffline(packet);
85                 }
86                 catch (Exception JavaDoc e1) {
87                     Log.error(e1);
88                 }
89             }
90
91         }
92         else {
93             packet.setTo(session.getAddress());
94             packet.setFrom((JID)null);
95             packet.setError(PacketError.Condition.not_authorized);
96             try {
97                 session.process(packet);
98             }
99             catch (UnauthorizedException ue) {
100                 Log.error(ue);
101             }
102         }
103     }
104
105     /**
106      * Forwards the received message to the list of users defined in the property
107      * <b>xmpp.forward.admins</b>. The property may include bare JIDs or just usernames separated
108      * by commas or white spaces. When using bare JIDs the target user may belong to a remote
109      * server.<p>
110      *
111      * If the property <b>xmpp.forward.admins</b> was not defined then the message will be sent
112      * to all the users allowed to enter the admin console.
113      *
114      * @param packet the message to forward.
115      */

116     private void sendMessageToAdmins(Message packet) {
117         String JavaDoc jids = JiveGlobals.getProperty("xmpp.forward.admins");
118         if (jids != null && jids.trim().length() > 0) {
119             // Forward the message to the users specified in the "xmpp.forward.admins" property
120
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(jids, ", ");
121             while (tokenizer.hasMoreTokens()) {
122                 String JavaDoc username = tokenizer.nextToken();
123                 Message forward = packet.createCopy();
124                 if (username.contains("@")) {
125                     // Use the specified bare JID address as the target address
126
forward.setTo(username);
127                 }
128                 else {
129                     forward.setTo(username + "@" + serverName);
130                 }
131                 route(forward);
132             }
133         }
134         else {
135             // Forward the message to the users allowed to log into the admin console
136
jids = JiveGlobals.getXMLProperty("adminConsole.authorizedUsernames");
137             jids = (jids == null || jids.trim().length() == 0) ? "admin" : jids;
138             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(jids, ",");
139             while (tokenizer.hasMoreTokens()) {
140                 String JavaDoc username = tokenizer.nextToken();
141                 Message forward = packet.createCopy();
142                 forward.setTo(username + "@" + serverName);
143                 route(forward);
144             }
145         }
146     }
147
148     public void initialize(XMPPServer server) {
149         super.initialize(server);
150         messageStrategy = server.getOfflineMessageStrategy();
151         routingTable = server.getRoutingTable();
152         sessionManager = server.getSessionManager();
153         serverName = server.getServerInfo().getName();
154     }
155 }
156
Popular Tags