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

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15
package org.jivesoftware.messenger.handler;

import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
16
import org.jivesoftware.messenger.disco.ServerFeaturesProvider;
Matt Tucker's avatar
Matt Tucker committed
17
import org.jivesoftware.messenger.roster.Roster;
18 19 20 21 22 23 24 25 26 27 28
import org.jivesoftware.messenger.roster.RosterItem;
import org.jivesoftware.messenger.user.User;
import org.jivesoftware.messenger.user.UserAlreadyExistsException;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError;
Matt Tucker's avatar
Matt Tucker committed
29

Matt Tucker's avatar
Matt Tucker committed
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
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Implements the TYPE_IQ jabber:iq:roster protocol. Clients
 * use this protocol to retrieve, update, and rosterMonitor roster
 * entries (buddy lists). The server manages the basics of
 * roster subscriptions and roster updates based on presence
 * and iq:roster packets, while the client maintains the user
 * interface aspects of rosters such as organizing roster
 * entries into groups.
 * <p/>
 * A 'get' query retrieves a snapshot of the roster.
 * A 'set' query updates the roster (typically with new group info).
 * The server sends 'set' updates asynchronously when roster
 * entries change status.
 * <p/>
 * Currently an empty implementation to allow usage with normal
 * clients. Future implementation needed.
 * <p/>
 * <h2>Assumptions</h2>
 * This handler assumes that the request is addressed to the server.
 * An appropriate TYPE_IQ tag matcher should be placed in front of this
 * one to route TYPE_IQ requests not addressed to the server to
 * another channel (probably for direct delivery to the recipient).
 * <p/>
 * <h2>Warning</h2>
 * There should be a way of determining whether a session has
 * authorization to access this feature. I'm not sure it is a good
 * idea to do authorization in each handler. It would be nice if
 * the framework could assert authorization policies across channels.
 *
 * @author Iain Shigeoka
 */
public class IQRosterHandler extends IQHandler implements ServerFeaturesProvider {

    private IQHandlerInfo info;

68 69 70 71 72
    private UserManager userManager;
    private XMPPServer localServer;
    private SessionManager sessionManager;
    private PacketRouter router;

Matt Tucker's avatar
Matt Tucker committed
73 74
    public IQRosterHandler() {
        super("XMPP Roster Handler");
75
        info = new IQHandlerInfo("query", "jabber:iq:roster");
Matt Tucker's avatar
Matt Tucker committed
76 77 78
    }

    /**
79 80
     * Handles all roster queries. There are two major types of queries:
     *
Matt Tucker's avatar
Matt Tucker committed
81
     * <ul>
82 83 84 85 86 87 88
     *      <li>Roster remove - A forced removal of items from a roster. Roster
     *      removals are the only roster queries allowed to
     *      directly affect the roster from another user.
     *      </li>
     *      <li>Roster management - A local user looking up or updating their
     *      roster.
     *      </li>
Matt Tucker's avatar
Matt Tucker committed
89 90 91 92 93
     * </ul>
     *
     * @param packet The update packet
     * @return The reply or null if no reply
     */
94
    public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
Matt Tucker's avatar
Matt Tucker committed
95 96
        try {
            IQ returnPacket = null;
Gaston Dombiak's avatar
Gaston Dombiak committed
97
            org.xmpp.packet.Roster roster = (org.xmpp.packet.Roster)packet;
Matt Tucker's avatar
Matt Tucker committed
98

Derek DeMoro's avatar
Derek DeMoro committed
99
            JID recipientJID = packet.getTo();
Matt Tucker's avatar
Matt Tucker committed
100 101

            // The packet is bound for the server and must be roster management
Derek DeMoro's avatar
Derek DeMoro committed
102
            if (recipientJID == null || recipientJID.getNode() == null) {
Matt Tucker's avatar
Matt Tucker committed
103 104
                returnPacket = manageRoster(roster);
            }
105 106
            // The packet must be a roster removal from a foreign domain user.
            else {
Matt Tucker's avatar
Matt Tucker committed
107 108 109 110
                removeRosterItem(roster);
            }
            return returnPacket;
        }
111 112
        catch (SharedGroupException e) {
            IQ result = IQ.createResultIQ(packet);
113
            result.setChildElement(packet.getChildElement().createCopy());
114 115 116
            result.setError(PacketError.Condition.not_acceptable);
            return result;
        }
Matt Tucker's avatar
Matt Tucker committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
        return null;
    }

    /**
     * Remove a roster item. At this stage, this is recipient who has received
     * a roster update. We must check that it is a removal, and if so, remove
     * the roster item based on the sender's id rather than what is in the item
     * listing itself.
     *
     * @param packet The packet suspected of containing a roster removal
     */
