2003-08-24 18:05:45 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* Copyright 2003-2004 Jive Software.
|
2003-08-24 18:05:45 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +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-08-24 18:05:45 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-08-24 18:05:45 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +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-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;
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
import org.jivesoftware.smack.Roster;
|
|
|
|
import org.jivesoftware.smack.RosterEntry;
|
|
|
|
import org.jivesoftware.smack.RosterGroup;
|
2003-08-02 20:46:55 +02:00
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
2006-07-18 07:14:33 +02:00
|
|
|
import org.jivesoftware.smackx.RemoteRosterEntry;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
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
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterEntry rosterEntry : roster.getEntries()) {
|
|
|
|
this.addRosterEntry(rosterEntry);
|
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
|
2006-07-17 10:39:08 +02:00
|
|
|
List<String> groupNamesList = new ArrayList<String>();
|
2003-08-26 18:05:57 +02:00
|
|
|
String[] groupNames;
|
2006-07-17 10:39:08 +02:00
|
|
|
for (RosterGroup group : rosterEntry.getGroups()) {
|
|
|
|
groupNamesList.add(group.getName());
|
2003-08-26 18:05:57 +02:00
|
|
|
}
|
2006-07-17 10:39:08 +02:00
|
|
|
groupNames = groupNamesList.toArray(new String[groupNamesList.size()]);
|
2003-08-26 18:05:57 +02:00
|
|
|
|
2003-08-24 18:05:45 +02:00
|
|
|
// Create a new Entry based on the rosterEntry and add it to the packet
|
2006-07-17 10:39:08 +02:00
|
|
|
RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getUser(),
|
|
|
|
rosterEntry.getName(), groupNames);
|
2003-08-26 18:05:57 +02:00
|
|
|
|
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() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|