IQDiscoInfoHandler.java 11.5 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
package org.jivesoftware.messenger.disco;

Derek DeMoro's avatar
Derek DeMoro committed
14
import org.jivesoftware.messenger.forms.spi.XDataFormImpl;
Matt Tucker's avatar
Matt Tucker committed
15
import org.jivesoftware.messenger.*;
16 17
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.UserNotFoundException;
18
import org.jivesoftware.messenger.handler.IQHandler;
Matt Tucker's avatar
Matt Tucker committed
19 20 21 22 23 24 25
import org.jivesoftware.messenger.auth.UnauthorizedException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
26
import org.dom4j.QName;
Derek DeMoro's avatar
Derek DeMoro committed
27 28 29
import org.xmpp.packet.IQ;
import org.xmpp.packet.PacketError;
import org.xmpp.packet.JID;
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

/**
 * IQDiscoInfoHandler is responsible for handling disco#info requests. This class holds a map with
 * the main entities and the associated DiscoInfoProvider. We are considering the host of the
 * recipient JIDs as main entities. It's the DiscoInfoProvider responsibility to provide information
 * about 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 DiscoInfoProviders. Now we
 * receive a disco#info request for the following JID: "room@conference.localhost" which is a disco
 * request for a MUC room. So IQDiscoInfoHandler will look for the DiscoInfoProvider 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 info specific to
 * the JID's name which in this case is "room". Among the information that a room could provide we
 * could find its identity and the features it supports (e.g. 'muc_passwordprotected',
 * 'muc_unmoderated', etc.). Finally, after we have collected all the information provided by the
 * provider we will add it 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.
 *
 * @author Gaston Dombiak
 */
51
public class IQDiscoInfoHandler extends IQHandler {
Matt Tucker's avatar
Matt Tucker committed
52 53 54 55 56

    private HashMap entities = new HashMap();
    private List serverFeatures = new ArrayList();
    private IQHandlerInfo info;

57 58 59
    private List userIdentities = new ArrayList();
    private List userFeatures = new ArrayList();

Matt Tucker's avatar
Matt Tucker committed
60 61
    public IQDiscoInfoHandler() {
        super("XMPP Disco Info Handler");
62
        info = new IQHandlerInfo("query", "http://jabber.org/protocol/disco#info");
Matt Tucker's avatar
Matt Tucker committed
63
        serverFeatures.add("http://jabber.org/protocol/disco#info");
64 65 66 67 68 69 70
        // Initialize the user identity and features collections (optimization to avoid creating
        // the same objects for each response)
        Element userIdentity = DocumentHelper.createElement("identity");
        userIdentity.addAttribute("category", "account");
        userIdentity.addAttribute("type", "registered");
        userIdentities.add(userIdentity);
        userFeatures.add("http://jabber.org/protocol/disco#info");
Matt Tucker's avatar
Matt Tucker committed
71 72 73 74 75 76
    }

    public IQHandlerInfo getInfo() {
        return info;
    }

Derek DeMoro's avatar
Derek DeMoro committed
77
    public IQ handleIQ(IQ packet) throws UnauthorizedException {
Matt Tucker's avatar
Matt Tucker committed
78 79 80 81 82
        // TODO Let configure an authorization policy (ACL?). Currently anyone can discover info.
        
        // Create a copy of the sent pack that will be used as the reply
        // we only need to add the requested info to the reply if any otherwise add 
        // a not found error
Derek DeMoro's avatar
Derek DeMoro committed
83
        IQ reply = IQ.createResultIQ(packet);
Matt Tucker's avatar
Matt Tucker committed
84 85 86 87 88

        // Look for a DiscoInfoProvider associated with the requested entity.
        // We consider the host of the recipient JID of the packet as the entity. It's the 
        // DiscoInfoProvider responsibility to provide information about the JID's name together 
        // with any possible requested node.  
89
        DiscoInfoProvider infoProvider = getProvider(packet.getTo() == null ?
90
                XMPPServer.getInstance().getServerInfo().getName() : packet.getTo().getDomain());
Matt Tucker's avatar
Matt Tucker committed
91 92
        if (infoProvider != 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 100
            Element iq = packet.getChildElement();
            String node = iq.attributeValue("node");
            //String node = metaData.getProperty("query:node");
Matt Tucker's avatar
Matt Tucker committed
101 102
            
            // Check if we have information about the requested name and node
Derek DeMoro's avatar
Derek DeMoro committed
103
            if (infoProvider.hasInfo(name, node, packet.getFrom())) {
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

                // Add to the reply all the identities provided by the DiscoInfoProvider
                Element identity;
Derek DeMoro's avatar
Derek DeMoro committed
109
                Iterator identities = infoProvider.getIdentities(name, node, packet.getFrom());
Matt Tucker's avatar
Matt Tucker committed
110 111
                while (identities.hasNext()) {
                    identity = (Element)identities.next();
112
                    identity.setQName(new QName(identity.getName(), queryElement.getNamespace()));
Matt Tucker's avatar
Matt Tucker committed
113 114 115 116
                    queryElement.add((Element)identity.clone());
                }
                
                // Add to the reply all the features provided by the DiscoInfoProvider
Derek DeMoro's avatar
Derek DeMoro committed
117
                Iterator features = infoProvider.getFeatures(name, node, packet.getFrom());
Matt Tucker's avatar
Matt Tucker committed
118
                while (features.hasNext()) {
119
                    queryElement.addElement("feature").addAttribute("var", (String)features.next());
Matt Tucker's avatar
Matt Tucker committed
120 121 122
                }

                // Add to the reply the extended info (XDataForm) provided by the DiscoInfoProvider
Derek DeMoro's avatar
Derek DeMoro committed
123
                XDataFormImpl dataForm = infoProvider.getExtendedInfo(name, node, packet.getFrom());
Matt Tucker's avatar
Matt Tucker committed
124 125 126 127 128 129 130
                if (dataForm != null) {
                    queryElement.add(dataForm.asXMLElement());
                }
            }
            else {
                // If the DiscoInfoProvider has no information for the requested name and node 
                // then answer a not found error
131
                reply.setChildElement(packet.getChildElement().createCopy());
Derek DeMoro's avatar
Derek DeMoro committed
132
                reply.setError(PacketError.Condition.item_not_found);
Matt Tucker's avatar
Matt Tucker committed
133 134 135 136
            }
        }
        else {
            // If we didn't find a DiscoInfoProvider then answer a not found error
137
            reply.setChildElement(packet.getChildElement().createCopy());
Derek DeMoro's avatar
Derek DeMoro committed
138
            reply.setError(PacketError.Condition.item_not_found);
Matt Tucker's avatar
Matt Tucker committed
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        }

        return reply;
    }

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

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

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

