001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.pubsub;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.packet.Message;
021import org.jivesoftware.smack.packet.XmlElement;
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024import org.jivesoftware.smackx.pubsub.form.ConfigureForm;
025import org.jivesoftware.smackx.pubsub.provider.ItemProvider;
026
027/**
028 * This class represents an item that has been, or will be published to a
029 * pubsub node.  An <code>Item</code> has several properties that are dependent
030 * on the configuration of the node to which it has been or will be published.
031 *
032 * <p>
033 * <b>An Item received from a node (via {@link LeafNode#getItems()} or {@link LeafNode#addItemEventListener(org.jivesoftware.smackx.pubsub.listener.ItemEventListener)}</b>
034 * </p>
035 * <ul>
036 * <li>Will always have an id (either user or server generated) unless node configuration has both
037 * {@link ConfigureForm#isPersistItems()} and {@link ConfigureForm#isDeliverPayloads()}set to false.</li>
038 * <li>Will have a payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
039 * to true, otherwise it will be null.</li>
040 * </ul>
041 *
042 * <p>
043 * <b>An Item created to send to a node (via {@link LeafNode#publish()}</b>
044 * </p>
045 * <ul>
046 * <li>The id is optional, since the server will generate one if necessary, but should be used if it is
047 * meaningful in the context of the node.  This value must be unique within the node that it is sent to, since
048 * resending an item with the same id will overwrite the one that already exists if the items are persisted.</li>
049 * <li>Will require payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
050 * to true.</li>
051 * </ul>
052 *
053 *
054 * <p>To customise the payload object being returned from the {@link #getPayload()} method, you can
055 * add a custom parser as explained in {@link ItemProvider}.</p>
056 *
057 * @author Robin Collier
058 */
059public class PayloadItem<E extends XmlElement> extends Item {
060    private final E payload;
061
062    /**
063     * Create an <code>Item</code> with no id and a payload  The id will be set by the server.
064     *
065     * @param payloadExt A {@link ExtensionElement} which represents the payload data.
066     */
067    public PayloadItem(E payloadExt) {
068        super();
069
070        if (payloadExt == null)
071            throw new IllegalArgumentException("payload cannot be 'null'");
072        payload = payloadExt;
073    }
074
075    /**
076     * Create an <code>Item</code> with an id and payload.
077     *
078     * @param itemId The id of this item.  It can be null if we want the server to set the id.
079     * @param payloadExt A {@link ExtensionElement} which represents the payload data.
080     */
081    public PayloadItem(String itemId, E payloadExt) {
082        super(itemId);
083
084        if (payloadExt == null)
085            throw new IllegalArgumentException("payload cannot be 'null'");
086        payload = payloadExt;
087    }
088
089    /**
090     * Create an <code>Item</code> with an id, node id and payload.
091     *
092     * <p>
093     * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
094     * one as part of {@link Message}.  If used to create an Item to publish
095     * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
096     * error for an invalid packet.
097     * </p>
098     *
099     * @param itemId The id of this item.
100     * @param nodeId The id of the node the item was published to.
101     * @param payloadExt A {@link ExtensionElement} which represents the payload data.
102     */
103    public PayloadItem(String itemId, String nodeId, E payloadExt) {
104        this(ItemNamespace.pubsub, itemId, nodeId, payloadExt);
105    }
106
107    /**
108     * Create an <code>Item</code> with an id, node id and payload.
109     *
110     * <p>
111     * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
112     * one as part of {@link Message}.  If used to create an Item to publish
113     * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
114     * error for an invalid packet.
115     * </p>
116     *
117     * @param itemNamespace the namespace of the item.
118     * @param itemId The id of this item.
119     * @param nodeId The id of the node the item was published to.
120     * @param payloadExt A {@link ExtensionElement} which represents the payload data.
121     */
122    public PayloadItem(ItemNamespace itemNamespace, String itemId, String nodeId, E payloadExt) {
123        super(itemNamespace, itemId, nodeId);
124
125        if (payloadExt == null)
126            throw new IllegalArgumentException("payload cannot be 'null'");
127        payload = payloadExt;
128    }
129
130    /**
131     * Get the payload associated with this <code>Item</code>.  Customising the payload
132     * parsing from the server can be accomplished as described in {@link ItemProvider}.
133     *
134     * @return The payload as a {@link ExtensionElement}.
135     */
136    public E getPayload() {
137        return payload;
138    }
139
140    @Override
141    protected void addXml(XmlStringBuilder xml) {
142        xml.optAttribute("id", getId());
143        xml.rightAngleBracket();
144        xml.append(payload);
145        xml.closeElement(this);
146    }
147
148    @Override
149    public String toString() {
150        return getClass().getName() + " | Content [" + toXML() + "]";
151    }
152}