Item.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub;

  18. import org.jivesoftware.smack.packet.Message;
  19. import org.jivesoftware.smackx.pubsub.provider.ItemProvider;

  20. /**
  21.  * This class represents an item that has been, or will be published to a
  22.  * pubsub node.  An <tt>Item</tt> has several properties that are dependent
  23.  * on the configuration of the node to which it has been or will be published.
  24.  *
  25.  * <h1>An Item received from a node (via {@link LeafNode#getItems()} or {@link LeafNode#addItemEventListener(org.jivesoftware.smackx.pubsub.listener.ItemEventListener)}</b>
  26.  * <li>Will always have an id (either user or server generated) unless node configuration has both
  27.  * {@link ConfigureForm#isPersistItems()} and {@link ConfigureForm#isDeliverPayloads()}set to false.
  28.  * <li>Will have a payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
  29.  * to true, otherwise it will be null.
  30.  *
  31.  * <h1>An Item created to send to a node (via {@link LeafNode#send()} or {@link LeafNode#publish()}</b>
  32.  * <li>The id is optional, since the server will generate one if necessary, but should be used if it is
  33.  * meaningful in the context of the node.  This value must be unique within the node that it is sent to, since
  34.  * resending an item with the same id will overwrite the one that already exists if the items are persisted.
  35.  * <li>Will require payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
  36.  * to true.
  37.  *
  38.  * <p>To customise the payload object being returned from the {@link PayloadItem#getPayload()} method, you can
  39.  * add a custom parser as explained in {@link ItemProvider}.
  40.  *
  41.  * @author Robin Collier
  42.  */
  43. public class Item extends NodeExtension
  44. {
  45.     private String id;
  46.    
  47.     /**
  48.      * Create an empty <tt>Item</tt> with no id.  This is a valid item for nodes which are configured
  49.      * so that {@link ConfigureForm#isDeliverPayloads()} is false.  In most cases an id will be generated by the server.
  50.      * For nodes configured with {@link ConfigureForm#isDeliverPayloads()} and {@link ConfigureForm#isPersistItems()}
  51.      * set to false, no <tt>Item</tt> is sent to the node, you have to use {@link LeafNode#send()} or {@link LeafNode#publish()}
  52.      * methods in this case.
  53.      */
  54.     public Item()
  55.     {
  56.         super(PubSubElementType.ITEM);
  57.     }
  58.    
  59.     /**
  60.      * Create an <tt>Item</tt> with an id but no payload.  This is a valid item for nodes which are configured
  61.      * so that {@link ConfigureForm#isDeliverPayloads()} is false.
  62.      *
  63.      * @param itemId The id if the item.  It must be unique within the node unless overwriting and existing item.
  64.      * Passing null is the equivalent of calling {@link #Item()}.
  65.      */
  66.     public Item(String itemId)
  67.     {
  68.         // The element type is actually irrelevant since we override getNamespace() to return null
  69.         super(PubSubElementType.ITEM);
  70.         id = itemId;
  71.     }

  72.     /**
  73.      * Create an <tt>Item</tt> with an id and a node id.  
  74.      * <p>
  75.      * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
  76.      * one as part of {@link Message}.  If used to create an Item to publish
  77.      * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
  78.      * error for an invalid packet.
  79.      *
  80.      * @param itemId The id of the item.
  81.      * @param nodeId The id of the node which the item was published to.
  82.      */
  83.     public Item(String itemId, String nodeId)
  84.     {
  85.         super(PubSubElementType.ITEM_EVENT, nodeId);
  86.         id = itemId;
  87.     }
  88.    
  89.     /**
  90.      * Get the item id.  Unique to the node it is associated with.
  91.      *
  92.      * @return The id
  93.      */
  94.     public String getId()
  95.     {
  96.         return id;
  97.     }
  98.    
  99.     @Override
  100.     public String getNamespace()
  101.     {
  102.         return null;
  103.     }

  104.     @Override
  105.     public String toXML()
  106.     {
  107.         StringBuilder builder = new StringBuilder("<item");
  108.        
  109.         if (id != null)
  110.         {
  111.             builder.append(" id='");
  112.             builder.append(id);
  113.             builder.append("'");
  114.         }
  115.        
  116.         if (getNode() != null) {
  117.             builder.append(" node='");
  118.             builder.append(getNode());
  119.             builder.append("'");
  120.         }
  121.         builder.append("/>");

  122.         return builder.toString();
  123.     }

  124.     @Override
  125.     public String toString()
  126.     {
  127.         return getClass().getName() + " | Content [" + toXML() + "]";
  128.     }
  129. }