1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 01:29:38 +02:00

Move Roster Item Exchange code to legacy project

XEP-93 has been deprecated and superseded by XEP-144.
This commit is contained in:
Florian Schmaus 2014-03-03 12:05:25 +01:00
parent fa289eac04
commit 768700b301
10 changed files with 72 additions and 8 deletions

View file

@ -0,0 +1,27 @@
/**
*
* Copyright the original author or authors
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smackx;
import org.jivesoftware.smack.initializer.UrlProviderFileInitializer;
public class LegacyProviderInitializer extends UrlProviderFileInitializer {
@Override
protected String getFilePath() {
return "classpath:org.jivesoftware.smackx/legacy.providers";
}
}

View file

@ -0,0 +1,111 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smackx.xroster;
import java.util.*;
/**
* Represents a roster item, which consists of a JID and , their name and
* the groups the roster item belongs to. This roster item does not belong
* to the local roster. Therefore, it does not persist in the server.<p>
*
* The idea of a RemoteRosterEntry is to be used as part of a roster exchange.
*
* @author Gaston Dombiak
*/
public class RemoteRosterEntry {
private String user;
private String name;
private final List<String> groupNames = new ArrayList<String>();
/**
* Creates a new remote roster entry.
*
* @param user the user.
* @param name the user's name.
* @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
* the roster entry won't belong to a group.
*/
public RemoteRosterEntry(String user, String name, String [] groups) {
this.user = user;
this.name = name;
if (groups != null) {
groupNames.addAll(Arrays.asList(groups));
}
}
/**
* Returns the user.
*
* @return the user.
*/
public String getUser() {
return user;
}
/**
* Returns the user's name.
*
* @return the user's name.
*/
public String getName() {
return name;
}
/**
* Returns an Iterator for the group names (as Strings) that the roster entry
* belongs to.
*
* @return an Iterator for the group names.
*/
public Iterator<String> getGroupNames() {
synchronized (groupNames) {
return Collections.unmodifiableList(groupNames).iterator();
}
}
/**
* Returns a String array for the group names that the roster entry
* belongs to.
*
* @return a String[] for the group names.
*/
public String[] getGroupArrayNames() {
synchronized (groupNames) {
return Collections.unmodifiableList(groupNames).toArray(new String[groupNames.size()]);
}
}
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<item jid=\"").append(user).append("\"");
if (name != null) {
buf.append(" name=\"").append(name).append("\"");
}
buf.append(">");
synchronized (groupNames) {
for (String groupName : groupNames) {
buf.append("<group>").append(groupName).append("</group>");
}
}
buf.append("</item>");
return buf.toString();
}
}

View file

@ -0,0 +1,39 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smackx.xroster;
import java.util.Iterator;
/**
*
* A listener that is fired anytime a roster exchange is received.
*
* @author Gaston Dombiak
*/
public interface RosterExchangeListener {
/**
* Called when roster entries are received as part of a roster exchange.
*
* @param from the user that sent the entries.
* @param remoteRosterEntries the entries sent by the user. The entries are instances of
* RemoteRosterEntry.
*/
public void entriesReceived(String from, Iterator<RemoteRosterEntry> remoteRosterEntries);
}

View file

