Commit 9ec55611 authored by Armando Jagucki's avatar Armando Jagucki Committed by ajagucki

PEP: Refactored the sendLastPublishedItem method in PEPService a bit.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/branches@8915 b35dd754-fafc-0310-a699-88a17e54d16e
parent 2f2a68dc
...@@ -32,9 +32,10 @@ import org.jivesoftware.openfire.pubsub.LeafNode; ...@@ -32,9 +32,10 @@ import org.jivesoftware.openfire.pubsub.LeafNode;
import org.jivesoftware.openfire.pubsub.Node; import org.jivesoftware.openfire.pubsub.Node;
import org.jivesoftware.openfire.pubsub.NodeSubscription; import org.jivesoftware.openfire.pubsub.NodeSubscription;
import org.jivesoftware.openfire.pubsub.PubSubEngine; import org.jivesoftware.openfire.pubsub.PubSubEngine;
import org.jivesoftware.openfire.pubsub.PublishedItem;
import org.jivesoftware.openfire.pubsub.models.AccessModel; import org.jivesoftware.openfire.pubsub.models.AccessModel;
import org.jivesoftware.openfire.roster.Roster; import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.roster.RosterEventDispatcher;
import org.jivesoftware.openfire.roster.RosterEventListener;
import org.jivesoftware.openfire.roster.RosterItem; import org.jivesoftware.openfire.roster.RosterItem;
import org.jivesoftware.openfire.session.ClientSession; import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.session.Session; import org.jivesoftware.openfire.session.Session;
...@@ -438,6 +439,8 @@ public class IQPEPHandler extends IQHandler implements ServerIdentitiesProvider, ...@@ -438,6 +439,8 @@ public class IQPEPHandler extends IQHandler implements ServerIdentitiesProvider,
} }
} }
} }
pepService.sendLastPublishedItem(subscriberJID);
} }
} }
...@@ -456,35 +459,7 @@ public class IQPEPHandler extends IQHandler implements ServerIdentitiesProvider, ...@@ -456,35 +459,7 @@ public class IQPEPHandler extends IQHandler implements ServerIdentitiesProvider,
for (PEPService pepService : pepServices.values()) { for (PEPService pepService : pepServices.values()) {
try { try {
if (presenceManager.canProbePresence(newlyAvailableJID, pepService.getAddress().getNode())) { if (presenceManager.canProbePresence(newlyAvailableJID, pepService.getAddress().getNode())) {
// Retrieve last published item. pepService.sendLastPublishedItem(newlyAvailableJID);
CollectionNode rootNode = pepService.getRootCollectionNode();
PublishedItem lastPublishedItem = null;
for (Node leafNode : rootNode.getNodes()) {
PublishedItem leafLastPublishedItem = leafNode.getLastPublishedItem();
if (leafLastPublishedItem == null) {
continue;
}
if (lastPublishedItem == null) {
lastPublishedItem = leafLastPublishedItem;
continue;
}
if (leafLastPublishedItem.getCreationDate().compareTo(lastPublishedItem.getCreationDate()) > 0) {
lastPublishedItem = leafLastPublishedItem;
}
}
// Send last published item to the newly-available resource.
NodeSubscription subscription = rootNode.getSubscription(newlyAvailableJID);
if (subscription == null) {
subscription = rootNode.getSubscription(new JID(newlyAvailableJID.toBareJID()));
}
if (subscription != null) {
pepService.sendLastPublishedItem(subscription, lastPublishedItem);
}
} }
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
...@@ -672,5 +647,4 @@ public class IQPEPHandler extends IQHandler implements ServerIdentitiesProvider, ...@@ -672,5 +647,4 @@ public class IQPEPHandler extends IQHandler implements ServerIdentitiesProvider,
public Map<String, HashSet<JID>> getKnownRemotePresenes() { public Map<String, HashSet<JID>> getKnownRemotePresenes() {
return knownRemotePresences; return knownRemotePresences;
} }
} }
...@@ -501,19 +501,47 @@ public class PEPService implements PubSubService { ...@@ -501,19 +501,47 @@ public class PEPService implements PubSubService {
} }
/** /**
* Sends an event notification for the last published item to the subscription's subscriber. If * Sends an event notification for the last published item to the recipient JID. For
* the subscription has not yet been authorized or is pending to be configured then * a PEPService, the last published item is the latest item published for all of the
* no notification is going to be sent.<p> * last published items of each leaf node under the root collection node. If the
* recipient has no subscription to the root collection node, has not yet been authorized,
* or is pending to be configured -- then no notification is going to be sent.<p>
* *
* Depending on the subscription configuration the event notification may or may not have * Depending on the subscription configuration the event notification may or may not have
* a payload, may not be sent if a keyword (i.e. filter) was defined and it was not matched. * a payload, may not be sent if a keyword (i.e. filter) was defined and it was not matched.
* *
* @param subscription the subscription the published item is being sent for. * @param recipientJID the recipient that is to receive the last published item notification.
* @param publishedItem the last item that was published to the node.
*/ */
public void sendLastPublishedItem(NodeSubscription subscription, PublishedItem publishedItem) { public void sendLastPublishedItem(JID recipientJID) {
// Retrieve last published item.
PublishedItem lastPublishedItem = null;
for (Node leafNode : rootCollectionNode.getNodes()) {
PublishedItem leafLastPublishedItem = leafNode.getLastPublishedItem();
if (leafLastPublishedItem == null) {
continue;
}
if (lastPublishedItem == null) {
lastPublishedItem = leafLastPublishedItem;
continue;
}
if (leafLastPublishedItem.getCreationDate().compareTo(lastPublishedItem.getCreationDate()) > 0) {
lastPublishedItem = leafLastPublishedItem;
}
}
NodeSubscription subscription = rootCollectionNode.getSubscription(recipientJID);
if (subscription == null) {
subscription = rootCollectionNode.getSubscription(new JID(recipientJID.toBareJID()));
}
if (subscription == null) {
return;
}
// Check if the published item can be sent to the subscriber // Check if the published item can be sent to the subscriber
if (!subscription.canSendPublicationEvent(publishedItem.getNode(), publishedItem)) { if (!subscription.canSendPublicationEvent(lastPublishedItem.getNode(), lastPublishedItem)) {
return; return;
} }
// Send event notification to the subscriber // Send event notification to the subscriber
...@@ -521,13 +549,13 @@ public class PEPService implements PubSubService { ...@@ -521,13 +549,13 @@ public class PEPService implements PubSubService {
Element event = notification.getElement() Element event = notification.getElement()
.addElement("event", "http://jabber.org/protocol/pubsub#event"); .addElement("event", "http://jabber.org/protocol/pubsub#event");
Element items = event.addElement("items"); Element items = event.addElement("items");
items.addAttribute("node", publishedItem.getNode().getNodeID()); items.addAttribute("node", lastPublishedItem.getNode().getNodeID());
Element item = items.addElement("item"); Element item = items.addElement("item");
if (((LeafNode) publishedItem.getNode()).isItemRequired()) { if (((LeafNode) lastPublishedItem.getNode()).isItemRequired()) {
item.addAttribute("id", publishedItem.getID()); item.addAttribute("id", lastPublishedItem.getID());
} }
if (publishedItem.getNode().isPayloadDelivered() && publishedItem.getPayload() != null) { if (lastPublishedItem.getNode().isPayloadDelivered() && lastPublishedItem.getPayload() != null) {
item.add(publishedItem.getPayload().createCopy()); item.add(lastPublishedItem.getPayload().createCopy());
} }
// Add a message body (if required) // Add a message body (if required)
if (subscription.isIncludingBody()) { if (subscription.isIncludingBody()) {
...@@ -535,7 +563,7 @@ public class PEPService implements PubSubService { ...@@ -535,7 +563,7 @@ public class PEPService implements PubSubService {
} }
// Include date when published item was created // Include date when published item was created
notification.getElement().addElement("x", "jabber:x:delay") notification.getElement().addElement("x", "jabber:x:delay")
.addAttribute("stamp", fastDateFormat.format(publishedItem.getCreationDate())); .addAttribute("stamp", fastDateFormat.format(lastPublishedItem.getCreationDate()));
// Send the event notification to the subscriber // Send the event notification to the subscriber
this.sendNotification(subscription.getNode(), notification, subscription.getJID()); this.sendNotification(subscription.getNode(), notification, subscription.getJID());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment