/** * Sends to the server a new stream element. This operation may be requested several times * so we need to encapsulate the logic in one place. This message will be sent while doing * TLS, SASL and resource binding. * * @throws IOException If an error occurs while sending the stanza to the server. */ void openStream() throws IOException { StringBuilder stream = new StringBuilder(); stream.append("<stream:stream"); stream.append(" to=\"").append(connection.getServiceName()).append("\""); stream.append(" xmlns=\"jabber:client\""); stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\""); stream.append(" version=\"1.0\">"); writer.write(stream.toString()); writer.flush(); }
/** * Sends to the server a new stream element. This operation may be requested several times * so we need to encapsulate the logic in one place. This message will be sent while doing * TLS, SASL and resource binding. * * @throws IOException If an error occurs while sending the stanza to the server. */ void openStream() throws IOException { StringBuilder stream = new StringBuilder(); stream.append("<stream:stream"); stream.append(" to=\"").append(connection.getServiceName()).append("\""); stream.append(" xmlns=\"jabber:client\""); stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\""); stream.append(" version=\"1.0\">"); writer.write(stream.toString()); writer.flush(); }
/** * Sends to the server a new stream element. This operation may be requested several times * so we need to encapsulate the logic in one place. This message will be sent while doing * TLS, SASL and resource binding. * * @throws IOException If an error occurs while sending the stanza to the server. */ void openStream() throws IOException { StringBuilder stream = new StringBuilder(); stream.append("<stream:stream"); stream.append(" to=\"").append(connection.getServiceName()).append("\""); stream.append(" xmlns=\"jabber:client\""); stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\""); stream.append(" version=\"1.0\">"); writer.write(stream.toString()); writer.flush(); } }
public void userHasLogged(String user) { boolean isAnonymous = "".equals(StringUtils.parseName(user)); String title = "Smack Debug Window -- " + (isAnonymous ? "" : StringUtils.parseBareAddress(user)) + "@" + connection.getServiceName() + ":" + connection.getPort(); title += "/" + StringUtils.parseResource(user); frame.setTitle(title); }
private void initProxies() { proxies = new ArrayList<String>(); ServiceDiscoveryManager manager = ServiceDiscoveryManager .getInstanceFor(connection); try { DiscoverItems discoItems = manager.discoverItems(connection.getServiceName()); Iterator it = discoItems.getItems(); while (it.hasNext()) { DiscoverItems.Item item = (DiscoverItems.Item) it.next(); String proxy = checkIsProxy(manager, item); if (proxy != null) { proxies.add(proxy); } } } catch (XMPPException e) { return; } if (proxies.size() > 0) { initStreamHosts(); } }
public void userHasLogged(String user) { boolean isAnonymous = "".equals(StringUtils.parseName(user)); String title = "User logged (" + connection.hashCode() + "): " + (isAnonymous ? "" : StringUtils.parseBareAddress(user)) + "@" + connection.getServiceName() + ":" + connection.getPort(); title += "/" + StringUtils.parseResource(user); System.out.println(title); // Add the connection listener to the connection so that the debugger can be notified // whenever the connection is closed. connection.addConnectionListener(connListener); }
discoItems = manager.discoverItems(connection.getServiceName());
/** * 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 getServiceNames(XMPPConnection connection) throws XMPPException { final List answer = new ArrayList(); ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection); DiscoverItems items = discoManager.discoverItems(connection.getServiceName()); for (Iterator it = items.getItems(); it.hasNext();) { DiscoverItems.Item item = (DiscoverItems.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 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 getSearchServices() throws XMPPException { List searchServices = new ArrayList(); ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con); DiscoverItems items = discoManager.discoverItems(con.getServiceName()); for (Iterator it = items.getItems(); it.hasNext();) { DiscoverItems.Item item = (DiscoverItems.Item) it.next(); try { DiscoverInfo info = discoManager.discoverInfo(item.getEntityID()); if (info.containsFeature("jabber:iq:search")) { searchServices.add(item.getEntityID()); } } catch (XMPPException e) { // No info found. break; } } return searchServices; } }
DiscoverInfo discoInfo = disco.discoverInfo(connection.getServiceName()); for (DiscoverInfo.Identity identity : discoInfo.getIdentities()) { if ((identity.getName() != null) && (identity.getName().startsWith("rtpbridge"))) {
/** * 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; }
/** * Get a new STUN Server Address and port from the server. * If a error occurs or the server don't support STUN Service, null is returned. * * @param connection * @return the STUN server address * @throws NotConnectedException */ public static STUN getSTUNServer(XMPPConnection connection) throws NotConnectedException { if (!connection.isConnected()) { return null; } STUN stunPacket = new STUN(); stunPacket.setTo(DOMAIN + "." + connection.getServiceName()); PacketCollector collector = connection.createPacketCollectorAndSend(stunPacket); STUN response = (STUN) collector.nextResult(); // Cancel the collector. collector.cancel(); return response; }
/** * Get a new RTPBridge Candidate from the server. * If a error occurs or the server don't support RTPBridge Service, null is returned. * * @param connection * @param sessionID * @return the new RTPBridge * @throws NotConnectedException */ public static RTPBridge getRTPBridge(XMPPConnection connection, String sessionID) throws NotConnectedException { if (!connection.isConnected()) { return null; } RTPBridge rtpPacket = new RTPBridge(sessionID); rtpPacket.setTo(RTPBridge.NAME + "." + connection.getServiceName()); PacketCollector collector = connection.createPacketCollectorAndSend(rtpPacket); RTPBridge response = (RTPBridge) collector.nextResult(); // Cancel the collector. collector.cancel(); return response; }
@Test public void testCompleteConfiguration() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("XmppConnectionParserTests-complete.xml", this.getClass()); XMPPConnection connection = ac.getBean("connection", XMPPConnection.class); assertEquals("foogle.com", connection.getServiceName()); assertFalse(connection.isConnected()); XmppConnectionFactoryBean xmppFb = ac.getBean("&connection", XmppConnectionFactoryBean.class); assertEquals("happy.user", TestUtils.getPropertyValue(xmppFb, "user")); assertEquals("blah", TestUtils.getPropertyValue(xmppFb, "password")); assertEquals("SpringSource", TestUtils.getPropertyValue(xmppFb, "resource")); assertEquals("reject_all", TestUtils.getPropertyValue(xmppFb, "subscriptionMode").toString()); ac.close(); }
@Test public void testSimpleConfiguration() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("XmppConnectionParserTests-simple.xml", this.getClass()); XMPPConnection connection = ac.getBean("connection", XMPPConnection.class); assertEquals("my.domain", connection.getServiceName()); assertFalse(connection.isConnected()); XmppConnectionFactoryBean xmppFb = ac.getBean("&connection", XmppConnectionFactoryBean.class); assertEquals("happy.user@my.domain", TestUtils.getPropertyValue(xmppFb, "user")); assertEquals("blah", TestUtils.getPropertyValue(xmppFb, "password")); assertNull(TestUtils.getPropertyValue(xmppFb, "resource")); assertEquals("accept_all", TestUtils.getPropertyValue(xmppFb, "subscriptionMode").toString()); xmppFb = ac.getBean("&connectionWithResource", XmppConnectionFactoryBean.class); assertEquals("Smack", TestUtils.getPropertyValue(xmppFb, "resource")); ac.close(); }
/** * Changes the password of the currently logged-in account. This operation can only * be performed after a successful login operation has been completed. Not all servers * support changing passwords; an XMPPException will be thrown when that is the case. * * @throws IllegalStateException if not currently logged-in to the server. * @throws XMPPException if an error occurs when changing the password. */ public void changePassword(String newPassword) throws XMPPException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); HashMap map = new HashMap(); map.put("username",StringUtils.parseName(connection.getUser())); map.put("password",newPassword); reg.setAttributes(map); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } }
reg.setTo(connection.getServiceName()); Map attributes = new HashMap();
/** * Check if the server support RTPBridge Service. * * @param connection * @return the RTPBridge * @throws NotConnectedException */ public static RTPBridge relaySession(XMPPConnection connection, String sessionID, String pass, TransportCandidate proxyCandidate, TransportCandidate localCandidate) throws NotConnectedException { if (!connection.isConnected()) { return null; } RTPBridge rtpPacket = new RTPBridge(sessionID, RTPBridge.BridgeAction.change); rtpPacket.setTo(RTPBridge.NAME + "." + connection.getServiceName()); rtpPacket.setType(Type.SET); rtpPacket.setPass(pass); rtpPacket.setPortA(localCandidate.getPort()); rtpPacket.setPortB(proxyCandidate.getPort()); rtpPacket.setHostA(localCandidate.getIp()); rtpPacket.setHostB(proxyCandidate.getIp()); // LOGGER.debug("Relayed to: " + candidate.getIp() + ":" + candidate.getPort()); PacketCollector collector = connection.createPacketCollectorAndSend(rtpPacket); RTPBridge response = (RTPBridge) collector.nextResult(); // Cancel the collector. collector.cancel(); return response; }
/** * Gets the account registration info from the server. * * @throws XMPPException if an error occurs. */ private synchronized void getRegistrationInfo() throws XMPPException { Registration reg = new Registration(); reg.setTo(connection.getServiceName()); PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = connection.createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Stop queuing results collector.cancel(); if (result == null) { throw new XMPPException("No response from server."); } else if (result.getType() == IQ.Type.ERROR) { throw new XMPPException(result.getError()); } else { info = (Registration)result; } } }
public OAReplyFilter(OAStanza request, XMPPConnection connection) { to = request.getTo(); if (connection.getUser() == null) { // We have not yet been assigned a username, this can happen if the connection is // in an early stage, i.e. when performing the SASL auth. local = null; } else { local = connection.getUser(); } server = connection.getServiceName(); stanzaId = request.getStanzaId(); StanzaFilter iqFilter = new OrFilter(IQTypeFilter.ERROR, IQTypeFilter.GET); StanzaFilter idFilter = new StanzaIdFilter(request.getStanzaId()); iqAndIdFilter = new AndFilter(iqFilter, idFilter); fromFilter = new OrFilter(); fromFilter.addFilter(FromMatchesFilter.createFull(to)); if (to == null) { if (local != null) { fromFilter.addFilter(FromMatchesFilter.createBare(local)); } fromFilter.addFilter(FromMatchesFilter.createFull(server)); } else if (local != null && to.equals(local.asBareJid())) { fromFilter.addFilter(FromMatchesFilter.createFull(null)); } }