Search in sources :

Example 1 with DiscoverInfo

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

the class UserSearchManager method getSearchServices.

/**
 * Returns a collection of search services found on the server.
 *
 * @return a Collection of search services found on the server.
 * @throws XMPPException thrown if a server error has occurred.
 */
public Collection<String> getSearchServices() throws XMPPException {
    final List<String> searchServices = new ArrayList<String>();
    ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
    DiscoverItems items = discoManager.discoverItems(con.getServiceName());
    Iterator<DiscoverItems.Item> iter = items.getItems();
    while (iter.hasNext()) {
        DiscoverItems.Item item = iter.next();
        try {
            DiscoverInfo info;
            try {
                info = discoManager.discoverInfo(item.getEntityID());
            } catch (XMPPException e) {
                // Ignore Case
                continue;
            }
            if (info.containsFeature("jabber:iq:search")) {
                searchServices.add(item.getEntityID());
            }
        } catch (Exception e) {
            // No info found.
            break;
        }
    }
    return searchServices;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) ArrayList(java.util.ArrayList) DiscoverItems(org.jivesoftware.smackx.packet.DiscoverItems) XMPPException(org.jivesoftware.smack.XMPPException) XMPPException(org.jivesoftware.smack.XMPPException) ServiceDiscoveryManager(org.jivesoftware.smackx.ServiceDiscoveryManager)

Example 2 with DiscoverInfo

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

the class EntityCapsManager method updateLocalEntityCaps.

/**
 * Updates the local user Entity Caps information with the data provided
 *
 * If we are connected and there was already a presence send, another
 * presence is send to inform others about your new Entity Caps node string.
 *
 * @param discoverInfo
 *            the local users discover info (mostly the service discovery
 *            features)
 * @param identityType
 *            the local users identity type
 * @param identityName
 *            the local users identity name
 * @param extendedInfo
 *            the local users extended info
 */
public void updateLocalEntityCaps() {
    Connection connection = weakRefConnection.get();
    DiscoverInfo discoverInfo = new DiscoverInfo();
    discoverInfo.setType(IQ.Type.RESULT);
    discoverInfo.setNode(getLocalNodeVer());
    if (connection != null)
        discoverInfo.setFrom(connection.getUser());
    sdm.addDiscoverInfoTo(discoverInfo);
    currentCapsVersion = generateVerificationString(discoverInfo, "sha-1");
    addDiscoverInfoByNode(ENTITY_NODE + '#' + currentCapsVersion, discoverInfo);
    if (lastLocalCapsVersions.size() > 10) {
        String oldCapsVersion = lastLocalCapsVersions.poll();
        sdm.removeNodeInformationProvider(ENTITY_NODE + '#' + oldCapsVersion);
    }
    lastLocalCapsVersions.add(currentCapsVersion);
    caps.put(currentCapsVersion, discoverInfo);
    if (connection != null)
        jidCaps.put(connection.getUser(), new NodeVerHash(ENTITY_NODE, currentCapsVersion, "sha-1"));
    sdm.setNodeInformationProvider(ENTITY_NODE + '#' + currentCapsVersion, new NodeInformationProvider() {

        List<String> features = sdm.getFeaturesList();

        List<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getIdentities());

        List<PacketExtension> packetExtensions = sdm.getExtendedInfoAsList();

        public List<Item> getNodeItems() {
            return null;
        }

        public List<String> getNodeFeatures() {
            return features;
        }

        public List<Identity> getNodeIdentities() {
            return identities;
        }

        public List<PacketExtension> getNodePacketExtensions() {
            return packetExtensions;
        }
    });
    // to respect ConnectionConfiguration.isSendPresence()
    if (connection != null && connection.isAuthenticated() && presenceSend) {
        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);
    }
}
Also used : PacketExtension(org.jivesoftware.smack.packet.PacketExtension) DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) NodeInformationProvider(org.jivesoftware.smackx.NodeInformationProvider) Connection(org.jivesoftware.smack.Connection) XMPPConnection(org.jivesoftware.smack.XMPPConnection) LinkedList(java.util.LinkedList) Presence(org.jivesoftware.smack.packet.Presence) LinkedList(java.util.LinkedList) List(java.util.List) Identity(org.jivesoftware.smackx.packet.DiscoverInfo.Identity)

