private void findMUCLightName(Chat chat) { if (XMPPSession.getInstance().getXMPPConnection().isAuthenticated()) { DiscoverItems discoverItems = XMPPSession.getInstance().discoverMUCLightItems(); if (discoverItems != null) { List<DiscoverItems.Item> items = discoverItems.getItems(); for (DiscoverItems.Item item : items) { String itemJid = item.getEntityID().toString(); if (itemJid.equals(chat.getJid())) { chat.setName(item.getName()); } } } } }
private List<String> discoverItemsByFeature(XmppURI uri, List<String> features) throws XMPPException, IOException, SmackException { // discover items ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(this.connectionManager.getConnection()); DiscoverItems discoverItems = discoveryManager.discoverItems(uri.getDomain()); List<DiscoverItems.Item> items = discoverItems.getItems(); List<String> result = new ArrayList<>(); // discover infos per item and check if specified feature set is supported for (DiscoverItems.Item item : items) { DiscoverInfo discoverInfo = discoveryManager.discoverInfo(item.getEntityID()); boolean conatinsAllFeatures = true; for (String feature : features) { if (!discoverInfo.containsFeature(feature)) { conatinsAllFeatures = false; break; } } if (conatinsAllFeatures) { result.add(item.getEntityID()); } else if (logger.isDebugEnabled()) { logger.debug("Entity {} does not support the specified features.", item.getEntityID()); } } return result; }
@Override public void processStanza(Stanza packet) { XMPPConnection conn = getConnection(); // we don't need this listener anymore conn.removeAsyncStanzaListener(this); DiscoverItems query = (DiscoverItems) packet; List<DiscoverItems.Item> items = query.getItems(); for (DiscoverItems.Item item : items) { DiscoverInfo info = new DiscoverInfo(); info.setTo(item.getEntityID()); StanzaFilter filter = new StanzaIdFilter(info.getStanzaId()); conn.addAsyncStanzaListener(new DiscoverInfoListener(getInstance()), filter); sendPacket(info); } }
public DiscoverItems discoverMUCLightItems() { DiscoverItems discoverItems = null; try { discoverItems = ServiceDiscoveryManager.getInstanceFor(mXMPPConnection).discoverItems(JidCreate.from(MUC_LIGHT_SERVICE_NAME)); } catch (Exception e) { e.printStackTrace(); } return discoverItems; }
/** * 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. */ public void publishCommands(String jid) throws XMPPException, SmackException { // 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); }
/** * Returns a List of HostedRooms where each HostedRoom has the XMPP address of the room and the room's name. * Once discovered the rooms hosted by a chat service it is possible to discover more detailed room information or * join the room. * * @param serviceName the service that is hosting the rooms to discover. * @return a collection of HostedRooms. * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException */ public List<HostedRoom> getHostedRooms(String serviceName) throws NoResponseException, XMPPErrorException, NotConnectedException { ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection()); DiscoverItems discoverItems = discoManager.discoverItems(serviceName); List<DiscoverItems.Item> items = discoverItems.getItems(); List<HostedRoom> answer = new ArrayList<HostedRoom>(items.size()); for (DiscoverItems.Item item : items) { answer.add(new HostedRoom(item)); } return answer; }
/** * 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 XMPPErrorException if there was an error querying the XMPP server for SOCKS5 proxies * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ private List<String> determineProxies() throws NoResponseException, XMPPErrorException, NotConnectedException { XMPPConnection connection = connection(); ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); List<String> proxies = new ArrayList<String>(); // get all items from XMPP server DiscoverItems discoverItems = serviceDiscoveryManager.discoverItems(connection.getServiceName()); // query all items if they are SOCKS5 proxies for (Item item : discoverItems.getItems()) { // skip blacklisted servers if (this.proxyBlacklist.contains(item.getEntityID())) { continue; } DiscoverInfo proxyInfo; try { proxyInfo = serviceDiscoveryManager.discoverInfo(item.getEntityID()); } catch (NoResponseException|XMPPErrorException e) { // blacklist errornous server proxyBlacklist.add(item.getEntityID()); continue; } if (proxyInfo.hasIdentity("proxy", "bytestreams")) { proxies.add(item.getEntityID()); } else { /* * server is not a SOCKS5 proxy, blacklist server to skip next time a Socks5 * bytestream should be established */ this.proxyBlacklist.add(item.getEntityID()); } } return proxies; }
@Override public DiscoverItems parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { DiscoverItems discoverItems = new DiscoverItems(); boolean done = false; DiscoverItems.Item item; String jid = ""; 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 = parser.getAttributeValue("", "jid"); 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; }
/** * Returns a response to an item discovery request. The stanza(/packet) doesn't contain any items. * * @param from the XMPP server * @param to the XMPP client * @return response to an item discovery request */ public static DiscoverItems createDiscoverItems(String from, String to) { DiscoverItems discoverItems = new DiscoverItems(); discoverItems.setFrom(from); discoverItems.setTo(to); discoverItems.setType(IQ.Type.result); return discoverItems; }
/** * Check if the server support STUN Service. * * @param connection the connection * @return true if the server support STUN * @throws SmackException * @throws XMPPException */ public static boolean serviceAvailable(XMPPConnection connection) throws XMPPException, SmackException { if (!connection.isConnected()) { return false; } LOGGER.fine("Service listing"); ServiceDiscoveryManager disco = ServiceDiscoveryManager.getInstanceFor(connection); DiscoverItems items = disco.discoverItems(connection.getServiceName()); 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; }
private void discover() throws XMPPErrorException, URISyntaxException, SmackException, XmlException { System.out.println("discover root services"); // Obtain the ServiceDiscoveryManager associated with my XMPPConnection ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection); // Get the items of a given XMPP entity // This gets the items associated with online catalog service DiscoverItems discoItems = discoManager.discoverItems(TEST_DOMAIN); // Get the discovered items of the queried XMPP entity List<Item> items = discoItems.getItems(); // set exchange and root server for(Item item : items) { System.out.println(item.getEntityID()); if(ServiceNames.RootComponentName.equals(item.getName())) rootURI = item.getEntityID(); else if(ServiceNames.ExchangeComponentName.equals(item.getName())) exchangeURI = item.getEntityID(); } // discover root features if(rootURI != null) { checkFeatures(discoManager, rootURI); } // discover exchange features if(exchangeURI != null) { checkFeatures(discoManager, exchangeURI); } }
@Override public void processStanza(Stanza packet) { // we don't need this listener anymore getConnection().removeAsyncStanzaListener(this); DiscoverItems query = (DiscoverItems) packet; List<DiscoverItems.Item> items = query.getItems(); for (DiscoverItems.Item item : items) { String jid = item.getEntityID().toString(); // google push notifications if (("gcm.push." + getServer().getNetwork()).equals(jid)) { String senderId = item.getNode(); setPushSenderId(senderId); if (isPushNotificationsEnabled()) { String oldSender = Preferences.getPushSenderId(); // store the new sender id Preferences.setPushSenderId(senderId); // begin a registration cycle if senderId is different if (oldSender != null && !oldSender.equals(senderId)) { IPushService service = PushServiceManager.getInstance(getContext()); if (service != null) service.unregister(getPushListener()); // unregister will see this as an attempt to register again startPushRegistrationCycle(); } else { // begin registration immediately pushRegister(); } } } } }
@Override public void processPacket(Packet packet) { // we don't need this listener anymore getConnection().removePacketListener(this); DiscoverItems query = (DiscoverItems) packet; List<DiscoverItems.Item> items = query.getItems(); for (DiscoverItems.Item item : items) { String jid = item.getEntityID(); // google push notifications if (("gcm.push." + getServer().getNetwork()).equals(jid)) { String senderId = item.getNode(); setPushSenderId(senderId); if (isPushNotificationsEnabled()) { String oldSender = Preferences.getPushSenderId(getContext()); // store the new sender id Preferences.setPushSenderId(getContext(), senderId); // begin a registration cycle if senderId is different if (oldSender != null && !oldSender.equals(senderId)) { PushServiceManager.getInstance(getContext()) .unregister(getPushListener()); // unregister will see this as an attempt to register again startPushRegistrationCycle(); } else { // begin registration immediately pushRegister(); } } } } }
@Override public void processPacket(Packet packet) { XMPPConnection conn = getConnection(); EndpointServer server = getServer(); // we don't need this listener anymore conn.removePacketListener(this); initUploadServices(); // store available services DiscoverItems query = (DiscoverItems) packet; List<DiscoverItems.Item> items = query.getItems(); for (DiscoverItems.Item item : items) { String jid = item.getEntityID(); if ((server.getNetwork()).equals(jid)) { setUploadService(item.getNode(), null); // request upload url UploadInfo iq = new UploadInfo(item.getNode()); iq.setType(IQ.Type.GET); iq.setTo(server.getNetwork()); conn.addPacketListener(new UploadInfoListener(getInstance()), new PacketIDFilter(iq.getPacketID())); sendPacket(iq); } } }
private EnumMap<Feature, JID> discover(JID entity, boolean withItems) { // NOTE: smack automatically creates instances of SDM and CapsM and connects them ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(mConn); // 1. get features from server EnumMap<Feature, JID> features = discover(discoManager, entity); if (features == null) return new EnumMap<>(FeatureDiscovery.Feature.class); if (!withItems) return features; // 2. get server items DiscoverItems items; try { items = discoManager.discoverItems(entity.toBareSmack()); } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException ex) { LOGGER.log(Level.WARNING, "can't get service discovery items", ex); return features; } // 3. get features from server items for (DiscoverItems.Item item: items.getItems()) { EnumMap<Feature, JID> itemFeatures = discover(discoManager, JID.fromSmack(item.getEntityID())); if (itemFeatures != null) features.putAll(itemFeatures); } LOGGER.info("supported server features: "+features); return features; }
public void loadMUCLightRooms() { final XMPPTCPConnection connection = XMPPSession.getInstance().getXMPPConnection(); if (connection.isAuthenticated()) { DiscoverItems discoverItems = XMPPSession.getInstance().discoverMUCLightItems(); if (discoverItems != null) { RealmManager.getInstance().hideAllMUCLightChats(); List<DiscoverItems.Item> items = discoverItems.getItems(); Realm realm = RealmManager.getInstance().getRealm(); try { for (DiscoverItems.Item item : items) { String itemJid = item.getEntityID().toString(); if (itemJid.contains(XMPPSession.MUC_LIGHT_SERVICE_NAME)) { Chat chatRoom = realm.where(Chat.class).equalTo("jid", itemJid).findFirst(); if (chatRoom == null) { chatRoom = new Chat(); chatRoom.setJid(item.getEntityID().toString()); chatRoom.setType(Chat.TYPE_MUC_LIGHT); getSubject(chatRoom); } realm.beginTransaction(); chatRoom.setShow(true); chatRoom.setName(item.getName()); realm.copyToRealmOrUpdate(chatRoom); realm.commitTransaction(); // set last retrieved from MAM ChatMessage chatMessage = RealmManager.getInstance().getFirstMessageForChat(chatRoom.getJid()); if (chatMessage != null) { realm.beginTransaction(); chatRoom.setLastRetrievedFromMAM(chatMessage.getMessageId()); realm.copyToRealmOrUpdate(chatRoom); realm.commitTransaction(); } } } } finally { realm.close(); mListener.onRoomsLoaded(); } } } }
protected CollectionViewActivity doInBackground(CollectionViewActivity... activity) { // Get the XMMPConnection from the manager AbstractXMPPConnection conn = XMPPConnectionManager.getConnection(); // Obtain the ServiceDiscoveryManager associated with my XMPP connection this.discoManager = ServiceDiscoveryManager.getInstanceFor(conn); DiscoverItems discoItems; // This example gets the items associated with online catalog service try { discoItems = this.discoManager.discoverItems( activity[0].currentEntity.getJid() , activity[0].currentEntity.getNode() ); } catch (Exception ex) { Log.w(LOGTAG, "XMPP Disco error " + ex); errorMessage = ex.getMessage() ; return activity[0]; } // Get the discovered items of the queried XMPP entity Iterator it = discoItems.getItems().iterator(); Log.i(LOGTAG, "Got items:" + it.toString()); this.entityList = new ArrayList<>(); this.entityList.clear(); // Display the items of the remote XMPP entity int id = 0; while (it.hasNext()) { DiscoverItems.Item item = (DiscoverItems.Item) it.next(); Entity entity = new Entity(item.getEntityID(), item.getNode() , item.getName() ); Log.i(LOGTAG, "Got item:" + item.getName() + " : " + item.getEntityID()); entity.setId(id); entity.setName(item.getName()); this.entityList.add(entity); id++; } Collections.sort(this.entityList); return activity[0]; }
private AdHocCommandManager(XMPPConnection connection) { super(connection); this.serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); // 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( NAMESPACE); // Set the NodeInformationProvider that will provide information about // which AdHoc-Commands are registered, whenever a disco request is // received ServiceDiscoveryManager.getInstanceFor(connection) .setNodeInformationProvider(NAMESPACE, new AbstractNodeInformationProvider() { @Override 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; } }); // The packet listener and the filter for processing some AdHoc Commands // Packets connection.registerIQRequestHandler(new AbstractIqRequestHandler(AdHocCommandData.ELEMENT, AdHocCommandData.NAMESPACE, IQ.Type.set, Mode.async) { @Override public IQ handleIQRequest(IQ iqRequest) { AdHocCommandData requestData = (AdHocCommandData) iqRequest; try { return processAdHocCommand(requestData); } catch (NoResponseException | NotConnectedException e) { LOGGER.log(Level.INFO, "processAdHocCommand threw exceptino", e); return null; } } }); sessionsSweeper = null; }
public HostedRoom(DiscoverItems.Item item) { jid = item.getEntityID(); name = item.getName(); }
public List<DiscoverItems.Item> getNodeItems() { return null; }
public OfflineMessageHeader(DiscoverItems.Item item) { super(); user = item.getEntityID(); jid = item.getName(); stamp = item.getNode(); }
@Override public void processStanza(Stanza packet) { XMPPConnection conn = getConnection(); EndpointServer server = getServer(); // we don't need this listener anymore conn.removeAsyncStanzaListener(this); DiscoverInfo query = (DiscoverInfo) packet; List<DiscoverInfo.Feature> features = query.getFeatures(); for (DiscoverInfo.Feature feat : features) { /* * TODO do not request info about push if disabled by user. * Of course if user enables push notification we should * reissue this discovery again. */ if (PushRegistration.NAMESPACE.equals(feat.getVar())) { // push notifications are enabled on this server // request items to check if gcm is supported and obtain the server id DiscoverItems items = new DiscoverItems(); items.setNode(PushRegistration.NAMESPACE); items.setTo(server.getNetwork()); StanzaFilter filter = new StanzaIdFilter(items.getStanzaId()); conn.addAsyncStanzaListener(new PushDiscoverItemsListener(getInstance()), filter); sendPacket(items); } /* * TODO upload info should be requested only when needed and * cached. This discovery should of course be issued before any * media message gets requeued. * Actually, delay any message from being requeued if at least * 1 media message is present; do the discovery first. */ else if (HTTPFileUpload.NAMESPACE.equals(feat.getVar())) { Log.d(MessageCenterService.TAG, "got upload service: " + packet.getFrom()); addUploadService(new HTTPFileUploadService(conn, packet.getFrom().asBareJid()), 0); // resend pending messages resendPendingMessages(true, false); } } }
/** * Get all the nodes that currently exist as a child of the specified * collection node. If the service does not support collection nodes * then all nodes will be returned. * * To retrieve contents of the root collection node (if it exists), * or there is no root collection node, pass null as the nodeId. * * @param nodeId - The id of the collection node for which the child * nodes will be returned. * @return {@link DiscoverItems} representing the existing nodes * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ public DiscoverItems discoverNodes(String nodeId) throws NoResponseException, XMPPErrorException, NotConnectedException { DiscoverItems items = new DiscoverItems(); if (nodeId != null) items.setNode(nodeId); items.setTo(to); DiscoverItems nodeItems = (DiscoverItems) con.createPacketCollectorAndSend(items).nextResultOrThrow(); return nodeItems; }
/** * Get information on the items in the node in standard * {@link DiscoverItems} format. * * @return The item details in {@link DiscoverItems} format * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ public DiscoverItems discoverItems() throws NoResponseException, XMPPErrorException, NotConnectedException { DiscoverItems items = new DiscoverItems(); items.setTo(to); items.setNode(getId()); return (DiscoverItems) con.createPacketCollectorAndSend(items).nextResultOrThrow(); }
/** * Returns the discovered items of a given XMPP entity addressed by its JID and * note attribute. Use this message only when trying to query information which is not * directly addressable. * * @param entityID the address of the XMPP entity. * @param node the optional attribute that supplements the 'jid' attribute. * @return the discovered items. * @throws XMPPErrorException if the operation failed for some reason. * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ public DiscoverItems discoverItems(String entityID, String node) throws NoResponseException, XMPPErrorException, NotConnectedException { // Discover the entity's items DiscoverItems disco = new DiscoverItems(); disco.setType(IQ.Type.get); disco.setTo(entityID); disco.setNode(node); Stanza result = connection().createPacketCollectorAndSend(disco).nextResultOrThrow(); return (DiscoverItems) result; }
/** * Publishes new items to a parent entity and node. The item elements to publish MUST have at * least a 'jid' attribute specifying the Entity ID of the item, and an action attribute which * specifies the action being taken for that item. Possible action values are: "update" and * "remove". * * @param entityID the address of the XMPP entity. * @param node the attribute that supplements the 'jid' attribute. * @param discoverItems the DiscoveryItems to publish. * @throws XMPPErrorException if the operation failed for some reason. * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ public void publishItems(String entityID, String node, DiscoverItems discoverItems) throws NoResponseException, XMPPErrorException, NotConnectedException { discoverItems.setType(IQ.Type.set); discoverItems.setTo(entityID); discoverItems.setNode(node); connection().createPacketCollectorAndSend(discoverItems).nextResultOrThrow(); }
/** * 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 */ public List<OfflineMessageHeader> getHeaders() throws NoResponseException, XMPPErrorException, NotConnectedException { 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; }
/** * Discover the commands of an specific JID. The <code>jid</code> is a * full JID. * * @param jid the full JID to retrieve the commands for. * @return the discovered items. * @throws XMPPException if the operation failed for some reason. * @throws SmackException if there was no response from the server. */ public DiscoverItems discoverCommands(String jid) throws XMPPException, SmackException { return serviceDiscoveryManager.discoverItems(jid, NAMESPACE); }
/** * Returns a list of the Items {@link org.jivesoftware.smackx.disco.packet.DiscoverItems.Item} * defined in the node. For example, the MUC protocol specifies that an XMPP client should * answer an Item for each joined room when asked for the rooms where the use has joined. * * @return a list of the Items defined in the node. */ List<DiscoverItems.Item> getNodeItems();
/** * Returns the discovered items of a given XMPP entity addressed by its JID. * * @param entityID the address of the XMPP entity. * @return the discovered information. * @throws XMPPErrorException if the operation failed for some reason. * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ public DiscoverItems discoverItems(String entityID) throws NoResponseException, XMPPErrorException, NotConnectedException { return discoverItems(entityID, null); }
/** * Publishes new items to a parent entity. The item elements to publish MUST have at least * a 'jid' attribute specifying the Entity ID of the item, and an action attribute which * specifies the action being taken for that item. Possible action values are: "update" and * "remove". * * @param entityID the address of the XMPP entity. * @param discoverItems the DiscoveryItems to publish. * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException */ public void publishItems(String entityID, DiscoverItems discoverItems) throws NoResponseException, XMPPErrorException, NotConnectedException { publishItems(entityID, null, discoverItems); }