PayloadItem.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.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smackx.pubsub.provider.ItemProvider;

  21. /**
  22.  * This class represents an item that has been, or will be published to a
  23.  * pubsub node.  An <tt>Item</tt> has several properties that are dependent
  24.  * on the configuration of the node to which it has been or will be published.
  25.  *
  26.  * <h1>An Item received from a node (via {@link LeafNode#getItems()} or {@link LeafNode#addItemEventListener(org.jivesoftware.smackx.pubsub.listener.ItemEventListener)}</b>
  27.  * <li>Will always have an id (either user or server generated) unless node configuration has both
  28.  * {@link ConfigureForm#isPersistItems()} and {@link ConfigureForm#isDeliverPayloads()}set to false.
  29.  * <li>Will have a payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
  30.  * to true, otherwise it will be null.
  31.  *
  32.  * <h1>An Item created to send to a node (via {@link LeafNode#send()} or {@link LeafNode#publish()}</b>
  33.  * <li>The id is optional, since the server will generate one if necessary, but should be used if it is
  34.  * meaningful in the context of the node.  This value must be unique within the node that it is sent to, since
  35.  * resending an item with the same id will overwrite the one that already exists if the items are persisted.
  36.  * <li>Will require payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
  37.  * to true.
  38.  *
  39.  * <p>To customise the payload object being returned from the {@link #getPayload()} method, you can
  40.  * add a custom parser as explained in {@link ItemProvider}.
  41.  *
  42.  * @author Robin Collier
  43.  */
  44. public class PayloadItem<E extends ExtensionElement> extends Item
  45. {
  46.     private E payload;
  47.    
  48.     /**
  49.      * Create an <tt>Item</tt> with no id and a payload  The id will be set by the server.  
  50.      *
  51.      * @param payloadExt A {@link ExtensionElement} which represents the payload data.
  52.      */
  53.     public PayloadItem(E payloadExt)
  54.     {
  55.         super();
  56.        
  57.         if (payloadExt == null)
  58.             throw new IllegalArgumentException("payload cannot be 'null'");
  59.         payload = payloadExt;
  60.     }

  61.     /**
  62.      * Create an <tt>Item</tt> with an id and payload.  
  63.      *
  64.      * @param itemId The id of this item.  It can be null if we want the server to set the id.
  65.      * @param payloadExt A {@link ExtensionElement} which represents the payload data.
  66.      */
  67.     public PayloadItem(String itemId, E payloadExt)
  68.     {
  69.         super(itemId);
  70.        
  71.         if (payloadExt == null)
  72.             throw new IllegalArgumentException("payload cannot be 'null'");
  73.         payload = payloadExt;
  74.     }
  75.    
  76.     /**
  77.      * Create an <tt>Item</tt> with an id, node id and payload.  
  78.      *
  79.      * <p>
  80.      * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
  81.      * one as part of {@link Message}.  If used to create an Item to publish
  82.      * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
  83.      * error for an invalid packet.
  84.      *
  85.      * @param itemId The id of this item.
  86.      * @param nodeId The id of the node the item was published to.
  87.      * @param payloadExt A {@link ExtensionElement} which represents the payload data.
  88.      */
  89.     public PayloadItem(String itemId, String nodeId, E payloadExt)
  90.     {
  91.         super(itemId, nodeId);
  92.        
  93.         if (payloadExt == null)
  94.             throw new IllegalArgumentException("payload cannot be 'null'");
  95.         payload = payloadExt;
  96.     }
  97.    
  98.     /**
  99.      * Get the payload associated with this <tt>Item</tt>.  Customising the payload
  100.      * parsing from the server can be accomplished as described in {@link ItemProvider}.
  101.      *
  102.      * @return The payload as a {@link ExtensionElement}.
  103.      */
  104.     public E getPayload()
  105.     {
  106.         return payload;
  107.     }
  108.    
  109.     @Override
  110.     public String toXML()
  111.     {
  112.         StringBuilder builder = new StringBuilder("<item");
  113.        
  114.         if (getId() != null)
  115.         {
  116.             builder.append(" id='");
  117.             builder.append(getId());
  118.             builder.append("'");
  119.         }
  120.        
  121.         if (getNode() != null) {
  122.             builder.append(" node='");
  123.             builder.append(getNode());
  124.             builder.append("'");
  125.         }
  126.         builder.append(">");
  127.         builder.append(payload.toXML());
  128.         builder.append("</item>");
  129.        
  130.         return builder.toString();
  131.     }

  132.     @Override
  133.     public String toString()
  134.     {
  135.         return getClass().getName() + " | Content [" + toXML() + "]";
  136.     }
  137. }