Example 3 with DiscoverInfo

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

the class EntityCapsManager method generateVerificationString.

/**
 * Generates a XEP-115 Verification String
 *
 * @see <a href="http://xmpp.org/extensions/xep-0115.html#ver">XEP-115
 *      Verification String</a>
 *
 * @param discoverInfo
 * @param hash
 *            the used hash function
 * @return The generated verification String or null if the hash is not
 *         supported
 */
protected static String generateVerificationString(DiscoverInfo discoverInfo, String hash) {
    MessageDigest md = SUPPORTED_HASHES.get(hash.toLowerCase());
    if (md == null)
        return null;
    DataForm extendedInfo = (DataForm) discoverInfo.getExtension(Form.ELEMENT, Form.NAMESPACE);
    // 1. Initialize an empty string S ('sb' in this method).
    // Use StringBuilder as we don't
    StringBuilder sb = new StringBuilder();
    // need thread-safe StringBuffer
    // 2. Sort the service discovery identities by category and then by
    // type and then by xml:lang
    // (if it exists), formatted as CATEGORY '/' [TYPE] '/' [LANG] '/'
    // [NAME]. Note that each slash is included even if the LANG or
    // NAME is not included (in accordance with XEP-0030, the category and
    // type MUST be included.
    SortedSet<DiscoverInfo.Identity> sortedIdentities = new TreeSet<DiscoverInfo.Identity>();
    for (Iterator<DiscoverInfo.Identity> it = discoverInfo.getIdentities(); it.hasNext(); ) sortedIdentities.add(it.next());
    // followed by the '<' character.
    for (Iterator<DiscoverInfo.Identity> it = sortedIdentities.iterator(); it.hasNext(); ) {
        DiscoverInfo.Identity identity = it.next();
        sb.append(identity.getCategory());
        sb.append("/");
        sb.append(identity.getType());
        sb.append("/");
        sb.append(identity.getLanguage() == null ? "" : identity.getLanguage());
        sb.append("/");
        sb.append(identity.getName() == null ? "" : identity.getName());
        sb.append("<");
    }
    // 4. Sort the supported service discovery features.
    SortedSet<String> features = new TreeSet<String>();
    for (Iterator<Feature> it = discoverInfo.getFeatures(); it.hasNext(); ) features.add(it.next().getVar());
    // character
    for (String f : features) {
        sb.append(f);
        sb.append("<");
    }
    // see XEP-0115 5.4 step 3.6
    if (extendedInfo != null && extendedInfo.hasHiddenFormTypeField()) {
        synchronized (extendedInfo) {
            // 6. If the service discovery information response includes
            // XEP-0128 data forms, sort the forms by the FORM_TYPE (i.e.,
            // by the XML character data of the <value/> element).
            SortedSet<FormField> fs = new TreeSet<FormField>(new Comparator<FormField>() {

                public int compare(FormField f1, FormField f2) {
                    return f1.getVariable().compareTo(f2.getVariable());
                }
            });
            FormField ft = null;
            for (Iterator<FormField> i = extendedInfo.getFields(); i.hasNext(); ) {
                FormField f = i.next();
                if (!f.getVariable().equals("FORM_TYPE")) {
                    fs.add(f);
                } else {
                    ft = f;
                }
            }
            // Add FORM_TYPE values
            if (ft != null) {
                formFieldValuesToCaps(ft.getValues(), sb);
            }
            // followed by the '<' character.
            for (FormField f : fs) {
                sb.append(f.getVariable());
                sb.append("<");
                formFieldValuesToCaps(f.getValues(), sb);
            }
        }
    }
    // 8. Ensure that S is encoded according to the UTF-8 encoding (RFC
    // 3269).
    // 9. Compute the verification string by hashing S using the algorithm
    // specified in the 'hash' attribute (e.g., SHA-1 as defined in RFC
    // 3174).
    // The hashed data MUST be generated with binary output and
    // encoded using Base64 as specified in Section 4 of RFC 4648
    // (note: the Base64 output MUST NOT include whitespace and MUST set
    // padding bits to zero).
    byte[] digest = md.digest(sb.toString().getBytes());
    return Base64.encodeBytes(digest);
}
Also used : DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) Feature(org.jivesoftware.smackx.packet.DiscoverInfo.Feature) Identity(org.jivesoftware.smackx.packet.DiscoverInfo.Identity) TreeSet(java.util.TreeSet) DataForm(org.jivesoftware.smackx.packet.DataForm) MessageDigest(java.security.MessageDigest) Identity(org.jivesoftware.smackx.packet.DiscoverInfo.Identity) FormField(org.jivesoftware.smackx.FormField)

