Search in sources :

Example 11 with NoResponseException

use of org.jivesoftware.smack.SmackException.NoResponseException in project Smack by igniterealtime.

the class PingTest method checkFailedPingOnTimeout.

/**
 * DummyConnection will not reply so it will timeout.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 * @throws InterruptedException if the calling thread was interrupted.
 */
@Test
public void checkFailedPingOnTimeout() throws SmackException, IOException, XMPPException, InterruptedException {
    DummyConnection dummyCon = getAuthenticatedDummyConnectionWithoutIqReplies();
    PingManager pinger = PingManager.getInstanceFor(dummyCon);
    try {
        pinger.ping(DUMMY_AT_EXAMPLE_ORG);
    } catch (NoResponseException e) {
        return;
    }
    fail();
}
Also used : DummyConnection(org.jivesoftware.smack.DummyConnection) ThreadedDummyConnection(org.jivesoftware.smack.ThreadedDummyConnection) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) Test(org.junit.jupiter.api.Test)

Example 12 with NoResponseException

use of org.jivesoftware.smack.SmackException.NoResponseException in project Smack by igniterealtime.

the class TransportNegotiator method delayedCheckBestCandidate.

/**
 * Launch a thread that checks, after some time, if any of the candidates
 * offered by the other endpoint is usable. The thread does not check the
 * candidates: it just checks if we have got a valid one and sends an Accept
 * in that case.
 */
