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;
}
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);
}
}
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);
}
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);
}
}
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;
}
Aggregations