-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathNode.java
More file actions
2807 lines (2557 loc) · 117 KB
/
Copy pathNode.java
File metadata and controls
2807 lines (2557 loc) · 117 KB
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2005-2008 Jive Software, 2017-2026 Ignite Realtime Foundation. 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.openfire.pubsub;
import org.dom4j.Element;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.cluster.ClusterManager;
import org.jivesoftware.openfire.pep.PEPServiceManager;
import org.jivesoftware.openfire.pubsub.cluster.*;
import org.jivesoftware.openfire.pubsub.models.AccessModel;
import org.jivesoftware.openfire.pubsub.models.PublisherModel;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.StringUtils;
import org.jivesoftware.util.cache.*;
import org.slf4j.Logger;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.PacketError;
import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import static org.jivesoftware.openfire.muc.spi.IQOwnerHandler.parseFirstValueAsBoolean;
/**
* A virtual location to which information can be published and from which event
* notifications and/or payloads can be received (in other pubsub systems, this may
* be labelled a "topic").
*
* @author Matt Tucker
*/
public abstract class Node implements Cacheable, Externalizable {
/**
* Unique reference to the publish and subscribe service.
*/
protected PubSubService.UniqueIdentifier serviceIdentifier;
/**
* The ID of the node that is containing this node (if any). This node can be expected to be a CollectionNode.
*/
protected Node.UniqueIdentifier parentIdentifier;
/**
* The unique identifier for a node within the context of a pubsub service.
*/
protected String nodeID;
/**
* Flag that indicates whether to deliver payloads with event notifications.
*/
protected boolean deliverPayloads;
/**
* Policy that defines whether owners or publisher should receive replies to items.
*/
protected ItemReplyPolicy replyPolicy;
/**
* Flag that indicates whether to notify subscribers when the node configuration changes.
*/
protected boolean notifyConfigChanges;
/**
* Flag that indicates whether to notify subscribers when the node is deleted.
*/
protected boolean notifyDelete;
/**
* Flag that indicates whether to notify subscribers when items are removed from the node.
*/
protected boolean notifyRetract;
/**
* Flag that indicates whether to deliver notifications to available users only.
*/
protected boolean presenceBasedDelivery;
/**
* Publisher model that specifies who is allowed to publish items to the node.
*/
protected PublisherModel publisherModel = PublisherModel.open;
/**
* Flag that indicates that subscribing and unsubscribing are enabled.
*/
protected boolean subscriptionEnabled;
/**
* Access model that specifies who is allowed to subscribe and retrieve items.
*/
protected AccessModel accessModel = AccessModel.open;
/**
* The roster group(s) allowed to subscribe and retrieve items.
*/
protected Collection<String> rosterGroupsAllowed = new ArrayList<>();
/**
* List of multi-user chat rooms to specify for replyroom.
*/
protected Collection<JID> replyRooms = new ArrayList<>();
/**
* List of JID(s) to specify for replyto.
*/
protected Collection<JID> replyTo = new ArrayList<>();
/**
* The type of payload data to be provided at the node. Usually specified by the
* namespace of the payload (if any).
*/
protected String payloadType = "";
/**
* The URL of an XSL transformation which can be applied to payloads in order
* to generate an appropriate message body element.
*/
protected String bodyXSLT = "";
/**
* 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.
*/
protected String dataformXSLT = "";
/**
* Indicates if the node is present in the database.
*/
private boolean savedToDB = false;
/**
* The datetime when the node was created.
*/
protected Date creationDate;
/**
* The last date when the ndoe's configuration was modified.
*/
private Date modificationDate;
/**
* The JID of the node creator.
*/
protected JID creator;
/**
* A description of the node.
*/
protected String description = "";
/**
* The default language of the node.
*/
protected String language = "";
/**
* The JIDs of those to contact with questions.
*/
protected Collection<JID> contacts = new ArrayList<>();
/**
* The name of the node.
*/
protected String name = "";
/**
* Flag that indicates whether new subscriptions should be configured to be active.
*/
protected boolean subscriptionConfigurationRequired = false;
/**
* The JIDs of those who have an affiliation with this node. When subscriptionModel is
* whitelist then this collection acts as the white list (unless user is an outcast)
*/
protected Collection<NodeAffiliate> affiliates = new CopyOnWriteArrayList<>();
/**
* Map that contains the current subscriptions to the node. Each subscription is uniquely
* identified by its ID.
*
* Key: Subscription ID, Value: the subscription.
*/
protected Map<String, NodeSubscription> subscriptionsByID = new ConcurrentHashMap<>();
/**
* A transient reference to the service that this node belongs to. Note that this value is lazily initialized in
* {@link #getService()}. That method should be used instead of accessing this field directly.
*/
private transient PubSubService service;
/**
* A transient reference to the node that is the parent of this node. Note that this value is lazily initialized in
* {@link #getParent()}. That method should be used instead of accessing this field directly.
*/
private transient CollectionNode parent;
Node() {} // to be used only for serialization;
Node(PubSubService.UniqueIdentifier serviceId, CollectionNode parent, String nodeID, JID creator, DefaultNodeConfiguration configuration ) {
this(serviceId, parent, nodeID, creator, configuration.isSubscriptionEnabled(), configuration.isDeliverPayloads(), configuration.isNotifyConfigChanges(), configuration.isNotifyDelete(), configuration.isNotifyRetract(), configuration.isPresenceBasedDelivery(), configuration.getAccessModel(), configuration.getPublisherModel(), configuration.getLanguage(), configuration.getReplyPolicy() );
}
Node(PubSubService.UniqueIdentifier serviceId, CollectionNode parent, String nodeID, JID creator, boolean subscriptionEnabled, boolean deliverPayloads, boolean notifyConfigChanges, boolean notifyDelete, boolean notifyRetract, boolean presenceBasedDelivery, AccessModel accessModel, PublisherModel publisherModel, String language, ItemReplyPolicy replyPolicy) {
this.serviceIdentifier = serviceId;
this.parentIdentifier = parent == null ? null : parent.getUniqueIdentifier();
this.nodeID = nodeID;
this.creator = creator;
long startTime = System.currentTimeMillis();
this.creationDate = new Date(startTime);
this.modificationDate = new Date(startTime);
this.subscriptionEnabled = subscriptionEnabled;
this.deliverPayloads = deliverPayloads;
this.notifyConfigChanges = notifyConfigChanges;
this.notifyDelete = notifyDelete;
this.notifyRetract = notifyRetract;
this.presenceBasedDelivery = presenceBasedDelivery;
this.accessModel = accessModel;
this.publisherModel = publisherModel;
this.language = language;
this.replyPolicy = replyPolicy;
}
/**
* Returns an identifier for this node that is unique within the XMPP domain.
*
* @return A unique identifier for this node.
*/
public UniqueIdentifier getUniqueIdentifier() {
return new UniqueIdentifier( this.serviceIdentifier, this.nodeID );
}
/**
* Adds a new affiliation or updates an existing affiliation of the specified entity JID
* to become a node owner.
*
* @param jid the JID of the user being added as a node owner.
* @return the newly created or modified affiliation to the node.
*/
public NodeAffiliate addOwner(JID jid) {
NodeAffiliate nodeAffiliate = addAffiliation(jid, NodeAffiliate.Affiliation.owner);
// Approve any pending subscription
for (NodeSubscription subscription : getSubscriptions(jid)) {
if (subscription.isAuthorizationPending()) {
subscription.approved();
}
}
return nodeAffiliate;
}
/**
* Removes the owner affiliation of the specified entity JID. If the user that is
* no longer an owner was subscribed to the node then his affiliation will be of
* type {@link NodeAffiliate.Affiliation#none}.
*
* @param jid the JID of the user being removed as a node owner.
*/
public void removeOwner(JID jid) {
// Get the current affiliation of the specified JID
NodeAffiliate affiliate = getAffiliate(jid);
if (affiliate.getSubscriptions().isEmpty()) {
removeAffiliation(jid, NodeAffiliate.Affiliation.owner);
removeSubscriptions(jid);
}
else {
// The user has subscriptions so change affiliation to NONE
addNoneAffiliation(jid);
}
}
/**
* Adds a new affiliation or updates an existing affiliation of the specified entity JID
* to become a node publisher.
*
* @param jid the JID of the user being added as a node publisher.
* @return the newly created or modified affiliation to the node.
*/
public NodeAffiliate addPublisher(JID jid) {
return addAffiliation(jid, NodeAffiliate.Affiliation.publisher);
}
/**
* Removes the publisher affiliation of the specified entity JID. If the user that is
* no longer a publisher was subscribed to the node then his affiliation will be of
* type {@link NodeAffiliate.Affiliation#none}.
*
* @param jid the JID of the user being removed as a node publisher.
*/
public void removePublisher(JID jid) {
// Get the current affiliation of the specified JID
NodeAffiliate affiliate = getAffiliate(jid);
if (affiliate.getSubscriptions().isEmpty()) {
removeAffiliation(jid, NodeAffiliate.Affiliation.publisher);
removeSubscriptions(jid);
}
else {
// The user has subscriptions so change affiliation to NONE
addNoneAffiliation(jid);
}
}
/**
* Adds a new affiliation or updates an existing affiliation of the specified entity JID
* to become a none affiliate. Affiliates of type none are allowed to subscribe to the node.
*
* @param jid the JID of the user with affiliation "none".
* @return the newly created or modified affiliation to the node.
*/
public NodeAffiliate addNoneAffiliation(JID jid) {
return addAffiliation(jid, NodeAffiliate.Affiliation.none);
}
/**
* Adds a new affiliation or updates an existing affiliation of the specified entity JID
* to become a member affiliate.
*
* @param jid the JID of the member.
* @return the newly created or modified affiliation to the node.
*/
public NodeAffiliate addMember(JID jid) {
return addAffiliation(jid, NodeAffiliate.Affiliation.member);
}
/**
* Sets that the specified entity is an outcast of the node. Outcast entities are not
* able to publish or subscribe to the node. Existing subscriptions will be deleted.
*
* @param jid the JID of the user that is no longer able to publish or subscribe to the node.
* @return the newly created or modified affiliation to the node.
*/
public NodeAffiliate addOutcast(JID jid) {
NodeAffiliate nodeAffiliate = addAffiliation(jid, NodeAffiliate.Affiliation.outcast);
// Delete existing subscriptions
removeSubscriptions(jid);
return nodeAffiliate;
}
/**
* Removes the banning to subscribe to the node for the specified entity.
*
* @param jid the JID of the user that is no longer an outcast.
*/
public void removeOutcast(JID jid) {
removeAffiliation(jid, NodeAffiliate.Affiliation.outcast);
}
private NodeAffiliate addAffiliation(JID jid, NodeAffiliate.Affiliation affiliation) {
getLogger().trace("Add '{}' as {}", jid, affiliation);
boolean created = false;
// Get the current affiliation of the specified JID
NodeAffiliate affiliate = getAffiliate(jid);
// Check if the user already has the same affiliation
if (affiliate != null && affiliation == affiliate.getAffiliation()) {
getLogger().trace("Do nothing since the user already has the expected affiliation");
return affiliate;
}
else if (affiliate != null) {
getLogger().trace("Update existing affiliation of type '{}' with new affiliation type", affiliate.getAffiliation());
affiliate.setAffiliation(affiliation);
}
else {
getLogger().trace("User did not have any affiliation with the node so create a new one");
affiliate = new NodeAffiliate(this, jid);
affiliate.setAffiliation(affiliation);
addAffiliate(affiliate);
created = true;
}
if (savedToDB) {
getLogger().trace("Add or update the affiliate in the database");
final PubSubPersistenceProvider persistenceProvider = XMPPServer.getInstance().getPubSubModule().getPersistenceProvider();
if ( created ) {
persistenceProvider.createAffiliation(this, affiliate);
} else {
persistenceProvider.updateAffiliation(this, affiliate);
}
}
getLogger().trace("Update the other cluster members with the new affiliation");
CacheFactory.doClusterTask(new AffiliationTask(this, jid, affiliation));
return affiliate;
}
private void removeAffiliation(JID jid, NodeAffiliate.Affiliation affiliation) {
// Get the current affiliation of the specified JID
NodeAffiliate affiliate = getAffiliate(jid);
// Check if the current affiliation of the user is the one to remove
if (affiliate != null && affiliation == affiliate.getAffiliation()) {
removeAffiliation(affiliate);
}
}
private void removeAffiliation(NodeAffiliate affiliate) {
getLogger().trace("Remove '{}' as {}", affiliate.getJID(), affiliate.getAffiliation());
// Remove the existing affiliate from the list in memory
affiliates.remove(affiliate);
if (savedToDB) {
getLogger().trace("Remove the affiliate in the database");
XMPPServer.getInstance().getPubSubModule().getPersistenceProvider().removeAffiliation(this, affiliate);
}
// TODO OF-2769 Investigate if it is desirable to update the other cluster members with the removed affiliation.
}
/**
* Removes all subscriptions owned by the specified entity.
*
* @param owner the owner of the subscriptions to be cancelled.
*/
private void removeSubscriptions(JID owner) {
for (NodeSubscription subscription : getSubscriptions(owner)) {
cancelSubscription(subscription);
}
}
/**
* Returns the list of subscriptions owned by the specified user. The returned subscriptions
* can target different subscription JID values. When multiple subscriptions are enabled,
* more than one subscription can additionally exist for the same literal subscription JID.
*
* @param owner the owner of the subscriptions. This is typically a bare JID.
* @return the list of subscriptions owned by the specified user.
*/
public Collection<NodeSubscription> getSubscriptions(JID owner) {
Collection<NodeSubscription> subscriptions = new ArrayList<>();
for (NodeSubscription subscription : subscriptionsByID.values()) {
if (owner.equals(subscription.getOwner())) {
subscriptions.add(subscription);
}
}
getLogger().trace("Got {} subscription(s) for '{}'", subscriptions.size(), owner);
return subscriptions;
}
/**
* Returns all subscriptions to the node.
*
* @return all subscriptions to the node.
*/
Collection<NodeSubscription> getSubscriptions() {
getLogger().trace("Got {} subscription(s)", subscriptionsByID.size());
return subscriptionsByID.values();
}
/**
* Returns all subscriptions to the node.
*
* @return All subscriptions to the node.
*/
public Collection<NodeSubscription> getAllSubscriptions() {
getLogger().trace("Got {} subscription(s)", subscriptionsByID.size());
return subscriptionsByID.values();
}
/**
* Returns all affiliates of the node.
*
* @return All affiliates of the node.
*/
public Collection<NodeAffiliate> getAllAffiliates() {
getLogger().trace("Got {} affiliate(s)", affiliates.size());
return affiliates;
}
/**
* Returns the {@link NodeAffiliate} of the specified {@link JID} or {@code null}
* if none was found. Users that have a subscription with the node will ALWAYS
* have an affiliation even if the affiliation is of type {@code none}.
*
* @param jid the JID of the user to look his affiliation with this node.
* @return the NodeAffiliate of the specified JID or {@code null} if none was found.
*/
public NodeAffiliate getAffiliate(JID jid) {
NodeAffiliate result = null;
for (NodeAffiliate affiliate : affiliates) {
if (jid.equals(affiliate.getJID())) {
result = affiliate;
break;
}
}
getLogger().trace("Found {} affiliate for '{}'", result == null ? "no" : result.getAffiliation(), jid);
return result;
}
/**
* Returns a collection with the JID of the node owners. Entities that are node owners have
* an affiliation of {@link NodeAffiliate.Affiliation#owner}. Owners are allowed to purge
* and delete the node. Moreover, owners may also get The collection can be modified
* since it represents a snapshot.
*
* @return a collection with the JID of the node owners.
*/
public Collection<JID> getOwners() {
Collection<JID> jids = new ArrayList<>();
for (NodeAffiliate affiliate : affiliates) {
if (NodeAffiliate.Affiliation.owner == affiliate.getAffiliation()) {
jids.add(affiliate.getJID());
}
}
getLogger().trace("Got {} owner(s)", jids.size());
return jids;
}
/**
* Returns a collection with the JID of the enitities with an affiliation of
* {@link NodeAffiliate.Affiliation#publisher}. When using the publisher model
* {@link org.jivesoftware.openfire.pubsub.models.OpenPublisher} anyone may publish
* to the node so this collection may be empty or may not contain the complete list
* of publishers. The returned collection can be modified since it represents a snapshot.
*
* @return a collection with the JID of the enitities with an affiliation of publishers.
*/
public Collection<JID> getPublishers() {
Collection<JID> jids = new ArrayList<>();
for (NodeAffiliate affiliate : affiliates) {
if (NodeAffiliate.Affiliation.publisher == affiliate.getAffiliation()) {
jids.add(affiliate.getJID());
}
}
getLogger().trace("Got {} publisher(s)", jids.size());
return jids;
}
/**
* Changes the node configuration based on the completed data form. Only owners or
* sysadmins are allowed to change the node configuration. The completed data form
* cannot remove all node owners. An exception is going to be thrown if the new form
* tries to leave the node without owners.
*
* @param completedForm the completed data form.
* @throws NotAcceptableException if completed data form tries to leave the node without owners.
*/
public void configure(DataForm completedForm) throws NotAcceptableException {
getLogger().trace("Apply configuration from provided data form...");
boolean wasPresenceBased = isPresenceBasedDelivery();
if (DataForm.Type.cancel.equals(completedForm.getType())) {
getLogger().trace("... Data form type cancel: Existing node configuration is applied (i.e. nothing is changed).");
}
else if (DataForm.Type.submit.equals(completedForm.getType())) {
List<String> values;
// Get the new list of owners
FormField ownerField = completedForm.getField("pubsub#owner");
boolean ownersSent = ownerField != null;
List<JID> owners = new ArrayList<>();
if (ownersSent) {
for (String value : ownerField.getValues()) {
try {
owners.add(new JID(value));
}
catch (Exception e) {
// Do nothing
}
}
}
if (ownersSent && owners.isEmpty()) {
getLogger().trace("... data form type submit not acceptable: all the current owners would be removed.");
throw new NotAcceptableException("This would remove all current owners.");
}
getLogger().trace("... data form type submit. Applying fields from form ...");
for (FormField field : completedForm.getFields()) {
getLogger().trace("... processing field '{}' ...", field.getVariable());
if ("FORM_TYPE".equals(field.getVariable())) {
// Do nothing
}
else if ("pubsub#deliver_payloads".equals(field.getVariable())) {
deliverPayloads = parseFirstValueAsBoolean( field, true ) ;
}
else if ("pubsub#notify_config".equals(field.getVariable())) {
notifyConfigChanges = parseFirstValueAsBoolean( field, true ) ;
}
else if ("pubsub#notify_delete".equals(field.getVariable())) {
notifyDelete = parseFirstValueAsBoolean( field, true ) ;
}
else if ("pubsub#notify_retract".equals(field.getVariable())) {
notifyRetract = parseFirstValueAsBoolean( field, true ) ;
}
else if ("pubsub#presence_based_delivery".equals(field.getVariable())) {
presenceBasedDelivery = parseFirstValueAsBoolean( field, true ) ;
}
else if ("pubsub#subscribe".equals(field.getVariable())) {
subscriptionEnabled = parseFirstValueAsBoolean( field, true ) ;
}
else if ("pubsub#subscription_required".equals(field.getVariable())) {
// TODO Replace this variable for the one defined in the JEP (once one is defined)
subscriptionConfigurationRequired = parseFirstValueAsBoolean( field, true ) ;
}
else if ("pubsub#type".equals(field.getVariable())) {
values = field.getValues();
payloadType = !values.isEmpty() ? values.get(0) : " ";
}
else if ("pubsub#body_xslt".equals(field.getVariable())) {
values = field.getValues();
bodyXSLT = !values.isEmpty() ? values.get(0) : " ";
}
else if ("pubsub#dataform_xslt".equals(field.getVariable())) {
values = field.getValues();
dataformXSLT = !values.isEmpty() ? values.get(0) : " ";
}
else if ("pubsub#access_model".equals(field.getVariable())) {
values = field.getValues();
if (!values.isEmpty()) {
accessModel = AccessModel.valueOf(values.get(0));
}
}
else if ("pubsub#publish_model".equals(field.getVariable())) {
values = field.getValues();
if (!values.isEmpty()) {
publisherModel = PublisherModel.valueOf(values.get(0));
}
}
else if ("pubsub#roster_groups_allowed".equals(field.getVariable())) {
// Get the new list of roster group(s) allowed to subscribe and retrieve items
rosterGroupsAllowed = new ArrayList<>();
for (String value : field.getValues()) {
addAllowedRosterGroup(value);
}
}
else if ("pubsub#contact".equals(field.getVariable())) {
// Get the new list of users that may be contacted with questions
contacts = new ArrayList<>();
for (String value : field.getValues()) {
try {
addContact(new JID(value));
}
catch (Exception e) {
// Do nothing
}
}
}
else if ("pubsub#description".equals(field.getVariable())) {
values = field.getValues();
description = !values.isEmpty() ? values.get(0) : " ";
}
else if ("pubsub#language".equals(field.getVariable())) {
values = field.getValues();
language = !values.isEmpty() ? values.get(0) : " ";
}
else if ("pubsub#title".equals(field.getVariable())) {
values = field.getValues();
name = !values.isEmpty() ? values.get(0) : " ";
}
else if ("pubsub#itemreply".equals(field.getVariable())) {
values = field.getValues();
if (!values.isEmpty()) {
replyPolicy = ItemReplyPolicy.valueOf(values.get(0));
}
}
else if ("pubsub#replyroom".equals(field.getVariable())) {
// Get the new list of multi-user chat rooms to specify for replyroom
replyRooms = new ArrayList<>();
for (String value : field.getValues()) {
try {
addReplyRoom(new JID(value));
}
catch (Exception e) {
// Do nothing
}
}
}
else if ("pubsub#replyto".equals(field.getVariable())) {
// Get the new list of JID(s) to specify for replyto
replyTo = new ArrayList<>();
for (String value : field.getValues()) {
try {
addReplyTo(new JID(value));
}
catch (Exception e) {
// Do nothing
}
}
}
else if ("pubsub#collection".equals(field.getVariable())) {
// Set the parent collection node
values = field.getValues();
String newParent = !values.isEmpty() ? values.get(0) : " ";
Node newParentNode = getService().getNode(newParent);
if (!(newParentNode instanceof CollectionNode))
{
throw new NotAcceptableException("Specified node in field pubsub#collection [" + newParent + "] " + ((newParentNode == null) ? "does not exist" : "is not a collection node"));
}
changeParent((CollectionNode)newParentNode);
}
else {
// Let subclasses be configured by specified fields
configure(field);
}
}
getLogger().trace("... applied all fields from form.");
// Set new list of owners of the node
if (ownersSent) {
getLogger().trace("... set new list of owners of the node");
// Calculate owners to remove and remove them from the DB
Collection<JID> oldOwners = getOwners();
oldOwners.removeAll(owners);
for (JID jid : oldOwners) {
removeOwner(jid);
}
// Calculate new owners and add them to the DB
owners.removeAll(getOwners());
for (JID jid : owners) {
addOwner(jid);
}
}
// TODO Before removing owner or admin check if user was changed from admin to owner or vice versa. This way his subscriptions are not going to be deleted.
FormField publisherField = completedForm.getField("pubsub#publisher");
if (publisherField != null) {
getLogger().trace("... set new list of owners of the node");
// New list of publishers was sent to update publishers of the node
List<JID> publishers = new ArrayList<>();
for (String value : publisherField.getValues()) {
try {
publishers.add(new JID(value));
}
catch (Exception e) {
// Do nothing
}
}
// Calculate publishers to remove and remove them from the DB
Collection<JID> oldPublishers = getPublishers();
oldPublishers.removeAll(publishers);
for (JID jid : oldPublishers) {
removePublisher(jid);
}
// Calculate new publishers and add them to the DB
publishers.removeAll(getPublishers());
for (JID jid : publishers) {
addPublisher(jid);
}
}
getLogger().trace("... Let subclasses have a chance to finish node configuration based on the completed form");
postConfigure(completedForm);
// Update the modification date to reflect the last time when the node's configuration was modified
modificationDate = new Date();
// Notify subscribers that the node configuration has changed
nodeConfigurationChanged();
}
// Store the new or updated node in the backend store
saveToDB();
// Check if we need to subscribe or unsubscribe from affiliate presences
if (wasPresenceBased != isPresenceBasedDelivery()) {
if (isPresenceBasedDelivery()) {
addPresenceSubscriptions();
}
else {
cancelPresenceSubscriptions();
}
}
}
/**
* Configures the node with the completed form field. Fields that are common to leaf
* and collection nodes are handled in {@link #configure(org.xmpp.forms.DataForm)}.
* Subclasses should implement this method in order to configure the node with form
* fields specific to the node type.
*
* @param field the form field specific to the node type.
* @throws NotAcceptableException if field cannot be configured because of invalid data.
*/
protected abstract void configure(FormField field) throws NotAcceptableException;
/**
* Node configuration was changed based on the completed form. Subclasses may implement
* this method to finsh node configuration based on the completed form.
*
* @param completedForm the form completed by the node owner.
*/
abstract void postConfigure(DataForm completedForm);
/**
* The node configuration has changed. If this is the first time the node is configured
* after it was created (i.e. is not yet persistent) then do nothing. Otherwise, send
* a notification to the node subscribers informing that the configuration has changed.
*/
private void nodeConfigurationChanged() {
if (!isNotifiedOfConfigChanges() || !savedToDB) {
getLogger().trace("Node configuration changed. No notifications needed: node was just created and configured or notification of config changes is disabled");
return;
}
// Build packet to broadcast to subscribers
Message message = new Message();
Element event = message.addChildElement("event", "http://jabber.org/protocol/pubsub#event");
Element config = event.addElement("configuration");
config.addAttribute("node", nodeID);
if (deliverPayloads) {
config.add(getConfigurationChangeForm(null).getElement()); // FIXME localize this form for each recipient.
}
getLogger().trace("Node configuration changed. Broadcast notification that the node configuration has changed.");
broadcastNodeEvent(message, false);
final CollectionNode parent = getParent();
if (parent != null){
getLogger().trace("Node configuration changed. Notify parent node '{}' so that it can notify subscribers with proper subscription depth.", parent);
parent.childNodeModified(this, message);
}
}
/**
* Returns the data form to be included in the authorization request to be sent to
* node owners when a new subscription needs to be approved.
*
* @param subscription the new subscription that needs to be approved.
* @return the data form to be included in the authorization request.
*/
DataForm getAuthRequestForm(NodeSubscription subscription, Locale preferredLocale) {
getLogger().trace("Get data form to be sent to node owners for them to approve a new subscription request from '{}'", subscription.getJID());
DataForm form = new DataForm(DataForm.Type.form);
form.setTitle(LocaleUtils.getLocalizedString("pubsub.form.authorization.title", preferredLocale));
form.addInstruction(LocaleUtils.getLocalizedString("pubsub.form.authorization.instruction", preferredLocale));
FormField formField = form.addField();
formField.setVariable("FORM_TYPE");
formField.setType(FormField.Type.hidden);
formField.addValue("http://jabber.org/protocol/pubsub#subscribe_authorization");
formField = form.addField();
formField.setVariable("pubsub#subid");
formField.setType(FormField.Type.hidden);
formField.addValue(subscription.getID());
formField = form.addField();
formField.setVariable("pubsub#node");
formField.setType(FormField.Type.text_single);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.authorization.node", preferredLocale));
formField.addValue(nodeID);
formField = form.addField();
formField.setVariable("pubsub#subscriber_jid");
formField.setType(FormField.Type.jid_single);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.authorization.subscriber", preferredLocale));
formField.addValue(subscription.getJID().toString());
formField = form.addField();
formField.setVariable("pubsub#allow");
formField.setType(FormField.Type.boolean_type);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.authorization.allow", preferredLocale));
formField.addValue(Boolean.FALSE);
return form;
}
/**
* Returns a data form used by the owner to edit the node configuration.
*
* @param preferredLocale The preferred locale to localize the form.
* @return data form used by the owner to edit the node configuration.
*/
public DataForm getConfigurationForm(Locale preferredLocale) {
getLogger().trace("Get data form used by owner to edit the node configuration.");
DataForm form = new DataForm(DataForm.Type.form);
form.setTitle(LocaleUtils.getLocalizedString("pubsub.form.conf.title", preferredLocale));
List<String> params = new ArrayList<>();
params.add(nodeID);
form.addInstruction(LocaleUtils.getLocalizedString("pubsub.form.conf.instruction", params, preferredLocale));
FormField formField = form.addField();
formField.setVariable("FORM_TYPE");
formField.setType(FormField.Type.hidden);
formField.addValue("http://jabber.org/protocol/pubsub#node_config");
// Add the form fields and configure them for edition
addFormFields(form, preferredLocale, true);
return form;
}
/**
* Adds the required form fields to the specified form. When editing is true the field type
* and a label is included in each field. The form being completed will contain the current
* node configuration. This information can be used for editing the node or for notifying that
* the node configuration has changed.
*
* @param form the form containing the node configuration.
* @param preferredLocale the preferred locale to localize the form.
* @param isEditing true when the form will be used to edit the node configuration.
*/
protected void addFormFields(DataForm form, Locale preferredLocale, boolean isEditing) {
FormField formField = form.addField();
formField.setVariable("pubsub#title");
if (isEditing) {
formField.setType(FormField.Type.text_single);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.short_name", preferredLocale));
}
formField.addValue(name);
formField = form.addField();
formField.setVariable("pubsub#description");
if (isEditing) {
formField.setType(FormField.Type.text_single);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.description", preferredLocale));
}
formField.addValue(description);
formField = form.addField();
formField.setVariable("pubsub#node_type");
if (isEditing) {
formField.setType(FormField.Type.text_single);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.node_type", preferredLocale));
}
formField = form.addField();
formField.setVariable("pubsub#collection");
if (isEditing) {
formField.setType(FormField.Type.text_single);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.collection", preferredLocale));
}
final CollectionNode parent = getParent();
if (parent != null && !parent.isRootCollectionNode()) {
formField.addValue(parent.getUniqueIdentifier().getNodeId());
}
formField = form.addField();
formField.setVariable("pubsub#subscribe");
if (isEditing) {
formField.setType(FormField.Type.boolean_type);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.subscribe", preferredLocale));
}
formField.addValue(subscriptionEnabled);
formField = form.addField();
formField.setVariable("pubsub#subscription_required");
// TODO Replace this variable for the one defined in the JEP (once one is defined)
if (isEditing) {
formField.setType(FormField.Type.boolean_type);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.subscription_required", preferredLocale));
}
formField.addValue(subscriptionConfigurationRequired);
formField = form.addField();
formField.setVariable("pubsub#deliver_payloads");
if (isEditing) {
formField.setType(FormField.Type.boolean_type);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.deliver_payloads", preferredLocale));
}
formField.addValue(deliverPayloads);
formField = form.addField();
formField.setVariable("pubsub#notify_config");
if (isEditing) {
formField.setType(FormField.Type.boolean_type);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.notify_config", preferredLocale));
}
formField.addValue(notifyConfigChanges);
formField = form.addField();
formField.setVariable("pubsub#notify_delete");
if (isEditing) {
formField.setType(FormField.Type.boolean_type);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.notify_delete", preferredLocale));
}
formField.addValue(notifyDelete);
formField = form.addField();
formField.setVariable("pubsub#notify_retract");
if (isEditing) {
formField.setType(FormField.Type.boolean_type);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.notify_retract", preferredLocale));
}
formField.addValue(notifyRetract);
formField = form.addField();
formField.setVariable("pubsub#presence_based_delivery");
if (isEditing) {
formField.setType(FormField.Type.boolean_type);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.presence_based", preferredLocale));
}
formField.addValue(presenceBasedDelivery);
formField = form.addField();
formField.setVariable("pubsub#type");
if (isEditing) {
formField.setType(FormField.Type.text_single);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.type", preferredLocale));
}
formField.addValue(payloadType);
formField = form.addField();
formField.setVariable("pubsub#body_xslt");
if (isEditing) {
formField.setType(FormField.Type.text_single);
formField.setLabel(LocaleUtils.getLocalizedString("pubsub.form.conf.body_xslt", preferredLocale));
}
formField.addValue(bodyXSLT);