@ -0,0 +1,184 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smackx.xroster;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.xroster.packet.RosterExchange;
/**
*
* Manages Roster exchanges. A RosterExchangeManager provides a high level access to send
* rosters, roster groups and roster entries to XMPP clients. It also provides an easy way
* to hook up custom logic when entries are received from another XMPP client through
* RosterExchangeListeners.
*
* @author Gaston Dombiak
*/
public class RosterExchangeManager {
public final static String NAMESPACE = "jabber:x:roster";
public final static String ELEMENT = "x";
private final static Map<Connection, RosterExchangeManager> INSTANCES =
Collections.synchronizedMap(new WeakHashMap<Connection, RosterExchangeManager>());
private final static PacketFilter PACKET_FILTER = new PacketExtensionFilter(ELEMENT, NAMESPACE);
private final Set<RosterExchangeListener> rosterExchangeListeners = Collections.synchronizedSet(new HashSet<RosterExchangeListener>());
private final WeakReference<Connection> weakRefConnection;
private final PacketListener packetListener;
public synchronized static RosterExchangeManager getInstanceFor(Connection connection) {
RosterExchangeManager rosterExchangeManager = INSTANCES.get(connection);
if (rosterExchangeManager == null) {
rosterExchangeManager = new RosterExchangeManager(connection);
}
return rosterExchangeManager;
}
/**
* Creates a new roster exchange manager.
*
* @param con a Connection which is used to send and receive messages.
*/
public RosterExchangeManager(Connection connection) {
weakRefConnection = new WeakReference<Connection>(connection);
// Listens for all roster exchange packets and fire the roster exchange listeners.
packetListener = new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
RosterExchange rosterExchange =
(RosterExchange) message.getExtension(ELEMENT, NAMESPACE);
// Fire event for roster exchange listeners
fireRosterExchangeListeners(message.getFrom(), rosterExchange.getRosterEntries());
};
};
connection.addPacketListener(packetListener, PACKET_FILTER);
}
/**
* Adds a listener to roster exchanges. The listener will be fired anytime roster entries
* are received from remote XMPP clients.
*
* @param rosterExchangeListener a roster exchange listener.
*/
public void addRosterListener(RosterExchangeListener rosterExchangeListener) {
rosterExchangeListeners.add(rosterExchangeListener);
}
/**
* Removes a listener from roster exchanges. The listener will be fired anytime roster
* entries are received from remote XMPP clients.
*
* @param rosterExchangeListener a roster exchange listener..
*/
public void removeRosterListener(RosterExchangeListener rosterExchangeListener) {
rosterExchangeListeners.remove(rosterExchangeListener);
}
/**
* Sends a roster to userID. All the entries of the roster will be sent to the
* target user.
*
* @param roster the roster to send
* @param targetUserID the user that will receive the roster entries
*/
public void send(Roster roster, String targetUserID) {
// Create a new message to send the roster
Message msg = new Message(targetUserID);
// Create a RosterExchange Package and add it to the message
RosterExchange rosterExchange = new RosterExchange(roster);
msg.addExtension(rosterExchange);
Connection connection = weakRefConnection.get();
// Send the message that contains the roster
connection.sendPacket(msg);
}
/**
* Sends a roster entry to userID.
*
* @param rosterEntry the roster entry to send
* @param targetUserID the user that will receive the roster entries
*/
public void send(RosterEntry rosterEntry, String targetUserID) {
// Create a new message to send the roster
Message msg = new Message(targetUserID);
// Create a RosterExchange Package and add it to the message
RosterExchange rosterExchange = new RosterExchange();
rosterExchange.addRosterEntry(rosterEntry);
msg.addExtension(rosterExchange);
Connection connection = weakRefConnection.get();
// Send the message that contains the roster
connection.sendPacket(msg);
}
/**
* Sends a roster group to userID. All the entries of the group will be sent to the
* target user.
*
* @param rosterGroup the roster group to send
* @param targetUserID the user that will receive the roster entries
*/
public void send(RosterGroup rosterGroup, String targetUserID) {
// Create a new message to send the roster
Message msg = new Message(targetUserID);
// Create a RosterExchange Package and add it to the message
RosterExchange rosterExchange = new RosterExchange();
for (RosterEntry entry : rosterGroup.getEntries()) {
rosterExchange.addRosterEntry(entry);
}
msg.addExtension(rosterExchange);
Connection connection = weakRefConnection.get();
// Send the message that contains the roster
connection.sendPacket(msg);
}
/**
* Fires roster exchange listeners.
*/
private void fireRosterExchangeListeners(String from, Iterator<RemoteRosterEntry> remoteRosterEntries) {
RosterExchangeListener[] listeners = null;
synchronized (rosterExchangeListeners) {
listeners = new RosterExchangeListener[rosterExchangeListeners.size()];
rosterExchangeListeners.toArray(listeners);
}
for (int i = 0; i < listeners.length; i++) {
listeners[i].entriesReceived(from, remoteRosterEntries);
}
}
}

View file

