Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 127c8494cdaa17b11f29b93f17b375d4f0acf80c (plain) (blame)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/**
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.pubsub;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.packet.DataForm;

/**
 * A decorator for a {@link Form} to easily enable reading and updating
 * of node configuration.  All operations read or update the underlying {@link DataForm}.
 * 
 * <p>Unlike the {@link Form}.setAnswer(XXX)} methods, which throw an exception if the field does not
 * exist, all <b>ConfigureForm.setXXX</b> methods will create the field in the wrapped form
 * if it does not already exist. 
 * 
 * @author Robin Collier
 */
public class ConfigureForm extends Form
{
	/**
	 * Create a decorator from an existing {@link DataForm} that has been
	 * retrieved from parsing a node configuration request.
	 * 
	 * @param configDataForm
	 */
	public ConfigureForm(DataForm configDataForm)
	{
		super(configDataForm);
	}
	
	/**
	 * Create a decorator from an existing {@link Form} for node configuration.
	 * Typically, this can be used to create a decorator for an answer form
	 * by using the result of {@link #createAnswerForm()} as the input parameter.
	 * 
	 * @param nodeConfigForm
	 */
	public ConfigureForm(Form nodeConfigForm)
	{
		super(nodeConfigForm.getDataFormToSend());
	}
	
	/**
	 * Create a new form for configuring a node.  This would typically only be used 
	 * when creating and configuring a node at the same time via {@link PubSubManager#createNode(String, Form)}, since 
	 * configuration of an existing node is typically accomplished by calling {@link LeafNode#getNodeConfiguration()} and
	 * using the resulting form to create a answer form.  See {@link #ConfigureForm(Form)}.
	 * @param formType
	 */
	public ConfigureForm(FormType formType)
	{
		super(formType.toString());
	}
	
	/**
	 * Get the currently configured {@link AccessModel}, null if it is not set.
	 * 
	 * @return The current {@link AccessModel}
	 */
	public AccessModel getAccessModel()
	{
		String value = getFieldValue(ConfigureNodeFields.access_model);
		
		if (value == null)
			return null;
		else
			return AccessModel.valueOf(value);
	}
	
	/**
	 * Sets the value of access model.
	 * 
	 * @param accessModel
	 */
	public void setAccessModel(AccessModel accessModel)
	{
		addField(ConfigureNodeFields.access_model, FormField.TYPE_LIST_SINGLE);
		setAnswer(ConfigureNodeFields.access_model.getFieldName(), getListSingle(accessModel.toString()));
	}

	/**
	 * Returns the URL of an XSL transformation which can be applied to payloads in order to 
	 * generate an appropriate message body element.
	 * 
	 * @return URL to an XSL
	 */
	public String getBodyXSLT()
	{
		return getFieldValue(ConfigureNodeFields.body_xslt);
	}

	/**
	 * Set the URL of an XSL transformation which can be applied to payloads in order to 
	 * generate an appropriate message body element.
	 * 
	 * @param bodyXslt The URL of an XSL
	 */
	public void setBodyXSLT(String bodyXslt)
	{
		addField(ConfigureNodeFields.body_xslt, FormField.TYPE_TEXT_SINGLE);
		setAnswer(ConfigureNodeFields.body_xslt.getFieldName(), bodyXslt);
	}
	
	/**
	 * The id's of the child nodes associated with a collection node (both leaf and collection).
	 * 
	 * @return Iterator over the list of child nodes.
	 */
	public Iterator<String> getChildren()
	{
		return getFieldValues(ConfigureNodeFields.children);
	}
	
	/**
	 * Set the list of child node ids that are associated with a collection node.
	 * 
	 * @param children
	 */
	public void setChildren(List<String> children)
	{
		addField(ConfigureNodeFields.children, FormField.TYPE_TEXT_MULTI);
		setAnswer(ConfigureNodeFields.children.getFieldName(), children);
	}
	
	/**
	 * Returns the policy that determines who may associate children with the node.
	 *  
	 * @return The current policy
	 */
	public ChildrenAssociationPolicy getChildrenAssociationPolicy()
	{
		String value = getFieldValue(ConfigureNodeFields.children_association_policy);
		
		if (value == null)
			return null;
		else
			return ChildrenAssociationPolicy.valueOf(value);
	}
	
