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