1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* $RCSfile: SocketPacketWriteHandler.java,v $
* $Revision: 3137 $
* $Date: 2005-12-01 02:11:05 -0300 (Thu, 01 Dec 2005) $
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.net;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
/**
* This ChannelHandler writes packet data to connections.
*
* @author Iain Shigeoka
* @see PacketRouter
*/
public class SocketPacketWriteHandler implements ChannelHandler {
private XMPPServer server;
private SessionManager sessionManager;
private OfflineMessageStrategy messageStrategy;
private RoutingTable routingTable;
public SocketPacketWriteHandler(SessionManager sessionManager, RoutingTable routingTable,
OfflineMessageStrategy messageStrategy) {
this.sessionManager = sessionManager;
this.messageStrategy = messageStrategy;
this.routingTable = routingTable;
this.server = XMPPServer.getInstance();
}
public void process(Packet packet) throws UnauthorizedException, PacketException {
try {
JID recipient = packet.getTo();
// Check if the target domain belongs to a remote server or a component
if (server.matchesComponent(recipient) || server.isRemote(recipient)) {
// Locate the route to the remote server or component and ask it
// to process the packet
ChannelHandler route = routingTable.getRoute(recipient);
if (route != null) {
route.process(packet);
}
else {
// No root was found so either drop or store the packet
handleUnprocessedPacket(packet);
}
return;
}
// The target domain belongs to the local server
if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) {
// no TO was found so send back the packet to the sender
Session senderSession = sessionManager.getSession(packet.getFrom());
if (senderSession != null && !senderSession.getConnection().isClosed()) {
senderSession.getConnection().deliver(packet);
}
else {
// The sender is no longer available so drop the packet
dropPacket(packet);
}
}
else {
Session session = sessionManager.getBestRoute(recipient);
if (session == null) {
handleUnprocessedPacket(packet);
}
else {
try {
session.getConnection().deliver(packet);
}
catch (Exception e) {
// do nothing
}
}
}
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.deliver") + "\n" + packet.toString(), e);
}
}
private void handleUnprocessedPacket(Packet packet) {
if (packet instanceof Message) {
messageStrategy.storeOffline((Message)packet);
}
else if (packet instanceof Presence) {
// presence packets are dropped silently
//dropPacket(packet);
}
else {
// IQ packets are logged but dropped
dropPacket(packet);
}
}
/**
* Drop the packet.
*
* @param packet The packet being dropped
*/
private void dropPacket(Packet packet) {
Log.warn(LocaleUtils.getLocalizedString("admin.error.routing") + "\n" + packet.toString());
}
}