Search in sources :

Example 1 with DiscoverItems

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

the class DiscoverItemsProvider method parse.

@Override
public DiscoverItems parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
    DiscoverItems discoverItems = new DiscoverItems();
    boolean done = false;
    DiscoverItems.Item item;
    Jid jid = null;
    String name = "";
    String action = "";
    String node = "";
    discoverItems.setNode(parser.getAttributeValue("", "node"));
    while (!done) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG && "item".equals(parser.getName())) {
            // Initialize the variables from the parsed XML
            jid = ParserUtils.getJidAttribute(parser);
            name = parser.getAttributeValue("", "name");
            node = parser.getAttributeValue("", "node");
            action = parser.getAttributeValue("", "action");
        } else if (eventType == XmlPullParser.END_TAG && "item".equals(parser.getName())) {
            // Create a new Item and add it to DiscoverItems.
            item = new DiscoverItems.Item(jid);
            item.setName(name);
            item.setNode(node);
            item.setAction(action);
            discoverItems.addItem(item);
        } else if (eventType == XmlPullParser.END_TAG && "query".equals(parser.getName())) {
            done = true;
        }
    }
    return discoverItems;
}
Also used : Jid(org.jxmpp.jid.Jid) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems)

Example 2 with DiscoverItems

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

the class AdHocCommandManager method publishCommands.

/**
     * Publish the commands to an specific JID.
     *
     * @param jid the full JID to publish the commands to.
     * @throws XMPPException if the operation failed for some reason.
     * @throws SmackException if there was no response from the server.
     * @throws InterruptedException 
     */
public void publishCommands(Jid jid) throws XMPPException, SmackException, InterruptedException {
    // Collects the commands to publish as items
    DiscoverItems discoverItems = new DiscoverItems();
    Collection<AdHocCommandInfo> xCommandsList = getRegisteredCommands();
    for (AdHocCommandInfo info : xCommandsList) {
        DiscoverItems.Item item = new DiscoverItems.Item(info.getOwnerJID());
        item.setName(info.getName());
        item.setNode(info.getNode());
        discoverItems.addItem(item);
    }
    serviceDiscoveryManager.publishItems(jid, NAMESPACE, discoverItems);
}
Also used : DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems)

Example 3 with DiscoverItems

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

the class ServiceDiscoveryManager method findServicesDiscoverInfo.

/**
     * Find all services under the users service that provide a given feature.
     * 
     * @param feature the feature to search for
     * @param stopOnFirst if true, stop searching after the first service was found
     * @param useCache if true, query a cache first to avoid network I/O
     * @return a possible empty list of services providing the given feature
     * @throws NoResponseException
     * @throws XMPPErrorException
     * @throws NotConnectedException
     * @throws InterruptedException 
     */
