1 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 ; 23 24 33 public class MessageRouter extends BasicModule { 34 35 private OfflineMessageStrategy messageStrategy; 36 private RoutingTable routingTable; 37 private SessionManager sessionManager; 38 39 private String serverName; 40 41 44 public MessageRouter() { 45 super("XMPP Message Router"); 46 } 47 48 61 public void route(Message packet) { 62 if (packet == null) { 63 throw new NullPointerException (); 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 (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 e) { 83 try { 84 messageStrategy.storeOffline(packet); 85 } 86 catch (Exception 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 116 private void sendMessageToAdmins(Message packet) { 117 String jids = JiveGlobals.getProperty("xmpp.forward.admins"); 118 if (jids != null && jids.trim().length() > 0) { 119 StringTokenizer tokenizer = new StringTokenizer (jids, ", "); 121 while (tokenizer.hasMoreTokens()) { 122 String username = tokenizer.nextToken(); 123 Message forward = packet.createCopy(); 124 if (username.contains("@")) { 125 forward.setTo(username); 127 } 128 else { 129 forward.setTo(username + "@" + serverName); 130 } 131 route(forward); 132 } 133 } 134 else { 135 jids = JiveGlobals.getXMLProperty("adminConsole.authorizedUsernames"); 137 jids = (jids == null || jids.trim().length() == 0) ? "admin" : jids; 138 StringTokenizer tokenizer = new StringTokenizer (jids, ","); 139 while (tokenizer.hasMoreTokens()) { 140 String 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 |