Search in sources :

Example 1 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class Roster method createEntry.

/**
     * Creates a new roster entry and presence subscription. The server will asynchronously
     * update the roster with the subscription status.
     *
     * @param user   the user. (e.g. johndoe@jabber.org)
     * @param name   the nickname of the user.
     * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
     *               the roster entry won't belong to a group.
     * @throws NoResponseException if there was no response from the server.
     * @throws XMPPErrorException if an XMPP exception occurs.
     * @throws NotLoggedInException If not logged in.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void createEntry(BareJid user, String name, String[] groups) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    // Create and send roster entry creation packet.
    RosterPacket rosterPacket = new RosterPacket();
    rosterPacket.setType(IQ.Type.set);
    RosterPacket.Item item = new RosterPacket.Item(user, name);
    if (groups != null) {
        for (String group : groups) {
            if (group != null && group.trim().length() > 0) {
                item.addGroupName(group);
            }
        }
    }
    rosterPacket.addRosterItem(item);
    connection.createStanzaCollectorAndSend(rosterPacket).nextResultOrThrow();
    sendSubscriptionRequest(user);
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 2 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class Roster method reload.

/**
     * Reloads the entire roster from the server. This is an asynchronous operation,
     * which means the method will return immediately, and the roster will be
     * reloaded at a later point when the server responds to the reload request.
     * @throws NotLoggedInException If not logged in.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    RosterPacket packet = new RosterPacket();
    if (rosterStore != null && isRosterVersioningSupported()) {
        packet.setVersion(rosterStore.getRosterVersion());
    }
    rosterState = RosterState.loading;
    connection.sendIqWithResponseCallback(packet, new RosterResultListener(), new ExceptionCallback() {

        @Override
        public void processException(Exception exception) {
            rosterState = RosterState.uninitialized;
            Level logLevel;
            if (exception instanceof NotConnectedException) {
                logLevel = Level.FINE;
            } else {
                logLevel = Level.SEVERE;
            }
            LOGGER.log(logLevel, "Exception reloading roster", exception);
            for (RosterLoadedListener listener : rosterLoadedListeners) {
                listener.onRosterLoadingFailed(exception);
            }
        }
    });
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Level(java.util.logging.Level) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ExceptionCallback(org.jivesoftware.smack.ExceptionCallback) SmackException(org.jivesoftware.smack.SmackException) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) NotLoggedInException(org.jivesoftware.smack.SmackException.NotLoggedInException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException)

Example 3 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class Roster method removeEntry.

/**
     * Removes a roster entry from the roster. The roster entry will also be removed from the
     * unfiled entries or from any roster group where it could belong and will no longer be part
     * of the roster. Note that this is a synchronous call -- Smack must wait for the server
     * to send an updated subscription status.
     *
     * @param entry a roster entry.
     * @throws XMPPErrorException if an XMPP error occurs.
     * @throws NotLoggedInException if not logged in.
     * @throws NoResponseException SmackException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    // The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
    if (!entries.containsKey(entry.getJid())) {
        return;
    }
    RosterPacket packet = new RosterPacket();
    packet.setType(IQ.Type.set);
    RosterPacket.Item item = RosterEntry.toRosterItem(entry);
    // Set the item type as REMOVE so that the server will delete the entry
    item.setItemType(RosterPacket.ItemType.remove);
    packet.addRosterItem(item);
    connection.createStanzaCollectorAndSend(packet).nextResultOrThrow();
}
Also used : RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 4 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class Socks5BytestreamManager method disableService.

/**
     * Disables the SOCKS5 Bytestream manager by removing the SOCKS5 Bytestream feature from the
     * service discovery, disabling the listener for SOCKS5 Bytestream initiation requests and
     * resetting its internal state, which includes removing this instance from the managers map.
     * <p>
     * To re-enable the SOCKS5 Bytestream feature invoke {@link #getBytestreamManager(XMPPConnection)}.
     * Using the file transfer API will automatically re-enable the SOCKS5 Bytestream feature.
     */
public synchronized void disableService() {
    XMPPConnection connection = connection();
    // remove initiation packet listener
    connection.unregisterIQRequestHandler(initiationListener);
    // shutdown threads
    this.initiationListener.shutdown();
    // clear listeners
    this.allRequestListeners.clear();
    this.userListeners.clear();
    // reset internal state
    this.lastWorkingProxy = null;
    this.proxyBlacklist.clear();
    this.ignoredBytestreamRequests.clear();
    // remove manager from static managers map
    managers.remove(connection);
    // shutdown local SOCKS5 proxy if there are no more managers for other connections
    if (managers.size() == 0) {
        Socks5Proxy.getSocks5Proxy().stop();
    }
    // remove feature from service discovery
    ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
    // check if service discovery is not already disposed by connection shutdown
    if (serviceDiscoveryManager != null) {
        serviceDiscoveryManager.removeFeature(Bytestream.NAMESPACE);
    }
}
Also used : XMPPConnection(org.jivesoftware.smack.XMPPConnection) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager)

Example 5 with XMPPConnection

use of org.jivesoftware.smack.XMPPConnection in project Smack by igniterealtime.

the class Socks5BytestreamManager method getLocalStreamHost.

/**
     * Returns the stream host information of the local SOCKS5 proxy containing the IP address and
     * the port or null if local SOCKS5 proxy is not running.
     * 
     * @return the stream host information of the local SOCKS5 proxy or null if local SOCKS5 proxy
     *         is not running
     */
private List<StreamHost> getLocalStreamHost() {
    XMPPConnection connection = connection();
    // get local proxy singleton
    Socks5Proxy socks5Server = Socks5Proxy.getSocks5Proxy();
    if (!socks5Server.isRunning()) {
        // server is not running
        return null;
    }
    List<String> addresses = socks5Server.getLocalAddresses();
    if (addresses.isEmpty()) {
        // local address could not be determined
        return null;
    }
    final int port = socks5Server.getPort();
    List<StreamHost> streamHosts = new ArrayList<StreamHost>();
    outerloop: for (String address : addresses) {
        // Prevent loopback addresses from appearing as streamhost
        final String[] loopbackAddresses = { "127.0.0.1", "0:0:0:0:0:0:0:1", "::1" };
        for (String loopbackAddress : loopbackAddresses) {
            // ie. the part after the '%' sign.
            if (address.startsWith(loopbackAddress)) {
                continue outerloop;
            }
        }
        streamHosts.add(new StreamHost(connection.getUser(), address, port));
    }
    return streamHosts;
}
Also used : ArrayList(java.util.ArrayList) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)

Aggregations

XMPPConnection (org.jivesoftware.smack.XMPPConnection)64 XMPPException (org.jivesoftware.smack.XMPPException)14 Message (org.jivesoftware.smack.packet.Message)9 SmackException (org.jivesoftware.smack.SmackException)7 IQ (org.jivesoftware.smack.packet.IQ)7 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 ArrayList (java.util.ArrayList)6 SynchronousQueue (java.util.concurrent.SynchronousQueue)6 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)6 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)6 TimeoutException (java.util.concurrent.TimeoutException)5 StanzaCollector (org.jivesoftware.smack.StanzaCollector)5 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 ConnectionThread (com.xabber.android.data.connection.ConnectionThread)4 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)4 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)4 Packet (org.jivesoftware.smack.packet.Packet)4 Jid (org.jxmpp.jid.Jid)4