SocketPacketWriteHandler.java 2.48 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile: SocketPacketWriteHandler.java,v $
 * $Revision: 3137 $
 * $Date: 2005-12-01 02:11:05 -0300 (Thu, 01 Dec 2005) $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
7 8
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
11 12
 */

13
package org.jivesoftware.openfire.net;
14

15 16
import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.auth.UnauthorizedException;
Gaston Dombiak's avatar
Gaston Dombiak committed
17 18
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
19 20
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
21
import org.xmpp.packet.Presence;
22 23 24 25 26 27 28 29 30 31 32 33

/**
 * This ChannelHandler writes packet data to connections.
 *
 * @author Iain Shigeoka
 * @see PacketRouter
 */
public class SocketPacketWriteHandler implements ChannelHandler {

    private XMPPServer server;
    private RoutingTable routingTable;

34
    public SocketPacketWriteHandler(RoutingTable routingTable) {
35 36 37 38 39 40 41 42 43
        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)) {
Gaston Dombiak's avatar
Gaston Dombiak committed
44
                routingTable.routePacket(recipient, packet, false);
45 46
            }
            // The target domain belongs to the local server
47
            else if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) {
48
                // no TO was found so send back the packet to the sender
Gaston Dombiak's avatar
Gaston Dombiak committed
49
                routingTable.routePacket(packet.getFrom(), packet, false);
50
            }
51 52
            else if (recipient.getResource() != null || !(packet instanceof Presence)) {
                // JID is of the form <user@domain/resource>
Gaston Dombiak's avatar
Gaston Dombiak committed
53
                routingTable.routePacket(recipient, packet, false);
54
            }
55 56
            else {
                // JID is of the form <user@domain>
57
                for (JID route : routingTable.getRoutes(recipient, null)) {
Gaston Dombiak's avatar
Gaston Dombiak committed
58
                    routingTable.routePacket(route, packet, false);
59 60
                }
            }
61 62 63 64 65 66
        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error.deliver") + "\n" + packet.toString(), e);
        }
    }
}