Search in sources :

Example 11 with DiscoverItems

use of org.jivesoftware.smackx.packet.DiscoverItems in project ecf by eclipse.

the class MultiUserChat method getJoinedRooms.

/**
 * Returns an Iterator on the rooms where the requested user has joined. The Iterator will
 * contain Strings where each String represents a room (e.g. room@muc.jabber.org).
 *
 * @param connection the connection to use to perform the service discovery.
 * @param user the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com.
 * @return an Iterator on the rooms where the requested user has joined.
 */
public static Iterator<String> getJoinedRooms(Connection connection, String user) {
    try {
        ArrayList<String> answer = new ArrayList<String>();
        // Send the disco packet to the user
        DiscoverItems result = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems(user, discoNode);
        // Collect the entityID for each returned item
        for (Iterator<DiscoverItems.Item> items = result.getItems(); items.hasNext(); ) {
            answer.add(items.next().getEntityID());
        }
        return answer.iterator();
    } catch (XMPPException e) {
        e.printStackTrace();
        // Return an iterator on an empty collection
        return new ArrayList<String>().iterator();
    }
}
Also used : ArrayList(java.util.ArrayList) DiscoverItems(org.jivesoftware.smackx.packet.DiscoverItems) XMPPException(org.jivesoftware.smack.XMPPException)

Example 12 with DiscoverItems

use of org.jivesoftware.smackx.packet.DiscoverItems in project ecf by eclipse.

the class MultiUserChat method getServiceNames.

/**
 * Returns a collection with the XMPP addresses of the Multi-User Chat services.
 *
 * @param connection the XMPP connection to use for discovering Multi-User Chat services.
 * @return a collection with the XMPP addresses of the Multi-User Chat services.
 * @throws XMPPException if an error occured while trying to discover MUC services.
 */
public static Collection<String> getServiceNames(Connection connection) throws XMPPException {
    final List<String> answer = new ArrayList<String>();
    ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection);
    DiscoverItems items = discoManager.discoverItems(connection.getServiceName());
    for (Iterator<DiscoverItems.Item> it = items.getItems(); it.hasNext(); ) {
        DiscoverItems.Item item = it.next();
        try {
            DiscoverInfo info = discoManager.discoverInfo(item.getEntityID());
            if (info.containsFeature("http://jabber.org/protocol/muc")) {
                answer.add(item.getEntityID());
            }
        } catch (XMPPException e) {
        // Trouble finding info in some cases. This is a workaround for
        // discovering info on remote servers.
        }
    }
    return answer;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) ArrayList(java.util.ArrayList) DiscoverItems(org.jivesoftware.smackx.packet.DiscoverItems) XMPPException(org.jivesoftware.smack.XMPPException) ServiceDiscoveryManager(org.jivesoftware.smackx.ServiceDiscoveryManager)

Example 13 with DiscoverItems

use of org.jivesoftware.smackx.packet.DiscoverItems in project ecf by eclipse.

the class LeafNode method discoverItems.

/**
 * Get information on the items in the node in standard
 * {@link DiscoverItems} format.
 *
 * @return The item details in {@link DiscoverItems} format
 *
 * @throws XMPPException
 */
public DiscoverItems discoverItems() throws XMPPException {
    DiscoverItems items = new DiscoverItems();
    items.setTo(to);
    items.setNode(getId());
    return (DiscoverItems) SyncPacketSend.getReply(con, items);
}
Also used : DiscoverItems(org.jivesoftware.smackx.packet.DiscoverItems)

Example 14 with DiscoverItems

use of org.jivesoftware.smackx.packet.DiscoverItems in project ecf by eclipse.

the class XMPPChatRoomManager method checkRoom.

/**
 * check if the MultiUserChat room is already existing on the XMPP server.
 *
 * @param conference
 * @param room
 *            the name of the room
 * @return true, if the room exists, false otherwise
 * @throws XMPPException
 */
protected boolean checkRoom(String conference, String room) throws XMPPException {
    final XMPPConnection conn = ecfConnection.getXMPPConnection();
    final ServiceDiscoveryManager serviceDiscoveryManager = new ServiceDiscoveryManager(conn);
    final DiscoverItems result = serviceDiscoveryManager.discoverItems(conference);
    for (final Iterator items = result.getItems(); items.hasNext(); ) {
        final DiscoverItems.Item item = ((DiscoverItems.Item) items.next());
        if (room.equals(item.getEntityID())) {
            return true;
        }
    }
    return false;
}
Also used : Iterator(java.util.Iterator) DiscoverItems(org.jivesoftware.smackx.packet.DiscoverItems) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ServiceDiscoveryManager(org.jivesoftware.smackx.ServiceDiscoveryManager)

Example 15 with DiscoverItems

use of org.jivesoftware.smackx.packet.DiscoverItems in project Smack by igniterealtime.

the class AdHocCommandDiscoTest method testAdHocCommands.

public void testAdHocCommands() {
    try {
        AdHocCommandManager manager1 = AdHocCommandManager.getAddHocCommandsManager(getConnection(0));
        manager1.registerCommand("test", "test node", LocalCommand.class);
        manager1.registerCommand("test2", "test node", new LocalCommandFactory() {

            public LocalCommand getInstance() throws InstantiationException, IllegalAccessException {
                return new LocalCommand() {

                    public boolean isLastStage() {
                        return true;
                    }

                    public boolean hasPermission(String jid) {
                        return true;
                    }

                    public void execute() throws XMPPException {
                        Form result = new Form(Form.TYPE_RESULT);
                        FormField resultField = new FormField("test2");
                        resultField.setLabel("test node");
                        resultField.addValue("it worked");
                        result.addField(resultField);
                        setForm(result);
                    }

                    public void next(Form response) throws XMPPException {
                    // 
                    }

                    public void complete(Form response) throws XMPPException {
                    // 
                    }

                    public void prev() throws XMPPException {
                    // 
                    }

                    public void cancel() throws XMPPException {
                    // 
                    }
                };
            }
        });
        AdHocCommandManager manager2 = AdHocCommandManager.getAddHocCommandsManager(getConnection(1));
        DiscoverItems items = manager2.discoverCommands(getFullJID(0));
        assertTrue("Disco for command test failed", items.getItems().next().getNode().equals("test"));
        RemoteCommand command = manager2.getRemoteCommand(getFullJID(0), "test2");
        command.execute();
        assertEquals("Disco for command test failed", command.getForm().getField("test2").getValues().next(), "it worked");
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Form(org.jivesoftware.smackx.Form) DiscoverItems(org.jivesoftware.smackx.packet.DiscoverItems) XMPPException(org.jivesoftware.smack.XMPPException) FormField(org.jivesoftware.smackx.FormField) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

DiscoverItems (org.jivesoftware.smackx.packet.DiscoverItems)16 ArrayList (java.util.ArrayList)6 XMPPException (org.jivesoftware.smack.XMPPException)6 ServiceDiscoveryManager (org.jivesoftware.smackx.ServiceDiscoveryManager)6 DiscoverInfo (org.jivesoftware.smackx.packet.DiscoverInfo)5 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)2 Iterator (java.util.Iterator)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)1 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)1 IQ (org.jivesoftware.smack.packet.IQ)1 Packet (org.jivesoftware.smack.packet.Packet)1 XMPPError (org.jivesoftware.smack.packet.XMPPError)1 Form (org.jivesoftware.smackx.Form)1 FormField (org.jivesoftware.smackx.FormField)1 Identity (org.jivesoftware.smackx.packet.DiscoverInfo.Identity)1 Item (org.jivesoftware.smackx.packet.DiscoverItems.Item)1