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