	/**
	 * Sets the policy that determines who may associate children with the node.
	 * 
	 * @param policy The policy being set
	 */
	public void setChildrenAssociationPolicy(ChildrenAssociationPolicy policy)
	{
		addField(ConfigureNodeFields.children_association_policy, FormField.TYPE_LIST_SINGLE);
        List<String> values = new ArrayList<String>(1);
        values.add(policy.toString());
        setAnswer(ConfigureNodeFields.children_association_policy.getFieldName(), values);
	}
	
	/**
	 * Iterator of JID's that are on the whitelist that determines who can associate child nodes 
	 * with the collection node.  This is only relevant if {@link #getChildrenAssociationPolicy()} is set to
	 * {@link ChildrenAssociationPolicy#whitelist}.
	 * 
	 * @return Iterator over whitelist
	 */
	public Iterator<String> getChildrenAssociationWhitelist()
	{
		return getFieldValues(ConfigureNodeFields.children_association_whitelist);
	}
	
	/**
	 * Set the JID's in the whitelist of users that can associate child nodes with the collection 
	 * node.  This is only relevant if {@link #getChildrenAssociationPolicy()} is set to
	 * {@link ChildrenAssociationPolicy#whitelist}.
	 * 
	 * @param whitelist The list of JID's
	 */
	public void setChildrenAssociationWhitelist(List<String> whitelist)
	{
		addField(ConfigureNodeFields.children_association_whitelist, FormField.TYPE_JID_MULTI);
		setAnswer(ConfigureNodeFields.children_association_whitelist.getFieldName(), whitelist);
	}

	/**
	 * Gets the maximum number of child nodes that can be associated with the collection node.
	 * 
	 * @return The maximum number of child nodes
	 */
	public int getChildrenMax()
	{
		return Integer.parseInt(getFieldValue(ConfigureNodeFields.children_max));
	}

	/**
	 * Set the maximum number of child nodes that can be associated with a collection node.
	 * 
	 * @param max The maximum number of child nodes.
	 */
	public void setChildrenMax(int max)
	{
		addField(ConfigureNodeFields.children_max, FormField.TYPE_TEXT_SINGLE);
		setAnswer(ConfigureNodeFields.children_max.getFieldName(), max);
	}

	/**
	 * Gets the collection node which the node is affiliated with.
	 * 
	 * @return The collection node id
	 */
	public String getCollection()
	{
		return getFieldValue(ConfigureNodeFields.collection);
	}

	/**
	 * Sets the collection node which the node is affiliated with.
	 * 
	 * @param collection The node id of the collection node
	 */
	public void setCollection(String collection)
	{
		addField(ConfigureNodeFields.collection, FormField.TYPE_TEXT_SINGLE);
		setAnswer(ConfigureNodeFields.collection.getFieldName(), collection);
	}

	/**
	 * Gets the URL of an XSL transformation which can be applied to the payload
	 * format in order to generate a valid Data Forms result that the client could
	 * display using a generic Data Forms rendering engine.
	 * 
	 * @return The URL of an XSL transformation
	 */
	public String getDataformXSLT()
	{
		return getFieldValue(ConfigureNodeFields.dataform_xslt);
	}

	/**
	 * Sets the URL of an XSL transformation which can be applied to the payload
	 * format in order to generate a valid Data Forms result that the client could
	 * display using a generic Data Forms rendering engine.
	 * 
	 * @param url The URL of an XSL transformation
	 */
	public void setDataformXSLT(String url)
	{
		addField(ConfigureNodeFields.dataform_xslt, FormField.TYPE_TEXT_SINGLE);
		setAnswer(ConfigureNodeFields.dataform_xslt.getFieldName(), url);
	}

	/**
	 * Does the node deliver payloads with event notifications.
	 * 
	 * @return true if it does, false otherwise
	 */
	public boolean isDeliverPayloads()
	{
		return parseBoolean(getFieldValue(ConfigureNodeFields.deliver_payloads));
	}
	
