KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > net > SocketPacketWriteHandler


1 /**
2  * $RCSfile: SocketPacketWriteHandler.java,v $
3  * $Revision: 1.6 $
4  * $Date: 2005/07/28 22:19:16 $
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.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 /**
24  * This ChannelHandler writes packet data to connections.
25  *
26  * @author Iain Shigeoka
27  * @see PacketRouter
28  */

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             // Check if the target domain belongs to a remote server
48
if (server.isRemote(recipient)) {
49                 try {
50                     // Locate the route to the remote server and ask it to process the packet
51
routingTable.getRoute(recipient).process(packet);
52                 }
53                 catch (NoSuchRouteException e) {
54                     // No root was found so either drop or store the packet
55
handleUnprocessedPacket(packet);
56                 }
57                 return;
58             }
59             // The target domain belongs to the local server
60
if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) {
61                 // no TO was found so send back the packet to the sender
62
Session senderSession = sessionManager.getSession(packet.getFrom());
63                 if (senderSession != null && !senderSession.getConnection().isClosed()) {
64                     senderSession.getConnection().deliver(packet);
65                 }
66                 else {
67                     // The sender is no longer available so drop the packet
68
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 JavaDoc e) {
81                         // do nothing
82
}
83                 }
84             }
85         }
86         catch (Exception JavaDoc 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             // presence packets are dropped silently
97
//dropPacket(packet);
98
}
99         else {
100             // IQ packets are logged but dropped
101
dropPacket(packet);
102         }
103     }
104
105     /**
106      * Drop the packet.
107      *
108      * @param packet The packet being dropped
109      */

110     private void dropPacket(Packet packet) {
111         Log.warn(LocaleUtils.getLocalizedString("admin.error.routing") + "\n" + packet.toString());
112     }
113 }
114
Popular Tags