1 11 12 package org.jivesoftware.messenger.net; 13 14 import org.jivesoftware.messenger.*; 15 import org.jivesoftware.messenger.auth.UnauthorizedException; 16 import org.jivesoftware.util.LocaleUtils; 17 import org.jivesoftware.util.Log; 18 import org.xmpp.packet.JID; 19 import org.xmpp.packet.Message; 20 import org.xmpp.packet.Packet; 21 import org.xmpp.packet.Presence; 22 23 29 public class SocketPacketWriteHandler implements ChannelHandler { 30 31 private XMPPServer server; 32 private SessionManager sessionManager; 33 private OfflineMessageStrategy messageStrategy; 34 private RoutingTable routingTable; 35 36 public SocketPacketWriteHandler(SessionManager sessionManager, RoutingTable routingTable, 37 OfflineMessageStrategy messageStrategy) { 38 this.sessionManager = sessionManager; 39 this.messageStrategy = messageStrategy; 40 this.routingTable = routingTable; 41 this.server = XMPPServer.getInstance(); 42 } 43 44 public void process(Packet packet) throws UnauthorizedException, PacketException { 45 try { 46 JID recipient = packet.getTo(); 47 if (server.isRemote(recipient)) { 49 try { 50 routingTable.getRoute(recipient).process(packet); 52 } 53 catch (NoSuchRouteException e) { 54 handleUnprocessedPacket(packet); 56 } 57 return; 58 } 59 if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) { 61 Session senderSession = sessionManager.getSession(packet.getFrom()); 63 if (senderSession != null && !senderSession.getConnection().isClosed()) { 64 senderSession.getConnection().deliver(packet); 65 } 66 else { 67 dropPacket(packet); 69 } 70 } 71 else { 72 Session session = sessionManager.getBestRoute(recipient); 73 if (session == null) { 74 handleUnprocessedPacket(packet); 75 } 76 else { 77 try { 78 session.getConnection().deliver(packet); 79 } 80 catch (Exception e) { 81 } 83 } 84 } 85 } 86 catch (Exception e) { 87 Log.error(LocaleUtils.getLocalizedString("admin.error.deliver") + "\n" + packet.toString(), e); 88 } 89 } 90 91 private void handleUnprocessedPacket(Packet packet) { 92 if (packet instanceof Message) { 93 messageStrategy.storeOffline((Message)packet); 94 } 95 else if (packet instanceof Presence) { 96 } 99 else { 100 dropPacket(packet); 102 } 103 } 104 105 110 private void dropPacket(Packet packet) { 111 Log.warn(LocaleUtils.getLocalizedString("admin.error.routing") + "\n" + packet.toString()); 112 } 113 } 114 | Popular Tags |