1/** 2 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 3 * you may not use this file except in compliance with the License. 4 * You may obtain a copy of the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14package org.jivesoftware.smackx.pubsub; 15 16import org.jivesoftware.smack.packet.Message; 17import org.jivesoftware.smack.packet.PacketExtension; 18import org.jivesoftware.smackx.pubsub.provider.ItemProvider; 19 20/** 21 * This class represents an item that has been, or will be published to a 22 * pubsub node. An <tt>Item</tt> has several properties that are dependent 23 * on the configuration of the node to which it has been or will be published. 24 * 25 * <h1>An Item received from a node (via {@link LeafNode#getItems()} or {@link LeafNode#addItemEventListener(org.jivesoftware.smackx.pubsub.listener.ItemEventListener)}</b> 26 * <li>Will always have an id (either user or server generated) unless node configuration has both 27 * {@link ConfigureForm#isPersistItems()} and {@link ConfigureForm#isDeliverPayloads()}set to false. 28 * <li>Will have a payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set 29 * to true, otherwise it will be null. 30 * 31 * <h1>An Item created to send to a node (via {@link LeafNode#send()} or {@link LeafNode#publish()}</b> 32 * <li>The id is optional, since the server will generate one if necessary, but should be used if it is 33 * meaningful in the context of the node. This value must be unique within the node that it is sent to, since 34 * resending an item with the same id will overwrite the one that already exists if the items are persisted. 35 * <li>Will require payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set 36 * to true. 37 * 38 * <p>To customise the payload object being returned from the {@link #getPayload()} method, you can 39 * add a custom parser as explained in {@link ItemProvider}. 40 * 41 * @author Robin Collier 42 */ 43public class PayloadItem<E extends PacketExtension> extends Item 44{ 45 private E payload; 46 47 /** 48 * Create an <tt>Item</tt> with no id and a payload The id will be set by the server. 49 * 50 * @param payloadExt A {@link PacketExtension} which represents the payload data. 51 */ 52 public PayloadItem(E payloadExt) 53 { 54 super(); 55 56 if (payloadExt == null) 57 throw new IllegalArgumentException("payload cannot be 'null'"); 58 payload = payloadExt; 59 } 60 61 /** 62 * Create an <tt>Item</tt> with an id and payload. 63 * 64 * @param itemId The id of this item. It can be null if we want the server to set the id. 65 * @param payloadExt A {@link PacketExtension} which represents the payload data. 66 */ 67 public PayloadItem(String itemId, E payloadExt) 68 { 69 super(itemId); 70 71 if (payloadExt == null) 72 throw new IllegalArgumentException("payload cannot be 'null'"); 73 payload = payloadExt; 74 } 75 76 /** 77 * Create an <tt>Item</tt> with an id, node id and payload. 78 * 79 * <p> 80 * <b>Note:</b> This is not valid for publishing an item to a node, only receiving from 81 * one as part of {@link Message}. If used to create an Item to publish 82 * (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an 83 * error for an invalid packet. 84 * 85 * @param itemId The id of this item. 86 * @param nodeId The id of the node the item was published to. 87 * @param payloadExt A {@link PacketExtension} which represents the payload data. 88 */ 89 public PayloadItem(String itemId, String nodeId, E payloadExt) 90 { 91 super(itemId, nodeId); 92 93 if (payloadExt == null) 94 throw new IllegalArgumentException("payload cannot be 'null'"); 95 payload = payloadExt; 96 } 97 98 /** 99 * Get the payload associated with this <tt>Item</tt>. Customising the payload 100 * parsing from the server can be accomplished as described in {@link ItemProvider}. 101 * 102 * @return The payload as a {@link PacketExtension}. 103 */ 104 public E getPayload() 105 { 106 return payload; 107 } 108 109 @Override 110 public String toXML() 111 { 112 StringBuilder builder = new StringBuilder("<item"); 113 114 if (getId() != null) 115 { 116 builder.append(" id='"); 117 builder.append(getId()); 118 builder.append("'"); 119 } 120 121 if (getNode() != null) { 122 builder.append(" node='"); 123 builder.append(getNode()); 124 builder.append("'"); 125 } 126 builder.append(">"); 127 builder.append(payload.toXML()); 128 builder.append("</item>"); 129 130 return builder.toString(); 131 } 132 133 @Override 134 public String toString() 135 { 136 return getClass().getName() + " | Content [" + toXML() + "]"; 137 } 138} 139