IQDiscoItemsHandler.java 9.38 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 16 17 18 19
package org.jivesoftware.messenger.disco;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
20
import org.dom4j.QName;
Derek DeMoro's avatar
Derek DeMoro committed
21 22
import org.jivesoftware.messenger.IQHandlerInfo;
import org.jivesoftware.messenger.XMPPServer;
23
import org.jivesoftware.messenger.handler.IQHandler;
Derek DeMoro's avatar
Derek DeMoro committed
24 25 26 27 28
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.StringUtils;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
Matt Tucker's avatar
Matt Tucker committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

/**
 * IQDiscoItemsHandler is responsible for handling disco#items requests. This class holds a map with
 * the main entities and the associated DiscoItemsProvider. We are considering the host of the
 * recipient JIDs as main entities. It's the DiscoItemsProvider responsibility to provide the items
 * associated with the JID's name together with any possible requested node.<p>
 * <p/>
 * For example, let's have in the entities map the following entries: "localhost" and
 * "conference.localhost". Associated with each entry we have different DiscoItemsProvider. Now we
 * receive a disco#items request for the following JID: "room@conference.localhost" which is a disco
 * request for a MUC room. So IQDiscoItemsHandler will look for the DiscoItemsProvider associated
 * with the JID's host which in this case is "conference.localhost". Once we have located the
 * provider we will delegate to the provider the responsibility to provide the items specific to
 * the JID's name which in this case is "room". Depending on the implementation, the items could be
 * the list of existing occupants if that information is publicly available. Finally, after we have
 * collected all the items provided by the provider we will add them to the reply. On the other
 * hand, if no provider was found or the provider has no information for the requested name/node
 * then a not-found error will be returned.<p>
 * <p/>
 * Publishing of client items is still not supported.
 *
 * @author Gaston Dombiak
 */
52
public class IQDiscoItemsHandler extends IQHandler implements ServerFeaturesProvider {
Matt Tucker's avatar
Matt Tucker committed
53 54

    private HashMap entities = new HashMap();
55
    private List<Element> serverItems = new ArrayList<Element>();
Matt Tucker's avatar
Matt Tucker committed
56
    private IQHandlerInfo info;
57
    private IQDiscoInfoHandler infoHandler;
Matt Tucker's avatar
Matt Tucker committed
58 59 60

    public IQDiscoItemsHandler() {
        super("XMPP Disco Items Handler");
61
        info = new IQHandlerInfo("query", "http://jabber.org/protocol/disco#items");
Matt Tucker's avatar
Matt Tucker committed
62 63 64 65 66 67
    }

    public IQHandlerInfo getInfo() {
        return info;
    }

Derek DeMoro's avatar
Derek DeMoro committed
68
    public IQ handleIQ(IQ packet) throws UnauthorizedException {
Matt Tucker's avatar
Matt Tucker committed
69 70 71 72 73
        // TODO Let configure an authorization policy (ACL?). Currently anyone can discover items.
        
        // Create a copy of the sent pack that will be used as the reply
        // we only need to add the requested items to the reply if any otherwise add 
        // a not found error
Derek DeMoro's avatar
Derek DeMoro committed
74 75 76 77
        IQ reply = IQ.createResultIQ(packet);
        reply.setType(IQ.Type.result);
        reply.setTo(packet.getFrom());
        reply.setFrom(packet.getFrom());
Matt Tucker's avatar
Matt Tucker committed
78 79
        
        // TODO Implement publishing client items
Derek DeMoro's avatar
Derek DeMoro committed
80 81
        if (IQ.Type.set == packet.getType()) {
            reply.setError(PacketError.Condition.feature_not_implemented);
Matt Tucker's avatar
Matt Tucker committed
82 83 84 85 86 87 88
            return reply;
        }

        // Look for a DiscoItemsProvider associated with the requested entity.
        // We consider the host of the recipient JID of the packet as the entity. It's the 
        // DiscoItemsProvider responsibility to provide the items associated with the JID's name  
        // together with any possible requested node.  
89
        DiscoItemsProvider itemsProvider = getProvider(packet.getTo() == null ?
90
                XMPPServer.getInstance().getServerInfo().getName() : packet.getTo().getDomain());
Matt Tucker's avatar
Matt Tucker committed
91 92
        if (itemsProvider != null) {
            // Get the JID's name
93
            String name = packet.getTo() == null ? null : packet.getTo().getNode();
Matt Tucker's avatar
Matt Tucker committed
94 95 96 97
            if (name == null || name.trim().length() == 0) {
                name = null;
            }
            // Get the requested node
Derek DeMoro's avatar
Derek DeMoro committed
98 99
            Element iq = packet.getChildElement();
            String node = iq.attributeValue("node");
100

Matt Tucker's avatar
Matt Tucker committed
101
            // Check if we have items associated with the requested name and node
102
            Iterator<Element> itemsItr = itemsProvider.getItems(name, node, packet.getFrom());
Matt Tucker's avatar
Matt Tucker committed
103
            if (itemsItr != null) {
104
                reply.setChildElement(iq.createCopy());
Derek DeMoro's avatar
Derek DeMoro committed
105
                Element queryElement = reply.getChildElement();
Matt Tucker's avatar
Matt Tucker committed
106 107 108 109

                // Add to the reply all the items provided by the DiscoItemsProvider
                Element item;
                while (itemsItr.hasNext()) {
110 111 112
                    item = itemsItr.next();
                    item.setQName(new QName(item.getName(), queryElement.getNamespace()));
                    queryElement.add(item.createCopy());
Matt Tucker's avatar
Matt Tucker committed
113 114 115 116 117 118
                }
                ;
            }
            else {
                // If the DiscoItemsProvider has no items for the requested name and node 
                // then answer a not found error
Derek DeMoro's avatar
Derek DeMoro committed
119
                reply.setError(PacketError.Condition.item_not_found);
Matt Tucker's avatar
Matt Tucker committed
120 121 122 123
            }
        }
        else {
            // If we didn't find a DiscoItemsProvider then answer a not found error
Derek DeMoro's avatar
Derek DeMoro committed
124
           reply.setError(PacketError.Condition.item_not_found);
Matt Tucker's avatar
Matt Tucker committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        }

        return reply;
    }