	/**
	 * Sets whether the node will deliver payloads with event notifications.
	 * 
	 * @param deliver true if the payload will be delivered, false otherwise
	 */
	public void setDeliverPayloads(boolean deliver)
	{
		addField(ConfigureNodeFields.deliver_payloads, FormField.TYPE_BOOLEAN);
		setAnswer(ConfigureNodeFields.deliver_payloads.getFieldName(), deliver);
	}

	/**
	 * Determines who should get replies to items
	 * 
	 * @return Who should get the reply
	 */
	public ItemReply getItemReply()
	{
		String value = getFieldValue(ConfigureNodeFields.itemreply);
		
		if (value == null)
			return null;
		else
			return ItemReply.valueOf(value);
	}

	/**
	 * Sets who should get the replies to items
	 * 
	 * @param reply Defines who should get the reply
	 */
	public void setItemReply(ItemReply reply)
	{
		addField(ConfigureNodeFields.itemreply, FormField.TYPE_LIST_SINGLE);
		setAnswer(ConfigureNodeFields.itemreply.getFieldName(), getListSingle(reply.toString()));
	}

	/**
	 * Gets the maximum number of items to persisted to this node if {@link #isPersistItems()} is
	 * true.
	 * 
	 * @return The maximum number of items to persist
	 */
	public int getMaxItems()
	{
		return Integer.parseInt(getFieldValue(ConfigureNodeFields.max_items));
	}

	/**
	 * Set the maximum number of items to persisted to this node if {@link #isPersistItems()} is
	 * true.
	 * 
	 * @param max The maximum number of items to persist
	 */
	public void setMaxItems(int max)
	{
		addField(ConfigureNodeFields.max_items, FormField.TYPE_TEXT_SINGLE);
		setAnswer(ConfigureNodeFields.max_items.getFieldName(), max);
	}
	
	/**
	 * Gets the maximum payload size in bytes.
	 * 
	 * @return The maximum payload size
	 */
	public int getMaxPayloadSize()
	{
		return Integer.parseInt(getFieldValue(ConfigureNodeFields.max_payload_size));
	}

	/**
	 * Sets the maximum payload size in bytes
	 * 
	 * @param max The maximum payload size
	 */
	public void setMaxPayloadSize(int max)
	{
		addField(ConfigureNodeFields.max_payload_size, FormField.TYPE_TEXT_SINGLE);
		setAnswer(ConfigureNodeFields.max_payload_size.getFieldName(), max);
	}
	
	/**
	 * Gets the node type
	 * 
	 * @return The node type
	 */
	public NodeType getNodeType()
	{
		String value = getFieldValue(ConfigureNodeFields.node_type);
		
		if (value == null)
			return null;
		else
			return NodeType.valueOf(value);
	}
	
	/**
	 * Sets the node type
	 * 
	 * @param type The node type
	 */
	public void setNodeType(NodeType type)
	{
		addField(ConfigureNodeFields.node_type, FormField.TYPE_LIST_SINGLE);
		setAnswer(ConfigureNodeFields.node_type.getFieldName(), getListSingle(type.toString()));
	}

	/**
	 * Determines if subscribers should be notified when the configuration changes.
	 * 
	 * @return true if they should be notified, false otherwise
	 */
	public boolean isNotifyConfig()
	{
		return parseBoolean(getFieldValue(ConfigureNodeFields.notify_config));
	}
	
	/**
	 * Sets whether subscribers should be notified when the configuration changes.
	 * 
	 * @param notify true if subscribers should be notified, false otherwise
	 */
	public void setNotifyConfig(boolean notify)
	{
		addField(ConfigureNodeFields.notify_config, FormField.TYPE_BOOLEAN);
		setAnswer(ConfigureNodeFields.notify_config.getFieldName(), notify);
	}

	/**
	 * Determines whether subscribers should be notified when the node is deleted.
	 * 
	 * @return true if subscribers should be notified, false otherwise
	 */
	public boolean isNotifyDelete()
	{
		return parseBoolean(getFieldValue(ConfigureNodeFields.notify_delete));
	}
	
	/**
	 * Sets whether subscribers should be notified when the node is deleted.
	 * 
	 * @param notify true if subscribers should be notified, false otherwise
	 */
	public void setNotifyDelete(boolean notify) 
	{
		addField(ConfigureNodeFields.notify_delete, FormField.TYPE_BOOLEAN);
		setAnswer(ConfigureNodeFields.notify_delete.getFieldName(), notify);
	}