@ -0,0 +1,179 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smackx.xroster.packet;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.xroster.RemoteRosterEntry;
import org.jivesoftware.smackx.xroster.RosterExchangeManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* 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 &lt;message/&gt; element an &lt;x/&gt; child scoped by the 'jabber:x:roster' namespace. This
* &lt;x/&gt; element may contain one or more &lt;item/&gt; children (one for each roster item to be sent).<p>
*
* Each &lt;item/&gt; element may possess the following attributes:<p>
*
* &lt;jid/&gt; -- The id of the contact being sent. This attribute is required.<br>
* &lt;name/&gt; -- A natural-language nickname for the contact. This attribute is optional.<p>
*
* Each &lt;item/&gt; element may also contain one or more &lt;group/&gt; children specifying the
* natural-language name of a user-specified group, for the purpose of categorizing this contact
* into one or more roster groups.
*
* @author Gaston Dombiak
*/
public class RosterExchange implements PacketExtension {
private List<RemoteRosterEntry> remoteRosterEntries = new ArrayList<RemoteRosterEntry>();
/**
* Creates a new empty roster exchange package.
*
*/
public RosterExchange() {
super();
}
/**
* 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 (RosterEntry rosterEntry : roster.getEntries()) {
this.addRosterEntry(rosterEntry);
}
}
/**
* Adds a roster entry to the packet.
*
* @param rosterEntry a roster entry to add.
*/
public void addRosterEntry(RosterEntry rosterEntry) {
// Obtain a String[] from the roster entry groups name
List<String> groupNamesList = new ArrayList<String>();
String[] groupNames;
for (RosterGroup group : rosterEntry.getGroups()) {
groupNamesList.add(group.getName());
}
groupNames = groupNamesList.toArray(new String[groupNamesList.size()]);
// Create a new Entry based on the rosterEntry and add it to the packet
RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getUser(),
rosterEntry.getName(), groupNames);
addRosterEntry(remoteRosterEntry);
}
/**
* Adds a remote roster entry to the packet.
*
* @param remoteRosterEntry a remote roster entry to add.
*/
public void addRosterEntry(RemoteRosterEntry remoteRosterEntry) {
synchronized (remoteRosterEntries) {
remoteRosterEntries.add(remoteRosterEntry);
}
}
/**
* 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 RosterExchangeManager.ELEMENT;
}
/**
* 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 RosterExchangeManager.NAMESPACE;
}
/**
* Returns an Iterator for the roster entries in the packet.
*
* @return an Iterator for the roster entries in the packet.
*/
public Iterator<RemoteRosterEntry> getRosterEntries() {
synchronized (remoteRosterEntries) {
List<RemoteRosterEntry> entries = Collections.unmodifiableList(new ArrayList<RemoteRosterEntry>(remoteRosterEntries));
return entries.iterator();
}
}
/**
* Returns a count of the entries in the roster exchange.
*
* @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.
*
* Usually the XML representation will be inside of a Message XML representation like
* in the following example:
* <pre>
* &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
* &lt;subject&gt;Any subject you want&lt;/subject&gt;
* &lt;body&gt;This message contains roster items.&lt;/body&gt;
* &lt;x xmlns="jabber:x:roster"&gt;
* &lt;item jid="gato1@gato.home"/&gt;
* &lt;item jid="gato2@gato.home"/&gt;
* &lt;/x&gt;
* &lt;/message&gt;
* </pre>
*
*/
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
"\">");
// Loop through all roster entries and append them to the string buffer
for (Iterator<RemoteRosterEntry> i = getRosterEntries(); i.hasNext();) {
RemoteRosterEntry remoteRosterEntry = i.next();
buf.append(remoteRosterEntry.toXML());
}
buf.append("</").append(getElementName()).append(">");
return buf.toString();
}
}

View file

@ -0,0 +1,87 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smackx.xroster.provider;
import java.util.ArrayList;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smackx.xroster.RemoteRosterEntry;
import org.jivesoftware.smackx.xroster.packet.RosterExchange;
import org.xmlpull.v1.XmlPullParser;
/**
*
* The RosterExchangeProvider parses RosterExchange packets.
*
* @author Gaston Dombiak
*/
public class RosterExchangeProvider implements PacketExtensionProvider {
/**
* Creates a new RosterExchangeProvider.
* ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor
*/
public RosterExchangeProvider() {
}
/**
* Parses a RosterExchange packet (extension sub-packet).
*
* @param parser the XML parser, positioned at the starting element of the extension.
* @return a PacketExtension.
* @throws Exception if a parsing error occurs.
*/
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
RosterExchange rosterExchange = new RosterExchange();
boolean done = false;
RemoteRosterEntry remoteRosterEntry = null;
String jid = "";
String name = "";
ArrayList<String> groupsName = new ArrayList<String>();
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("item")) {
// Reset this variable since they are optional for each item
groupsName = new ArrayList<String>();
// Initialize the variables from the parsed XML
jid = parser.getAttributeValue("", "jid");
name = parser.getAttributeValue("", "name");
}
if (parser.getName().equals("group")) {
groupsName.add(parser.nextText());
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("item")) {
// Create packet.
remoteRosterEntry = new RemoteRosterEntry(jid, name, (String[]) groupsName.toArray(new String[groupsName.size()]));
rosterExchange.addRosterEntry(remoteRosterEntry);
}
if (parser.getName().equals("x")) {
done = true;
}
}
}
return rosterExchange;
}
}

View file

@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!-- Providers file for legacy Smack extensions -->
<smackProviders>
<!-- Roster Exchange -->
<extensionProvider>
<elementName>x</elementName>
<namespace>jabber:x:roster</namespace>
<className>org.jivesoftware.smackx.xroster.provider.RosterExchangeProvider</className>
</extensionProvider>
</smackProviders>

View file

@ -0,0 +1,31 @@
/**
*
* Copyright the original author or authors
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smackx;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class LegacyProviderInitializerTest {
@Test
public void testWorkgroupProviderInitializer() {
LegacyProviderInitializer lpi = new LegacyProviderInitializer();
lpi.initialize();
assertTrue(lpi.getExceptions().size() == 0);
}
}