/** * 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()); } }
/** * Discovers {@link DiscoveryInfo} and {@link DiscoveryInfo.Identity} of a gateway * and creates a {@link Gateway} object representing this gateway. * @param itemJID * @throws XMPPException */ private void discoverGateway(String itemJID) throws XMPPException{ DiscoverInfo info = sdManager.discoverInfo(itemJID); Iterator<Identity> i = info.getIdentities(); while(i.hasNext()){ Identity identity = i.next(); String category = identity.getCategory(); if(category.toLowerCase().equals("gateway")){ gateways.put(itemJID, new Gateway(connection,itemJID)); if(itemJID.contains(connection.getHost())){ localGateways.put(itemJID, new Gateway(connection,itemJID,info,identity)); } else{ nonLocalGateways.put(itemJID, new Gateway(connection,itemJID,info,identity)); } break; } } }
/** * Discovers {@link DiscoveryInfo} and {@link DiscoveryInfo.Identity} of a * gateway and creates a {@link Gateway} object representing this gateway. * * @param itemJID * @throws XMPPException */ private void discoverGateway(String itemJID) throws XMPPException { DiscoverInfo info = sdManager.discoverInfo(itemJID); Iterator<Identity> i = info.getIdentities(); while (i.hasNext()) { Identity identity = i.next(); String category = identity.getCategory(); if (category.toLowerCase().equals("gateway")) { gateways.put(itemJID, new Gateway(connection, itemJID)); if (itemJID.contains(connection.getHost())) { localGateways.put(itemJID, new Gateway(connection, itemJID, info, identity)); } else { nonLocalGateways.put(itemJID, new Gateway(connection, itemJID, info, identity)); } break; } } }
/** * 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", ServiceDiscoveryManager.getIdentityName(), identity.getName()); assertEquals("Category in identity is wrong", "client", identity.getCategory()); assertEquals("Type in identity is wrong", ServiceDiscoveryManager.getIdentityType(), identity.getType()); assertFalse("More identities were found", identities.hasNext()); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
public void testDiscoverNodeInfo() throws Exception { LeafNode myNode = getManager().createNode("DiscoNode" + System.currentTimeMillis()); DiscoverInfo info = myNode.discoverInfo(); assertTrue(info.getIdentities().hasNext()); Identity ident = info.getIdentities().next(); assertEquals("leaf", ident.getType()); }
private void discoverInfo() throws XMPPException{ info = sdManager.discoverInfo(entityJID); Iterator<Identity> iterator = info.getIdentities(); while(iterator.hasNext()){ Identity temp = iterator.next(); if(temp.getCategory().equalsIgnoreCase("gateway")){ this.identity = temp; break; } } }
/** * Returns all identities of this client as unmodifiable Collection * * @return */ public Set<DiscoverInfo.Identity> getIdentities() { Set<Identity> res = new HashSet<Identity>(identities); // Add the default identity that must exist res.add(defaultIdentity); return Collections.unmodifiableSet(res); }
private void discoverInfo() throws XMPPException { info = sdManager.discoverInfo(entityJID); Iterator<Identity> iterator = info.getIdentities(); while (iterator.hasNext()) { Identity temp = iterator.next(); if (temp.getCategory().equalsIgnoreCase("gateway")) { this.identity = temp; break; } } }
/** * Returns the name of the client that will be returned when asked for the client identity * in a disco request. The name could be any value you need to identity this client. * * @return the name of the client that will be returned when asked for the client identity * in a disco request. */ public static String getIdentityName() { DiscoverInfo.Identity identity = identities.get(0); if (identity != null) { return identity.getName(); } else { return null; } }
/** * Returns the type of client that will be returned when asked for the client identity in a * disco request. The valid types are defined by the category client. Follow this link to learn * the possible types: <a href="http://xmpp.org/registrar/disco-categories.html#client">Jabber::Registrar</a>. * * @return the type of client that will be returned when asked for the client identity in a * disco request. */ public static String getIdentityType() { DiscoverInfo.Identity identity = identities.get(0); if (identity != null) { return identity.getType(); } else { return null; } }
/** * Sets the type of client that will be returned when asked for the client identity in a * disco request. The valid types are defined by the category client. Follow this link to learn * the possible types: <a href="http://xmpp.org/registrar/disco-categories.html#client">Jabber::Registrar</a>. * * @param type the type of client that will be returned when asked for the client identity in a * disco request. */ public static void setIdentityType(String type) { DiscoverInfo.Identity identity = identities.get(0); if (identity != null) { identity.setType(type); } else { identity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, type); identities.add(identity); } }
/** * 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; }
private AdHocCommandManager(Connection connection) { this.connection = new WeakReference<Connection>(connection); this.serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); // Register the new instance and associate it with the connection instances.put(connection, this); // Add the feature to the service discovery manage to show that this // connection supports the AdHoc-Commands protocol. // This information will be used when another client tries to // discover whether this client supports AdHoc-Commands or not. ServiceDiscoveryManager.getInstanceFor(connection).addFeature( DISCO_NAMESPACE); // Set the NodeInformationProvider that will provide information about // which AdHoc-Commands are registered, whenever a disco request is // received ServiceDiscoveryManager.getInstanceFor(connection) .setNodeInformationProvider(discoNode, new NodeInformationProvider() { public List<DiscoverItems.Item> getNodeItems() { List<DiscoverItems.Item> answer = new ArrayList<DiscoverItems.Item>(); Collection<AdHocCommandInfo> commandsList = getRegisteredCommands(); for (AdHocCommandInfo info : commandsList) { DiscoverItems.Item item = new DiscoverItems.Item( info.getOwnerJID()); item.setName(info.getName()); item.setNode(info.getNode()); answer.add(item); } return answer; } public List<String> getNodeFeatures() { return null; } public List<Identity> getNodeIdentities() { return null; } @Override public List<PacketExtension> getNodePacketExtensions() { return null; } }); // The packet listener and the filter for processing some AdHoc Commands // Packets PacketListener listener = new PacketListener() { public void processPacket(Packet packet) { AdHocCommandData requestData = (AdHocCommandData) packet; processAdHocCommand(requestData); } }; PacketFilter filter = new PacketTypeFilter(AdHocCommandData.class); connection.addPacketListener(listener, filter); sessionsSweeper = null; }
Gateway(Connection connection, String entityJID, DiscoverInfo info, Identity identity){ this(connection, entityJID); this.info = info; this.identity = identity; }
private Identity getIdentity() throws XMPPException{ if(identity==null){ discoverInfo(); } return identity; }
/** * 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")); final List<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getInstanceFor(connection).getIdentities()); sdm.setNodeInformationProvider(ENTITY_NODE + '#' + currentCapsVersion, new NodeInformationProvider() { List<String> features = sdm.getFeaturesList(); List<PacketExtension> packetExtensions = sdm.getExtendedInfoAsList(); @Override public List<Item> getNodeItems() { return null; } @Override public List<String> getNodeFeatures() { return features; } @Override public List<Identity> getNodeIdentities() { return identities; } @Override public List<PacketExtension> getNodePacketExtensions() { return packetExtensions; } }); // Send an empty presence, and let the packet intercepter // add a <c/> node to it. // See http://xmpp.org/extensions/xep-0115.html#advertise // We only send a presence packet if there was already one send // to respect ConnectionConfiguration.isSendPresence() if (connection != null && connection.isAuthenticated() && presenceSend) { Presence presence = new Presence(Presence.Type.available); connection.sendPacket(presence); } }
Gateway(Connection connection, String entityJID, DiscoverInfo info, Identity identity) { this(connection, entityJID); this.info = info; this.identity = identity; }
private Identity getIdentity() throws XMPPException { if (identity == null) { discoverInfo(); } return identity; }
/** * 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; }
/** * 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(); @Override public List<Item> getNodeItems() { return null; } @Override public List<String> getNodeFeatures() { return features; } @Override public List<Identity> getNodeIdentities() { return identities; } @Override public List<PacketExtension> getNodePacketExtensions() { return packetExtensions; } }); // Send an empty presence, and let the packet intercepter // add a <c/> node to it. // See http://xmpp.org/extensions/xep-0115.html#advertise // We only send a presence packet if there was already one send // to respect ConnectionConfiguration.isSendPresence() if (connection != null && connection.isAuthenticated() && presenceSend) { Presence presence = new Presence(Presence.Type.available); connection.sendPacket(presence); } }
/** * Remove an identity from the client. Note that the client needs at least one identity, the default identity, which * can not be removed. * * @param identity * @return true, if successful. Otherwise the default identity was given. */ public boolean removeIdentity(DiscoverInfo.Identity identity) { if (identity.equals(this.identity)) return false; identities.remove(identity); renewEntityCapsVersion(); return true; }
/** * Set the default identity all new connections will have. If unchanged the default identity is an * identity where category is set to 'client', type is set to 'pc' and name is set to 'Smack'. * * @param identity */ public static void setDefaultIdentity(DiscoverInfo.Identity identity) { defaultIdentity = identity; }
/** * Add an identity to the client. * * @param identity */ public void addIdentity(DiscoverInfo.Identity identity) { identities.add(identity); renewEntityCapsVersion(); }
/** * Sets the name of the client that will be returned when asked for the client identity * in a disco request. The name could be any value you need to identity this client. * * @param name the name of the client that will be returned when asked for the client identity * in a disco request. */ public static void setIdentityName(String name) { DiscoverInfo.Identity identity = identities.remove(0); identity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, name, DEFAULT_IDENTITY_TYPE); identities.add(identity); }