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.util.XmlStringBuilder;
022
023import org.jivesoftware.smackx.pubsub.provider.ItemProvider;
024
025/**
026 * This class represents an item that has been, or will be published to a
027 * pubsub node.  An <tt>Item</tt> has several properties that are dependent
028 * on the configuration of the node to which it has been or will be published.
029 *
030 * <h3>An Item received from a node (via {@link LeafNode#getItems()} or {@link LeafNode#addItemEventListener(org.jivesoftware.smackx.pubsub.listener.ItemEventListener)}</h3>
031 * <ul>
032 * <li>Will always have an id (either user or server generated) unless node configuration has both
033 * {@link ConfigureForm#isPersistItems()} and {@link ConfigureForm#isDeliverPayloads()}set to false.</li>
034 * <li>Will have a payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
035 * to true, otherwise it will be null.</li>
036 * </ul>
037 *
038 * <h3>An Item created to send to a node (via {@link LeafNode#publish()}</h3>
039 * <ul>
040 * <li>The id is optional, since the server will generate one if necessary, but should be used if it is
041 * meaningful in the context of the node.  This value must be unique within the node that it is sent to, since
042 * resending an item with the same id will overwrite the one that already exists if the items are persisted.</li>
043 * <li>Will require payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
044 * to true.</li>
045 * </ul>
046 *
047 * <p>To customise the payload object being returned from the {@link #getPayload()} method, you can
048 * add a custom parser as explained in {@link ItemProvider}.</p>
049 *
050 * @author Robin Collier
051 */
052public class PayloadItem<E extends ExtensionElement> extends Item {
053    private final E payload;
054
055    /**
056     * Create an <tt>Item</tt> with no id and a payload  The id will be set by the server.
057     *
058     * @param payloadExt A {@link ExtensionElement} which represents the payload data.
059     */
060    public PayloadItem(E payloadExt) {
061        super();
062
063        if (payloadExt == null)
064            throw new IllegalArgumentException("payload cannot be 'null'");
065        payload = payloadExt;
066    }
067
068    /**
069     * Create an <tt>Item</tt> with an id and payload.
070     *
071     * @param itemId The id of this item.  It can be null if we want the server to set the id.
072     * @param payloadExt A {@link ExtensionElement} which represents the payload data.
073     */
074    public PayloadItem(String itemId, E payloadExt) {
075        super(itemId);
076
077        if (payloadExt == null)
078            throw new IllegalArgumentException("payload cannot be 'null'");
079        payload = payloadExt;
080    }
081
082    /**
083     * Create an <tt>Item</tt> with an id, node id and payload.
084     *
085     * <p>
086     * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
087     * one as part of {@link Message}.  If used to create an Item to publish
088     * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
089     * error for an invalid packet.
090     * </p>
091     *
092     * @param itemId The id of this item.
093     * @param nodeId The id of the node the item was published to.
094     * @param payloadExt A {@link ExtensionElement} which represents the payload data.
095     */
096    public PayloadItem(String itemId, String nodeId, E payloadExt) {
097        this(ItemNamespace.pubsub, itemId, nodeId, payloadExt);
098    }
099
100    /**
101     * Create an <tt>Item</tt> with an id, node id and payload.
102     *
103     * <p>
104     * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
105     * one as part of {@link Message}.  If used to create an Item to publish
106     * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
107     * error for an invalid packet.
108     * </p>
109     *
110     * @param itemId The id of this item.
111     * @param nodeId The id of the node the item was published to.
112     * @param payloadExt A {@link ExtensionElement} which represents the payload data.
113     */
114    public PayloadItem(ItemNamespace itemNamespace, String itemId, String nodeId, E payloadExt) {
115        super(itemNamespace, itemId, nodeId);
116
117        if (payloadExt == null)
118            throw new IllegalArgumentException("payload cannot be 'null'");
119        payload = payloadExt;
120    }
121
122    /**
123     * Get the payload associated with this <tt>Item</tt>.  Customising the payload
124     * parsing from the server can be accomplished as described in {@link ItemProvider}.
125     *
126     * @return The payload as a {@link ExtensionElement}.
127     */
128    public E getPayload() {
129        return payload;
130    }
131
132    @Override
133    public XmlStringBuilder toXML(String enclosingNamespace) {
134        XmlStringBuilder xml = getCommonXml();
135
136        xml.rightAngleBracket();
137        xml.append(payload.toXML(null));
138        xml.closeElement(this);
139
140        return xml;
141    }
142
143    @Override
144    public String toString() {
145        return getClass().getName() + " | Content [" + toXML(null) + "]";
146    }
147}