	/**
	 * Determines whether subscribers should be notified when items are deleted 
	 * from the node.
	 * 
	 * @return true if subscribers should be notified, false otherwise
	 */
	public boolean isNotifyRetract()
	{
		return parseBoolean(getFieldValue(ConfigureNodeFields.notify_retract));
	}
	
	/**
	 * Sets whether subscribers should be notified when items are deleted 
	 * from the node.
	 * 
	 * @param notify true if subscribers should be notified, false otherwise
	 */
	public void setNotifyRetract(boolean notify) 
	{
		addField(ConfigureNodeFields.notify_retract, FormField.TYPE_BOOLEAN);
		setAnswer(ConfigureNodeFields.notify_retract.getFieldName(), notify);
	}
	
	/**
	 * Determines whether items should be persisted in the node.
	 * 
	 * @return true if items are persisted
	 */
	public boolean isPersistItems()
	{
		return parseBoolean(getFieldValue(ConfigureNodeFields.persist_items));
	}
	
	/**
	 * Sets whether items should be persisted in the node.
	 * 
	 * @param persist true if items should be persisted, false otherwise
	 */
	public void setPersistentItems(boolean persist) 
	{
		addField(ConfigureNodeFields.persist_items, FormField.TYPE_BOOLEAN);
		setAnswer(ConfigureNodeFields.persist_items.getFieldName(), persist);
	}

	/**
	 * Determines whether to deliver notifications to available users only.
	 * 
	 * @return true if users must be available
	 */
	public boolean isPresenceBasedDelivery()
	{
		return parseBoolean(getFieldValue(ConfigureNodeFields.presence_based_delivery));
	}
	
	/**
	 * Sets whether to deliver notifications to available users only.
	 * 
	 * @param presenceBased true if user must be available, false otherwise
	 */
	public void setPresenceBasedDelivery(boolean presenceBased) 
	{
		addField(ConfigureNodeFields.presence_based_delivery, FormField.TYPE_BOOLEAN);
		setAnswer(ConfigureNodeFields.presence_based_delivery.getFieldName(), presenceBased);
	}

	/**
	 * Gets the publishing model for the node, which determines who may publish to it.
	 * 
	 * @return The publishing model
	 */
	public PublishModel getPublishModel()
	{
		String value = getFieldValue(ConfigureNodeFields.publish_model);
		
		if (value == null)
			return null;
		else
			return PublishModel.valueOf(value);
	}

	/**
	 * Sets the publishing model for the node, which determines who may publish to it.
	 * 
	 * @param publish The enum representing the possible options for the publishing model
	 */
	public void setPublishModel(PublishModel publish) 
	{
		addField(ConfigureNodeFields.publish_model, FormField.TYPE_LIST_SINGLE);
		setAnswer(ConfigureNodeFields.publish_model.getFieldName(), getListSingle(publish.toString()));
	}
	
	/**
	 * Iterator over the multi user chat rooms that are specified as reply rooms.
	 * 
	 * @return The reply room JID's
	 */
	public Iterator<String> getReplyRoom()
	{
		return getFieldValues(ConfigureNodeFields.replyroom);
	}
	
	/**
	 * Sets the multi user chat rooms that are specified as reply rooms.
	 * 
	 * @param replyRooms The multi user chat room to use as reply rooms
	 */
	public void setReplyRoom(List<String> replyRooms) 
	{
		addField(ConfigureNodeFields.replyroom, FormField.TYPE_LIST_MULTI);
		setAnswer(ConfigureNodeFields.replyroom.getFieldName(), replyRooms);
	}
	
	/**
	 * Gets the specific JID's for reply to.
	 *  
	 * @return The JID's
	 */
	public Iterator<String> getReplyTo()
	{
		return getFieldValues(ConfigureNodeFields.replyto);
	}
	
	/**
	 * Sets the specific JID's for reply to.
	 * 
	 * @param replyTos The JID's to reply to
	 */
	public void setReplyTo(List<String> replyTos)
	{
		addField(ConfigureNodeFields.replyto, FormField.TYPE_LIST_MULTI);
		setAnswer(ConfigureNodeFields.replyto.getFieldName(), replyTos);
	}
	
