PresenceRouter.java 9.53 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile: PresenceRouter.java,v $
 * $Revision: 3138 $
 * $Date: 2005-12-01 02:13:26 -0300 (Thu, 01 Dec 2005) $
 *
6
 * Copyright (C) 2007 Jive Software. All rights reserved.
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;
13

14 15 16 17 18 19 20
import org.jivesoftware.openfire.container.BasicModule;
import org.jivesoftware.openfire.handler.PresenceSubscribeHandler;
import org.jivesoftware.openfire.handler.PresenceUpdateHandler;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.session.Session;
21
import org.jivesoftware.openfire.user.RemotePresenceEventDispatcher;
Gaston Dombiak's avatar
Gaston Dombiak committed
22 23 24
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.*;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

/**
 * <p>Route presence packets throughout the server.</p>
 * <p>Routing is based on the recipient and sender addresses. The typical
 * packet will often be routed twice, once from the sender to some internal
 * server component for handling or processing, and then back to the router
 * to be delivered to it's final destination.</p>
 *
 * @author Iain Shigeoka
 */
public class PresenceRouter extends BasicModule {

    private RoutingTable routingTable;
    private PresenceUpdateHandler updateHandler;
    private PresenceSubscribeHandler subscribeHandler;
    private PresenceManager presenceManager;
    private SessionManager sessionManager;
42
    private MulticastRouter multicastRouter;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    private String serverName;

    /**
     * Constructs a presence router.
     */
    public PresenceRouter() {
        super("XMPP Presence Router");
    }

    /**
     * Routes presence packets.
     *
     * @param packet the packet to route.
     * @throws NullPointerException if the packet is null.
     */
    public void route(Presence packet) {
        if (packet == null) {
            throw new NullPointerException();
        }
62
        ClientSession session = sessionManager.getSession(packet.getFrom());
63 64 65 66 67 68 69 70 71 72 73 74 75 76
        try {
            // Invoke the interceptors before we process the read packet
            InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);
            if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
                handle(packet);
            }
            else {
                packet.setTo(session.getAddress());
                packet.setFrom((JID)null);
                packet.setError(PacketError.Condition.not_authorized);
                session.process(packet);
            }
            // Invoke the interceptors after we have processed the read packet
            InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);
