/** * 获取用户的所有聊天室 * @param xmppConnection * @return */ public static List<HostedRoom> getHostRooms(XMPPConnection xmppConnection){ List<HostedRoom> roominfos = new ArrayList<HostedRoom>(); try { new ServiceDiscoveryManager(xmppConnection); Collection<HostedRoom> hostrooms = MultiUserChat.getHostedRooms(xmppConnection,xmppConnection.getServiceName()); for (HostedRoom entry : hostrooms) { roominfos.add(entry); Log.i("room", "名字:" + entry.getName() + " - ID:" + entry.getJid()); } Log.i("room", "服务会议数量:" + roominfos.size()); } catch (XMPPException e) { Log.e("getHostRooms",e.getMessage()); e.printStackTrace(); } return roominfos; }
/** * Tests that IQ packets can be sent to/from room occupants. This case will try to discover * information about other room occupants. */ public void testPrivateIQ() { try { // User2 joins the new room MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); muc2.join("testbot2"); // User2 discovers information about User1 DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(getConnection(1)) .discoverInfo(room + "/testbot", null); assertNotNull("No info was discovered from room occupant", info); assertEquals("Wrong IQ type", IQ.Type.result, info.getType()); assertEquals("Wrong IQ sender", room + "/testbot", info.getFrom()); // User2 leaves the room muc2.leave(); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
/** * 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. */ public void publishCommands(String jid) throws XMPPException { ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager .getInstanceFor(connection); // 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, discoNode, discoverItems); }
/** * Returns a collection with the XMPP addresses of the Multi-User Chat services. * * @param connection the XMPP connection to use for discovering Multi-User Chat services. * @return a collection with the XMPP addresses of the Multi-User Chat services. * @throws XMPPException if an error occured while trying to discover MUC services. */ public static Collection<String> getServiceNames(Connection connection) throws XMPPException { final List<String> answer = new ArrayList<String>(); ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection); DiscoverItems items = discoManager.discoverItems(connection.getServiceName()); for (Iterator<DiscoverItems.Item> it = items.getItems(); it.hasNext();) { DiscoverItems.Item item = it.next(); try { DiscoverInfo info = discoManager.discoverInfo(item.getEntityID()); if (info.containsFeature("http://jabber.org/protocol/muc")) { answer.add(item.getEntityID()); } } catch (XMPPException e) { // Trouble finding info in some cases. This is a workaround for // discovering info on remote servers. } } return answer; }
/** * Returns the reserved room nickname for the user in the room. A user may have a reserved * nickname, for example through explicit room registration or database integration. In such * cases it may be desirable for the user to discover the reserved nickname before attempting * to enter the room. * * @return the reserved room nickname or <tt>null</tt> if none. */ public String getReservedNickname() { try { DiscoverInfo result = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo( room, "x-roomuser-item"); // Look for an Identity that holds the reserved nickname and return its name for (Iterator<DiscoverInfo.Identity> identities = result.getIdentities(); identities.hasNext();) { DiscoverInfo.Identity identity = identities.next(); return identity.getName(); } // If no Identity was found then the user does not have a reserved room nickname return null; } catch (XMPPException e) { e.printStackTrace(); return null; } }
/** * Enable the Jabber services related to file transfer on the particular * connection. * * @param connection The connection on which to enable or disable the services. * @param isEnabled True to enable, false to disable. */ public static void setServiceEnabled(final Connection connection, final boolean isEnabled) { ServiceDiscoveryManager manager = ServiceDiscoveryManager .getInstanceFor(connection); List<String> namespaces = new ArrayList<String>(); namespaces.addAll(Arrays.asList(NAMESPACE)); namespaces.add(InBandBytestreamManager.NAMESPACE); if (!IBB_ONLY) { namespaces.add(Socks5BytestreamManager.NAMESPACE); } for (String namespace : namespaces) { if (isEnabled) { if (!manager.includesFeature(namespace)) { manager.addFeature(namespace); } } else { manager.removeFeature(namespace); } } }
/** * Checks to see if all file transfer related services are enabled on the * connection. * * @param connection The connection to check * @return True if all related services are enabled, false if they are not. */ public static boolean isServiceEnabled(final Connection connection) { ServiceDiscoveryManager manager = ServiceDiscoveryManager .getInstanceFor(connection); List<String> namespaces = new ArrayList<String>(); namespaces.addAll(Arrays.asList(NAMESPACE)); namespaces.add(InBandBytestreamManager.NAMESPACE); if (!IBB_ONLY) { namespaces.add(Socks5BytestreamManager.NAMESPACE); } for (String namespace : namespaces) { if (!manager.includesFeature(namespace)) { return false; } } return true; }
/** * 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. */ public void publishCommands(String jid) throws XMPPException { ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager .getInstanceFor(connection); // 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, discoNode, discoverItems); }
/** * Returns a collection with the XMPP addresses of the Multi-User Chat * services. * * @param connection * the XMPP connection to use for discovering Multi-User Chat * services. * @return a collection with the XMPP addresses of the Multi-User Chat * services. * @throws XMPPException * if an error occured while trying to discover MUC services. */ public static Collection<String> getServiceNames(Connection connection) throws XMPPException { final List<String> answer = new ArrayList<String>(); ServiceDiscoveryManager discoManager = ServiceDiscoveryManager .getInstanceFor(connection); DiscoverItems items = discoManager.discoverItems(connection .getServiceName()); for (Iterator<DiscoverItems.Item> it = items.getItems(); it.hasNext();) { DiscoverItems.Item item = it.next(); try { DiscoverInfo info = discoManager.discoverInfo(item .getEntityID()); if (info.containsFeature("http://jabber.org/protocol/muc")) { answer.add(item.getEntityID()); } } catch (XMPPException e) { // Trouble finding info in some cases. This is a workaround for // discovering info on remote servers. } } return answer; }
/** * Returns the reserved room nickname for the user in the room. A user may * have a reserved nickname, for example through explicit room registration * or database integration. In such cases it may be desirable for the user * to discover the reserved nickname before attempting to enter the room. * * @return the reserved room nickname or <tt>null</tt> if none. */ public String getReservedNickname() { try { DiscoverInfo result = ServiceDiscoveryManager.getInstanceFor( connection).discoverInfo(room, "x-roomuser-item"); // Look for an Identity that holds the reserved nickname and return // its name for (Iterator<DiscoverInfo.Identity> identities = result .getIdentities(); identities.hasNext();) { DiscoverInfo.Identity identity = identities.next(); return identity.getName(); } // If no Identity was found then the user does not have a reserved // room nickname return null; } catch (XMPPException e) { e.printStackTrace(); return null; } }
/** * Enable the Jabber services related to file transfer on the particular * connection. * * @param connection * The connection on which to enable or disable the services. * @param isEnabled * True to enable, false to disable. */ public static void setServiceEnabled(final Connection connection, final boolean isEnabled) { ServiceDiscoveryManager manager = ServiceDiscoveryManager .getInstanceFor(connection); List<String> namespaces = new ArrayList<String>(); namespaces.addAll(Arrays.asList(NAMESPACE)); namespaces.add(InBandBytestreamManager.NAMESPACE); if (!IBB_ONLY) { namespaces.add(Socks5BytestreamManager.NAMESPACE); } for (String namespace : namespaces) { if (isEnabled) { if (!manager.includesFeature(namespace)) { manager.addFeature(namespace); } } else { manager.removeFeature(namespace); } } }
/** * Checks to see if all file transfer related services are enabled on the * connection. * * @param connection * The connection to check * @return True if all related services are enabled, false if they are not. */ public static boolean isServiceEnabled(final Connection connection) { ServiceDiscoveryManager manager = ServiceDiscoveryManager .getInstanceFor(connection); List<String> namespaces = new ArrayList<String>(); namespaces.addAll(Arrays.asList(NAMESPACE)); namespaces.add(InBandBytestreamManager.NAMESPACE); if (!IBB_ONLY) { namespaces.add(Socks5BytestreamManager.NAMESPACE); } for (String namespace : namespaces) { if (!manager.includesFeature(namespace)) { return false; } } return true; }
/** * Retrieves the configuration of the space. * @param space The space to get the configuration for. * @return The parsed result or, if there was an error, null. * @throws SpaceManagementException Thrown when no response was received from the server. */ private SpaceConfiguration getSpaceConfiguration(Space space) throws SpaceManagementException{ if (space == null){ throw new IllegalArgumentException("The given space must not be null"); } ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); DiscoverInfo info; try { info = discoveryManager.discoverInfo(SERVICE_PREFIX + space.getDomain(), space.getId()); } catch (XMPPException e) { if (e.getXMPPError().getCode() == 404) { return null; } else { throw new SpaceManagementException("The Server didn't respond.", Type.OTHER, e); } } Element config = parsePacketToElement(info); return parseSpaceConfiguration(config); }
/** * Tests that IQ packets can be sent to/from room occupants. This case will try to discover * information about other room occupants. */ public void testPrivateIQ() { try { // User2 joins the new room MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); muc2.join("testbot2"); // User2 discovers information about User1 DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(getConnection(1)) .discoverInfo(room + "/testbot", null); assertNotNull("No info was discovered from room occupant", info); assertEquals("Wrong IQ type", IQ.Type.RESULT, info.getType()); assertEquals("Wrong IQ sender", room + "/testbot", info.getFrom()); // User2 leaves the room muc2.leave(); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
public static synchronized final void enableJingle() { if( enabled ) return; enabled = true; ProviderManager providerManager = ProviderManager.getInstance(); providerManager.addIQProvider( JingleIQ.ELEMENT_NAME, JingleIQ.NAMESPACE, new JingleIQProvider()); Connection.addConnectionCreationListener(new ConnectionCreationListener() { public synchronized void connectionCreated(Connection connection) { if( ! ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(JingleIQ.NAMESPACE) ) ServiceDiscoveryManager.getInstanceFor(connection).addFeature(JingleIQ.NAMESPACE); } }); }
/** * Checks the service discovery item returned from a server component to verify if it is * a File Transfer proxy or not. * * @param manager the service discovery manager which will be used to query the component * @param item the discovered item on the server relating * @return returns the JID of the proxy if it is a proxy or null if the item is not a proxy. */ private String checkIsProxy(ServiceDiscoveryManager manager, DiscoverItems.Item item) { DiscoverInfo info; try { info = manager.discoverInfo(item.getEntityID()); } catch (XMPPException e) { return null; } Iterator<DiscoverInfo.Identity> itx = info.getIdentities(); while (itx.hasNext()) { DiscoverInfo.Identity identity = itx.next(); if ("proxy".equalsIgnoreCase(identity.getCategory()) && "bytestreams".equalsIgnoreCase( identity.getType())) { return info.getFrom(); } } return null; }
private void initProxies() { proxies = new ArrayList<String>(); ServiceDiscoveryManager manager = ServiceDiscoveryManager .getInstanceFor(connection); try { DiscoverItems discoItems = manager.discoverItems(connection.getServiceName()); Iterator<DiscoverItems.Item> it = discoItems.getItems(); while (it.hasNext()) { DiscoverItems.Item item = it.next(); String proxy = checkIsProxy(manager, item); if (proxy != null) { proxies.add(proxy); } } } catch (XMPPException e) { return; } if (proxies.size() > 0) { initStreamHosts(); } }