Search in sources :

Example 61 with XmppStringprepException

use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.

the class AgentRoster method addListener.

/**
 * Adds a listener to this roster. The listener will be fired anytime one or more
 * changes to the roster are pushed from the server.
 *
 * @param listener an agent roster listener.
 */
public void addListener(AgentRosterListener listener) {
    synchronized (listeners) {
        if (!listeners.contains(listener)) {
            listeners.add(listener);
            // Fire events for the existing entries and presences in the roster
            for (EntityBareJid jid : getAgents()) {
                // but possible)
                if (entries.contains(jid)) {
                    // Fire the agent added event
                    listener.agentAdded(jid);
                    Jid j;
                    try {
                        j = JidCreate.from(jid);
                    } catch (XmppStringprepException e) {
                        throw new IllegalStateException(e);
                    }
                    Map<Resourcepart, Presence> userPresences = presenceMap.get(j);
                    if (userPresences != null) {
                        Iterator<Presence> presences = userPresences.values().iterator();
                        while (presences.hasNext()) {
                            // Fire the presence changed event
                            listener.presenceChanged(presences.next());
                        }
                    }
                }
            }
        }
    }
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) Presence(org.jivesoftware.smack.packet.Presence) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) EntityBareJid(org.jxmpp.jid.EntityBareJid) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 62 with XmppStringprepException

use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.

the class RTPBridge method getRTPBridge.

/**
 * 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 TODO javadoc me please
 * @param sessionID TODO javadoc me please
 * @return the new RTPBridge
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 */
public static RTPBridge getRTPBridge(XMPPConnection connection, String sessionID) throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException {
    if (!connection.isConnected()) {
        return null;
    }
    RTPBridge rtpPacket = new RTPBridge(sessionID);
    DomainBareJid jid;
    try {
        jid = JidCreate.domainBareFrom(RTPBridge.NAME + "." + connection.getXMPPServiceDomain());
    } catch (XmppStringprepException e) {
        throw new AssertionError(e);
    }
    rtpPacket.setTo(jid);
    RTPBridge response = connection.sendIqRequestAndWaitForResponse(rtpPacket);
    return response;
}
Also used : XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 63 with XmppStringprepException

use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.

the class RTPBridge method relaySession.

/**
 * Check if the server support RTPBridge Service.
 *
 * @param connection TODO javadoc me please
 * @param sessionID the session id.
 * @param pass the password.
 * @param proxyCandidate the proxy candidate.
 * @param localCandidate the local candidate.
 * @return the RTPBridge
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 */
public static RTPBridge relaySession(XMPPConnection connection, String sessionID, String pass, TransportCandidate proxyCandidate, TransportCandidate localCandidate) throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException {
    if (!connection.isConnected()) {
        return null;
    }
    RTPBridge rtpPacket = new RTPBridge(sessionID, RTPBridge.BridgeAction.change);
    DomainBareJid jid;
    try {
        jid = JidCreate.domainBareFrom(RTPBridge.NAME + "." + connection.getXMPPServiceDomain());
    } catch (XmppStringprepException e) {
        throw new AssertionError(e);
    }
    rtpPacket.setTo(jid);
    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());
    RTPBridge response = connection.sendIqRequestAndWaitForResponse(rtpPacket);
    return response;
}
Also used : XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Example 64 with XmppStringprepException

use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.

the class OmemoDeviceTest method testEquals.

/**
 * Test, if the equals() method works as intended.
 */
@Test
public void testEquals() {
    BareJid romeo, juliet, guyUnderTheBalcony;
    try {
        romeo = JidCreate.bareFrom("romeo@shakespeare.lit");
        guyUnderTheBalcony = JidCreate.bareFrom("romeo@shakespeare.lit/underTheBalcony");
        juliet = JidCreate.bareFrom("juliet@shakespeare.lit");
    } catch (XmppStringprepException e) {
        Assert.fail(e.getMessage());
        return;
    }
    OmemoDevice r = new OmemoDevice(romeo, 1);
    OmemoDevice g = new OmemoDevice(guyUnderTheBalcony, 1);
    OmemoDevice r2 = new OmemoDevice(romeo, 2);
    OmemoDevice j = new OmemoDevice(juliet, 3);
    OmemoDevice j2 = new OmemoDevice(juliet, 1);
    assertTrue(r.equals(g));
    assertFalse(r.equals(r2));
    assertFalse(j.equals(j2));
    assertFalse(j2.equals(r2));
}
Also used : OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) BareJid(org.jxmpp.jid.BareJid) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) Test(org.junit.Test)

Example 65 with XmppStringprepException

use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.

the class PacketWriterTest method shouldBlockAndUnblockTest.