	/**
	 * Gets the roster groups that are allowed to subscribe and retrieve items.
	 *  
	 * @return The roster groups
	 */
	public Iterator<String> getRosterGroupsAllowed()
	{
		return getFieldValues(ConfigureNodeFields.roster_groups_allowed);
	}
	
	/**
	 * Sets the roster groups that are allowed to subscribe and retrieve items.
	 * 
	 * @param groups The roster groups
	 */
	public void setRosterGroupsAllowed(List<String> groups)
	{
		addField(ConfigureNodeFields.roster_groups_allowed, FormField.TYPE_LIST_MULTI);
		setAnswer(ConfigureNodeFields.roster_groups_allowed.getFieldName(), groups);
	}
	
	/**
	 * Determines if subscriptions are allowed.
	 * 
	 * @return true if subscriptions are allowed, false otherwise
	 */
	public boolean isSubscibe()
	{
		return parseBoolean(getFieldValue(ConfigureNodeFields.subscribe));
	}

	/**
	 * Sets whether subscriptions are allowed.
	 * 
	 * @param subscribe true if they are, false otherwise
	 */
	public void setSubscribe(boolean subscribe)
	{
		addField(ConfigureNodeFields.subscribe, FormField.TYPE_BOOLEAN);
		setAnswer(ConfigureNodeFields.subscribe.getFieldName(), subscribe);
	}
	
	/**
	 * Gets the human readable node title.
	 * 
	 * @return The node title
	 */
	public String getTitle()
	{
		return getFieldValue(ConfigureNodeFields.title);
	}

	/**
	 * Sets a human readable title for the node.
	 * 
	 * @param title The node title
	 */
	public void setTitle(String title) 
	{
		addField(ConfigureNodeFields.title, FormField.TYPE_TEXT_SINGLE);
		setAnswer(ConfigureNodeFields.title.getFieldName(), title);
	}
	
	/**
	 * The type of node data, usually specified by the namespace of the payload (if any).
	 * 
	 * @return The type of node data
	 */
	public String getDataType()
	{
		return getFieldValue(ConfigureNodeFields.type);
	}

	/**
	 * Sets the type of node data, usually specified by the namespace of the payload (if any).
	 * 
	 * @param type The type of node data
	 */
	public void setDataType(String type) 
	{
		addField(ConfigureNodeFields.type, FormField.TYPE_TEXT_SINGLE);
		setAnswer(ConfigureNodeFields.type.getFieldName(), type);
	}
	
	@Override
	public String toString()
	{
		StringBuilder result = new StringBuilder(getClass().getName() + " Content [");
		
		Iterator<FormField> fields = getFields();
		
		while (fields.hasNext())
		{
			FormField formField = fields.next();
			result.append('(');
			result.append(formField.getVariable());
			result.append(':');
			
			Iterator<String> values = formField.getValues();
			StringBuilder valuesBuilder = new StringBuilder();
				
			while (values.hasNext())
			{
				if (valuesBuilder.length() > 0)
					result.append(',');
				String value = (String)values.next();
				valuesBuilder.append(value);
			}
			
			if (valuesBuilder.length() == 0)
				valuesBuilder.append("NOT SET");
			result.append(valuesBuilder);
			result.append(')');
		}
		result.append(']');
		return result.toString();
	}

	static private boolean parseBoolean(String fieldValue)
	{
		return ("1".equals(fieldValue) || "true".equals(fieldValue));
	}

	private String getFieldValue(ConfigureNodeFields field)
	{
		FormField formField = getField(field.getFieldName());
		
		return (formField.getValues().hasNext()) ? formField.getValues().next() : null;
	}

	private Iterator<String> getFieldValues(ConfigureNodeFields field)
	{
		FormField formField = getField(field.getFieldName());
		
		return formField.getValues();
	}

	private void addField(ConfigureNodeFields nodeField, String type)
	{
		String fieldName = nodeField.getFieldName();
		
		if (getField(fieldName) == null)
		{
			FormField field = new FormField(fieldName);
			field.setType(type);
			addField(field);
		}
	}

	private List<String> getListSingle(String value)
	{
		List<String> list = new ArrayList<String>(1);
		list.add(value);
		return list;
	}

}

Back to the top