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