77
        }
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        catch (PacketRejectedException e) {
            if (session != null) {
                // An interceptor rejected this packet so answer a not_allowed error
                Presence reply = new Presence();
                reply.setID(packet.getID());
                reply.setTo(session.getAddress());
                reply.setFrom(packet.getTo());
                reply.setError(PacketError.Condition.not_allowed);
                session.process(reply);
                // Check if a message notifying the rejection should be sent
                if (e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
                    // A message for the rejection will be sent to the sender of the rejected packet
                    Message notification = new Message();
                    notification.setTo(session.getAddress());
                    notification.setFrom(packet.getTo());
                    notification.setBody(e.getRejectionMessage());
                    session.process(notification);
                }
            }
97 98 99 100 101
        }
    }

    private void handle(Presence packet) {
        JID recipientJID = packet.getTo();
102
        JID senderJID = packet.getFrom();
103 104 105 106 107 108 109 110 111 112
        // Check if the packet was sent to the server hostname
        if (recipientJID != null && recipientJID.getNode() == null &&
                recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) {
            if (packet.getElement().element("addresses") != null) {
                // Presence includes multicast processing instructions. Ask the multicastRouter
                // to route this packet
                multicastRouter.route(packet);
                return;
            }
        }
113
        try {
114 115 116 117 118 119 120 121
            // Presences sent between components are just routed to the component
            if (recipientJID != null && !XMPPServer.getInstance().isLocal(recipientJID) &&
                    !XMPPServer.getInstance().isLocal(senderJID)) {
                // Route the packet
                routingTable.routePacket(recipientJID, packet, false);
                return;
            }

122 123 124 125 126 127 128 129 130 131 132
            Presence.Type type = packet.getType();
            // Presence updates (null is 'available')
            if (type == null || Presence.Type.unavailable == type) {
                // check for local server target
                if (recipientJID == null || recipientJID.getDomain() == null ||
                        "".equals(recipientJID.getDomain()) || (recipientJID.getNode() == null &&
                        recipientJID.getResource() == null) &&
                        serverName.equals(recipientJID.getDomain())) {
                    updateHandler.process(packet);
                }
                else {
133 134 135
                    // Trigger events for presences of remote users
                    if (senderJID != null && !serverName.equals(senderJID.getDomain()) &&
                            !routingTable.hasComponentRoute(senderJID)) {
136 137 138 139 140 141 142 143 144 145
                        if (type == null) {
                            // Remote user has become available
                            RemotePresenceEventDispatcher.remoteUserAvailable(packet);
                        }
                        else if (type == Presence.Type.unavailable) {
                            // Remote user is now unavailable
                            RemotePresenceEventDispatcher.remoteUserUnavailable(packet);
                        }
                    }
                    
146 147 148 149 150 151 152
                    // Check that sender session is still active
                    Session session = sessionManager.getSession(packet.getFrom());
                    if (session != null && session.getStatus() == Session.STATUS_CLOSED) {
                        Log.warn("Rejected available presence: " + packet + " - " + session);
                        return;
                    }

153
                    // The user sent a directed presence to an entity
154
                    // Broadcast it to all connected resources
Gaston Dombiak's avatar
Gaston Dombiak committed
155
                    for (JID jid : routingTable.getRoutes(recipientJID)) {
156
                        // Register the sent directed presence
Gaston Dombiak's avatar
Gaston Dombiak committed
157
                        updateHandler.directedPresenceSent(packet, jid, recipientJID.toString());
158
                        // Route the packet
Gaston Dombiak's avatar
Gaston Dombiak committed
159
                        routingTable.routePacket(jid, packet, false);
160 161 162 163 164 165 166 167 168 169 170 171 172
                    }
                }

            }
            else if (Presence.Type.subscribe == type // presence subscriptions
                    || Presence.Type.unsubscribe == type
                    || Presence.Type.subscribed == type
                    || Presence.Type.unsubscribed == type)
            {
                subscribeHandler.process(packet);
            }
            else if (Presence.Type.probe == type) {
                // Handle a presence probe sent by a remote server
173
                if (!XMPPServer.getInstance().isLocal(recipientJID)) {
Gaston Dombiak's avatar
Gaston Dombiak committed
174
                    routingTable.routePacket(recipientJID, packet, false);
175 176 177 178 179
                }
                else {
                    // Handle probe to a local user
                    presenceManager.handleProbe(packet);
                }
180 181 182 183
            }
            else {
                // It's an unknown or ERROR type, just deliver it because there's nothing
                // else to do with it
Gaston Dombiak's avatar
Gaston Dombiak committed
184
                routingTable.routePacket(recipientJID, packet, false);
185 186 187 188 189 190 191
            }

        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
            Session session = sessionManager.getSession(packet.getFrom());
            if (session != null) {
192
                session.close();
193 194 195 196 197 198 199 200 201 202 203
            }
        }
    }

    public void initialize(XMPPServer server) {
        super.initialize(server);
        serverName = server.getServerInfo().getName();
        routingTable = server.getRoutingTable();
        updateHandler = server.getPresenceUpdateHandler();
        subscribeHandler = server.getPresenceSubscribeHandler();
        presenceManager = server.getPresenceManager();
204
        multicastRouter = server.getMulticastRouter();
205 206
        sessionManager = server.getSessionManager();
    }
Gaston Dombiak's avatar
Gaston Dombiak committed
207 208 209 210

    /**
     * Notification message indicating that a packet has failed to be routed to the receipient.
     *
211
     * @param receipient address of the entity that failed to receive the packet.
Gaston Dombiak's avatar
Gaston Dombiak committed
212 213
     * @param packet Presence packet that failed to be sent to the receipient.
     */
214
    public void routingFailed(JID receipient, Packet packet) {
Gaston Dombiak's avatar
Gaston Dombiak committed
215 216
        // presence packets are dropped silently
    }
217
}