diff --git a/source/org/jivesoftware/smackx/muc/MultiUserChat.java b/source/org/jivesoftware/smackx/muc/MultiUserChat.java index cdcd2e115..d56478c88 100644 --- a/source/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/source/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -216,6 +216,28 @@ public class MultiUserChat { return answer; } + /** + * Returns a collection of HostedRooms where each HostedRoom has the XMPP address of the room + * and the room's name. Once discovered the rooms hosted by a chat service it is possible to + * discover more detailed room information or join the room. + * + * @param connection the XMPP connection to use for discovering hosted rooms by the MUC service. + * @param serviceName the service that is hosting the rooms to discover. + * @return a collection of HostedRooms. + * @throws XMPPException if an error occured while trying to discover the information. + */ + public static Collection getHostedRooms(XMPPConnection connection, String serviceName) + throws XMPPException { + List answer = new ArrayList(); + ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection); + DiscoverItems items = discoManager.discoverItems(serviceName); + for (Iterator it = items.getItems(); it.hasNext();) { + DiscoverItems.Item item = (DiscoverItems.Item) it.next(); + answer.add(new HostedRoom(item)); + } + return answer; + } + /** * Returns the name of the room this MultiUserChat object represents. * diff --git a/test/org/jivesoftware/smackx/muc/MultiUserChatTest.java b/test/org/jivesoftware/smackx/muc/MultiUserChatTest.java index 936b21cbd..2a09f5b27 100644 --- a/test/org/jivesoftware/smackx/muc/MultiUserChatTest.java +++ b/test/org/jivesoftware/smackx/muc/MultiUserChatTest.java @@ -420,6 +420,14 @@ public class MultiUserChatTest extends SmackTestCase { try { Collection services = MultiUserChat.getServiceNames(getConnection(1)); assertFalse("No MUC service was found", services.isEmpty()); + + // Discover the hosted rooms by the chat service. + Collection rooms = MultiUserChat.getHostedRooms(getConnection(1), + (String) services.toArray()[0]); + // Check that we have discovered the room used by this test + assertFalse("No room was found", rooms.isEmpty()); + // Check that we have discovered the room used by this test + assertEquals("Wrong room JID found", room, ((HostedRoom)rooms.toArray()[0]).getJid()); } catch (XMPPException e) { e.printStackTrace();