public List<DiscoverInfo> findServicesDiscoverInfo(String feature, boolean stopOnFirst, boolean useCache) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    List<DiscoverInfo> serviceDiscoInfo = null;
    DomainBareJid serviceName = connection().getXMPPServiceDomain();
    if (useCache) {
        serviceDiscoInfo = services.lookup(feature);
        if (serviceDiscoInfo != null) {
            return serviceDiscoInfo;
        }
    }
    serviceDiscoInfo = new LinkedList<>();
    // Send the disco packet to the server itself
    DiscoverInfo info;
    try {
        info = discoverInfo(serviceName);
    } catch (XMPPErrorException e) {
        // Be extra robust here: Return the empty linked list and log this situation
        LOGGER.log(Level.WARNING, "Could not discover information about service", e);
        return serviceDiscoInfo;
    }
    // Check if the server supports the feature
    if (info.containsFeature(feature)) {
        serviceDiscoInfo.add(info);
        if (stopOnFirst) {
            if (useCache) {
                // Cache the discovered information
                services.put(feature, serviceDiscoInfo);
            }
            return serviceDiscoInfo;
        }
    }
    DiscoverItems items;
    try {
        // Get the disco items and send the disco packet to each server item
        items = discoverItems(serviceName);
    } catch (XMPPErrorException e) {
        LOGGER.log(Level.WARNING, "Could not discover items about service", e);
        return serviceDiscoInfo;
    }
    for (DiscoverItems.Item item : items.getItems()) {
        try {
            // TODO is it OK here in all cases to query without the node attribute?
            // MultipleRecipientManager queried initially also with the node attribute, but this
            // could be simply a fault instead of intentional.
            info = discoverInfo(item.getEntityID());
        } catch (XMPPErrorException | NoResponseException e) {
            // Don't throw this exceptions if one of the server's items fail
            LOGGER.log(Level.WARNING, "Exception while discovering info for feature " + feature + " of " + item.getEntityID() + " node: " + item.getNode(), e);
            continue;
        }
        if (info.containsFeature(feature)) {
            serviceDiscoInfo.add(info);
            //serviceAddresses.add(item.getEntityID().asDomainBareJid());
            if (stopOnFirst) {
                break;
            }
        }
    }
    if (useCache) {
        // Cache the discovered information
        services.put(feature, serviceDiscoInfo);
    }
    return serviceDiscoInfo;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 4 with DiscoverItems

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

the class STUN method serviceAvailable.

/**
     * Check if the server support STUN Service.
     *
     * @param connection the connection
     * @return true if the server support STUN
     * @throws SmackException 
     * @throws XMPPException 
     * @throws InterruptedException 
     */
public static boolean serviceAvailable(XMPPConnection connection) throws XMPPException, SmackException, InterruptedException {
    if (!connection.isConnected()) {
        return false;
    }
    LOGGER.fine("Service listing");
    ServiceDiscoveryManager disco = ServiceDiscoveryManager.getInstanceFor(connection);
    DiscoverItems items = disco.discoverItems(connection.getXMPPServiceDomain());
    for (DiscoverItems.Item item : items.getItems()) {
        DiscoverInfo info = disco.discoverInfo(item.getEntityID());
        for (DiscoverInfo.Identity identity : info.getIdentities()) {
            if (identity.getCategory().equals("proxy") && identity.getType().equals("stun"))
                if (info.containsFeature(NAMESPACE))
                    return true;
        }
        LOGGER.fine(item.getName() + "-" + info.getType());
    }
    return false;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager)

Example 5 with DiscoverItems

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

the class OfflineMessageManager method getHeaders.

/**
     * Returns a List of <tt>OfflineMessageHeader</tt> that keep information about the
     * offline message. The OfflineMessageHeader includes a stamp that could be used to retrieve
     * the complete message or delete the specific message.
     *
     * @return a List of <tt>OfflineMessageHeader</tt> that keep information about the offline
     *         message.
     * @throws XMPPErrorException If the user is not allowed to make this request or the server does
     *                       not support offline message retrieval.
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public List<OfflineMessageHeader> getHeaders() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    List<OfflineMessageHeader> answer = new ArrayList<OfflineMessageHeader>();
    DiscoverItems items = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems(null, namespace);
    for (DiscoverItems.Item item : items.getItems()) {
        answer.add(new OfflineMessageHeader(item));
    }
    return answer;
}
Also used : ArrayList(java.util.ArrayList) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems)

Aggregations

DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)22 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)12 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)8 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)8 Test (org.junit.Test)8 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)7 IOException (java.io.IOException)6 ConnectException (java.net.ConnectException)6 SmackException (org.jivesoftware.smack.SmackException)6 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)6 XMPPException (org.jivesoftware.smack.XMPPException)6 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)6 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)6 ArrayList (java.util.ArrayList)5 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)3 IQ (org.jivesoftware.smack.packet.IQ)3 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)3 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)3 Jid (org.jxmpp.jid.Jid)3 InputStream (java.io.InputStream)2