    /**
     * Adds the features provided by the new service that implements the ServerFeaturesProvider
     * interface. This information will be used whenever a disco for information is made against
     * the server (i.e. the packet's target is the server).
     * Example of features are: jabber:iq:agents, jabber:iq:time, etc.
     *
     * @param provider the ServerFeaturesProvider that provides new server features.
     */
185
    private void addServerFeaturesProvider(ServerFeaturesProvider provider) {
Matt Tucker's avatar
Matt Tucker committed
186 187 188 189 190
        for (Iterator it = provider.getFeatures(); it.hasNext();) {
            serverFeatures.add(it.next());
        }
    }

191 192
    public void initialize(XMPPServer server) {
        super.initialize(server);
Matt Tucker's avatar
Matt Tucker committed
193 194
        // Track the implementors of ServerFeaturesProvider so that we can collect the features
        // provided by the server
195 196
        for (ServerFeaturesProvider provider : server.getServerFeaturesProviders()) {
            addServerFeaturesProvider(provider);
Matt Tucker's avatar
Matt Tucker committed
197
        }
198
        setProvider(server.getServerInfo().getName(), getServerInfoProvider());
Matt Tucker's avatar
Matt Tucker committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212
    }

    /**
     * Returns the DiscoInfoProvider responsible for providing information at the server level. This
     * means that this DiscoInfoProvider will provide information whenever a disco request whose
     * recipient JID is the server (e.g. localhost) is made.
     *
     * @return the DiscoInfoProvider responsible for providing information at the server level.
     */
    private DiscoInfoProvider getServerInfoProvider() {
        DiscoInfoProvider discoInfoProvider = new DiscoInfoProvider() {
            ArrayList identities = new ArrayList();
            ArrayList features = new ArrayList();

Derek DeMoro's avatar
Derek DeMoro committed
213
            public Iterator getIdentities(String name, String node, JID senderJID) {
214 215 216 217 218 219 220 221 222 223 224
                if (name == null) {
                    // Answer identity of the server
                    synchronized (identities) {
                        if (identities.isEmpty()) {
                            Element identity = DocumentHelper.createElement("identity");
                            identity.addAttribute("category", "services");
                            identity.addAttribute("name", "Messenger Server");
                            identity.addAttribute("type", "jabber");

                            identities.add(identity);
                        }
Matt Tucker's avatar
Matt Tucker committed
225
                    }
226 227 228 229 230 231
                    return identities.iterator();
                }
                else {
                    // Answer identity of a registered user.
                    // Note: We know that this user exists because #hasInfo returned true
                    return userIdentities.iterator();
Matt Tucker's avatar
Matt Tucker committed
232 233 234
                }
            }

Derek DeMoro's avatar
Derek DeMoro committed
235
            public Iterator getFeatures(String name, String node, JID senderJID) {
236 237 238 239 240 241 242 243
                if (name == null) {
                    // Answer features of the server
                    return serverFeatures.iterator();
                }
                else {
                    // Answer features of the user
                    return userFeatures.iterator();
                }
Matt Tucker's avatar
Matt Tucker committed
244 245
            }

246 247 248 249 250 251 252 253 254 255
            public boolean hasInfo(String name, String node, JID senderJID) {
                try {
                    // True if it is an info request of the server or of a registered user. We
                    // now support disco of user's bare JIDs
                    return node == null &&
                            (name == null || UserManager.getInstance().getUser(name) != null);
                }
                catch (UserNotFoundException e) {
                    return false;
                }
Matt Tucker's avatar
Matt Tucker committed
256 257
            }

Derek DeMoro's avatar
Derek DeMoro committed
258
            public XDataFormImpl getExtendedInfo(String name, String node, JID senderJID) {
Matt Tucker's avatar
Matt Tucker committed
259 260 261 262 263 264
                return null;
            }
        };
        return discoInfoProvider;
    }
}