Search in sources :

Example 41 with XMPPConnection

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

the class PingManager method ping.

/**
     * Pings the given jid. This method will return false if an error occurs.  The exception 
     * to this, is a server ping, which will always return true if the server is reachable, 
     * event if there is an error on the ping itself (i.e. ping not supported).
     * <p>
     * Use {@link #isPingSupported(Jid)} to determine if XMPP Ping is supported 
     * by the entity.
     * 
     * @param jid The id of the entity the ping is being sent to
     * @param pingTimeout The time to wait for a reply in milliseconds
     * @return true if a reply was received from the entity, false otherwise.
     * @throws NoResponseException if there was no response from the jid.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public boolean ping(Jid jid, long pingTimeout) throws NotConnectedException, NoResponseException, InterruptedException {
    final XMPPConnection connection = connection();
    // otherwise the client JID will be null causing an NPE
    if (!connection.isAuthenticated()) {
        throw new NotConnectedException();
    }
    Ping ping = new Ping(jid);
    try {
        connection.createStanzaCollectorAndSend(ping).nextResultOrThrow(pingTimeout);
    } catch (XMPPException exc) {
        return jid.equals(connection.getXMPPServiceDomain());
    }
    return true;
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) Ping(org.jivesoftware.smackx.ping.packet.Ping) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException)

Example 42 with XMPPConnection

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

the class ConnectionUtils method createMockedConnection.

/**
     * Creates a mocked XMPP connection that stores every stanza(/packet) that is send over this
     * connection in the given protocol instance and returns the predefined answer packets
     * form the protocol instance.
     * <p>
     * This mocked connection can used to collect packets that require a reply using a
     * StanzaCollector.
     * 
     * <pre>
     * <code>
     *   StanzaCollector collector = connection.createStanzaCollector(new PacketFilter());
     *   connection.sendStanza(packet);
     *   Stanza(/Packet) reply = collector.nextResult();
     * </code>
     * </pre>
     * 
     * @param protocol protocol helper containing answer packets
     * @param initiatorJID the user associated to the XMPP connection
     * @param xmppServer the XMPP server associated to the XMPP connection
     * @return a mocked XMPP connection
     * @throws SmackException 
     * @throws XMPPErrorException 
     * @throws InterruptedException 
     */
public static XMPPConnection createMockedConnection(final Protocol protocol, EntityFullJid initiatorJID, DomainBareJid xmppServer) throws SmackException, XMPPErrorException, InterruptedException {
    // mock XMPP connection
    XMPPConnection connection = mock(XMPPConnection.class);
    when(connection.getUser()).thenReturn(initiatorJID);
    when(connection.getXMPPServiceDomain()).thenReturn(xmppServer);
    // mock packet collector
    final StanzaCollector collector = mock(StanzaCollector.class);
    when(connection.createStanzaCollector(isA(StanzaFilter.class))).thenReturn(collector);
    Answer<StanzaCollector> collectorAndSend = new Answer<StanzaCollector>() {

        @Override
        public StanzaCollector answer(InvocationOnMock invocation) throws Throwable {
            Stanza packet = (Stanza) invocation.getArguments()[0];
            protocol.getRequests().add(packet);
            return collector;
        }
    };
    when(connection.createStanzaCollectorAndSend(isA(IQ.class))).thenAnswer(collectorAndSend);
    // mock send method
    Answer<Object> addIncoming = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            protocol.getRequests().add((Stanza) invocation.getArguments()[0]);
            return null;
        }
    };
    doAnswer(addIncoming).when(connection).sendStanza(isA(Stanza.class));
    // mock receive methods
    Answer<Stanza> answer = new Answer<Stanza>() {

        @Override
        public Stanza answer(InvocationOnMock invocation) throws Throwable {
            return protocol.getResponses().poll();
        }
    };
    when(collector.nextResult(anyInt())).thenAnswer(answer);
    when(collector.nextResult()).thenAnswer(answer);
    Answer<Stanza> answerOrThrow = new Answer<Stanza>() {

        @Override
        public Stanza answer(InvocationOnMock invocation) throws Throwable {
            Stanza packet = protocol.getResponses().poll();
            if (packet == null)
                return packet;
            XMPPErrorException.ifHasErrorThenThrow(packet);
            return packet;
        }
    };
    when(collector.nextResultOrThrow()).thenAnswer(answerOrThrow);
    when(collector.nextResultOrThrow(anyLong())).thenAnswer(answerOrThrow);
    // initialize service discovery manager for this connection
    ServiceDiscoveryManager.getInstanceFor(connection);
    return connection;
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 43 with XMPPConnection

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

the class Roster method sendSubscriptionRequest.

public void sendSubscriptionRequest(BareJid jid) throws NotLoggedInException, NotConnectedException, InterruptedException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    // Create a presence subscription packet and send.
    Presence presencePacket = new Presence(Presence.Type.subscribe);
    presencePacket.setTo(jid);
    connection.sendStanza(presencePacket);
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 44 with XMPPConnection

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

the class Roster method preApprove.

/**
     * Pre-approve user presence subscription.
     *
     * @param user the user. (e.g. johndoe@jabber.org)
     * @throws NotLoggedInException if not logged in.
     * @throws NotConnectedException
     * @throws InterruptedException
     * @throws FeatureNotSupportedException if pre-approving is not supported.
     * @since 4.2
     */
public void preApprove(BareJid user) throws NotLoggedInException, NotConnectedException, InterruptedException, FeatureNotSupportedException {
    final XMPPConnection connection = connection();
    if (!isSubscriptionPreApprovalSupported()) {
        throw new FeatureNotSupportedException("Pre-approving");
    }
    Presence presencePacket = new Presence(Presence.Type.subscribed);
    presencePacket.setTo(user);
    connection.sendStanza(presencePacket);
}
Also used : FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 45 with XMPPConnection

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

the class Roster method createGroup.

/**
     * Creates a new group.
     * <p>
     * Note: you must add at least one entry to the group for the group to be kept
     * after a logout/login. This is due to the way that XMPP stores group information.
     * </p>
     *
     * @param name the name of the group.
     * @return a new group, or null if the group already exists
     */
public RosterGroup createGroup(String name) {
    final XMPPConnection connection = connection();
    if (groups.containsKey(name)) {
        return groups.get(name);
    }
    RosterGroup group = new RosterGroup(name, connection);
    groups.put(name, group);
    return group;
}
Also used : XMPPConnection(org.jivesoftware.smack.XMPPConnection)

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