aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/filetransfer/http/upload/HttpUploadRequestSlotPacketGenerator.java
blob: db455b7ad758388780c4c67a6d3dc28c177d4c5a (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
package de.thedevstack.conversationsplus.xmpp.filetransfer.http.upload;

import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.xmpp.filetransfer.http.FileTransferHttp;
import de.thedevstack.conversationsplus.xmpp.jid.Jid;
import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket;

/**
 * Generates the IQ Packets for requesting a http upload slot
 * as defined in XEP-0363.
 * @see <a href="http://xmpp.org/extensions/xep-0363.html">http://xmpp.org/extensions/xep-0363.html</a>
 */
public final class HttpUploadRequestSlotPacketGenerator {
    /**
     * Generates the IqPacket to request a http upload slot.
     * The attributes from and id are not set in here - this is added while sending the packet.
     * <pre>
     * <iq from='romeo@montague.tld/garden'
     *     id='step_03'
     *     to='upload.montague.tld'
     *     type='get'>
     *   <request xmlns='urn:xmpp:http:upload'>
     *     <filename>my_juliet.png</filename>
     *     <size>23456</size>
     *     <content-type>image/jpeg</content-type>
     *   </request>
     * </iq>
     * </pre>
     * @param account the account requesting a slot
     * @param filename the filename of the file which will be transferred
     * @param filesize the filesize of the file which will be transferred
     * @param mime the mime type of the file which will be transferred - <code>optional</code> and therefore nullable
     * @return the IqPacket
     */
    public static IqPacket generate(Account account, Jid recipient, String filename, long filesize, String mime) {
        String namespace = getNamespace(account);
        Jid host = getHost(account, namespace);
        IqPacket requestPacket;
        switch (namespace) {
            case HttpUpload.NAMESPACE:
                requestPacket = new HttpUploadSlotRequestPacket(filename, filesize, mime);
            break;
            case FileTransferHttp.NAMESPACE:
            default:
                requestPacket = new FileTransferHttpUploadSlotRequestPacket(recipient.toString(), filename, filesize, mime);
        }
        requestPacket.setTo(host);
        return requestPacket;
    }

    private static String getNamespace(Account account) {
        if (null != account.getXmppConnection().findDiscoItemByFeature(FileTransferHttp.NAMESPACE)) {
            return FileTransferHttp.NAMESPACE;
        } else {
            return HttpUpload.NAMESPACE;
        }
    }

    private static Jid getHost(Account account, String namespace) {
        return account.getXmppConnection().findDiscoItemByFeature(namespace);
    }

    /**
     * Utility class - avoid instantiation
     */
    private HttpUploadRequestSlotPacketGenerator() {
        // Helper class - avoid instantiation
    }
}