131 132
    private void removeRosterItem(org.xmpp.packet.Roster packet) throws UnauthorizedException,
            SharedGroupException {
Derek DeMoro's avatar
Derek DeMoro committed
133 134
        JID recipientJID = packet.getTo();
        JID senderJID = packet.getFrom();
Matt Tucker's avatar
Matt Tucker committed
135
        try {
Gaston Dombiak's avatar
Gaston Dombiak committed
136 137
            for (org.xmpp.packet.Roster.Item packetItem : packet.getItems()) {
                if (packetItem.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
Derek DeMoro's avatar
Derek DeMoro committed
138
                    Roster roster = userManager.getUser(recipientJID.getNode()).getRoster();
Matt Tucker's avatar
Matt Tucker committed
139
                    RosterItem item = roster.getRosterItem(senderJID);
140
                    roster.deleteRosterItem(senderJID, true);
Matt Tucker's avatar
Matt Tucker committed
141 142 143
                    item.setSubStatus(RosterItem.SUB_REMOVE);
                    item.setSubStatus(RosterItem.SUB_NONE);

Matt Tucker's avatar
Matt Tucker committed
144 145
                    Packet itemPacket = packet.createCopy();
                    sessionManager.userBroadcast(recipientJID.getNode(), itemPacket);
Matt Tucker's avatar
Matt Tucker committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                }
            }
        }
        catch (UserNotFoundException e) {
            throw new UnauthorizedException(e);
        }
    }

    /**
     * The packet is a typical 'set' or 'get' update targeted at the server.
     * Notice that the set could be a roster removal in which case we have to
     * generate a local roster removal update as well as a new roster removal
     * to send to the the roster item's owner.
     *
     * @param packet The packet that triggered this update
     * @return Either a response to the roster update or null if the packet is corrupt and the session was closed down
     */
Gaston Dombiak's avatar
Gaston Dombiak committed
163
    private IQ manageRoster(org.xmpp.packet.Roster packet) throws UnauthorizedException,
164
            UserAlreadyExistsException, SharedGroupException {
Matt Tucker's avatar
Matt Tucker committed
165 166

        IQ returnPacket = null;
167
        ClientSession session = sessionManager.getSession(packet.getFrom());
Matt Tucker's avatar
Matt Tucker committed
168

Matt Tucker's avatar
Matt Tucker committed
169
        IQ.Type type = packet.getType();
Matt Tucker's avatar
Matt Tucker committed
170 171

        try {
172
            User sessionUser = userManager.getUser(session.getUsername());
173
            Roster cachedRoster = sessionUser.getRoster();
Matt Tucker's avatar
Matt Tucker committed
174
            if (IQ.Type.get == type) {
Matt Tucker's avatar
Matt Tucker committed
175
                returnPacket = cachedRoster.getReset();
Matt Tucker's avatar
Matt Tucker committed
176 177
                returnPacket.setType(IQ.Type.result);
                returnPacket.setTo(session.getAddress());
Matt Tucker's avatar
Matt Tucker committed
178 179 180 181 182 183
                returnPacket.setID(packet.getID());
                // Force delivery of the response because we need to trigger
                // a presence probe from all contacts
                deliverer.deliver(returnPacket);
                returnPacket = null;
            }
Matt Tucker's avatar
Matt Tucker committed
184
            else if (IQ.Type.set == type) {
Matt Tucker's avatar
Matt Tucker committed
185

Gaston Dombiak's avatar
Gaston Dombiak committed
186 187
                for (org.xmpp.packet.Roster.Item item : packet.getItems()) {
                    if (item.getSubscription() == org.xmpp.packet.Roster.Subscription.remove) {
Matt Tucker's avatar
Matt Tucker committed
188
                        removeItem(cachedRoster, packet.getFrom(), item);
Matt Tucker's avatar
Matt Tucker committed
189 190
                    }
                    else {
Gaston Dombiak's avatar
Gaston Dombiak committed
191
                        if (cachedRoster.isRosterItem(item.getJID())) {
Matt Tucker's avatar
Matt Tucker committed
192
                            // existing item
193
                            RosterItem cachedItem = cachedRoster.getRosterItem(item.getJID());
Matt Tucker's avatar
Matt Tucker committed
194 195 196 197 198 199 200 201 202
                            cachedItem.setAsCopyOf(item);
                            cachedRoster.updateRosterItem(cachedItem);
                        }
                        else {
                            // new item
                            cachedRoster.createRosterItem(item);
                        }
                    }
                }
Matt Tucker's avatar
Matt Tucker committed
203
                returnPacket = IQ.createResultIQ(packet);
Matt Tucker's avatar
Matt Tucker committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
            }
        }
        catch (UserNotFoundException e) {
            throw new UnauthorizedException(e);
        }

        return returnPacket;

    }

    /**
     * Remove the roster item from the sender's roster (and possibly the recipient's).
     * Actual roster removal is done in the removeItem(Roster,RosterItem) method.
     *
     * @param roster The sender's roster.
Derek DeMoro's avatar
Derek DeMoro committed
219
     * @param sender The JID of the sender of the removal request
Matt Tucker's avatar
Matt Tucker committed
220 221
     * @param item   The removal item element
     */
