aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/avatar/AvatarPacketGenerator.java
blob: 1574604efaa2d44185369ac9e7c2ac2e16e01dd0 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package de.thedevstack.conversationsplus.xmpp.avatar;

import de.thedevstack.conversationsplus.xmpp.pubsub.PubSubPacketGenerator;
import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.jid.Jid;
import de.thedevstack.conversationsplus.xmpp.pep.Avatar;
import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacket;

/**
 * Generates the IQ Packets for handling Avatars
 * as defined in XEP-0084.
 * @see {@link http://xmpp.org/extensions/xep-0084.html}
 */
public final class AvatarPacketGenerator {
    public static final String NAMESPACE_AVATAR_DATA = "urn:xmpp:avatar:data";
    public static final String NAMESPACE_AVATAR_METADATA = "urn:xmpp:avatar:metadata";

    /**
     * Generates an IqPacket for publishing avatar data.
     * The attributes from and id are not set in here - this is added while sending the packet.
     * <pre>
     *     <iq type='set'>
     *          <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     *              <publish node='urn:xmpp:avatar:data'>
     *                  <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'>
     *                      <data xmlns='urn:xmpp:avatar:data'>
     *                      qANQR1DBwU4DX7jmYZnncm...
     *                      </data>
     *                  </item>
     *              </publish>
     *          </pubsub>
     *      </iq>
     * </pre>
     * @param avatar the avatar to publish
     * @return the IqPacket
     */
    public static IqPacket generatePublishAvatarPacket(Avatar avatar) {
        final Element item = new Element("item");
        item.setAttribute("id", avatar.sha1sum);
        final Element data = item.addChild("data", NAMESPACE_AVATAR_DATA);
        data.setContent(avatar.image);
        return PubSubPacketGenerator.generatePubSubPublishPacket(NAMESPACE_AVATAR_DATA, item);
    }

    /**
     * Generates an IqPacket to retrieve avatar data.
     * The attributes from and id are not set in here - this is added while sending the packet.
     * <pre>
     *     <iq type='get'>
     *       <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     *           <items node='urn:xmpp:avatar:data'>
     *              <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'/>
     *           </items>
     *       </pubsub>
     *      </iq>
     * </pre>
     * @param avatar the avatar to retrieve
     * @return the IqPacket
     */
    public static IqPacket generateRetrieveAvatarPacket(Avatar avatar) {
        final Element item = new Element("item");
        item.setAttribute("id", avatar.sha1sum);
        final IqPacket packet = PubSubPacketGenerator.generatePubSubRetrievePacket(NAMESPACE_AVATAR_DATA, item);
        packet.setTo(avatar.owner);
        return packet;
    }

    /**
     * Generates an IqPacket to publish metadata for an avatar.
     * The attributes from and id are not set in here - this is added while sending the packet.
     * <pre>
     *     <iq type='set'>
     *          <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     *              <publish node='urn:xmpp:avatar:metadata'>
     *                  <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'>
     *                      <metadata xmlns='urn:xmpp:avatar:metadata'>
     *                          <info bytes='12345'
     *                                id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'
     *                                height='64'
     *                                type='image/png'
     *                                width='64'/>
     *                      </metadata>
     *                  </item>
     *              </publish>
     *          </pubsub>
     *      </iq>
     * </pre>
     * @param avatar the avatar to publish the metadata
     * @return the IqPacket
     */
    public static IqPacket generatePublishAvatarMetadataPacket(Avatar avatar) {
        final Element item = new Element("item");
        item.setAttribute("id", avatar.sha1sum);
        final Element metadata = item.addChild("metadata", NAMESPACE_AVATAR_METADATA);
        final Element info = metadata.addChild("info");
        info.setAttribute("bytes", avatar.size);
        info.setAttribute("id", avatar.sha1sum);
        info.setAttribute("height", avatar.height);
        info.setAttribute("width", avatar.height);
        info.setAttribute("type", avatar.type);
        return PubSubPacketGenerator.generatePubSubPublishPacket(NAMESPACE_AVATAR_METADATA, item);
    }

    /**
     * Generates an IqPacket to retrieve metadata of an avatar.
     * The attributes from and id are not set in here - this is added while sending the packet.
     * @param to the Jid to deliver the metadata to
     * @return the IqPacket
     */
    public static IqPacket generateRetrieveAvatarMetadataPacket(Jid to) {
        final IqPacket packet = PubSubPacketGenerator.generatePubSubRetrievePacket(NAMESPACE_AVATAR_METADATA, null);
        if (to != null) {
            packet.setTo(to);
        }
        return packet;
    }

    /**
     * Helper class - private constructor to avoid instantiation
     */
    private AvatarPacketGenerator() {
        // avoid instantiation
    }
}