Author: mjakl
Date: Fri Jul  3 19:02:11 2009
New Revision: 790984

URL: http://svn.apache.org/viewvc?rev=790984&view=rev
Log:
Introduced a new wrapper for the payload (PayloadItem) which stores the time we 
received the message and hence is used to return the list of items in a 
non-arbitrary ordering for disco#items (reversed chronological). Fixes 
VYSPER-84.

Added:
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/PayloadItem.java
Modified:
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/ItemVisitor.java
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/NodeDiscoItemsVisitor.java
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/LeafNode.java
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/storageprovider/LeafNodeInMemoryStorageProvider.java
    
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/disco/PubSubDiscoItemsTestCase.java

Modified: 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/ItemVisitor.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/ItemVisitor.java?rev=790984&r1=790983&r2=790984&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/ItemVisitor.java
 (original)
+++ 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/ItemVisitor.java
 Fri Jul  3 19:02:11 2009
@@ -22,8 +22,8 @@
 import java.util.List;
 
 import org.apache.vysper.compliance.SpecCompliant;
+import 
org.apache.vysper.xmpp.modules.extension.xep0060_pubsub.model.PayloadItem;
 import org.apache.vysper.xmpp.modules.servicediscovery.management.Item;
-import org.apache.vysper.xmpp.xmlfragment.XMLElement;
 
 /**
  * The SubscriberVisitor is used to visit all subscriptions of a node.
@@ -40,10 +40,11 @@
      * @param itemID the id of the message
      * @param payload the payload of the message
      */
-    void visit(String itemID, XMLElement payload);
+    void visit(String itemID, PayloadItem payload);
     
     /**
-     * @return the ordered list of items.
+     * @return the ordered list of items. The items should be reversed 
chronologically
+     * ordered.
      */
     public List<Item> getItemList();
 

Modified: 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/NodeDiscoItemsVisitor.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/NodeDiscoItemsVisitor.java?rev=790984&r1=790983&r2=790984&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/NodeDiscoItemsVisitor.java
 (original)
+++ 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/NodeDiscoItemsVisitor.java
 Fri Jul  3 19:02:11 2009
@@ -20,11 +20,12 @@
 package org.apache.vysper.xmpp.modules.extension.xep0060_pubsub;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.vysper.xmpp.addressing.Entity;
+import 
org.apache.vysper.xmpp.modules.extension.xep0060_pubsub.model.PayloadItem;
 import org.apache.vysper.xmpp.modules.servicediscovery.management.Item;
-import org.apache.vysper.xmpp.xmlfragment.XMLElement;
 
 /**
  * This visitor is used to collect all items of a node for disco#items 
requests.
@@ -35,7 +36,7 @@
 public class NodeDiscoItemsVisitor implements ItemVisitor {
 
     // list to hold the items (ordered)
-    List<Item> itemList = new ArrayList<Item>();
+    List<PayloadItem> itemList = new ArrayList<PayloadItem>();
     // The JID of the pubsub service
     Entity serviceJID;
     
@@ -49,15 +50,20 @@
      * 
      * @see 
org.apache.vysper.xmpp.modules.extension.xep0060_pubsub.ItemVisitor#visit(java.lang.String,
 org.apache.vysper.xmpp.xmlfragment.XMLElement)
      */
-    public void visit(String itemID, XMLElement payload) {
-        itemList.add(new Item(serviceJID, itemID, null));
+    public void visit(String itemID, PayloadItem payload) {
+        itemList.add(payload);
     }
     
     /**
      * @return the ordered list of items.
      */
     public List<Item> getItemList() {
-        return itemList;
+        List<Item> discoItems = new ArrayList<Item>();
+        Collections.sort(itemList);
+        for(PayloadItem pi : itemList) {
+            discoItems.add(new Item(serviceJID, pi.getItemID(), null));
+        }
+        return discoItems;
     }
 
 }

Modified: 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/LeafNode.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/LeafNode.java?rev=790984&r1=790983&r2=790984&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/LeafNode.java
 (original)
+++ 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/LeafNode.java
 Fri Jul  3 19:02:11 2009
@@ -153,11 +153,11 @@
      * Publish an item to this node.
      * @param sender the sender of the message (publisher).
      * @param relay the relay for sending the messages.
-     * @param messageID the ID of the published message.
+     * @param itemID the ID of the published message.
      * @param item the payload of the message.
      */
