2003-10-14 00:57:25 +02:00
|
|
|
/**
|
2004-11-03 00:37:00 +01:00
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2004-11-03 00:37:00 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:37:00 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-02-15 11:35:08 +01:00
|
|
|
package org.jivesoftware.smackx.disco.packet;
|
2003-10-14 00:57:25 +02:00
|
|
|
|
2013-03-18 09:50:48 +01:00
|
|
|
import java.util.Collection;
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.Collections;
|
2014-03-24 22:31:42 +01:00
|
|
|
import java.util.LinkedList;
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
|
|
|
|
|
|
|
import org.jxmpp.jid.Jid;
|
|
|
|
|
2003-10-14 00:57:25 +02:00
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* A DiscoverItems IQ packet, which is used by XMPP clients to request and receive items
|
2003-10-14 00:57:25 +02:00
|
|
|
* associated with XMPP entities.<p>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
|
|
|
* The items could also be queried in order to discover if they contain items inside. Some items
|
2003-10-14 00:57:25 +02:00
|
|
|
* may be addressable by its JID and others may require to be addressed by a JID and a node name.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public class DiscoverItems extends IQ {
|
|
|
|
|
2014-11-07 21:12:01 +01:00
|
|
|
public static final String ELEMENT = QUERY_ELEMENT;
|
2013-02-11 18:22:31 +01:00
|
|
|
public static final String NAMESPACE = "http://jabber.org/protocol/disco#items";
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private final List<Item> items = new LinkedList<>();
|
2003-10-14 00:57:25 +02:00
|
|
|
private String node;
|
|
|
|
|
2014-11-07 21:12:01 +01:00
|
|
|
public DiscoverItems() {
|
|
|
|
super(ELEMENT, NAMESPACE);
|
|
|
|
}
|
|
|
|
|
2003-10-14 00:57:25 +02:00
|
|
|
/**
|
|
|
|
* Adds a new item to the discovered information.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2003-10-14 00:57:25 +02:00
|
|
|
* @param item the discovered entity's item
|
|
|
|
*/
|
|
|
|
public void addItem(Item item) {
|
2014-03-24 22:31:42 +01:00
|
|
|
items.add(item);
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
|
|
|
|
2013-03-18 09:50:48 +01:00
|
|
|
/**
|
|
|
|
* Adds a collection of items to the discovered information. Does nothing if itemsToAdd is null
|
|
|
|
*
|
|
|
|
* @param itemsToAdd
|
|
|
|
*/
|
|
|
|
public void addItems(Collection<Item> itemsToAdd) {
|
|
|
|
if (itemsToAdd == null) return;
|
|
|
|
for (Item i : itemsToAdd) {
|
|
|
|
addItem(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 22:31:42 +01:00
|
|
|
|
2003-10-14 00:57:25 +02:00
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Returns the discovered items of the queried XMPP entity.
|
2003-10-14 00:57:25 +02:00
|
|
|
*
|
2014-03-24 22:31:42 +01:00
|
|
|
* @return an unmodifiable list of the discovered entity's items
|
2003-10-14 00:57:25 +02:00
|
|
|
*/
|
2014-03-24 22:31:42 +01:00
|
|
|
public List<DiscoverItems.Item> getItems() {
|
|
|
|
return Collections.unmodifiableList(items);
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Returns the node attribute that supplements the 'jid' attribute. A node is merely
|
|
|
|
* something that is associated with a JID and for which the JID can provide information.<p>
|
|
|
|
*
|
|
|
|
* Node attributes SHOULD be used only when trying to provide or query information which
|
2003-10-14 00:57:25 +02:00
|
|
|
* is not directly addressable.
|
|
|
|
*
|
|
|
|
* @return the node attribute that supplements the 'jid' attribute
|
|
|
|
*/
|
|
|
|
public String getNode() {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Sets the node attribute that supplements the 'jid' attribute. A node is merely
|
|
|
|
* something that is associated with a JID and for which the JID can provide information.<p>
|
|
|
|
*
|
|
|
|
* Node attributes SHOULD be used only when trying to provide or query information which
|
2003-10-14 00:57:25 +02:00
|
|
|
* is not directly addressable.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2003-10-14 00:57:25 +02:00
|
|
|
* @param node the node attribute that supplements the 'jid' attribute
|
|
|
|
*/
|
|
|
|
public void setNode(String node) {
|
|
|
|
this.node = node;
|
|
|
|
}
|
|
|
|
|
2014-11-07 21:12:01 +01:00
|
|
|
@Override
|
|
|
|
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
|
2014-03-21 09:51:52 +01:00
|
|
|
xml.optAttribute("node", getNode());
|
2014-08-20 21:25:14 +02:00
|
|
|
xml.rightAngleBracket();
|
2014-03-21 09:51:52 +01:00
|
|
|
|
2014-03-24 22:31:42 +01:00
|
|
|
for (Item item : items) {
|
|
|
|
xml.append(item.toXML());
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
2014-03-24 22:31:42 +01:00
|
|
|
|
2014-03-21 09:51:52 +01:00
|
|
|
return xml;
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* An item is associated with an XMPP Entity, usually thought of a children of the parent
|
|
|
|
* entity and normally are addressable as a JID.<p>
|
|
|
|
*
|
|
|
|
* An item associated with an entity may not be addressable as a JID. In order to handle
|
|
|
|
* such items, Service Discovery uses an optional 'node' attribute that supplements the
|
2003-10-14 00:57:25 +02:00
|
|
|
* 'jid' attribute.
|
|
|
|
*/
|
|
|
|
public static class Item {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request to create or update the item.
|
|
|
|
*/
|
|
|
|
public static final String UPDATE_ACTION = "update";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request to remove the item.
|
|
|
|
*/
|
|
|
|
public static final String REMOVE_ACTION = "remove";
|
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
private final Jid entityID;
|
2003-10-14 00:57:25 +02:00
|
|
|
private String name;
|
|
|
|
private String node;
|
|
|
|
private String action;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Item associated with a given entity.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2003-10-14 00:57:25 +02:00
|
|
|
* @param entityID the id of the entity that contains the item
|
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public Item(Jid entityID) {
|
2003-10-14 00:57:25 +02:00
|
|
|
this.entityID = entityID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the entity's ID.
|
|
|
|
*
|
|
|
|
* @return the entity's ID.
|
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public Jid getEntityID() {
|
2003-10-14 00:57:25 +02:00
|
|
|
return entityID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the entity's name.
|
|
|
|
*
|
|
|
|
* @return the entity's name.
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the entity's name.
|
|
|
|
*
|
|
|
|
* @param name the entity's name.
|
|
|
|
*/
|
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Returns the node attribute that supplements the 'jid' attribute. A node is merely
|
|
|
|
* something that is associated with a JID and for which the JID can provide information.<p>
|
|
|
|
*
|
|
|
|
* Node attributes SHOULD be used only when trying to provide or query information which
|
2003-10-14 00:57:25 +02:00
|
|
|
* is not directly addressable.
|
|
|
|
*
|
|
|
|
* @return the node attribute that supplements the 'jid' attribute
|
|
|
|
*/
|
|
|
|
public String getNode() {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Sets the node attribute that supplements the 'jid' attribute. A node is merely
|
|
|
|
* something that is associated with a JID and for which the JID can provide information.<p>
|
|
|
|
*
|
|
|
|
* Node attributes SHOULD be used only when trying to provide or query information which
|
2003-10-14 00:57:25 +02:00
|
|
|
* is not directly addressable.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2003-10-14 00:57:25 +02:00
|
|
|
* @param node the node attribute that supplements the 'jid' attribute
|
|
|
|
*/
|
|
|
|
public void setNode(String node) {
|
|
|
|
this.node = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Returns the action that specifies the action being taken for this item. Possible action
|
|
|
|
* values are: "update" and "remove". Update should either create a new entry if the node
|
|
|
|
* and jid combination does not already exist, or simply update an existing entry. If
|
2003-10-14 00:57:25 +02:00
|
|
|
* "remove" is used as the action, the item should be removed from persistent storage.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2003-10-14 00:57:25 +02:00
|
|
|
* @return the action being taken for this item
|
|
|
|
*/
|
|
|
|
public String getAction() {
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Sets the action that specifies the action being taken for this item. Possible action
|
|
|
|
* values are: "update" and "remove". Update should either create a new entry if the node
|
|
|
|
* and jid combination does not already exist, or simply update an existing entry. If
|
2003-10-14 00:57:25 +02:00
|
|
|
* "remove" is used as the action, the item should be removed from persistent storage.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2003-10-14 00:57:25 +02:00
|
|
|
* @param action the action being taken for this item
|
|
|
|
*/
|
|
|
|
public void setAction(String action) {
|
|
|
|
this.action = action;
|
|
|
|
}
|
|
|
|
|
2014-03-21 09:51:52 +01:00
|
|
|
public XmlStringBuilder toXML() {
|
|
|
|
XmlStringBuilder xml = new XmlStringBuilder();
|
|
|
|
xml.halfOpenElement("item");
|
|
|
|
xml.attribute("jid", entityID);
|
|
|
|
xml.optAttribute("name", name);
|
|
|
|
xml.optAttribute("node", node);
|
|
|
|
xml.optAttribute("action", action);
|
|
|
|
xml.closeEmptyElement();
|
|
|
|
return xml;
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
2019-05-23 23:06:39 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return toXML().toString();
|
|
|
|
}
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
2013-01-31 23:30:36 +01:00
|
|
|
}
|