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.ExtensionElement;
  19. import org.jivesoftware.smack.packet.Message;
  20. import org.jivesoftware.smack.packet.XmlElement;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;

  22. import org.jivesoftware.smackx.pubsub.form.ConfigureForm;
  23. import org.jivesoftware.smackx.pubsub.provider.ItemProvider;

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

  58.     /**
  59.      * Create an <code>Item</code> with no id and a payload  The id will be set by the server.
  60.      *
  61.      * @param payloadExt A {@link ExtensionElement} which represents the payload data.
  62.      */
  63.     public PayloadItem(E payloadExt) {
  64.         super();

  65.         if (payloadExt == null)
  66.             throw new IllegalArgumentException("payload cannot be 'null'");
  67.         payload = payloadExt;
  68.     }

  69.     /**
  70.      * Create an <code>Item</code> with an id and payload.
  71.      *
  72.      * @param itemId The id of this item.  It can be null if we want the server to set the id.
  73.      * @param payloadExt A {@link ExtensionElement} which represents the payload data.
  74.      */
  75.     public PayloadItem(String itemId, E payloadExt) {
  76.         super(itemId);

  77.         if (payloadExt == null)
  78.             throw new IllegalArgumentException("payload cannot be 'null'");
  79.         payload = payloadExt;
  80.     }

  81.     /**
  82.      * Create an <code>Item</code> with an id, node id and payload.
  83.      *
  84.      * <p>
  85.      * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
  86.      * one as part of {@link Message}.  If used to create an Item to publish
  87.      * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
  88.      * error for an invalid packet.
  89.      * </p>
  90.      *
  91.      * @param itemId The id of this item.
  92.      * @param nodeId The id of the node the item was published to.
  93.      * @param payloadExt A {@link ExtensionElement} which represents the payload data.
  94.      */
  95.     public PayloadItem(String itemId, String nodeId, E payloadExt) {
  96.         this(ItemNamespace.pubsub, itemId, nodeId, payloadExt);
  97.     }

  98.     /**
  99.      * Create an <code>Item</code> with an id, node id and payload.
  100.      *
  101.      * <p>
  102.      * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
  103.      * one as part of {@link Message}.  If used to create an Item to publish
  104.      * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
  105.      * error for an invalid packet.
  106.      * </p>
  107.      *
  108.      * @param itemNamespace the namespace of the item.
  109.      * @param itemId The id of this item.
  110.      * @param nodeId The id of the node the item was published to.
  111.      * @param payloadExt A {@link ExtensionElement} which represents the payload data.
  112.      */
  113.     public PayloadItem(ItemNamespace itemNamespace, String itemId, String nodeId, E payloadExt) {
  114.         super(itemNamespace, itemId, nodeId);

  115.         if (payloadExt == null)
  116.             throw new IllegalArgumentException("payload cannot be 'null'");
  117.         payload = payloadExt;
  118.     }

  119.     /**
  120.      * Get the payload associated with this <code>Item</code>.  Customising the payload
  121.      * parsing from the server can be accomplished as described in {@link ItemProvider}.
  122.      *
  123.      * @return The payload as a {@link ExtensionElement}.
  124.      */
  125.     public E getPayload() {
  126.         return payload;
  127.     }

  128.     @Override
  129.     protected void addXml(XmlStringBuilder xml) {
  130.         xml.optAttribute("id", getId());
  131.         xml.rightAngleBracket();
  132.         xml.append(payload);
  133.         xml.closeElement(this);
  134.     }

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