-    public void publish(Entity sender, StanzaRelay relay, String messageID, 
XMLElement item) {
-        storage.addMessage(name, messageID, item);
+    public void publish(Entity sender, StanzaRelay relay, String itemID, 
XMLElement item) {
+        storage.addMessage(name, itemID, item);
         sendMessageToSubscriber(relay, item);
     }
 

Added: 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/PayloadItem.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/PayloadItem.java?rev=790984&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/PayloadItem.java
 (added)
+++ 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/model/PayloadItem.java
 Fri Jul  3 19:02:11 2009
@@ -0,0 +1,66 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.vysper.xmpp.modules.extension.xep0060_pubsub.model;
+
+import java.util.Date;
+
+import org.apache.vysper.xmpp.xmlfragment.XMLElement;
+
+/**
+ * This class encapsulates the payload of published notifications. It stores
+ * additional information (like the date published) etc.
+ * 
+ * @author The Apache MINA Project (http://mina.apache.org)
+ */
+public class PayloadItem implements Comparable<PayloadItem> {
+    
+    // the id of the item
+    protected String itemID;
+    // tha payload
+    protected XMLElement payload;
+    // the date-time we received the payload
+    protected Date publishedDate;
+    
+    /**
+     * Create new PayloadItem with the XML encoded payload and its itemID.
+     * @param payload
+     * @param itemID
+     */
+    public PayloadItem(XMLElement payload, String itemID) {
+        this.payload = payload;
+        this.itemID = itemID;
+        this.publishedDate = new Date(); // initialized with the current 
date/time
+    }
+
+    /**
+     * Compares the two publishedDates.
+     */
+    public int compareTo(PayloadItem o) {
+        return this.publishedDate.compareTo(o.publishedDate);
+    }
+
+    /**
+     * @return the itemID of the item.
+     */
+    public String getItemID() {
+        return itemID;
+    }
+
+}

Modified: 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/storageprovider/LeafNodeInMemoryStorageProvider.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/storageprovider/LeafNodeInMemoryStorageProvider.java?rev=790984&r1=790983&r2=790984&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/storageprovider/LeafNodeInMemoryStorageProvider.java
 (original)
+++ 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/storageprovider/LeafNodeInMemoryStorageProvider.java
 Fri Jul  3 19:02:11 2009
@@ -25,6 +25,7 @@
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.modules.extension.xep0060_pubsub.ItemVisitor;
 import 
org.apache.vysper.xmpp.modules.extension.xep0060_pubsub.SubscriberVisitor;
+import 
org.apache.vysper.xmpp.modules.extension.xep0060_pubsub.model.PayloadItem;
 import org.apache.vysper.xmpp.xmlfragment.XMLElement;
 
 /**
@@ -38,14 +39,14 @@
     // stores subscribers, access via subid
     protected Map<String, Entity> subscribers;
     // stores messages, access via itemid
-    protected Map<String, XMLElement> messages;
+    protected Map<String, PayloadItem> messages;
 
     /**
      * Initialize the storage maps.
      */
     public LeafNodeInMemoryStorageProvider() {
         this.subscribers = new TreeMap<String, Entity>();
-        this.messages = new TreeMap<String, XMLElement>();
+        this.messages = new TreeMap<String, PayloadItem>();
     }
 
     /**
@@ -113,8 +114,8 @@
     /**
      * Add a message to the storage.
      */
-    public void addMessage(String nodeName, String messageID, XMLElement item) 
{
-        messages.put(messageID, item);
+    public void addMessage(String nodeName, String itemID, XMLElement payload) 
{
+        messages.put(itemID, new PayloadItem(payload, itemID));
     }
 
     /**

Modified: 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/disco/PubSubDiscoItemsTestCase.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/disco/PubSubDiscoItemsTestCase.java?rev=790984&r1=790983&r2=790984&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/disco/PubSubDiscoItemsTestCase.java
 (original)
+++ 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0060_pubsub/disco/PubSubDiscoItemsTestCase.java
 Fri Jul  3 19:02:11 2009
@@ -132,7 +132,9 @@
         XMLElement item2 = new 
XMLElement("item2","namespace2",(Attribute[])null, (XMLFragment[])null);
         XMLElement item3 = new 
XMLElement("item3","namespace3",(Attribute[])null, (XMLFragment[])null);
         node.publish(client, relay, "itemid1", item1);
+        Thread.sleep(10);
         node.publish(client, relay, "itemid2", item2);
+        Thread.sleep(10);
         node.publish(client, relay, "itemid3", item3);
         
         
@@ -157,18 +159,18 @@
         // since we have no messages, there should be no items.
         assertEquals(3, inner.size());
         
+        // the items should be returned in the reversed ordering of sending, 
make sure they are.
         boolean bItem1 = false;
         boolean bItem2 = false;
         boolean bItem3 = false;
-        
         for(XMLElement el : inner) {
             if(el.getName().equals("item") 
                     && 
el.getAttributeValue("jid").equals(serverEntity.getFullQualifiedName())) {
-                if(el.getAttributeValue("name").equals("itemid1")) {
+                if(!bItem1 && el.getAttributeValue("name").equals("itemid1")) {
                     bItem1 = true;
-                } else if(el.getAttributeValue("name").equals("itemid2")) {
+                } else if(bItem1 && !bItem2 && 
el.getAttributeValue("name").equals("itemid2")) {
                     bItem2 = true;
-                } else if(el.getAttributeValue("name").equals("itemid3")) {
+                } else if(bItem1 && bItem2 && !bItem3 && 
el.getAttributeValue("name").equals("itemid3")) {
                     bItem3 = true;
                 }
             }


Reply via email to