SocketPacketWriteHandler.java 3.18 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 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19 20
 */

21
package org.jivesoftware.openfire.net;
22

23 24 25 26 27
import org.jivesoftware.openfire.ChannelHandler;
import org.jivesoftware.openfire.PacketException;
import org.jivesoftware.openfire.PacketRouter;
import org.jivesoftware.openfire.RoutingTable;
import org.jivesoftware.openfire.XMPPServer;
28
import org.jivesoftware.openfire.auth.UnauthorizedException;
Gaston Dombiak's avatar
Gaston Dombiak committed
29
import org.jivesoftware.util.LocaleUtils;
30 31
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
32 33
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
34
import org.xmpp.packet.Presence;
35 36 37 38 39 40 41 42 43

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

44 45
	private static final Logger Log = LoggerFactory.getLogger(SocketPacketWriteHandler.class);

46 47 48
    private XMPPServer server;
    private RoutingTable routingTable;

49
    public SocketPacketWriteHandler(RoutingTable routingTable) {
50 51 52 53 54 55 56 57 58
        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
59
                routingTable.routePacket(recipient, packet, false);
60 61
            }
            // The target domain belongs to the local server
62
            else if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) {
63
                // no TO was found so send back the packet to the sender
Gaston Dombiak's avatar
Gaston Dombiak committed
64
                routingTable.routePacket(packet.getFrom(), packet, false);
65
            }
66 67
            else if (recipient.getResource() != null || !(packet instanceof Presence)) {
                // JID is of the form <user@domain/resource>
Gaston Dombiak's avatar
Gaston Dombiak committed
68
                routingTable.routePacket(recipient, packet, false);
69
            }
70 71
            else {
                // JID is of the form <user@domain>
72
                for (JID route : routingTable.getRoutes(recipient, null)) {
Gaston Dombiak's avatar
Gaston Dombiak committed
73
                    routingTable.routePacket(route, packet, false);
74 75
                }
            }
76 77 78 79 80 81
        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error.deliver") + "\n" + packet.toString(), e);
        }
    }
}