2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.pubsub;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2018-04-19 11:05:43 +02:00
|
|
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2020-05-13 20:14:41 +02:00
|
|
|
import org.jivesoftware.smackx.pubsub.form.ConfigureForm;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.pubsub.provider.ItemProvider;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents an item that has been, or will be published to a
|
2019-07-19 18:10:36 +02:00
|
|
|
* PubSub node. An <code>Item</code> has several properties that are dependent
|
2014-02-17 23:58:40 +01:00
|
|
|
* on the configuration of the node to which it has been or will be published.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-04-12 18:13:19 +02:00
|
|
|
* <h3>An Item received from a node (via {@link LeafNode#getItems()} or {@link LeafNode#addItemEventListener(org.jivesoftware.smackx.pubsub.listener.ItemEventListener)}</h3>
|
|
|
|
* <ul>
|
2014-02-17 23:58:40 +01:00
|
|
|
* <li>Will always have an id (either user or server generated) unless node configuration has both
|
|
|
|
* {@link ConfigureForm#isPersistItems()} and {@link ConfigureForm#isDeliverPayloads()}set to false.
|
2018-05-09 23:06:12 +02:00
|
|
|
* <li>Will have a payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
|
2014-02-17 23:58:40 +01:00
|
|
|
* to true, otherwise it will be null.
|
2015-04-12 18:13:19 +02:00
|
|
|
* </ul>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-08-16 14:10:00 +02:00
|
|
|
* <h3>An Item created to send to a node (via {@link LeafNode#publish()} or {@link LeafNode#publish()}</h3>
|
2015-04-12 18:13:19 +02:00
|
|
|
* <ul>
|
2018-05-09 23:06:12 +02:00
|
|
|
* <li>The id is optional, since the server will generate one if necessary, but should be used if it is
|
2014-02-17 23:58:40 +01:00
|
|
|
* meaningful in the context of the node. This value must be unique within the node that it is sent to, since
|
|
|
|
* resending an item with the same id will overwrite the one that already exists if the items are persisted.
|
|
|
|
* <li>Will require payload if the node configuration has {@link ConfigureForm#isDeliverPayloads()} set
|
2015-04-12 18:13:19 +02:00
|
|
|
* to true.
|
|
|
|
* </ul>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-04-12 18:13:19 +02:00
|
|
|
* <p>
|
|
|
|
* To customise the payload object being returned from the {@link PayloadItem#getPayload()} method, you can
|
2014-02-17 23:58:40 +01:00
|
|
|
* add a custom parser as explained in {@link ItemProvider}.
|
2015-04-12 18:13:19 +02:00
|
|
|
* </p>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Robin Collier
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public class Item extends NodeExtension {
|
2018-04-19 11:05:43 +02:00
|
|
|
public enum ItemNamespace {
|
|
|
|
pubsub(PubSubElementType.ITEM),
|
|
|
|
event(PubSubElementType.ITEM_EVENT),
|
|
|
|
;
|
|
|
|
private final PubSubElementType type;
|
|
|
|
|
|
|
|
ItemNamespace(PubSubElementType type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemNamespace fromXmlns(String xmlns) {
|
|
|
|
for (ItemNamespace itemNamespace : ItemNamespace.values()) {
|
|
|
|
if (itemNamespace.type.getNamespace().getXmlns().equals(xmlns)) {
|
|
|
|
return itemNamespace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Invalid item namespace: " + xmlns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final String itemId;
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
/**
|
2019-07-19 18:10:36 +02:00
|
|
|
* Create an empty <code>Item</code> with no id. This is a valid item for nodes which are configured
|
2017-02-07 22:02:40 +01:00
|
|
|
* so that {@link ConfigureForm#isDeliverPayloads()} is false. In most cases an id will be generated by the server.
|
|
|
|
* For nodes configured with {@link ConfigureForm#isDeliverPayloads()} and {@link ConfigureForm#isPersistItems()}
|
2019-07-19 18:10:36 +02:00
|
|
|
* set to false, no <code>Item</code> is sent to the node, you have to use the {@link LeafNode#publish()}
|
2017-08-16 14:10:00 +02:00
|
|
|
* method in this case.
|
2017-02-07 22:02:40 +01:00
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public Item() {
|
2018-04-19 11:05:43 +02:00
|
|
|
this(ItemNamespace.pubsub, null, null);
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
/**
|
2019-07-19 18:10:36 +02:00
|
|
|
* Create an <code>Item</code> with an id but no payload. This is a valid item for nodes which are configured
|
2017-02-07 22:02:40 +01:00
|
|
|
* so that {@link ConfigureForm#isDeliverPayloads()} is false.
|
|
|
|
*
|
|
|
|
* @param itemId The id if the item. It must be unique within the node unless overwriting and existing item.
|
|
|
|
* Passing null is the equivalent of calling {@link #Item()}.
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public Item(String itemId) {
|
2018-04-19 11:05:43 +02:00
|
|
|
this(ItemNamespace.pubsub, itemId, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-19 18:10:36 +02:00
|
|
|
* Create an <code>Item</code> with an id but no payload. This is a valid item for nodes which are configured
|
2018-04-19 11:05:43 +02:00
|
|
|
* so that {@link ConfigureForm#isDeliverPayloads()} is false.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param itemNamespace the namespace of the item.
|
2018-04-19 11:05:43 +02:00
|
|
|
* @param itemId The id if the item. It must be unique within the node unless overwriting and existing item.
|
|
|
|
* Passing null is the equivalent of calling {@link #Item()}.
|
|
|
|
*/
|
|
|
|
public Item(ItemNamespace itemNamespace, String itemId) {
|
|
|
|
this(itemNamespace, itemId, null);
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
/**
|
2019-07-19 18:10:36 +02:00
|
|
|
* Create an <code>Item</code> with an id and a node id.
|
2017-02-07 22:02:40 +01:00
|
|
|
* <p>
|
|
|
|
* <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
|
|
|
|
* one as part of {@link Message}. If used to create an Item to publish
|
|
|
|
* (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
|
|
|
|
* error for an invalid packet.
|
|
|
|
*
|
|
|
|
* @param itemId The id of the item.
|
|
|
|
* @param nodeId The id of the node which the item was published to.
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public Item(String itemId, String nodeId) {
|
2018-04-19 11:05:43 +02:00
|
|
|
this(ItemNamespace.pubsub, itemId, nodeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-19 18:10:36 +02:00
|
|
|
* Create an <code>Item</code> with an id and a node id.
|
2018-04-19 11:05:43 +02:00
|
|
|
* <p>
|
|
|
|
* <b>Note:</b> This is not valid for publishing an item to a node, only receiving from
|
|
|
|
* one as part of {@link Message}. If used to create an Item to publish
|
|
|
|
* (via {@link LeafNode#publish(Item)}, the server <i>may</i> return an
|
|
|
|
* error for an invalid packet.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param itemNamespace the namespace of the item.
|
2018-04-19 11:05:43 +02:00
|
|
|
* @param itemId The id of the item.
|
|
|
|
* @param nodeId The id of the node which the item was published to.
|
|
|
|
*/
|
|
|
|
public Item(ItemNamespace itemNamespace, String itemId, String nodeId) {
|
|
|
|
super(itemNamespace.type, nodeId);
|
|
|
|
this.itemId = itemId;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
/**
|
|
|
|
* Get the item id. Unique to the node it is associated with.
|
|
|
|
*
|
|
|
|
* @return The id
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public String getId() {
|
2018-04-19 11:05:43 +02:00
|
|
|
return itemId;
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
@Override
|
2020-06-14 16:52:13 +02:00
|
|
|
protected void addXml(XmlStringBuilder xml) {
|
2018-04-19 11:05:43 +02:00
|
|
|
xml.optAttribute("id", getId());
|
2020-06-14 16:52:13 +02:00
|
|
|
xml.closeEmptyElement();
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
@Override
|
2018-04-06 10:21:46 +02:00
|
|
|
public String toString() {
|
2019-02-04 13:27:41 +01:00
|
|
|
return getClass().getName() + " | Content [" + toXML() + "]";
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2018-04-19 11:05:43 +02:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|