private void delayedCheckBestCandidate(final JingleSession js, final Jingle jin) {
    // 
    if (resultThread == null && !getRemoteCandidates().isEmpty()) {
        resultThread = new Thread(new Runnable() {

            @Override
            public void run() {
                // Sleep for some time, waiting for the candidates checks
                int totalTime = CANDIDATES_ACCEPT_PERIOD + TransportResolver.CHECK_TIMEOUT;
                int tries = (int) Math.ceil(totalTime / 1000);
                for (int i = 0; i < tries - 1; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        LOGGER.log(Level.WARNING, "exception", e);
                    }
                    // Once we are in pending state, look for any valid remote
                    // candidate, and send an "accept" if we have one...
                    TransportCandidate bestRemote = getBestRemoteCandidate();
                    if (bestRemote != null && getNegotiatorState() == JingleNegotiatorState.PENDING) {
                        // Accepting the remote candidate
                        if (!acceptedRemoteCandidates.contains(bestRemote)) {
                            Jingle jout = new Jingle(JingleActionEnum.CONTENT_ACCEPT);
                            JingleContent content = parentNegotiator.getJingleContent();
                            content.addJingleTransport(getJingleTransport(bestRemote));
                            jout.addContent(content);
                            // Send the packet
                            try {
                                js.sendFormattedJingle(jin, jout);
                            } catch (InterruptedException | NotConnectedException e) {
                                throw new IllegalStateException(e);
                            }
                            acceptedRemoteCandidates.add(bestRemote);
                        }
                        if (isEstablished() && getNegotiatorState() == JingleNegotiatorState.PENDING) {
                            setNegotiatorState(JingleNegotiatorState.SUCCEEDED);
                            try {
                                triggerTransportEstablished(getAcceptedLocalCandidate(), bestRemote);
                            } catch (InterruptedException | NotConnectedException | NoResponseException | XMPPErrorException e) {
                                throw new IllegalStateException(e);
                            }
                            break;
                        }
                    }
                }
                // Once we are in pending state, look for any valid remote
                // candidate, and send an "accept" if we have one...
                TransportCandidate bestRemote = getBestRemoteCandidate();
                if (bestRemote == null) {
                    boolean foundRemoteRelay = false;
                    for (TransportCandidate candidate : remoteCandidates) {
                        if (candidate instanceof ICECandidate) {
                            ICECandidate iceCandidate = (ICECandidate) candidate;
                            if (iceCandidate.getType().equals(ICECandidate.Type.relay)) {
                                // TODO Check if the relay is reachable.
                                addValidRemoteCandidate(iceCandidate);
                                foundRemoteRelay = true;
                            }
                        }
                    }
                    // We should accept the Public One if we received it, otherwise, accepts any.
                    if (!foundRemoteRelay) {
                        boolean foundLocalRelay = false;
                        for (TransportCandidate candidate : offeredCandidates) {
                            if (candidate instanceof ICECandidate) {
                                ICECandidate iceCandidate = (ICECandidate) candidate;
                                if (iceCandidate.getType().equals(ICECandidate.Type.relay)) {
                                    foundLocalRelay = true;
                                }
                            }
                        }
                        if (foundLocalRelay) {
                            boolean foundRemotePublic = false;
                            for (TransportCandidate candidate : remoteCandidates) {
                                if (candidate instanceof ICECandidate) {
                                    ICECandidate iceCandidate = (ICECandidate) candidate;
                                    if (iceCandidate.getType().equals(ICECandidate.Type.srflx)) {
                                        addValidRemoteCandidate(iceCandidate);
                                        foundRemotePublic = true;
                                    }
                                }
                            }
                            if (!foundRemotePublic) {
                                for (TransportCandidate candidate : remoteCandidates) {
                                    if (candidate instanceof ICECandidate) {
                                        ICECandidate iceCandidate = (ICECandidate) candidate;
                                        addValidRemoteCandidate(iceCandidate);
                                    }
                                }
                            }
                        }
                    }
                }
                for (int i = 0; i < 6; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        LOGGER.log(Level.WARNING, "exception", e);
                    }
                    bestRemote = getBestRemoteCandidate();
                    // State state = getState();
                    if (bestRemote != null && getNegotiatorState() == JingleNegotiatorState.PENDING) {
                        if (!acceptedRemoteCandidates.contains(bestRemote)) {
                            Jingle jout = new Jingle(JingleActionEnum.CONTENT_ACCEPT);
                            JingleContent content = parentNegotiator.getJingleContent();
                            content.addJingleTransport(getJingleTransport(bestRemote));
                            jout.addContent(content);
                            // Send the packet
                            try {
                                js.sendFormattedJingle(jin, jout);
                            } catch (InterruptedException | NotConnectedException e) {
                                throw new IllegalStateException(e);
                            }
                            acceptedRemoteCandidates.add(bestRemote);
                        }
                        if (isEstablished()) {
                            setNegotiatorState(JingleNegotiatorState.SUCCEEDED);
                            break;
                        }
                    }
                }
                if (getNegotiatorState() != JingleNegotiatorState.SUCCEEDED) {
                    try {
                        session.terminate("Unable to negotiate session. This may be caused by firewall configuration problems.");
                    } catch (Exception e) {
                        LOGGER.log(Level.WARNING, "exception", e);
                    }
                }
            }
        }, "Waiting for all the transport candidates checks...");
        resultThread.setName("Transport Resolver Result");
        resultThread.start();
    }
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) JingleException(org.jivesoftware.smackx.jingleold.JingleException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) XMPPException(org.jivesoftware.smack.XMPPException) Jingle(org.jivesoftware.smackx.jingleold.packet.Jingle) JingleTransportCandidate(org.jivesoftware.smackx.jingleold.packet.JingleTransport.JingleTransportCandidate) JingleContent(org.jivesoftware.smackx.jingleold.packet.JingleContent) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException)

Example 13 with NoResponseException

use of org.jivesoftware.smack.SmackException.NoResponseException in project Smack by igniterealtime.

the class PainlessOpenPgpProvider method decryptAndOrVerify.

