summaryrefslogtreecommitdiffstats
blob: 2af92bf105d77014326bfd445134dd1e5a673a47 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (c) 2014, Isode Limited, London, England.
* All rights reserved.
*/
/*
* Copyright (c) 2014, Remko Tronçon.
* All rights reserved.
*/

package com.isode.stroke.pubsub;

import com.isode.stroke.elements.DiscoInfo;
import com.isode.stroke.elements.DiscoItems;
import com.isode.stroke.elements.ErrorPayload;
import com.isode.stroke.elements.Form;
import com.isode.stroke.elements.FormField;
import com.isode.stroke.elements.FormField.Type;
import com.isode.stroke.elements.IQ;
import com.isode.stroke.elements.Payload;
import com.isode.stroke.elements.PubSub;
import com.isode.stroke.elements.PubSubCreate;
import com.isode.stroke.elements.PubSubItem;
import com.isode.stroke.elements.PubSubItems;
import com.isode.stroke.elements.PubSubOwnerAffiliation;
import com.isode.stroke.elements.PubSubOwnerAffiliations;
import com.isode.stroke.elements.PubSubOwnerConfigure;
import com.isode.stroke.elements.PubSubOwnerDefault;
import com.isode.stroke.elements.PubSubOwnerDelete;
import com.isode.stroke.elements.PubSubOwnerPubSub;
import com.isode.stroke.elements.PubSubOwnerPurge;
import com.isode.stroke.elements.PubSubOwnerRedirect;
import com.isode.stroke.elements.PubSubOwnerSubscriptions;
import com.isode.stroke.elements.PubSubPublish;
import com.isode.stroke.elements.PubSubRetract;
import com.isode.stroke.elements.PubSubSubscribe;
import com.isode.stroke.elements.PubSubSubscriptions;
import com.isode.stroke.elements.PubSubUnsubscribe;
import com.isode.stroke.jid.JID;
import com.isode.stroke.queries.GenericRequest;
import com.isode.stroke.signals.Slot2;