    /**
     * Returns the DiscoItemsProvider responsible for providing the items related to a given entity
     * or null if none was found.
     *
     * @param name the name of the identity.
     * @return the DiscoItemsProvider responsible for providing the items related to a given entity
     *         or null if none was found.
     */
    private DiscoItemsProvider getProvider(String name) {
        return (DiscoItemsProvider)entities.get(name);
    }

    /**
     * Sets that a given DiscoItemsProvider will provide the items related to a given entity. This
     * message must be used when new modules (e.g. MUC) are implemented and need to provide
     * the items related to them.
     *
     * @param name     the name of the entity.
     * @param provider the DiscoItemsProvider that will provide the entity's items.
     */
    protected void setProvider(String name, DiscoItemsProvider provider) {
        entities.put(name, provider);
    }

    /**
     * Removes the DiscoItemsProvider related to a given entity.
     *
     * @param name the name of the entity.
     */
    protected void removeProvider(String name) {
        entities.remove(name);
    }

    /**
     * Adds the items provided by the new service that implements the ServerItemsProvider
     * interface. This information will be used whenever a disco for items is made against
     * the server (i.e. the packet's target is the server).
     * Example of item is: &lt;item jid='conference.localhost' name='Public chatrooms'/&gt;
     *
     * @param provider the ServerItemsProvider that provides new server items.
     */
171
    private void addServerItemsProvider(ServerItemsProvider provider) {
Matt Tucker's avatar
Matt Tucker committed
172 173 174 175 176 177 178 179 180 181 182 183
        DiscoServerItem discoItem;
        for (Iterator it = provider.getItems(); it.hasNext();) {
            discoItem = (DiscoServerItem)it.next();
            // Create a new element based on the provided DiscoItem
            Element element = DocumentHelper.createElement("item");
            element.addAttribute("jid", discoItem.getJID());
            element.addAttribute("node", discoItem.getNode());
            element.addAttribute("name", discoItem.getName());
            // Add the element to the list of items related to the server
            serverItems.add(element);
            
            // Add the new item as a valid entity that could receive info and items disco requests
184
            String host = StringUtils.parseServer(discoItem.getJID());
Matt Tucker's avatar
Matt Tucker committed
185 186 187 188 189
            infoHandler.setProvider(host, discoItem.getDiscoInfoProvider());
            setProvider(host, discoItem.getDiscoItemsProvider());
        }
    }

190 191
    public void initialize(XMPPServer server) {
        super.initialize(server);
Matt Tucker's avatar
Matt Tucker committed
192 193
        // Track the implementors of ServerItemsProvider so that we can collect the items
        // provided by the server
194
        infoHandler = server.getIQDiscoInfoHandler();
195 196 197 198 199
        setProvider(server.getServerInfo().getName(), getServerItemsProvider());
    }

    public void start() throws IllegalStateException {
        super.start();
200
        for (ServerItemsProvider provider : XMPPServer.getInstance().getServerItemsProviders()) {
201
            addServerItemsProvider(provider);
Matt Tucker's avatar
Matt Tucker committed
202 203 204 205 206 207 208 209 210 211 212 213 214
        }
    }

    public Iterator getFeatures() {
        ArrayList features = new ArrayList();
        features.add("http://jabber.org/protocol/disco#items");
        // TODO Comment out this line when publishing of client items is implemented
        //features.add("http://jabber.org/protocol/disco#publish");
        return features.iterator();
    }

    private DiscoItemsProvider getServerItemsProvider() {
        DiscoItemsProvider discoItemsProvider = new DiscoItemsProvider() {
215
            public Iterator<Element> getItems(String name, String node, JID senderJID)
Matt Tucker's avatar
Matt Tucker committed
216 217 218 219 220 221 222 223
                    throws UnauthorizedException {
                return serverItems.iterator();
            }
        };
        return discoItemsProvider;
    }

}