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 <message/> element an <x/> child scoped by the 'jabber:x:roster' namespace. This 041 * <x/> element may contain one or more <item/> children (one for each roster item to be sent).<p> 042 * 043 * Each <item/> element may possess the following attributes:<p> 044 * 045 * <jid/> -- The id of the contact being sent. This attribute is required.<br> 046 * <name/> -- A natural-language nickname for the contact. This attribute is optional.<p> 047 * 048 * Each <item/> element may also contain one or more <group/> 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 @SuppressWarnings("this-escape") 074 public RosterExchange(Roster roster) { 075 // Add all the roster entries to the new RosterExchange 076 for (RosterEntry rosterEntry : roster.getEntries()) { 077 this.addRosterEntry(rosterEntry); 078 } 079 } 080 081 /** 082 * Adds a roster entry to the packet. 083 * 084 * @param rosterEntry a roster entry to add. 085 */ 086 public void addRosterEntry(RosterEntry rosterEntry) { 087 // Obtain a String[] from the roster entry groups name 088 List<String> groupNamesList = new ArrayList<>(); 089 String[] groupNames; 090 for (RosterGroup group : rosterEntry.getGroups()) { 091 groupNamesList.add(group.getName()); 092 } 093 groupNames = groupNamesList.toArray(new String[groupNamesList.size()]); 094 095 // Create a new Entry based on the rosterEntry and add it to the packet 096 RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getJid(), 097 rosterEntry.getName(), groupNames); 098 099 addRosterEntry(remoteRosterEntry); 100 } 101 102 /** 103 * Adds a remote roster entry to the packet. 104 * 105 * @param remoteRosterEntry a remote roster entry to add. 106 */ 107 public void addRosterEntry(RemoteRosterEntry remoteRosterEntry) { 108 synchronized (remoteRosterEntries) { 109 remoteRosterEntries.add(remoteRosterEntry); 110 } 111 } 112 113 /** 114 * Returns the XML element name of the extension sub-packet root element. 115 * Always returns "x" 116 * 117 * @return the XML element name of the stanza extension. 118 */ 119 @Override 120 public String getElementName() { 121 return QNAME.getLocalPart(); 122 } 123 124 /** 125 * Returns the XML namespace of the extension sub-packet root element. 126 * According the specification the namespace is always "jabber:x:roster" 127 * (which is not to be confused with the 'jabber:iq:roster' namespace 128 * 129 * @return the XML namespace of the stanza extension. 130 */ 131 @Override 132 public String getNamespace() { 133 return QNAME.getNamespaceURI(); 134 } 135 136 /** 137 * Returns an Iterator for the roster entries in the packet. 138 * 139 * @return an Iterator for the roster entries in the packet. 140 */ 141 public Iterator<RemoteRosterEntry> getRosterEntries() { 142 synchronized (remoteRosterEntries) { 143 List<RemoteRosterEntry> entries = Collections.unmodifiableList(new ArrayList<>(remoteRosterEntries)); 144 return entries.iterator(); 145 } 146 } 147 148 /** 149 * Returns a count of the entries in the roster exchange. 150 * 151 * @return the number of entries in the roster exchange. 152 */ 153 public int getEntryCount() { 154 return remoteRosterEntries.size(); 155 } 156 157 /** 158 * Returns the XML representation of a Roster Item Exchange according the specification. 159 * 160 * Usually the XML representation will be inside of a Message XML representation like 161 * in the following example: 162 * <pre> 163 * <message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"> 164 * <subject>Any subject you want</subject> 165 * <body>This message contains roster items.</body> 166 * <x xmlns="jabber:x:roster"> 167 * <item jid="gato1@gato.home"/> 168 * <item jid="gato2@gato.home"/> 169 * </x> 170 * </message> 171 * </pre> 172 * 173 */ 174 @Override 175 public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 176 StringBuilder buf = new StringBuilder(); 177 buf.append('<').append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( 178 "\">"); 179 // Loop through all roster entries and append them to the string buffer 180 for (Iterator<RemoteRosterEntry> i = getRosterEntries(); i.hasNext();) { 181 RemoteRosterEntry remoteRosterEntry = i.next(); 182 buf.append(remoteRosterEntry.toXML()); 183 } 184 buf.append("</").append(getElementName()).append('>'); 185 return buf.toString(); 186 } 187 188}