package de.thedevstack.smackx.filetransferhttp; import java.util.Map; import java.util.WeakHashMap; import java.util.logging.Logger; import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnectionRegistry; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import de.thedevstack.smackx.filetransferhttp.element.FileList; import de.thedevstack.smackx.filetransferhttp.element.Request; public class FileTransferHttpManager extends Manager { private static final Logger LOGGER = Logger.getLogger(FileTransferHttpManager.class.getName()); static { XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { @Override public void connectionCreated(XMPPConnection connection) { getInstanceFor(connection); } }); } private static final Map INSTANCES = new WeakHashMap<>(); /** * Obtain the HttpFileUploadManager responsible for a connection. * * @param connection the connection object. * @return a HttpFileUploadManager instance */ public static synchronized FileTransferHttpManager getInstanceFor(XMPPConnection connection) { FileTransferHttpManager fileTransferHttpManager = INSTANCES.get(connection); if (fileTransferHttpManager == null) { fileTransferHttpManager = new FileTransferHttpManager(connection); INSTANCES.put(connection, fileTransferHttpManager); } return fileTransferHttpManager; } private FileTransferHttpManager(XMPPConnection connection) { super(connection); } public FileList requestFileList() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { final XMPPConnection connection = connection(); Request request = new Request(); request.setTo(connection.getXMPPServiceDomain()); return connection.createStanzaCollectorAndSend(request).nextResultOrThrow(); } /** * Returns true if XMPP Carbons are supported by the server. * * @return true if supported * @throws NotConnectedException * @throws XMPPErrorException * @throws NoResponseException * @throws InterruptedException */ public boolean isSupportedByServer() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { return ServiceDiscoveryManager.getInstanceFor(connection()).serverSupportsFeature(FileTransferHttp.NAMESPACE); } }