@Override
public OpenPgpMessage decryptAndOrVerify(XMPPConnection connection, OpenPgpElement element, final OpenPgpSelf self, final OpenPgpContact sender) throws IOException, PGPException {
    ByteArrayOutputStream plainText = new ByteArrayOutputStream();
    InputStream cipherText = element.toInputStream();
    PGPPublicKeyRingCollection announcedPublicKeys = sender.getAnnouncedPublicKeys();
    if (announcedPublicKeys == null) {
        try {
            sender.updateKeys(connection);
            announcedPublicKeys = sender.getAnnouncedPublicKeys();
        } catch (InterruptedException | NotALeafNodeException | NotAPubSubNodeException | NotConnectedException | NoResponseException | XMPPErrorException e) {
            throw new PGPException("Abort decryption due to lack of keys", e);
        }
    }
    MissingPublicKeyCallback missingPublicKeyCallback = new MissingPublicKeyCallback() {

        @Override
        public PGPPublicKeyRing onMissingPublicKeyEncountered(Long keyId) {
            try {
                sender.updateKeys(connection);
                PGPPublicKeyRingCollection anyKeys = sender.getAnyPublicKeys();
                for (PGPPublicKeyRing ring : anyKeys) {
                    if (ring.getPublicKey(keyId) != null) {
                        return ring;
                    }
                }
                return null;
            } catch (InterruptedException | NotALeafNodeException | NotAPubSubNodeException | NotConnectedException | NoResponseException | XMPPErrorException | IOException | PGPException e) {
                LOGGER.log(Level.WARNING, "Cannot fetch missing key " + keyId, e);
                return null;
            }
        }
    };
    DecryptionStream cipherStream = PGPainless.decryptAndOrVerify().onInputStream(cipherText).withOptions(new ConsumerOptions().addDecryptionKeys(self.getSecretKeys(), getStore().getKeyRingProtector()).addVerificationCerts(announcedPublicKeys).setMissingCertificateCallback(missingPublicKeyCallback));
    Streams.pipeAll(cipherStream, plainText);
    cipherText.close();
    cipherStream.close();
    plainText.close();
    OpenPgpMetadata info = cipherStream.getResult();
    OpenPgpMessage.State state;
    if (info.isSigned()) {
        if (info.isEncrypted()) {
            state = OpenPgpMessage.State.signcrypt;
        } else {
            state = OpenPgpMessage.State.sign;
        }
    } else if (info.isEncrypted()) {
        state = OpenPgpMessage.State.crypt;
    } else {
        throw new PGPException("Received message appears to be neither encrypted, nor signed.");
    }
    return new OpenPgpMessage(plainText.toByteArray(), state, info);
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NotALeafNodeException(org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) InputStream(java.io.InputStream) ConsumerOptions(org.pgpainless.decryption_verification.ConsumerOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DecryptionStream(org.pgpainless.decryption_verification.DecryptionStream) PGPException(org.bouncycastle.openpgp.PGPException) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) NotAPubSubNodeException(org.jivesoftware.smackx.pubsub.PubSubException.NotAPubSubNodeException) OpenPgpMessage(org.jivesoftware.smackx.ox.OpenPgpMessage) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) MissingPublicKeyCallback(org.pgpainless.decryption_verification.MissingPublicKeyCallback) OpenPgpMetadata(org.pgpainless.decryption_verification.OpenPgpMetadata)

Example 14 with NoResponseException

use of org.jivesoftware.smack.SmackException.NoResponseException in project Smack by igniterealtime.

the class IntTestUtil method disconnectAndMaybeDelete.

public static void disconnectAndMaybeDelete(XMPPTCPConnection connection, Configuration config) throws InterruptedException {
    try {
        if (!config.isAccountRegistrationPossible()) {
            return;
        }
        Configuration.AccountRegistration accountDeletionMethod = config.accountRegistration;
        AccountManager accountManager = AccountManager.getInstance(connection);
        try {
            if (accountManager.isSupported()) {
                accountDeletionMethod = AccountRegistration.inBandRegistration;
            }
        } catch (NoResponseException | XMPPErrorException | NotConnectedException e) {
            LOGGER.log(Level.WARNING, "Could not test if XEP-0077 account deletion is possible", e);
        }
        switch(accountDeletionMethod) {
            case inBandRegistration:
                deleteViaIbr(connection);
                break;
            case serviceAdministration:
                deleteViaServiceAdministration(connection, config);
                break;
            default:
                throw new AssertionError();
        }
    } finally {
        connection.disconnect();
    }
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) AccountRegistration(org.igniterealtime.smack.inttest.Configuration.AccountRegistration) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) AccountManager(org.jivesoftware.smackx.iqregister.AccountManager) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException)

