2003-08-24 18:05:45 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
|
|
|
* ====================================================================
|
|
|
|
* The Jive Software License (based on Apache Software License, Version 1.1)
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. The end-user documentation included with the redistribution,
|
|
|
|
* if any, must include the following acknowledgment:
|
|
|
|
* "This product includes software developed by
|
|
|
|
* Jive Software (http://www.jivesoftware.com)."
|
|
|
|
* Alternately, this acknowledgment may appear in the software itself,
|
|
|
|
* if and wherever such third-party acknowledgments normally appear.
|
|
|
|
*
|
|
|
|
* 4. The names "Smack" and "Jive Software" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please
|
|
|
|
* contact webmaster@jivesoftware.com.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "Smack",
|
|
|
|
* nor may "Smack" appear in their name, without prior written
|
|
|
|
* permission of Jive Software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
|
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
2003-08-02 20:46:55 +02:00
|
|
|
*/
|
2003-08-24 18:05:45 +02:00
|
|
|
|
2003-08-02 20:46:55 +02:00
|
|
|
package org.jivesoftware.smackx.packet;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.*;
|
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
2003-08-26 18:05:57 +02:00
|
|
|
import org.jivesoftware.smackx.*;
|
2003-08-02 20:46:55 +02:00
|
|
|
|
|
|
|
/**
|
2003-08-24 18:05:45 +02:00
|
|
|
* Represents XMPP Roster Item Exchange packets.<p>
|
|
|
|
*
|
|
|
|
* The 'jabber:x:roster' namespace (which is not to be confused with the 'jabber:iq:roster'
|
|
|
|
* namespace) is used to send roster items from one client to another. A roster item is sent by
|
|
|
|
* adding to the <message/> element an <x/> child scoped by the 'jabber:x:roster' namespace. This
|
|
|
|
* <x/> element may contain one or more <item/> children (one for each roster item to be sent).<p>
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
2003-08-24 18:05:45 +02:00
|
|
|
* Each <item/> element may possess the following attributes:<p>
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
2003-08-24 18:05:45 +02:00
|
|
|
* <jid/> -- The id of the contact being sent. This attribute is required.<br>
|
|
|
|
* <name/> -- A natural-language nickname for the contact. This attribute is optional.<p>
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
2003-08-24 18:05:45 +02:00
|
|
|
* Each <item/> element may also contain one or more <group/> children specifying the
|
|
|
|
* natural-language name of a user-specified group, for the purpose of categorizing this contact
|
|
|
|
* into one or more roster groups.
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public class RosterExchange implements PacketExtension {
|
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
private List remoteRosterEntries = new ArrayList();
|
2003-08-02 20:46:55 +02:00
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
/**
|
|
|
|
* Creates a new empty roster exchange package.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public RosterExchange() {
|
2003-08-02 20:46:55 +02:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
/**
|
|
|
|
* Creates a new roster exchange package with the entries specified in roster.
|
|
|
|
*
|
|
|
|
* @param roster the roster to send to other XMPP entity.
|
|
|
|
*/
|
|
|
|
public RosterExchange(Roster roster) {
|
|
|
|
// Add all the roster entries to the new RosterExchange
|
|
|
|
for (Iterator rosterEntries = roster.getEntries(); rosterEntries.hasNext();) {
|
|
|
|
this.addRosterEntry((RosterEntry) rosterEntries.next());
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
|
|
|
}
|
2003-08-24 18:05:45 +02:00
|
|
|
|
2003-08-02 20:46:55 +02:00
|
|
|
/**
|
|
|
|
* Adds a roster entry to the packet.
|
|
|
|
*
|
2003-08-24 18:05:45 +02:00
|
|
|
* @param rosterEntry a roster entry to add.
|
2003-08-02 20:46:55 +02:00
|
|
|
*/
|
2003-08-24 18:05:45 +02:00
|
|
|
public void addRosterEntry(RosterEntry rosterEntry) {
|
2003-08-26 18:05:57 +02:00
|
|
|
// Obtain a String[] from the roster entry groups name
|
|
|
|
ArrayList groupNamesList = new ArrayList();
|
|
|
|
String[] groupNames;
|
|
|
|
for (Iterator groups = rosterEntry.getGroups(); groups.hasNext();) {
|
|
|
|
groupNamesList.add(((RosterGroup) groups.next()).getName());
|
|
|
|
}
|
|
|
|
groupNames = (String[]) groupNamesList.toArray(new String[groupNamesList.size()]);
|
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
// Create a new Entry based on the rosterEntry and add it to the packet
|
2003-08-26 18:05:57 +02:00
|
|
|
RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getUser(), rosterEntry.getName(), groupNames);
|
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
addRosterEntry(remoteRosterEntry);
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-08-24 18:05:45 +02:00
|
|
|
* Adds a remote roster entry to the packet.
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
2003-08-24 18:05:45 +02:00
|
|
|
* @param remoteRosterEntry a remote roster entry to add.
|
2003-08-02 20:46:55 +02:00
|
|
|
*/
|
2003-08-24 18:05:45 +02:00
|
|
|
public void addRosterEntry(RemoteRosterEntry remoteRosterEntry) {
|
|
|
|
synchronized (remoteRosterEntries) {
|
|
|
|
remoteRosterEntries.add(remoteRosterEntry);
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
|
|
|
}
|
2003-08-24 18:05:45 +02:00
|
|
|
|
2003-08-02 20:46:55 +02:00
|
|
|
/**
|
|
|
|
* Returns the XML element name of the extension sub-packet root element.
|
|
|
|
* Always returns "x"
|
|
|
|
*
|
|
|
|
* @return the XML element name of the packet extension.
|
|
|
|
*/
|
|
|
|
public String getElementName() {
|
|
|
|
return "x";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the XML namespace of the extension sub-packet root element.
|
|
|
|
* According the specification the namespace is always "jabber:x:roster"
|
|
|
|
* (which is not to be confused with the 'jabber:iq:roster' namespace
|
|
|
|
*
|
|
|
|
* @return the XML namespace of the packet extension.
|
|
|
|
*/
|
|
|
|
public String getNamespace() {
|
|
|
|
return "jabber:x:roster";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-08-24 18:05:45 +02:00
|
|
|
* Returns an Iterator for the roster entries in the packet.
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
2003-08-24 18:05:45 +02:00
|
|
|
* @return an Iterator for the roster entries in the packet.
|
2003-08-02 20:46:55 +02:00
|
|
|
*/
|
2003-08-24 18:05:45 +02:00
|
|
|
public Iterator getRosterEntries() {
|
|
|
|
synchronized (remoteRosterEntries) {
|
|
|
|
List entries = Collections.unmodifiableList(new ArrayList(remoteRosterEntries));
|
2003-08-02 20:46:55 +02:00
|
|
|
return entries.iterator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-08-24 18:05:45 +02:00
|
|
|
* Returns a count of the entries in the roster exchange.
|
2003-08-11 15:50:03 +02:00
|
|
|
*
|
2003-08-24 18:05:45 +02:00
|
|
|
* @return the number of entries in the roster exchange.
|
|
|
|
*/
|
|
|
|
public int getEntryCount() {
|
|
|
|
return remoteRosterEntries.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the XML representation of a Roster Item Exchange according the specification.
|
|
|
|
*
|
2003-08-02 20:46:55 +02:00
|
|
|
* Usually the XML representation will be inside of a Message XML representation like
|
|
|
|
* in the following example:
|
2003-08-24 18:05:45 +02:00
|
|
|
* <pre>
|
|
|
|
* <message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack">
|
|
|
|
* <subject>Any subject you want</subject>
|
|
|
|
* <body>This message contains roster items.</body>
|
|
|
|
* <x xmlns="jabber:x:roster">
|
|
|
|
* <item jid="gato1@gato.home"/>
|
|
|
|
* <item jid="gato2@gato.home"/>
|
|
|
|
* </x>
|
|
|
|
* </message>
|
|
|
|
* </pre>
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public String toXML() {
|
|
|
|
StringBuffer buf = new StringBuffer();
|
2003-08-24 18:05:45 +02:00
|
|
|
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
|
|
|
|
"\">");
|
|
|
|
// Loop through all roster entries and append them to the string buffer
|
|
|
|
for (Iterator i = getRosterEntries(); i.hasNext();) {
|
|
|
|
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) i.next();
|
|
|
|
buf.append(remoteRosterEntry.toXML());
|
|
|
|
}
|
2003-08-02 20:46:55 +02:00
|
|
|
buf.append("</").append(getElementName()).append(">");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|