1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-13 23:24:50 +02:00
Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManager.java
Florian Schmaus 3d4e7938a7 Make ExtensionElement marker interface wrt. QNAME field
ExtensionElement is now a marker interface that requires all
implementation non-abstract classes to carry a static final QNAME
field (of type QName). This is verified by a new unit test.

Also FullyQualifiedElement is renamed to simply XmlElement. XmlElement
is used over ExtensionElement when implementing classes do not
statically know the qualified name of the XML elements they
represent. In general, XmlElement should be used sparingly, and every
XML element should be modeled by its own Java class (implementing
ExtensionElement).
2021-04-18 21:07:19 +02:00

92 lines
3.6 KiB
Java

/**
*
* Copyright 2020 Aditya Borikar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.softwareinfo;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.jxmpp.jid.Jid;
/**
* Entry point for Smack's API for XEP-0232: Software Information.
* <br>
* @see <a href="https://xmpp.org/extensions/xep-0232.html">
* XEP-0232 : Software Information.
* </a>
*/
public final class SoftwareInfoManager extends Manager {
private static final Map<XMPPConnection, SoftwareInfoManager> INSTANCES = new WeakHashMap<>();
private final ServiceDiscoveryManager serviceDiscoveryManager;
public static synchronized SoftwareInfoManager getInstanceFor (XMPPConnection connection) {
SoftwareInfoManager manager = INSTANCES.get(connection);
if (manager == null) {
manager = new SoftwareInfoManager(connection);
INSTANCES.put(connection, manager);
}
return manager;
}
private SoftwareInfoManager(XMPPConnection connection) {
super(connection);
serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
}
/**
* Publishes the provided {@link SoftwareInfoForm} as an extended info.
* <br>
* @param softwareInfoForm form to be added as an extended info
*/
public void publishSoftwareInformationForm(SoftwareInfoForm softwareInfoForm) {
serviceDiscoveryManager.addExtendedInfo(softwareInfoForm.getDataForm());
}
/**
* Get Software Information from the provided XMPP address. Returns <code>null</code> in case the queried entity does not announce that information.
*
* @param jid jid to get software information from
* @return {@link SoftwareInfoForm} Form containing software information or <code>null</code>.
* @throws NoResponseException if there was no response from the remote entity
* @throws XMPPErrorException if there was an XMPP error returned
* @throws NotConnectedException if the XMPP connection is not connected
* @throws InterruptedException if the calling thread was interrupted
*/
public SoftwareInfoForm fromJid(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
DiscoverInfo discoverInfo = serviceDiscoveryManager.discoverInfo(jid);
DataForm dataForm = DataForm.from(discoverInfo, SoftwareInfoForm.FORM_TYPE);
if (dataForm == null) {
return null;
}
return SoftwareInfoForm.getBuilder()
.setDataForm(dataForm)
.build();
}
}