-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathNodeAffiliate.java
More file actions
380 lines (344 loc) · 17.7 KB
/
Copy pathNodeAffiliate.java
File metadata and controls
380 lines (344 loc) · 17.7 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
/*
* 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.pep.PEPService;
import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.util.cache.CacheSizes;
import org.jivesoftware.util.cache.Cacheable;
import org.jivesoftware.util.cache.CannotCalculateSizeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import java.util.*;
import java.util.stream.Collectors;
/**
* A NodeAffiliate keeps information about the affiliation of an entity with a node. Possible
* affiliations are: owner, publisher, none or outcast. All except for outcast affiliations
* may have a {@link NodeSubscription} with the node.
*
* @author Matt Tucker
*/
public class NodeAffiliate implements Cacheable
{
private final Logger Log;
private final JID jid;
private final Node node;
private Affiliation affiliation;
public NodeAffiliate(Node node, JID jid) {
this.node = node;
this.jid = jid;
Log = LoggerFactory.getLogger(getClass().getName() + "[" + node.getNodeID() + " " + jid +"]");
}
public Node getNode() {
return node;
}
public JID getJID() {
return jid;
}
public Affiliation getAffiliation() {
return affiliation;
}
public void setAffiliation(Affiliation affiliation) {
this.affiliation = affiliation;
}
/**
* Returns the list of subscriptions of the affiliate in the node.
*
* @return the list of subscriptions of the affiliate in the node.
*/
public Collection<NodeSubscription> getSubscriptions() {
return node.getSubscriptions(jid);
}
/**
* Sends an event notification for the published items to the affiliate. The event
* notification may contain zero, one or many published items based on the items
* included in the original publication. If the affiliate has many subscriptions and
* many items were published then the affiliate will get a notification for each set
* of items that affected the same subscriptions.
*
* If the affiliate is an owner of the node, and the node is in a PEP service, then
* all connected resources of the affiliated user will be sent an event notification.
*
* @param notification the message to sent to the subscribers. The message will be completed
* with the items to include in each notification.
* @param event the event Element included in the notification message. Passed as an
* optimization to avoid future look ups.
* @param leafNode the leaf node where the items where published.
* @param publishedItems the list of items that were published. Could be an empty list.
*/
void sendPublishedNotifications(Message notification, Element event, LeafNode leafNode, List<PublishedItem> publishedItems)
{
if (!publishedItems.isEmpty()) {
Map<List<NodeSubscription>, List<PublishedItem>> itemsBySubs = getItemsBySubscriptions(leafNode, publishedItems);
Log.trace("Sending publication notification for {} published item(s) to {} subscription(s) in {} batch(es).", publishedItems.size(), itemsBySubs.keySet().stream().flatMap(Collection::stream).collect(Collectors.toSet()).size(), itemsBySubs.size());
// Send one notification for published items that affect the same subscriptions
for (final Map.Entry<List<NodeSubscription>, List<PublishedItem>> entry : itemsBySubs.entrySet()) {
final List<NodeSubscription> nodeSubscriptions = entry.getKey();
final List<PublishedItem> groupedItems = entry.getValue();
Log.trace("Preparing publication notification batch for {} item(s) to {} subscription(s).", groupedItems.size(), nodeSubscriptions.size());
// Add items information
Element items = event.addElement("items");
items.addAttribute("node", getNode().getUniqueIdentifier().getNodeId());
for (PublishedItem publishedItem : groupedItems) {
// FIXME: This was added for compatibility with PEP supporting clients.
// Alternate solution needed when XEP-0163 version > 1.0 is released.
//
// If the node ID looks like a JID, replace it with the published item's node ID.
if (getNode().getUniqueIdentifier().getNodeId().contains("@")) {
items.addAttribute("node", publishedItem.getNodeID());
}
// Add item information to the event notification
Element item = items.addElement("item");
if (leafNode.isItemRequired()) {
item.addAttribute("id", publishedItem.getID());
}
if (leafNode.isPayloadDelivered()) {
item.add(publishedItem.getPayload().createCopy());
}
// Add leaf leafNode information if affiliated leafNode and node
// where the item was published are different
if (leafNode != getNode()) {
item.addAttribute("node", leafNode.getUniqueIdentifier().getNodeId());
}
}
// Send the event notification
sendEventNotification(notification, nodeSubscriptions);
// Remove the added items information
event.remove(items);
}
}
else {
// Filter affiliate subscriptions and only use approved and configured ones
List<NodeSubscription> affectedSubscriptions = new ArrayList<>();
for (NodeSubscription subscription : getSubscriptions()) {
if (subscription.canSendPublicationEvent(leafNode, null)) {
affectedSubscriptions.add(subscription);
}
}
Log.trace("Sending publication notification to {} subscription(s)", affectedSubscriptions.size());
// Add item information to the event notification
Element items = event.addElement("items");
items.addAttribute("node", leafNode.getUniqueIdentifier().getNodeId());
// Send the event notification
sendEventNotification(notification, affectedSubscriptions);
// Remove the added items information
event.remove(items);
}
// TODO this horribly duplicates part of the functionality above. Code-clutter should be reduced.
// XEP-0136 specifies that all connected resources of the owner of the PEP service should also get a notification.
if (leafNode.getService() instanceof PEPService) {
final PEPService service = (PEPService) leafNode.getService();
final Collection<ClientSession> sessions = SessionManager.getInstance().getSessions(service.getAddress().getNode());
Log.trace("Sending publication notification to {} session(s) of PEP node owner.", sessions.size());
for( final ClientSession session : sessions ) {
// Add item information to the event notification
Element items = event.addElement("items");
items.addAttribute("node", leafNode.getUniqueIdentifier().getNodeId());
for (PublishedItem publishedItem : publishedItems) {
// FIXME: This was added for compatibility with PEP supporting clients.
// Alternate solution needed when XEP-0163 version > 1.0 is released.
//
// If the node ID looks like a JID, replace it with the published item's node ID.
if (getNode().getUniqueIdentifier().getNodeId().contains("@")) {
items.addAttribute("node", publishedItem.getNodeID());
}
// Add item information to the event notification
Element item = items.addElement("item");
if (leafNode.isItemRequired()) {
item.addAttribute("id", publishedItem.getID());
}
if (leafNode.isPayloadDelivered()) {
item.add(publishedItem.getPayload().createCopy());
}
// Add leaf leafNode information if affiliated leafNode and node
// where the item was published are different
if (leafNode != getNode()) {
item.addAttribute("node", leafNode.getUniqueIdentifier().getNodeId());
}
}
// Send the event notification
service.sendNotification(leafNode, notification, session.getAddress());
// Remove the added items information
event.remove(items);
}
}
}
/**
* Sends an event notification to the affiliate for the deleted items. The event
* notification may contain one or many published items based on the items included
* in the original publication. If the affiliate has many subscriptions and many
* items were deleted then the affiliate will get a notification for each set
* of items that affected the same subscriptions.
*
* @param notification the message to sent to the subscribers. The message will be completed
* with the items to include in each notification.
* @param event the event Element included in the notification message. Passed as an
* optimization to avoid future look ups.
* @param leafNode the leaf node where the items where deleted from.
* @param publishedItems the list of items that were deleted.
*/
void sendDeletionNotifications(Message notification, Element event, LeafNode leafNode,
List<PublishedItem> publishedItems) {
if (publishedItems.isEmpty()) {
Log.debug("Skip sending delete notifications as the list of deleted items was empty.");
return;
}
Map<List<NodeSubscription>, List<PublishedItem>> itemsBySubs = getItemsBySubscriptions(leafNode, publishedItems);
Log.trace("Sending delete notification for {} published item(s) to {} subscription(s) in {} batch(es).", publishedItems.size(), itemsBySubs.keySet().stream().flatMap(Collection::stream).collect(Collectors.toSet()).size(), itemsBySubs.size());
// Send one notification for published items that affect the same subscriptions
for (final Map.Entry<List<NodeSubscription>, List<PublishedItem>> entry : itemsBySubs.entrySet()) {
final List<NodeSubscription> nodeSubscriptions = entry.getKey();
final List<PublishedItem> groupedItems = entry.getValue();
Log.trace("Preparing delete notification batch for {} item(s) to {} subscription(s).", groupedItems.size(), nodeSubscriptions.size());
// Add items information
Element items = event.addElement("items");
items.addAttribute("node", leafNode.getUniqueIdentifier().getNodeId());
for (PublishedItem publishedItem : itemsBySubs.get(nodeSubscriptions)) {
// Add retract information to the event notification
Element item = items.addElement("retract");
if (leafNode.isItemRequired()) {
item.addAttribute("id", publishedItem.getID());
}
}
// Send the event notification
sendEventNotification(notification, nodeSubscriptions);
// Remove the added items information
event.remove(items);
}
}
/**
* Sends an event notification to each affected subscription of the affiliate. If the owner
* has many subscriptions from the same full JID then a single notification is going to be
* sent including a detail of the subscription IDs for which the notification is being sent.<p>
*
* Event notifications may include notifications of new published items or of items that
* were deleted.<p>
*
* The original publication to the node may or may not contain a {@link PublishedItem}. The
* subscriptions of the affiliation will be filtered based on the published item (if one was
* specified), the subscription status and originating node.
*
* @param notification the message to send containing the event notification.
* @param notifySubscriptions list of subscriptions that were affected and are going to be
* included in the notification message. The list should not be empty.
*/
private void sendEventNotification(Message notification, List<NodeSubscription> notifySubscriptions) {
if (node.isMultipleSubscriptionsEnabled() || node.isCollectionNode()) {
// Group subscriptions with the same subscriber JID
Map<JID, Collection<String>> groupedSubs = new HashMap<>();
for (NodeSubscription subscription : notifySubscriptions) {
groupedSubs.computeIfAbsent(subscription.getJID(), k -> new ArrayList<>()).add(subscription.getID());
}
// Send an event notification to each subscriber with a different JID
for (final Map.Entry<JID, Collection<String>> entry : groupedSubs.entrySet()) {
final JID subscriberJID = entry.getKey();
final Collection<String> subIDs = entry.getValue();
// Send the notification to the subscriber
node.sendEventNotification(subscriberJID, notification, subIDs);
}
}
else {
// Affiliate should have at most one subscription per unique JID
if (!notifySubscriptions.isEmpty()) {
List<JID> subs = new ArrayList<>();
for(NodeSubscription subscription: notifySubscriptions) {
JID sub = subscription.getJID();
if (!subs.contains(sub)) {
node.sendEventNotification(sub, notification, null);
subs.add(sub);
}
}
}
}
}
private Map<List<NodeSubscription>, List<PublishedItem>> getItemsBySubscriptions(LeafNode leafNode, List<PublishedItem> publishedItems) {
// Identify which subscriptions can receive each item
Map<PublishedItem, List<NodeSubscription>> subsByItem = new HashMap<>();
// Filter affiliate subscriptions and only use approved and configured ones
Collection<NodeSubscription> subscriptions = getSubscriptions();
for (PublishedItem publishedItem : publishedItems) {
for (NodeSubscription subscription : subscriptions) {
if (subscription.canSendPublicationEvent(leafNode, publishedItem)) {
subsByItem.computeIfAbsent(publishedItem, k -> new ArrayList<>()).add(subscription);
}
}
}
// Identify which items should be sent together to the same subscriptions
Map<List<NodeSubscription>, List<PublishedItem>> itemsBySubs = new HashMap<>();
List<PublishedItem> affectedSubscriptions;
for (PublishedItem publishedItem : subsByItem.keySet()) {
affectedSubscriptions = itemsBySubs.get(subsByItem.get(publishedItem));
if (affectedSubscriptions == null) {
List<PublishedItem> items = new ArrayList<>(publishedItems.size());
items.add(publishedItem);
itemsBySubs.put(subsByItem.get(publishedItem), items);
}
else {
affectedSubscriptions.add(publishedItem);
}
}
return itemsBySubs;
}
@Override
public String toString() {
return super.toString() + " - JID: " + getJID() + " - Affiliation: " +
getAffiliation().name();
}
/**
* Returns the approximate size of the Object in bytes. The size should be
* considered to be a best estimate of how much memory the Object occupies
* and may be based on empirical trials or dynamic calculations.<p>
*
* @return the size of the Object in bytes.
*/
@Override
public int getCachedSize() throws CannotCalculateSizeException
{
int size = CacheSizes.sizeOfObject();
size += CacheSizes.sizeOfAnything( jid );
size += CacheSizes.sizeOfInt(); // affiliation
size += CacheSizes.sizeOfInt(); // reference to node
return size;
}
/**
* Affiliation with a node defines user permissions.
*/
public static enum Affiliation {
/**
* An owner can publish, delete and purge items as well as configure and delete the node.
*/
owner,
/**
* A publisher can subscribe and publish items to the node.
*/
publisher,
/**
* A member can subscribe to and retrieve items from whitelist nodes.
*/
member,
/**
* A user with no affiliation can susbcribe to the node.
*/
none,
/**
* Outcast users are not allowed to subscribe to the node.
*/
outcast
}
}