001/**
002 *
003 * Copyright 2014-2015 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.xdata;
018
019import java.util.Map;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.ConnectionCreationListener;
023import org.jivesoftware.smack.Manager;
024import org.jivesoftware.smack.SmackException.NoResponseException;
025import org.jivesoftware.smack.SmackException.NotConnectedException;
026import org.jivesoftware.smack.XMPPConnection;
027import org.jivesoftware.smack.XMPPConnectionRegistry;
028import org.jivesoftware.smack.XMPPException.XMPPErrorException;
029import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
030import org.jivesoftware.smackx.xdata.packet.DataForm;
031import org.jxmpp.jid.Jid;
032
033public final class XDataManager extends Manager {
034
035    /**
036     * The value of {@link DataForm#NAMESPACE}.
037     */
038    public static final String NAMESPACE = DataForm.NAMESPACE;
039
040    static {
041        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
042            @Override
043            public void connectionCreated(XMPPConnection connection) {
044                getInstanceFor(connection);
045            }
046        });
047    }
048
049    private static final Map<XMPPConnection, XDataManager> INSTANCES = new WeakHashMap<>();
050
051    /**
052     * Get the XDataManager for the given XMPP connection.
053     *
054     * @param connection the XMPPConnection.
055     * @return the XDataManager
056     */
057    public static synchronized XDataManager getInstanceFor(XMPPConnection connection) {
058        XDataManager xDataManager = INSTANCES.get(connection);
059        if (xDataManager == null) {
060            xDataManager = new XDataManager(connection);
061            INSTANCES.put(connection, xDataManager);
062        }
063        return xDataManager;
064    }
065
066    private XDataManager(XMPPConnection connection) {
067        super(connection);
068        ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
069        serviceDiscoveryManager.addFeature(NAMESPACE);
070    }
071
072    /**
073     * Check if the given entity supports data forms.
074     *
075     * @param jid the JID of the entity to check.
076     * @return true if the entity supports data forms.
077     * @throws NoResponseException
078     * @throws XMPPErrorException
079     * @throws NotConnectedException
080     * @throws InterruptedException 
081     * @see <a href="http://xmpp.org/extensions/xep-0004.html#disco">XEP-0004: Data Forms ยง 6. Service Discovery</a>
082     * @since 4.1
083     */
084    public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
085        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
086    }
087}