222
    private void removeItem(org.jivesoftware.messenger.roster.Roster roster, JID sender,
223
            org.xmpp.packet.Roster.Item item) throws SharedGroupException {
Gaston Dombiak's avatar
Gaston Dombiak committed
224
        JID recipient = item.getJID();
Matt Tucker's avatar
Matt Tucker committed
225
        // Remove recipient from the sender's roster
226
        roster.deleteRosterItem(item.getJID(), true);
Matt Tucker's avatar
Matt Tucker committed
227 228 229
        // Forward set packet to the subscriber
        if (localServer.isLocal(recipient)) { // Recipient is local so let's handle it here
            try {
230
                Roster recipientRoster = userManager.getUser(recipient.getNode()).getRoster();
231
                recipientRoster.deleteRosterItem(sender, true);
Matt Tucker's avatar
Matt Tucker committed
232 233 234 235
            }
            catch (UserNotFoundException e) {
            }
        }
236 237 238 239 240 241 242 243 244 245 246
        else {
            // Recipient is remote so we just forward the packet to them
            String serverDomain = localServer.getServerInfo().getName();
            // Check if the recipient may be hosted by this server
            if (!recipient.getDomain().contains(serverDomain)) {
                // TODO Implete when s2s is implemented
            }
            else {
                Packet removePacket = createRemoveForward(sender, recipient);
                router.route(removePacket);
            }
Matt Tucker's avatar
Matt Tucker committed
247 248 249 250 251 252 253 254 255 256
        }
    }

    /**
     * Creates a forwarded removal packet.
     *
     * @param from The sender address to use
     * @param to   The recipient address to use
     * @return The forwarded packet generated
     */
Gaston Dombiak's avatar
Gaston Dombiak committed
257 258
    private Packet createRemoveForward(JID from, JID to) {
        org.xmpp.packet.Roster response = new org.xmpp.packet.Roster(IQ.Type.set);
Derek DeMoro's avatar
Derek DeMoro committed
259 260
        response.setFrom(from);
        response.setTo(to);
Gaston Dombiak's avatar
Gaston Dombiak committed
261
        response.addItem(from, org.xmpp.packet.Roster.Subscription.remove);
Matt Tucker's avatar
Matt Tucker committed
262 263 264 265

        return response;
    }

266 267 268 269 270 271
    public void initialize(XMPPServer server) {
        super.initialize(server);
        localServer = server;
        userManager = server.getUserManager();
        router = server.getPacketRouter();
        sessionManager = server.getSessionManager();
Matt Tucker's avatar
Matt Tucker committed
272 273 274 275 276 277 278 279 280 281 282
    }

    public IQHandlerInfo getInfo() {
        return info;
    }

    public Iterator getFeatures() {
        ArrayList features = new ArrayList();
        features.add("jabber:iq:roster");
        return features.iterator();
    }
Matt Tucker's avatar
Matt Tucker committed
283
}