aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/pubsub/PubSubPacketGenerator.java
blob: f72bf777d27099741ab01ca23487acbc62c0f3db (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
package de.thedevstack.conversationsplus.xmpp.pubsub;

import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;

/**
 * Generates the IQ packets for Pubsub Subscription as defined in XEP-0060.
 * @see <a href="http://xmpp.org/extensions/xep-0060.html">http://xmpp.org/extensions/xep-0060.html</a>
 */
public final class PubSubPacketGenerator {

    /**
     * Generates a pubsub publish packet.
     * The attributes from and id are not set in here - this is added while sending the packet.
     * <pre>
     * <iq type='set'
     *     from='hamlet@denmark.lit/blogbot'
     *     to='pubsub.shakespeare.lit'
     *     id='publish1'>
     *   <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     *     <publish node='princely_musings'>
     *       <item id='bnd81g37d61f49fgn581'>
     *         ...
     *       </item>
     *     </publish>
     *   </pubsub>
     * </iq>
     * </pre>
     * @param nodeName the name of the publish node
     * @param item the item element
     * @return the generated PubSubPacket
     */
    public static PubSubPacket generatePubSubPublishPacket(String nodeName, Element item) {
        final PubSubPacket pubsub = new PubSubPacket(IqPacket.TYPE.SET);
        final Element publish = pubsub.addChild("publish");
        publish.setAttribute("node", nodeName);
        publish.addChild(item);
        return pubsub;
    }

    /**
     * Generates a pubsub retrieve packet.
     * The attributes from and id are not set in here - this is added while sending the packet.
     * <pre>
     * <iq type='get'
     *     from='romeo@montague.lit/home'
     *     to='juliet@capulet.lit'
     *     id='retrieve1'>
     *   <pubsub xmlns='http://jabber.org/protocol/pubsub'>
     *     <items node='urn:xmpp:avatar:data'>
     *       <item id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'/>
     *     </items>
     *   </pubsub>
     * </iq>
     * </pre>
     * @param nodeName
     * @param item
     * @return
     */
    public static PubSubPacket generatePubSubRetrievePacket(String nodeName, Element item) {
        final PubSubPacket pubsub = new PubSubPacket(IqPacket.TYPE.GET);
        final Element items = pubsub.addChild("items");
        items.setAttribute("node", nodeName);
        if (item != null) {
            items.addChild(item);
        }
        return pubsub;
    }

    /**
     * Utility class - avoid instantiation
     */
    private PubSubPacketGenerator() {
        // Avoid instantiation
    }
}