Example 4 with DiscoverInfo

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

the class SimpleDirectoryPersistentCache method replay.

public void replay() throws IOException {
    File[] files = cacheDir.listFiles();
    for (File f : files) {
        String node = filenameEncoder.decode(f.getName());
        DiscoverInfo info = restoreInfoFromFile(f);
        if (info == null)
            continue;
        EntityCapsManager.addDiscoverInfoByNode(node, info);
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) File(java.io.File)

Example 5 with DiscoverInfo

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

the class SimpleDirectoryPersistentCache method restoreInfoFromFile.

/**
 * Tries to restore an DiscoverInfo packet from a file.
 *
 * @param file
 * @return
 * @throws IOException
 */
private static DiscoverInfo restoreInfoFromFile(File file) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream(file));
    String fileContent = null;
    String id;
    String from;
    String to;
    try {
        fileContent = dis.readUTF();
    } finally {
        dis.close();
    }
    if (fileContent == null)
        return null;
    Reader reader = new StringReader(fileContent);
    XmlPullParser parser;
    try {
        parser = new MXParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        parser.setInput(reader);
    } catch (XmlPullParserException xppe) {
        xppe.printStackTrace();
        return null;
    }
    DiscoverInfo iqPacket;
    IQProvider provider = new DiscoverInfoProvider();
    // Parse the IQ, we only need the id
    try {
        parser.next();
        id = parser.getAttributeValue("", "id");
        from = parser.getAttributeValue("", "from");
        to = parser.getAttributeValue("", "to");
        parser.next();
    } catch (XmlPullParserException e1) {
        return null;
    }
    try {
        iqPacket = (DiscoverInfo) provider.parseIQ(parser);
    } catch (Exception e) {
        return null;
    }
    iqPacket.setPacketID(id);
    iqPacket.setFrom(from);
    iqPacket.setTo(to);
    iqPacket.setType(IQ.Type.RESULT);
    return iqPacket;
}
Also used : DiscoverInfo(org.jivesoftware.smackx.packet.DiscoverInfo) MXParser(org.xmlpull.mxp1.MXParser) DiscoverInfoProvider(org.jivesoftware.smackx.provider.DiscoverInfoProvider) StringReader(java.io.StringReader) XmlPullParser(org.xmlpull.v1.XmlPullParser) Reader(java.io.Reader) StringReader(java.io.StringReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IQProvider(org.jivesoftware.smack.provider.IQProvider)

Aggregations

DiscoverInfo (org.jivesoftware.smackx.packet.DiscoverInfo)20 XMPPException (org.jivesoftware.smack.XMPPException)6 ServiceDiscoveryManager (org.jivesoftware.smackx.ServiceDiscoveryManager)5 Identity (org.jivesoftware.smackx.packet.DiscoverInfo.Identity)5 DiscoverItems (org.jivesoftware.smackx.packet.DiscoverItems)5 ArrayList (java.util.ArrayList)3 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 MessageDigest (java.security.MessageDigest)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 Connection (org.jivesoftware.smack.Connection)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)1 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)1