001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.xroster.packet;
019
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.Iterator;
023import java.util.List;
024
025import org.jivesoftware.smack.packet.ExtensionElement;
026import org.jivesoftware.smack.roster.Roster;
027import org.jivesoftware.smack.roster.RosterEntry;
028import org.jivesoftware.smack.roster.RosterGroup;
029
030import org.jivesoftware.smackx.xroster.RemoteRosterEntry;
031import org.jivesoftware.smackx.xroster.RosterExchangeManager;
032
033/**
034 * Represents XMPP Roster Item Exchange packets.<p>
035 *
036 * The 'jabber:x:roster' namespace (which is not to be confused with the 'jabber:iq:roster'
037 * namespace) is used to send roster items from one client to another. A roster item is sent by
038 * adding to the &lt;message/&gt; element an &lt;x/&gt; child scoped by the 'jabber:x:roster' namespace. This
039 * &lt;x/&gt; element may contain one or more &lt;item/&gt; children (one for each roster item to be sent).<p>
040 *
041 * Each &lt;item/&gt; element may possess the following attributes:<p>
042 *
043 * &lt;jid/&gt; -- The id of the contact being sent. This attribute is required.<br>
044 * &lt;name/&gt; -- A natural-language nickname for the contact. This attribute is optional.<p>
045 *
046 * Each &lt;item/&gt; element may also contain one or more &lt;group/&gt; children specifying the
047 * natural-language name of a user-specified group, for the purpose of categorizing this contact
048 * into one or more roster groups.
049 *
050 * @author Gaston Dombiak
051 */
052public class RosterExchange implements ExtensionElement {
053
054    private final List<RemoteRosterEntry> remoteRosterEntries = new ArrayList<>();
055
056    /**
057     * Creates a new empty roster exchange package.
058     *
059     */
060    public RosterExchange() {
061        super();
062    }
063
064    /**
065     * Creates a new roster exchange package with the entries specified in roster.
066     *
067     * @param roster the roster to send to other XMPP entity.
068     */
069    public RosterExchange(Roster roster) {
070        // Add all the roster entries to the new RosterExchange
071        for (RosterEntry rosterEntry : roster.getEntries()) {
072            this.addRosterEntry(rosterEntry);
073        }
074    }
075
076    /**
077     * Adds a roster entry to the packet.
078     *
079     * @param rosterEntry a roster entry to add.
080     */
081    public void addRosterEntry(RosterEntry rosterEntry) {
082        // Obtain a String[] from the roster entry groups name
083        List<String> groupNamesList = new ArrayList<>();
084        String[] groupNames;
085        for (RosterGroup group : rosterEntry.getGroups()) {
086            groupNamesList.add(group.getName());
087        }
088        groupNames = groupNamesList.toArray(new String[groupNamesList.size()]);
089
090        // Create a new Entry based on the rosterEntry and add it to the packet
091        RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getJid(),
092                rosterEntry.getName(), groupNames);
093
094        addRosterEntry(remoteRosterEntry);
095    }
096
097    /**
098     * Adds a remote roster entry to the packet.
099     *
100     * @param remoteRosterEntry a remote roster entry to add.
101     */
102    public void addRosterEntry(RemoteRosterEntry remoteRosterEntry) {
103        synchronized (remoteRosterEntries) {
104            remoteRosterEntries.add(remoteRosterEntry);
105        }
106    }
107
108    /**
109    * Returns the XML element name of the extension sub-packet root element.
110    * Always returns "x"
111    *
112    * @return the XML element name of the stanza extension.
113    */
114    @Override
115    public String getElementName() {
116        return RosterExchangeManager.ELEMENT;
117    }
118
119    /**
120     * Returns the XML namespace of the extension sub-packet root element.
121     * According the specification the namespace is always "jabber:x:roster"
122     * (which is not to be confused with the 'jabber:iq:roster' namespace
123     *
124     * @return the XML namespace of the stanza extension.
125     */
126    @Override
127    public String getNamespace() {
128        return RosterExchangeManager.NAMESPACE;
129    }
130
131    /**
132     * Returns an Iterator for the roster entries in the packet.
133     *
134     * @return an Iterator for the roster entries in the packet.
135     */
136    public Iterator<RemoteRosterEntry> getRosterEntries() {
137        synchronized (remoteRosterEntries) {
138            List<RemoteRosterEntry> entries = Collections.unmodifiableList(new ArrayList<>(remoteRosterEntries));
139            return entries.iterator();
140        }
141    }
142
143    /**
144     * Returns a count of the entries in the roster exchange.
145     *
146     * @return the number of entries in the roster exchange.
147     */
148    public int getEntryCount() {
149        return remoteRosterEntries.size();
150    }
151
152    /**
153     * Returns the XML representation of a Roster Item Exchange according the specification.
154     *
155     * Usually the XML representation will be inside of a Message XML representation like
156     * in the following example:
157     * <pre>
158     * &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
159     *     &lt;subject&gt;Any subject you want&lt;/subject&gt;
160     *     &lt;body&gt;This message contains roster items.&lt;/body&gt;
161     *     &lt;x xmlns="jabber:x:roster"&gt;
162     *         &lt;item jid="gato1@gato.home"/&gt;
163     *         &lt;item jid="gato2@gato.home"/&gt;
164     *     &lt;/x&gt;
165     * &lt;/message&gt;
166     * </pre>
167     *
168     */
169    @Override
170    public String toXML(String enclosingNamespace) {
171        StringBuilder buf = new StringBuilder();
172        buf.append('<').append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
173            "\">");
174        // Loop through all roster entries and append them to the string buffer
175        for (Iterator<RemoteRosterEntry> i = getRosterEntries(); i.hasNext();) {
176            RemoteRosterEntry remoteRosterEntry = i.next();
177            buf.append(remoteRosterEntry.toXML());
178        }
179        buf.append("</").append(getElementName()).append('>');
180        return buf.toString();
181    }
182
183}