Permalink
Browse files

add the ability to register for roster events before logging in (SMAC…

…K-156)

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@11826 b35dd754-fafc-0310-a699-88a17e54d16e
  • Loading branch information...
1 parent 2b9fcbf commit c0ad78da322251ab59fc6d1d49966268af26bcde henning committed with René Treffer Aug 15, 2010
@@ -22,7 +22,7 @@
etc.<p>
A <tt>Roster</tt> instance is obtained using the <tt>Connection.getRoster()</tt>
-method, but only after successfully logging into a server.
+method.
<p class="subheader">Roster Entries</p>
@@ -70,7 +70,9 @@
<p>The presence information will likely
change often, and it's also possible for the roster entries to change or be deleted.
-To listen for changing roster and presence data, a RosterListener should be used.
+To listen for changing roster and presence data, a RosterListener should be used.
+To be informed about all changes to the roster the RosterListener should be registered
+before logging into the XMPP server.
The following code snippet registers a RosterListener with the Roster that prints
any presence changes in the roster to standard out. A normal client would use
similar code to update the roster UI with the changing information.
@@ -428,11 +428,15 @@ public synchronized ChatManager getChatManager() {
}
/**
- * Returns the roster for the user logged into the server. If the user has not yet
- * logged into the server (or if the user is logged in anonymously), this method will return
- * <tt>null</tt>.
- *
- * @return the user's roster, or <tt>null</tt> if the user has not logged in yet.
+ * Returns the roster for the user.
+ * <p>
+ * This method will never return <code>null</code>, instead if the user has not yet logged into
+ * the server or is logged in anonymously all modifying methods of the returned roster object
+ * like {@link Roster#createEntry(String, String, String[])},
+ * {@link Roster#removeEntry(RosterEntry)} , etc. except adding or removing
+ * {@link RosterListener}s will throw an IllegalStateException.
+ *
+ * @return the user's roster.
*/
public abstract Roster getRoster();
@@ -63,7 +63,7 @@
private final List<RosterListener> rosterListeners;
private Map<String, Map<String, Presence>> presenceMap;
// The roster is marked as initialized when at least a single roster packet
- // has been recieved and processed.
+ // has been received and processed.
boolean rosterInitialized = false;
private PresencePacketListener presencePacketListener;
@@ -123,8 +123,10 @@ public static void setDefaultSubscriptionMode(SubscriptionMode subscriptionMode)
PacketFilter presenceFilter = new PacketTypeFilter(Presence.class);
presencePacketListener = new PresencePacketListener();
connection.addPacketListener(presencePacketListener, presenceFilter);
+
// Listen for connection events
- connection.addConnectionListener(new ConnectionListener() {
+ final ConnectionListener connectionListener = new AbstractConnectionListener() {
+
public void connectionClosed() {
// Changes the presence available contacts to unavailable
setOfflinePresences();
@@ -135,19 +137,22 @@ public void connectionClosedOnError(Exception e) {
setOfflinePresences();
}
- public void reconnectingIn(int seconds) {
- // Ignore
- }
-
- public void reconnectionFailed(Exception e) {
- // Ignore
- }
-
- public void reconnectionSuccessful() {
- // Ignore
- }
- });
+ };
+ // if not connected add listener after successful login
+ if(!this.connection.isConnected()) {
+ Connection.addConnectionCreationListener(new ConnectionCreationListener() {
+
+ public void connectionCreated(Connection connection) {
+ if(connection.equals(Roster.this.connection)) {
+ Roster.this.connection.addConnectionListener(connectionListener);
+ }
+
+ }
+ });
+ } else {
+ connection.addConnectionListener(connectionListener);
+ }
}
/**
@@ -184,8 +189,17 @@ public void setSubscriptionMode(SubscriptionMode subscriptionMode) {
* Reloads the entire roster from the server. This is an asynchronous operation,
* which means the method will return immediately, and the roster will be
* reloaded at a later point when the server responds to the reload request.
+ *
+ * @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public void reload() {
+ if (!connection.isAuthenticated()) {
+ throw new IllegalStateException("Not logged in to server.");
+ }
+ if (connection.isAnonymous()) {
+ throw new IllegalStateException("Anonymous users can't have a roster.");
+ }
+
RosterPacket packet = new RosterPacket();
if(persistentStorage!=null){
packet.setVersion(persistentStorage.getRosterVersion());
@@ -226,11 +240,19 @@ public void removeRosterListener(RosterListener rosterListener) {
*
* @param name the name of the group.
* @return a new group.
+ * @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public RosterGroup createGroup(String name) {
+ if (!connection.isAuthenticated()) {
+ throw new IllegalStateException("Not logged in to server.");
+ }
+ if (connection.isAnonymous()) {
+ throw new IllegalStateException("Anonymous users can't have a roster.");
+ }
if (groups.containsKey(name)) {
throw new IllegalArgumentException("Group with name " + name + " alread exists.");
}
+
RosterGroup group = new RosterGroup(name, connection);
groups.put(name, group);
return group;
@@ -245,8 +267,16 @@ public RosterGroup createGroup(String 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.
* @throws XMPPException if an XMPP exception occurs.
+ * @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public void createEntry(String user, String name, String[] groups) throws XMPPException {
+ if (!connection.isAuthenticated()) {
+ throw new IllegalStateException("Not logged in to server.");
+ }
+ if (connection.isAnonymous()) {
+ throw new IllegalStateException("Anonymous users can't have a roster.");
+ }
+
// Create and send roster entry creation packet.
RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
@@ -408,8 +438,16 @@ private void insertRosterItem(RosterPacket.Item item, Collection<String> addedEn
*
* @param entry a roster entry.
* @throws XMPPException if an XMPP error occurs.
+ * @throws IllegalStateException if connection is not logged in or logged in anonymously
*/
public void removeEntry(RosterEntry entry) throws XMPPException {
+ if (!connection.isAuthenticated()) {
+ throw new IllegalStateException("Not logged in to server.");
+ }
+ if (connection.isAnonymous()) {
+ throw new IllegalStateException("Anonymous users can't have a roster.");
+ }
+
// Only remove the entry if it's in the entry list.
// The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
if (!entries.containsKey(entry.getUser())) {
@@ -530,7 +568,7 @@ public int getGroupCount() {
}
/**
- * Returns an unmodiable collections of all the roster groups.
+ * Returns an unmodifiable collections of all the roster groups.
*
* @return an iterator for all roster groups.
*/
@@ -119,7 +119,7 @@ public XMPPConnection(String serviceName, CallbackHandler callbackHandler) {
}
/**
- * Creates a new XMPP conection in the same way {@link #XMPPConnection(String,CallbackHandler)} does, but
+ * Creates a new XMPP connection in the same way {@link #XMPPConnection(String,CallbackHandler)} does, but
* with no callback handler for password prompting of the keystore. This will work
* in most cases, provided the client is not required to provide a certificate to
* the server.
@@ -135,7 +135,7 @@ public XMPPConnection(String serviceName) {
}
/**
- * Creates a new XMPP conection in the same way {@link #XMPPConnection(ConnectionConfiguration,CallbackHandler)} does, but
+ * Creates a new XMPP connection in the same way {@link #XMPPConnection(ConnectionConfiguration,CallbackHandler)} does, but
* with no callback handler for password prompting of the keystore. This will work
* in most cases, provided the client is not required to provide a certificate to
* the server.
@@ -210,7 +210,7 @@ public String getUser() {
* @param resource the resource.
* @throws XMPPException if an error occurs.
* @throws IllegalStateException if not connected to the server, or already logged in
- * to the serrver.
+ * to the server.
*/
public synchronized void login(String username, String password, String resource) throws XMPPException {
if (!isConnected()) {
@@ -257,7 +257,11 @@ public synchronized void login(String username, String password, String resource
useCompression();
}
- // Create the roster if it is not a reconnection.
+ // Indicate that we're now authenticated.
+ authenticated = true;
+ anonymous = false;
+
+ // Create the roster if it is not a reconnection or roster already created by getRoster()
if (this.roster == null) {
if(rosterStorage==null){
this.roster = new Roster(this);
@@ -275,11 +279,7 @@ public synchronized void login(String username, String password, String resource
packetWriter.sendPacket(new Presence(Presence.Type.available));
}
- // Indicate that we're now authenticated.
- authenticated = true;
- anonymous = false;
-
- // Stores the autentication for future reconnection
+ // Stores the authentication for future reconnection
config.setLoginInfo(username, password, resource);
// If debugging is enabled, change the the debug window title to include the
@@ -299,7 +299,7 @@ public synchronized void login(String username, String password, String resource
*
* @throws XMPPException if an error occurs or anonymous logins are not supported by the server.
* @throws IllegalStateException if not connected to the server, or already logged in
- * to the serrver.
+ * to the server.
*/
public synchronized void loginAnonymously() throws XMPPException {
if (!isConnected()) {
@@ -329,9 +329,6 @@ public synchronized void loginAnonymously() throws XMPPException {
useCompression();
}
- // Anonymous users can't have a roster.
- roster = null;
-
// Set presence to online.
packetWriter.sendPacket(new Presence(Presence.Type.available));
@@ -349,8 +346,16 @@ public synchronized void loginAnonymously() throws XMPPException {
}
public Roster getRoster() {
- if (roster == null) {
- return null;
+ // synchronize against login()
+ synchronized(this) {
+ // if connection is authenticated the roster is already set by login()
+ // or a previous call to getRoster()
+ if (!isAuthenticated() || isAnonymous()) {
+ if (roster == null) {
+ roster = new Roster(this);
+ }
+ return roster;
+ }
}
// If this is the first time the user has asked for the roster after calling
// login, we want to wait for the server to send back the user's roster. This
@@ -0,0 +1,92 @@
+package org.jivesoftware.smack;
+
+import static org.junit.Assert.*;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.jivesoftware.smack.Roster.SubscriptionMode;
+import org.jivesoftware.smack.packet.Presence;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests the behavior of the roster if the connection is not authenticated yet.
+ *
+ * @author Henning Staib
+ */
+public class RosterOfflineTest {
+
+ Connection connection;
+
+ Roster roster;
+
+ @Before
+ public void setup() {
+ this.connection = new XMPPConnection("localhost");
+ assertFalse(connection.isConnected());
+
+ roster = connection.getRoster();
+ assertNotNull(roster);
+ }
+
+ @Test
+ public void shouldThrowNoExceptionOnGetterMethods() {
+ // all getter methods should work
+ assertFalse(roster.contains("test"));
+
+ Collection<RosterEntry> entries = roster.getEntries();
+ assertTrue(entries.size() == 0);
+
+ assertNull(roster.getEntry("test"));
+
+ assertEquals(0, roster.getEntryCount());
+
+ assertNull(roster.getGroup("test"));
+
+ assertEquals(0, roster.getGroupCount());
+
+ Collection<RosterGroup> groups = roster.getGroups();
+ assertEquals(0, groups.size());
+
+ Presence presence = roster.getPresence("test");
+ assertEquals(Presence.Type.unavailable, presence.getType());
+
+ Presence presenceResource = roster.getPresenceResource("test");
+ assertEquals(Presence.Type.unavailable, presenceResource.getType());
+
+ Iterator<Presence> iterator = roster.getPresences("test");
+ assertTrue(iterator.hasNext());
+ assertEquals(Presence.Type.unavailable, iterator.next().getType());
+ assertFalse(iterator.hasNext());
+
+ assertEquals(0, roster.getUnfiledEntries().size());
+
+ assertEquals(0, roster.getUnfiledEntryCount());
+
+ roster.setSubscriptionMode(SubscriptionMode.accept_all);
+ assertEquals(SubscriptionMode.accept_all, roster.getSubscriptionMode());
+
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldThrowExceptionOnCreateEntry() throws Exception {
+ roster.createEntry("test", "test", null);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldThrowExceptionOnCreateGroup() throws Exception {
+ roster.createGroup("test");
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldThrowExceptionOnReload() throws Exception {
+ roster.reload();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void shouldThrowExceptionRemoveEntry() throws Exception {
+ roster.removeEntry(null);
+ }
+
+}
@@ -0,0 +1,34 @@
+package org.jivesoftware.smack;
+
+/**
+ * Run all tests defined in RosterTest but initialize the roster before connection is logged in and
+ * authenticated.
+ *
+ * @author Henning Staib
+ */
+public class RosterInitializedBeforeConnectTest extends RosterSmackTest {
+
+ public RosterInitializedBeforeConnectTest(String name) {
+ super(name);
+ }
+
+ protected boolean createOfflineConnections() {
+ return true;
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ // initialize all rosters before login
+ for (int i = 0; i < getMaxConnections(); i++) {
+ XMPPConnection connection = getConnection(i);
+ assertFalse(connection.isConnected());
+
+ Roster roster = connection.getRoster();
+ assertNotNull(roster);
+
+ connectAndLogin(i);
+ }
+ }
+
+}
Oops, something went wrong.

0 comments on commit c0ad78d

Please sign in to comment.