public class PubSubTools {
    static void create(Client client, String domain, String node, Slot2<PubSub, ErrorPayload> callback) {
        /* create a node on a pubsub domain */
        
        PubSubCreate create = new PubSubCreate();
        create.setNode(node);
        
        PubSub pubSub = new PubSub();
        pubSub.setPayload(create);
        
        GenericRequest<PubSub> req = new GenericRequest<PubSub>(IQ.Type.Set, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void delete(Client client, String domain, String node, String redirectUri, Slot2<PubSubOwnerPubSub, ErrorPayload> callback) {
        /* delete a node on a pubsub domain */
        
        PubSubOwnerDelete delete = new PubSubOwnerDelete();
        delete.setNode(node);
        
        if (!redirectUri.isEmpty()) {
            PubSubOwnerRedirect redirect = new PubSubOwnerRedirect();
            redirect.setURI(redirectUri);
            delete.setRedirect(redirect);
        }
        
        PubSubOwnerPubSub pubSub = new PubSubOwnerPubSub();
        pubSub.setPayload(delete);
        
        GenericRequest<PubSubOwnerPubSub> req = new GenericRequest<PubSubOwnerPubSub>(IQ.Type.Set, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void itemList(Client client, String domain, Slot2<DiscoItems, ErrorPayload> callback) {
        /* use disco to list the nodes on a pubsub domain */
        
        DiscoItems disco = new DiscoItems();
        
        GenericRequest<DiscoItems> req = new GenericRequest<DiscoItems>(IQ.Type.Get, new JID(domain), disco, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void featureList(Client client, String domain, Slot2<DiscoInfo, ErrorPayload> callback) {
        /* use disco to list the features of the domain */
        
        DiscoInfo disco = new DiscoInfo();
        
        GenericRequest<DiscoInfo> req = new GenericRequest<DiscoInfo>(IQ.Type.Get, new JID(domain), disco, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void subscriptionList(Client client, String domain, String node, Slot2<PubSub, ErrorPayload> callback) {
        /* list subscriptions on a pubsub node */
        
        PubSubSubscriptions subscriptions = new PubSubSubscriptions();
        subscriptions.setNode(node);
        
        PubSub pubsub = new PubSub();
        pubsub.setPayload(subscriptions);
        
        GenericRequest<PubSub> listreq = new GenericRequest<PubSub>(IQ.Type.Get, new JID(domain), pubsub, client.getIQRouter());
        listreq.onResponse.connect(callback);
        listreq.send();
    }
    
    static void ownerSubscriptionlist(Client client, String domain, String node, Slot2<PubSubOwnerPubSub, ErrorPayload> callback) {
        /* list subscriptions on a pubsub node */
        
        PubSubOwnerSubscriptions ownerSubscription = new PubSubOwnerSubscriptions();
        ownerSubscription.setNode(node);
        
        PubSubOwnerPubSub pubsub = new PubSubOwnerPubSub();
        pubsub.setPayload(ownerSubscription);
        
        GenericRequest<PubSubOwnerPubSub> listreq = new GenericRequest<PubSubOwnerPubSub>(IQ.Type.Get, new JID(domain), pubsub, client.getIQRouter());
        listreq.onResponse.connect(callback);
        listreq.send();
    }
    
    static void subscribe(Client client, String domain, String node, Slot2<PubSub, ErrorPayload> callback) {
        /* subscribe to a pubsub node */
        
        PubSubSubscribe subscribe = new PubSubSubscribe();
        subscribe.setJID(client.getJID());
        subscribe.setNode(node);
        
        PubSub pubsub = new PubSub();
        pubsub.setPayload(subscribe);
        
        GenericRequest<PubSub> subreq = new GenericRequest<PubSub>(IQ.Type.Set, new JID(domain), pubsub, client.getIQRouter());
        subreq.onResponse.connect(callback);
        subreq.send();
    }
    
    static void unsubscribe(Client client, String domain, String node, Slot2<PubSub, ErrorPayload> callback) {
        /* ubsubscribe from a pubsub node */
        
        PubSubUnsubscribe subscribe = new PubSubUnsubscribe();
        subscribe.setJID(client.getJID());
        subscribe.setNode(node);
        
        PubSub pubsub = new PubSub();
        pubsub.setPayload(subscribe);
        
        GenericRequest<PubSub> subreq = new GenericRequest<PubSub>(IQ.Type.Set, new JID(domain), pubsub, client.getIQRouter());
        subreq.onResponse.connect(callback);
        subreq.send();
    }
    
    static void ownerConfigure(Client client, String domain, String node, Form form, Slot2<PubSubOwnerPubSub, ErrorPayload> callback) {
        /* set/get the current configuration for a node on a pubsub domain */
        
        PubSubOwnerConfigure config = new PubSubOwnerConfigure();
        config.setNode(node);
        
        IQ.Type type;
        if (form == null) {
            type = IQ.Type.Get; /* configuration request */
            config.setData(new Form());
        } else {
            type = IQ.Type.Set; /* setting the configuration */
            config.setData(form);
        }
        
        PubSubOwnerPubSub pubSub = new PubSubOwnerPubSub();
        pubSub.setPayload(config);
        
        GenericRequest<PubSubOwnerPubSub> req = new GenericRequest<PubSubOwnerPubSub>(type, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void ownerConfigure(final Client client, final String domain, final String node, final String parameter, final String newValue, final Slot2<PubSubOwnerPubSub, ErrorPayload> callback) {
        /* change a parameter for a node on a pubsub domain */
        
        ownerConfigure(client, domain, node, null, new Slot2<PubSubOwnerPubSub, ErrorPayload>() { /* make a request to get the current configuration */
            public void call(PubSubOwnerPubSub pubSub, ErrorPayload error) {
                if (pubSub == null) {
                    callback.call(null, new ErrorPayload());
                }
                PubSubOwnerConfigure config = (PubSubOwnerConfigure)pubSub.getPayload();
                Form form = config.getData();
                for (FormField field : form.getFields()) {
                    if (field.getName().equals(parameter)) {
                        if (field.getType() == Type.TEXT_SINGLE_TYPE) { /* find and update the specified parameter */
                            FormField fieldText = field;
                            fieldText.addValue(newValue);
                        } else if (field.getType() == Type.LIST_SINGLE_TYPE) {
                            FormField fieldList = field;
                            fieldList.addValue(newValue);
                        } else if (field.getType() == Type.BOOLEAN_TYPE) {
                            FormField fieldBoolean = field;
                            fieldBoolean.setBoolValue(newValue.equals("1"));
                        }
                    }
                }
                ownerConfigure(client, domain, node, form, callback); /* request the configuration to be changed */
            }
        });
    }
    
    static void ownerDefaultConfiguration(Client client, String domain, final Slot2<Form, ErrorPayload> callback) {
        /* retrieve the default configuration for nodes on the domain */
        
        PubSubOwnerDefault ownerDefault = new PubSubOwnerDefault();
        ownerDefault.setData(new Form());
        
        PubSubOwnerPubSub pubSub = new PubSubOwnerPubSub();
        pubSub.setPayload(ownerDefault);
        
        GenericRequest<PubSubOwnerPubSub> req = new GenericRequest<PubSubOwnerPubSub>(IQ.Type.Get, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(new Slot2<PubSubOwnerPubSub, ErrorPayload>() {
            public void call(PubSubOwnerPubSub pubSub, ErrorPayload error) {
                if (pubSub!=null && pubSub.getPayload()!=null) {
                    PubSubOwnerDefault defaultConfig = (PubSubOwnerDefault)pubSub.getPayload();
                    callback.call(defaultConfig.getData(), error);
                } else {
                    callback.call(null, error);
                }
                
            }
        });
        req.send();
    }
    
    static void ownerSetAffiliations(Client client, String domain, String node, JID subscriber, PubSubOwnerAffiliation.Type type, Slot2<PubSubOwnerPubSub, ErrorPayload> callback) {
        /* set the affiliations for a subscriber to a node on a domain */
        
        PubSubOwnerAffiliation affiliation = new PubSubOwnerAffiliation();
        affiliation.setJID(subscriber);
        affiliation.setType(type);
        
        PubSubOwnerAffiliations affiliations = new PubSubOwnerAffiliations();
        affiliations.setNode(node);
        affiliations.addAffiliation(affiliation);
        
        PubSubOwnerPubSub pubSub = new PubSubOwnerPubSub();
        pubSub.setPayload(affiliations);
        
        GenericRequest<PubSubOwnerPubSub> req = new GenericRequest<PubSubOwnerPubSub>(IQ.Type.Set, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void ownerGetAffiliations(Client client, String domain, String node, Slot2<PubSubOwnerPubSub, ErrorPayload> callback) {
        /* get the affiliations for a subscriber to a node on a domain */
        
        PubSubOwnerAffiliations affiliations = new PubSubOwnerAffiliations();
        affiliations.setNode(node);
        
        PubSubOwnerPubSub pubSub = new PubSubOwnerPubSub();
        pubSub.setPayload(affiliations);
        
        GenericRequest<PubSubOwnerPubSub> req = new GenericRequest<PubSubOwnerPubSub>(IQ.Type.Get, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void publish(Client client, String domain, String node, String id, Payload payload, Slot2<PubSubPublish, ErrorPayload> callback) {
        /* publish some data to a node */
        
        PubSubItem items = new PubSubItem();
        items.addData(payload);
        items.setID(id);
        
        PubSubPublish publish = new PubSubPublish();
        publish.addItem(items);
        publish.setNode(node);
        
        PubSub pubSub = new PubSub();
        pubSub.setPayload(publish);
        
        GenericRequest<PubSubPublish> req = new GenericRequest<PubSubPublish>(IQ.Type.Set, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void retract(Client client, String domain, String node, String id, boolean notify, Slot2<PubSub, ErrorPayload> callback) {
        /* delete an item from a node */
        
        PubSubItem item = new PubSubItem();
        item.setID(id);
        
        PubSubRetract retract = new PubSubRetract();
        retract.setNode(node);
        retract.addItem(item);
        retract.setNotify(notify);
        
        PubSub pubSub = new PubSub();
        pubSub.setPayload(retract);
        
        GenericRequest<PubSub> req = new GenericRequest<PubSub>(IQ.Type.Set, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void getItems(Client client, String domain, String node, long maxResults, Slot2<PubSub, ErrorPayload> callback) {
        /* retrieve maxResults items from a node */
        
        PubSubItems items = new PubSubItems();
        items.setNode(node);
        items.setMaximumItems(maxResults);
        
        PubSub pubSub = new PubSub();
        pubSub.setPayload(items);
        
        GenericRequest<PubSub> req = new GenericRequest<PubSub>(IQ.Type.Get, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void getItem(Client client, String domain, String node, String id, Slot2<PubSub, ErrorPayload> callback) {
        /* retrieve a specific item from a node */
        
        PubSubItem pubSubItem = new PubSubItem();
        pubSubItem.setID(id);
        
        PubSubItems items = new PubSubItems();
        items.setNode(node);
        items.addItem(pubSubItem);
        
        PubSub pubSub = new PubSub();
        pubSub.setPayload(items);
        
        GenericRequest<PubSub> req = new GenericRequest<PubSub>(IQ.Type.Get, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
    
    static void purge(Client client, String domain, String node, Slot2<PubSubOwnerPubSub, ErrorPayload> callback) {
        /* purge all items on a node */
        
        PubSubOwnerPurge purge = new PubSubOwnerPurge();
        purge.setNode(node);
        
        PubSubOwnerPubSub pubSub = new PubSubOwnerPubSub();
        pubSub.setPayload(purge);
        
        GenericRequest<PubSubOwnerPubSub> req = new GenericRequest<PubSubOwnerPubSub>(IQ.Type.Get, new JID(domain), pubSub, client.getIQRouter());
        req.onResponse.connect(callback);
        req.send();
    }
}