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;
029
030import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
031import org.jivesoftware.smackx.xdata.packet.DataForm;
032
033import org.jxmpp.jid.Jid;
034
035public final class XDataManager extends Manager {
036
037    /**
038     * The value of {@link DataForm#NAMESPACE}.
039     */
040    public static final String NAMESPACE = DataForm.NAMESPACE;
041
042    static {
043        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
044            @Override
045            public void connectionCreated(XMPPConnection connection) {
046                getInstanceFor(connection);
047            }
048        });
049    }
050
051    private static final Map<XMPPConnection, XDataManager> INSTANCES = new WeakHashMap<>();
052
053    /**
054     * Get the XDataManager for the given XMPP connection.
055     *
056     * @param connection the XMPPConnection.
057     * @return the XDataManager
058     */
059    public static synchronized XDataManager getInstanceFor(XMPPConnection connection) {
060        XDataManager xDataManager = INSTANCES.get(connection);
061        if (xDataManager == null) {
062            xDataManager = new XDataManager(connection);
063            INSTANCES.put(connection, xDataManager);
064        }
065        return xDataManager;
066    }
067
068    private XDataManager(XMPPConnection connection) {
069        super(connection);
070        ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
071        serviceDiscoveryManager.addFeature(NAMESPACE);
072    }
073
074    /**
075     * Check if the given entity supports data forms.
076     *
077     * @param jid the JID of the entity to check.
078     * @return true if the entity supports data forms.
079     * @throws NoResponseException
080     * @throws XMPPErrorException
081     * @throws NotConnectedException
082     * @throws InterruptedException 
083     * @see <a href="http://xmpp.org/extensions/xep-0004.html#disco">XEP-0004: Data Forms ยง 6. Service Discovery</a>
084     * @since 4.1
085     */
086    public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
087        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
088    }
089}