use of org.jivesoftware.smackx.packet.DiscoverInfo in project ecf by eclipse.
the class Socks5BytestreamManager method determineProxies.
/**
* Returns a list of JIDs of SOCKS5 proxies by querying the XMPP server. The SOCKS5 proxies are
* in the same order as returned by the XMPP server.
*
* @return list of JIDs of SOCKS5 proxies
* @throws XMPPException if there was an error querying the XMPP server for SOCKS5 proxies
*/
private List<String> determineProxies() throws XMPPException {
ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(this.connection);
List<String> proxies = new ArrayList<String>();
// get all items form XMPP server
DiscoverItems discoverItems = serviceDiscoveryManager.discoverItems(this.connection.getServiceName());
Iterator<Item> itemIterator = discoverItems.getItems();
// query all items if they are SOCKS5 proxies
while (itemIterator.hasNext()) {
Item item = itemIterator.next();
// skip blacklisted servers
if (this.proxyBlacklist.contains(item.getEntityID())) {
continue;
}
try {
DiscoverInfo proxyInfo;
proxyInfo = serviceDiscoveryManager.discoverInfo(item.getEntityID());
Iterator<Identity> identities = proxyInfo.getIdentities();
// item must have category "proxy" and type "bytestream"
while (identities.hasNext()) {
Identity identity = identities.next();
if ("proxy".equalsIgnoreCase(identity.getCategory()) && "bytestreams".equalsIgnoreCase(identity.getType())) {
proxies.add(item.getEntityID());
break;
}
/*
* server is not a SOCKS5 proxy, blacklist server to skip next time a Socks5
* bytestream should be established
*/
this.proxyBlacklist.add(item.getEntityID());
}
} catch (XMPPException e) {
// blacklist errornous server
this.proxyBlacklist.add(item.getEntityID());
}
}
return proxies;
}
use of org.jivesoftware.smackx.packet.DiscoverInfo in project ecf by eclipse.
the class MultipleRecipientManager method getMultipleRecipienServiceAddress.
/**
* Returns the address of the multiple recipients service. To obtain such address service
* discovery is going to be used on the connected server and if none was found then another
* attempt will be tried on the server items. The discovered information is going to be
* cached for 24 hours.
*
* @param connection the connection to use for disco. The connected server is going to be
* queried.
* @return the address of the multiple recipients service or <tt>null</tt> if none was found.
*/
private static String getMultipleRecipienServiceAddress(Connection connection) {
String serviceName = connection.getServiceName();
String serviceAddress = (String) services.get(serviceName);
if (serviceAddress == null) {
synchronized (services) {
serviceAddress = (String) services.get(serviceName);
if (serviceAddress == null) {
// Send the disco packet to the server itself
try {
DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(serviceName);
// Check if the server supports JEP-33
if (info.containsFeature("http://jabber.org/protocol/address")) {
serviceAddress = serviceName;
} else {
// Get the disco items and send the disco packet to each server item
DiscoverItems items = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems(serviceName);
for (Iterator<DiscoverItems.Item> it = items.getItems(); it.hasNext(); ) {
DiscoverItems.Item item = it.next();
info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(item.getEntityID(), item.getNode());
if (info.containsFeature("http://jabber.org/protocol/address")) {
serviceAddress = serviceName;
break;
}
}
}
// Cache the discovered information
services.put(serviceName, serviceAddress == null ? "" : serviceAddress);
} catch (XMPPException e) {
e.printStackTrace();
}
}
}
}
return "".equals(serviceAddress) ? null : serviceAddress;
}
use of org.jivesoftware.smackx.packet.DiscoverInfo in project ecf by eclipse.
the class OfflineMessageManager method getMessageCount.
/**
* Returns the number of offline messages for the user of the connection.
*
* @return the number of offline messages for the user of the connection.
* @throws XMPPException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
*/
public int getMessageCount() throws XMPPException {
DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(null, namespace);
Form extendedInfo = Form.getFormFrom(info);
if (extendedInfo != null) {
String value = extendedInfo.getField("number_of_messages").getValues().next();
return Integer.parseInt(value);
}
return 0;
}
use of org.jivesoftware.smackx.packet.DiscoverInfo in project ecf by eclipse.
the class ServiceDiscoveryManager method init.
/**
* Initializes the packet listeners of the connection that will answer to any
* service discovery request.
*/
private void init() {
// Register the new instance and associate it with the connection
instances.put(connection, this);
addFeature(DiscoverInfo.NAMESPACE);
addFeature(DiscoverItems.NAMESPACE);
// Add a listener to the connection that removes the registered instance when
// the connection is closed
connection.addConnectionListener(new ConnectionListener() {
public void connectionClosed() {
// Unregister this instance since the connection has been closed
instances.remove(connection);
}
public void connectionClosedOnError(Exception e) {
// ignore
}
public void reconnectionFailed(Exception e) {
// ignore
}
public void reconnectingIn(int seconds) {
// ignore
}
public void reconnectionSuccessful() {
// ignore
}
});
// Listen for disco#items requests and answer with an empty result
PacketFilter packetFilter = new PacketTypeFilter(DiscoverItems.class);
PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
DiscoverItems discoverItems = (DiscoverItems) packet;
// Send back the items defined in the client if the request is of type GET
if (discoverItems != null && discoverItems.getType() == IQ.Type.GET) {
DiscoverItems response = new DiscoverItems();
response.setType(IQ.Type.RESULT);
response.setTo(discoverItems.getFrom());
response.setPacketID(discoverItems.getPacketID());
response.setNode(discoverItems.getNode());
// Add the defined items related to the requested node. Look for
// the NodeInformationProvider associated with the requested node.
NodeInformationProvider nodeInformationProvider = getNodeInformationProvider(discoverItems.getNode());
if (nodeInformationProvider != null) {
// Specified node was found, add node items
response.addItems(nodeInformationProvider.getNodeItems());
// Add packet extensions
response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
} else if (discoverItems.getNode() != null) {
// Return <item-not-found/> error since client doesn't contain
// the specified node
response.setType(IQ.Type.ERROR);
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
}
connection.sendPacket(response);
}
}
};
connection.addPacketListener(packetListener, packetFilter);
// Listen for disco#info requests and answer the client's supported features
// To add a new feature as supported use the #addFeature message
packetFilter = new PacketTypeFilter(DiscoverInfo.class);
packetListener = new PacketListener() {
public void processPacket(Packet packet) {
DiscoverInfo discoverInfo = (DiscoverInfo) packet;
// Answer the client's supported features if the request is of the GET type
if (discoverInfo != null && discoverInfo.getType() == IQ.Type.GET) {
DiscoverInfo response = new DiscoverInfo();
response.setType(IQ.Type.RESULT);
response.setTo(discoverInfo.getFrom());
response.setPacketID(discoverInfo.getPacketID());
response.setNode(discoverInfo.getNode());
// if the right node is chosen
if (discoverInfo.getNode() == null) {
addDiscoverInfoTo(response);
} else {
// Disco#info was sent to a node. Check if we have information of the
// specified node
NodeInformationProvider nodeInformationProvider = getNodeInformationProvider(discoverInfo.getNode());
if (nodeInformationProvider != null) {
// Node was found. Add node features
response.addFeatures(nodeInformationProvider.getNodeFeatures());
// Add node identities
response.addIdentities(nodeInformationProvider.getNodeIdentities());
// Add packet extensions
response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
} else {
// Return <item-not-found/> error since specified node was not found
response.setType(IQ.Type.ERROR);
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
}
}
connection.sendPacket(response);
}
}
};
connection.addPacketListener(packetListener, packetFilter);
}
use of org.jivesoftware.smackx.packet.DiscoverInfo in project Smack by igniterealtime.
the class ServiceDiscoveryManagerTest method testSmackInfo.
/**
* Tests info discovery of a Smack client.
*/
public void testSmackInfo() {
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(getConnection(0));
try {
// Discover the information of another Smack client
DiscoverInfo info = discoManager.discoverInfo(getFullJID(1));
// Check the identity of the Smack client
Iterator<Identity> identities = info.getIdentities();
assertTrue("No identities were found", identities.hasNext());
Identity identity = identities.next();
assertEquals("Name in identity is wrong", discoManager.getIdentityName(), identity.getName());
assertEquals("Category in identity is wrong", "client", identity.getCategory());
assertEquals("Type in identity is wrong", discoManager.getIdentityType(), identity.getType());
assertFalse("More identities were found", identities.hasNext());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations