MultiplexerPacketDeliverer.java 4.37 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2007 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
7 8 9 10 11
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

12
package org.jivesoftware.openfire.multiplex;
Gaston Dombiak's avatar
Gaston Dombiak committed
13 14

import org.dom4j.Element;
15 16 17 18 19 20
import org.jivesoftware.openfire.OfflineMessageStrategy;
import org.jivesoftware.openfire.PacketDeliverer;
import org.jivesoftware.openfire.PacketException;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.session.ConnectionMultiplexerSession;
21 22
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
Gaston Dombiak's avatar
Gaston Dombiak committed
23 24 25 26 27 28
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;

/**
29
 * Fallback method used by {@link org.jivesoftware.openfire.net.SocketConnection} when
Gaston Dombiak's avatar
Gaston Dombiak committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 * connected to a connection manager. The fallback method will be used when a SocketConnection
 * fails to send a {@link Packet} (probably because the socket was closed).<p>
 *
 * The first attempt will be to send the packet using another connection to the same connection
 * manager (since managers may have a pool of connections to the server). And if that fails then
 * instances of {@link Message} may be stored offline for later retrieval. Since packets may be
 * wrapped by special IQ packets (read the Connection Manager JEP for more information) we need
 * to unwrap the packet and store the wrapped packet offline.
 *
 * @author Gaston Dombiak
 */
public class MultiplexerPacketDeliverer implements PacketDeliverer {

    private OfflineMessageStrategy messageStrategy;
    private String connectionManagerDomain;
    private ConnectionMultiplexerManager multiplexerManager;

    public MultiplexerPacketDeliverer() {
        this.messageStrategy = XMPPServer.getInstance().getOfflineMessageStrategy();
        multiplexerManager = ConnectionMultiplexerManager.getInstance();
    }

52
    public void setConnectionManagerDomain(String connectionManagerDomain) {
Gaston Dombiak's avatar
Gaston Dombiak committed
53 54 55 56 57 58 59 60 61 62 63 64 65
        this.connectionManagerDomain = connectionManagerDomain;
    }

    public void deliver(Packet packet) throws UnauthorizedException, PacketException {
        // Check if we can send the packet using another session
        if (connectionManagerDomain == null) {
            // Packet deliverer has not yet been configured so handle unprocessed packet
            handleUnprocessedPacket(packet);
        }
        else {
            // Try getting another session to the same connection manager 
            ConnectionMultiplexerSession session =
                    multiplexerManager.getMultiplexerSession(connectionManagerDomain);
66
            if (session == null || session.isClosed()) {
Gaston Dombiak's avatar
Gaston Dombiak committed
67 68 69 70 71
                // No other session was found so handle unprocessed packet
                handleUnprocessedPacket(packet);
            }
            else {
                // Send the packet using this other session to the same connection manager
72
                session.process(packet);
Gaston Dombiak's avatar
Gaston Dombiak committed
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
            }
        }
    }

    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 if (packet instanceof IQ) {
            IQ iq = (IQ) packet;
            // Check if we need to unwrap the packet
            Element child = iq.getChildElement();
            if (child != null && "session".equals(child.getName()) &&
                    "http://jabber.org/protocol/connectionmanager"
                            .equals(child.getNamespacePrefix())) {
                Element send = child.element("send");
                if (send != null) {
                    // Unwrap packet
                    Element wrappedElement = (Element) send.elements().get(0);
                    if ("message".equals(wrappedElement.getName())) {
                        handleUnprocessedPacket(new Message(wrappedElement));
                    }
                }
            }
            else {
                // IQ packets are logged but dropped
                Log.warn(LocaleUtils.getLocalizedString("admin.error.routing") + "\n" +
                        packet.toString());
            }
        }
    }
}