summaryrefslogtreecommitdiffstats
path: root/src/de/thedevstack/smackx/filetransferhttp/FileTransferHttpManager.java
blob: fffbcbd317fe46ad92f7bd5d765f4581efbb8e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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<XMPPConnection, FileTransferHttpManager> 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);
    }

}