1 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 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 44 public PresenceRouter() { 45 super("XMPP Presence Router"); 46 } 47 48 54 public void route(Presence packet) { 55 if (packet == null) { 56 throw new NullPointerException (); 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 if (type == null || Presence.Type.unavailable == type) { 81 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 ChannelHandler handler = routingTable.getRoute(recipientJID); 92 handler.process(packet); 93 updateHandler.directedPresenceSent(packet, handler, recipientJID.toString()); 95 } 96 97 } 98 else if (Presence.Type.subscribe == type || 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 presenceManager.handleProbe(packet); 108 } 109 else { 110 routingTable.getRoute(recipientJID).process(packet); 112 } 113 114 } 115 catch (NoSuchRouteException e) { 116 } 118 catch (Exception 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 |