Example 15 with NoResponseException

use of org.jivesoftware.smack.SmackException.NoResponseException in project Smack by igniterealtime.

the class XMPPTCPConnection method shutdown.

private void shutdown(boolean instant) {
    if (disconnectedButResumeable) {
        return;
    }
    // the server
    if (packetWriter != null) {
        LOGGER.finer("PacketWriter shutdown()");
        packetWriter.shutdown(instant);
    }
    LOGGER.finer("PacketWriter has been shut down");
    if (!instant) {
        try {
            // After we send the closing stream element, check if there was already a
            // closing stream element sent by the server or wait with a timeout for a
            // closing stream element to be received from the server.
            @SuppressWarnings("unused") Exception res = closingStreamReceived.checkIfSuccessOrWait();
        } catch (InterruptedException | NoResponseException e) {
            LOGGER.log(Level.INFO, "Exception while waiting for closing stream element from the server " + this, e);
        }
    }
    if (packetReader != null) {
        LOGGER.finer("PacketReader shutdown()");
        packetReader.shutdown();
    }
    LOGGER.finer("PacketReader has been shut down");
    try {
        socket.close();
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "shutdown", e);
    }
    setWasAuthenticated();
    // connected (e.g. sendStanza should not throw a NotConnectedException).
    if (isSmResumptionPossible() && instant) {
        disconnectedButResumeable = true;
    } else {
        disconnectedButResumeable = false;
        // Reset the stream management session id to null, since if the stream is cleanly closed, i.e. sending a closing
        // stream tag, there is no longer a stream to resume.
        smSessionId = null;
    }
    authenticated = false;
    connected = false;
    secureSocket = null;
    reader = null;
    writer = null;
    maybeCompressFeaturesReceived.init();
    compressSyncPoint.init();
    smResumedSyncPoint.init();
    smEnabledSyncPoint.init();
    initalOpenStreamSend.init();
}
Also used : NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) FailedNonzaException(org.jivesoftware.smack.XMPPException.FailedNonzaException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionException(org.jivesoftware.smack.SmackException.ConnectionException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) StreamManagementException(org.jivesoftware.smack.sm.StreamManagementException) AlreadyLoggedInException(org.jivesoftware.smack.SmackException.AlreadyLoggedInException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StreamIdDoesNotMatchException(org.jivesoftware.smack.sm.StreamManagementException.StreamIdDoesNotMatchException) StreamManagementNotEnabledException(org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) SecurityRequiredByServerException(org.jivesoftware.smack.SmackException.SecurityRequiredByServerException) AlreadyConnectedException(org.jivesoftware.smack.SmackException.AlreadyConnectedException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)21 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)15 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)14 SmackException (org.jivesoftware.smack.SmackException)7 XMPPException (org.jivesoftware.smack.XMPPException)7 IOException (java.io.IOException)6 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)4 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)3 AccountManager (org.jivesoftware.smackx.iqregister.AccountManager)3 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)3 InputStream (java.io.InputStream)2 KeyManagementException (java.security.KeyManagementException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 UnrecoverableKeyException (java.security.UnrecoverableKeyException)2 CertificateException (java.security.cert.CertificateException)2 AlreadyConnectedException (org.jivesoftware.smack.SmackException.AlreadyConnectedException)2 AlreadyLoggedInException (org.jivesoftware.smack.SmackException.AlreadyLoggedInException)2 ConnectionException (org.jivesoftware.smack.SmackException.ConnectionException)2