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