2003-02-10 06:01:01 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Copyright 2003-2004 Jive Software.
|
2003-02-10 06:01:01 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* All rights reserved. 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
|
2003-02-10 06:01:01 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-02-10 06:01:01 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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.
|
2003-02-10 06:01:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack.packet;
|
|
|
|
|
2006-04-20 20:52:28 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
import java.util.*;
|
2006-07-17 10:39:08 +02:00
|
|
|
import java.util.concurrent.CopyOnWriteArraySet;
|
2003-02-10 06:01:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents XMPP roster packets.
|
|
|
|
*
|
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
|
|
|
public class RosterPacket extends IQ {
|
|
|
|
|
2006-07-17 10:39:08 +02:00
|
|
|
private final List<Item> rosterItems = new ArrayList<Item>();
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Adds a roster item to the packet.
|
|
|
|
*
|
|
|
|
* @param item a roster item.
|
|
|
|
*/
|
|
|
|
public void addRosterItem(Item item) {
|
2003-02-10 06:01:01 +01:00
|
|
|
synchronized (rosterItems) {
|
2003-03-10 00:42:51 +01:00
|
|
|
rosterItems.add(item);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-16 05:33:37 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of roster items in this roster packet.
|
|
|
|
*
|
|
|
|
* @return the number of roster items.
|
|
|
|
*/
|
|
|
|
public int getRosterItemCount() {
|
|
|
|
synchronized (rosterItems) {
|
|
|
|
return rosterItems.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
2006-07-17 10:39:08 +02:00
|
|
|
* Returns an unmodifiable collection for the roster items in the packet.
|
2003-03-10 00:42:51 +01:00
|
|
|
*
|
2006-07-17 10:39:08 +02:00
|
|
|
* @return an unmodifiable collection for the roster items in the packet.
|
2003-03-10 00:42:51 +01:00
|
|
|
*/
|
2006-07-17 10:39:08 +02:00
|
|
|
public Collection<Item> getRosterItems() {
|
2003-02-10 06:01:01 +01:00
|
|
|
synchronized (rosterItems) {
|
2006-07-17 10:39:08 +02:00
|
|
|
return Collections.unmodifiableList(new ArrayList<Item>(rosterItems));
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-14 23:51:03 +02:00
|
|
|
public String getChildElementXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2003-02-10 06:01:01 +01:00
|
|
|
buf.append("<query xmlns=\"jabber:iq:roster\">");
|
|
|
|
synchronized (rosterItems) {
|
2006-07-17 10:39:08 +02:00
|
|
|
for (Item entry : rosterItems) {
|
2003-02-10 06:01:01 +01:00
|
|
|
buf.append(entry.toXML());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.append("</query>");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* A roster item, which consists of a JID, their name, the type of subscription, and
|
|
|
|
* the groups the roster item belongs to.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static class Item {
|
|
|
|
|
|
|
|
private String user;
|
|
|
|
private String name;
|
|
|
|
private ItemType itemType;
|
2003-06-17 19:19:44 +02:00
|
|
|
private ItemStatus itemStatus;
|
2006-07-17 10:39:08 +02:00
|
|
|
private final Set<String> groupNames;
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Creates a new roster item.
|
|
|
|
*
|
|
|
|
* @param user the user.
|
|
|
|
* @param name the user's name.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public Item(String user, String name) {
|
2006-01-24 19:14:41 +01:00
|
|
|
this.user = user.toLowerCase();
|
2003-02-10 06:01:01 +01:00
|
|
|
this.name = name;
|
|
|
|
itemType = null;
|
2003-06-17 19:19:44 +02:00
|
|
|
itemStatus = null;
|
2006-07-17 10:39:08 +02:00
|
|
|
groupNames = new CopyOnWriteArraySet<String>();
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Returns the user.
|
|
|
|
*
|
|
|
|
* @return the user.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public String getUser() {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Returns the user's name.
|
|
|
|
*
|
|
|
|
* @return the user's name.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Sets the user's name.
|
|
|
|
*
|
|
|
|
* @param name the user's name.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Returns the roster item type.
|
|
|
|
*
|
|
|
|
* @return the roster item type.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public ItemType getItemType() {
|
|
|
|
return itemType;
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Sets the roster item type.
|
|
|
|
*
|
|
|
|
* @param itemType the roster item type.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public void setItemType(ItemType itemType) {
|
|
|
|
this.itemType = itemType;
|
|
|
|
}
|
|
|
|
|
2003-06-17 22:22:43 +02:00
|
|
|
/**
|
|
|
|
* Returns the roster item status.
|
|
|
|
*
|
|
|
|
* @return the roster item status.
|
|
|
|
*/
|
|
|
|
public ItemStatus getItemStatus() {
|
|
|
|
return itemStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the roster item status.
|
|
|
|
*
|
|
|
|
* @param itemStatus the roster item status.
|
|
|
|
*/
|
|
|
|
public void setItemStatus(ItemStatus itemStatus) {
|
|
|
|
this.itemStatus = itemStatus;
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
2006-07-17 10:39:08 +02:00
|
|
|
* Returns an unmodifiable set of the group names that the roster item
|
2003-03-10 00:42:51 +01:00
|
|
|
* belongs to.
|
|
|
|
*
|
2006-07-17 10:39:08 +02:00
|
|
|
* @return an unmodifiable set of the group names.
|
2003-03-10 00:42:51 +01:00
|
|
|
*/
|
2006-07-17 10:39:08 +02:00
|
|
|
public Set<String> getGroupNames() {
|
|
|
|
return Collections.unmodifiableSet(groupNames);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Adds a group name.
|
|
|
|
*
|
|
|
|
* @param groupName the group name.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public void addGroupName(String groupName) {
|
2006-07-17 10:39:08 +02:00
|
|
|
groupNames.add(groupName);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Removes a group name.
|
|
|
|
*
|
|
|
|
* @param groupName the group name.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public void removeGroupName(String groupName) {
|
2006-07-17 10:39:08 +02:00
|
|
|
groupNames.remove(groupName);
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2006-09-16 00:42:06 +02:00
|
|
|
buf.append("<item jid=\"").append(user).append("\"");
|
2003-02-10 06:01:01 +01:00
|
|
|
if (name != null) {
|
2006-08-10 23:35:08 +02:00
|
|
|
buf.append(" name=\"").append(StringUtils.escapeForXML(name)).append("\"");
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
if (itemType != null) {
|
|
|
|
buf.append(" subscription=\"").append(itemType).append("\"");
|
|
|
|
}
|
2003-06-17 19:19:44 +02:00
|
|
|
if (itemStatus != null) {
|
|
|
|
buf.append(" ask=\"").append(itemStatus).append("\"");
|
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
buf.append(">");
|
2006-07-17 10:39:08 +02:00
|
|
|
for (String groupName : groupNames) {
|
|
|
|
buf.append("<group>").append(StringUtils.escapeForXML(groupName)).append("</group>");
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
buf.append("</item>");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
2003-06-17 19:19:44 +02:00
|
|
|
* The subscription status of a roster item. An optional element that indicates
|
|
|
|
* the subscription status if a change request is pending.
|
2003-03-10 00:42:51 +01:00
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static class ItemStatus {
|
|
|
|
|
2003-06-17 19:19:44 +02:00
|
|
|
/**
|
|
|
|
* Request to subcribe.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static final ItemStatus SUBSCRIPTION_PENDING = new ItemStatus("subscribe");
|
2003-06-17 19:19:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request to unsubscribe.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static final ItemStatus UNSUBCRIPTION_PENDING = new ItemStatus("unsubscribe");
|
|
|
|
|
|
|
|
public static ItemStatus fromString(String value) {
|
2003-06-19 16:40:48 +02:00
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
value = value.toLowerCase();
|
2003-06-17 19:19:44 +02:00
|
|
|
if ("unsubscribe".equals(value)) {
|
|
|
|
return SUBSCRIPTION_PENDING;
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
else if ("subscribe".equals(value)) {
|
|
|
|
return SUBSCRIPTION_PENDING;
|
|
|
|
}
|
|
|
|
else {
|
2003-06-17 19:19:44 +02:00
|
|
|
return null;
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String value;
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Returns the item status associated with the specified string.
|
|
|
|
*
|
|
|
|
* @param value the item status.
|
|
|
|
*/
|
|
|
|
private ItemStatus(String value) {
|
2003-02-10 06:01:01 +01:00
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* The subscription type of a roster item.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static class ItemType {
|
|
|
|
|
2003-06-17 19:19:44 +02:00
|
|
|
/**
|
|
|
|
* The user and subscriber have no interest in each other's presence.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static final ItemType NONE = new ItemType("none");
|
2003-06-17 19:19:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The user is interested in receiving presence updates from the subscriber.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static final ItemType TO = new ItemType("to");
|
2003-06-17 19:19:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The subscriber is interested in receiving presence updates from the user.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static final ItemType FROM = new ItemType("from");
|
2003-06-17 19:19:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The user and subscriber have a mutual interest in each other's presence.
|
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public static final ItemType BOTH = new ItemType("both");
|
|
|
|
|
2003-06-19 16:46:04 +02:00
|
|
|
/**
|
|
|
|
* The user wishes to stop receiving presence updates from the subscriber.
|
|
|
|
*/
|
|
|
|
public static final ItemType REMOVE = new ItemType("remove");
|
|
|
|
|
2003-02-10 06:01:01 +01:00
|
|
|
public static ItemType fromString(String value) {
|
2003-06-19 16:40:48 +02:00
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
value = value.toLowerCase();
|
2003-02-10 06:01:01 +01:00
|
|
|
if ("none".equals(value)) {
|
|
|
|
return NONE;
|
|
|
|
}
|
|
|
|
else if ("to".equals(value)) {
|
|
|
|
return TO;
|
|
|
|
}
|
|
|
|
else if ("from".equals(value)) {
|
|
|
|
return FROM;
|
|
|
|
}
|
|
|
|
else if ("both".equals(value)) {
|
|
|
|
return BOTH;
|
|
|
|
}
|
2003-06-19 16:46:04 +02:00
|
|
|
else if ("remove".equals(value)) {
|
|
|
|
return REMOVE;
|
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String value;
|
|
|
|
|
2003-03-10 00:42:51 +01:00
|
|
|
/**
|
|
|
|
* Returns the item type associated with the specified string.
|
|
|
|
*
|
|
|
|
* @param value the item type.
|
|
|
|
*/
|
|
|
|
public ItemType(String value) {
|
2003-02-10 06:01:01 +01:00
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
2003-06-17 19:19:44 +02:00
|
|
|
}
|