001/**
002 *
003 * Copyright 2020 Aditya Borikar
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.softwareinfo;
018
019import java.util.Map;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.Manager;
023import org.jivesoftware.smack.SmackException.NoResponseException;
024import org.jivesoftware.smack.SmackException.NotConnectedException;
025import org.jivesoftware.smack.XMPPConnection;
026import org.jivesoftware.smack.XMPPException.XMPPErrorException;
027
028import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
029import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
030import org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm;
031import org.jivesoftware.smackx.xdata.packet.DataForm;
032
033import org.jxmpp.jid.Jid;
034
035/**
036* Entry point for Smack's API for XEP-0232: Software Information.
037* <br>
038* @see <a href="https://xmpp.org/extensions/xep-0232.html">
039*     XEP-0232 : Software Information.
040*     </a>
041*/
042public final class SoftwareInfoManager extends Manager {
043
044    private static final Map<XMPPConnection, SoftwareInfoManager> INSTANCES = new WeakHashMap<>();
045
046    private final ServiceDiscoveryManager serviceDiscoveryManager;
047
048    public static synchronized SoftwareInfoManager getInstanceFor (XMPPConnection connection) {
049        SoftwareInfoManager manager = INSTANCES.get(connection);
050        if (manager == null) {
051            manager = new SoftwareInfoManager(connection);
052            INSTANCES.put(connection, manager);
053        }
054        return manager;
055    }
056
057    private SoftwareInfoManager(XMPPConnection connection) {
058        super(connection);
059        serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
060    }
061
062    /**
063     * Publishes the provided {@link SoftwareInfoForm} as an extended info.
064     * <br>
065     * @param softwareInfoForm form to be added as an extended info
066     */
067    public void publishSoftwareInformationForm(SoftwareInfoForm softwareInfoForm) {
068        serviceDiscoveryManager.addExtendedInfo(softwareInfoForm.getDataForm());
069    }
070
071    /**
072     * Get Software Information from the provided XMPP address. Returns <code>null</code> in case the queried entity does not announce that information.
073     *
074     * @param jid jid to get software information from
075     * @return {@link SoftwareInfoForm} Form containing software information or <code>null</code>.
076     * @throws NoResponseException if there was no response from the remote entity
077     * @throws XMPPErrorException if there was an XMPP error returned
078     * @throws NotConnectedException if the XMPP connection is not connected
079     * @throws InterruptedException if the calling thread was interrupted
080     */
081    public SoftwareInfoForm fromJid(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
082        DiscoverInfo discoverInfo = serviceDiscoveryManager.discoverInfo(jid);
083        DataForm dataForm = DataForm.from(discoverInfo, SoftwareInfoForm.FORM_TYPE);
084        if (dataForm == null) {
085            return null;
086        }
087        return SoftwareInfoForm.getBuilder()
088                               .setDataForm(dataForm)
089                               .build();
090    }
091}