/**
 * Make sure that stanza writer does block once the queue reaches
 * {@link PacketWriter#QUEUE_SIZE} and that
 * {@link PacketWriter#sendStanza(org.jivesoftware.smack.tcp.packet.Packet)} does unblock after the
 * interrupt.
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws BrokenBarrierException in case of a broken barrier.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws XmppStringprepException if the provided string is invalid.
 * @throws SecurityException if there was a security violation.
 * @throws NoSuchFieldException if there is no such field.
 * @throws IllegalAccessException if there was an illegal access.
 * @throws IllegalArgumentException if an illegal argument was given.
 */
@Test
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException, XmppStringprepException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    XMPPTCPConnection connection = new XMPPTCPConnection("user", "pass", "example.org");
    // Get the protected "Reader reader" field from AbstractXMPPConnection and set it to a dummy Reader,
    // as otherwise it will be null when we perform the writer threads init() which will make the StAX parser
    // to throw an exception.
    Field readerField = AbstractXMPPConnection.class.getDeclaredField("reader");
    readerField.setAccessible(true);
    readerField.set(connection, DUMMY_READER);
    final PacketWriter pw = connection.packetWriter;
    BlockingStringWriter blockingStringWriter = new BlockingStringWriter();
    connection.setWriter(blockingStringWriter);
    connection.packetWriter.init();
    // blocking writer.
    for (int i = 0; i < XMPPTCPConnection.PacketWriter.QUEUE_SIZE + 1; i++) {
        pw.sendStreamElement(StanzaBuilder.buildMessage().build());
    }
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final AtomicReference<Exception> unexpectedThreadExceptionReference = new AtomicReference<>();
    final AtomicReference<Exception> expectedThreadExceptionReference = new AtomicReference<>();
    shutdown = false;
    prematureUnblocked = false;
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                barrier.await();
                pw.sendStreamElement(StanzaBuilder.buildMessage().build());
                // should only return after the pw was interrupted
                if (!shutdown) {
                    prematureUnblocked = true;
                }
            } catch (InterruptedException | SmackException.NotConnectedException e) {
                // This is the exception we expect.
                expectedThreadExceptionReference.set(e);
            } catch (BrokenBarrierException e) {
                unexpectedThreadExceptionReference.set(e);
            }
            try {
                barrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                unexpectedThreadExceptionReference.set(e);
            }
        }
    });
    t.start();
    // This barrier is not strictly necessary, but may increases the chances that the threat
    // will block before we call shutdown. Otherwise we may get false positives (which is still
    // better then false negatives).
    barrier.await();
    // Not really cool, but may increases the chances for 't' to block in sendStanza.
    Thread.sleep(250);
    // Shutdown the packetwriter, this will also interrupt the writer thread, which is what we hope to happen in the
    // thread created above.
    pw.shutdown(false);
    shutdown = true;
    barrier.await();
    t.join(60000);
    Exception unexpectedThreadException = unexpectedThreadExceptionReference.get();
    try {
        if (prematureUnblocked) {
            String failureMessage = "Should not unblock before the thread got shutdown.";
            if (unexpectedThreadException != null) {
                String stacktrace = ExceptionUtil.getStackTrace(unexpectedThreadException);
                failureMessage += " Unexpected thread exception thrown: " + unexpectedThreadException + "\n" + stacktrace;
            }
            fail(failureMessage);
        } else if (unexpectedThreadException != null) {
            String stacktrace = ExceptionUtil.getStackTrace(unexpectedThreadException);
            fail("Unexpected thread exception: " + unexpectedThreadException + "\n" + stacktrace);
        }
        assertNotNull(expectedThreadExceptionReference.get(), "Did not encounter expected exception on sendStreamElement()");
    } finally {
        blockingStringWriter.unblock();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) SmackException(org.jivesoftware.smack.SmackException) AtomicReference(java.util.concurrent.atomic.AtomicReference) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Field(java.lang.reflect.Field) PacketWriter(org.jivesoftware.smack.tcp.XMPPTCPConnection.PacketWriter) Test(org.junit.jupiter.api.Test)

Aggregations

XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)76 DomainBareJid (org.jxmpp.jid.DomainBareJid)20 SmackException (org.jivesoftware.smack.SmackException)18 Jid (org.jxmpp.jid.Jid)18 EntityBareJid (org.jxmpp.jid.EntityBareJid)16 AccountJid (com.xabber.android.data.entity.AccountJid)12 XMPPException (org.jivesoftware.smack.XMPPException)12 UserJid (com.xabber.android.data.entity.UserJid)11 BareJid (org.jxmpp.jid.BareJid)9 Resourcepart (org.jxmpp.jid.parts.Resourcepart)9 ArrayList (java.util.ArrayList)8 Localpart (org.jxmpp.jid.parts.Localpart)8 Cursor (android.database.Cursor)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 HashMap (java.util.HashMap)5 SwingWorker (org.jivesoftware.spark.util.SwingWorker)5 IOException (java.io.IOException)4 KeyManagementException (java.security.KeyManagementException)4 NetworkException (com.xabber.android.data.NetworkException)3 InetAddress (java.net.InetAddress)3