Search in sources :

Example 1 with ConnectionException

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

the class SmackExceptionTest method testConnectionException.

@Test
public void testConnectionException() throws UnknownHostException {
    List<HostAddress> failedAddresses = new LinkedList<HostAddress>();
    String host = "foo.bar.example";
    InetAddress inetAddress = InetAddress.getByAddress(host, new byte[] { 0, 0, 0, 0 });
    List<InetAddress> inetAddresses = Collections.singletonList(inetAddress);
    HostAddress hostAddress = new HostAddress(host, 1234, inetAddresses);
    hostAddress.setException(new Exception("Failed for some reason"));
    failedAddresses.add(hostAddress);
    host = "barz.example";
    inetAddress = InetAddress.getByAddress(host, new byte[] { 0, 0, 0, 0 });
    inetAddresses = Collections.singletonList(inetAddress);
    hostAddress = new HostAddress(host, 5678, inetAddresses);
    hostAddress.setException(new Exception("Failed for some other reason"));
    failedAddresses.add(hostAddress);
    ConnectionException connectionException = ConnectionException.from(failedAddresses);
    String message = connectionException.getMessage();
    assertEquals("The following addresses failed: 'foo.bar.example:1234' failed because: java.lang.Exception: Failed for some reason, 'barz.example:5678' failed because: java.lang.Exception: Failed for some other reason", message);
}
Also used : HostAddress(org.jivesoftware.smack.util.dns.HostAddress) InetAddress(java.net.InetAddress) LinkedList(java.util.LinkedList) ConnectionException(org.jivesoftware.smack.SmackException.ConnectionException) UnknownHostException(java.net.UnknownHostException) ConnectionException(org.jivesoftware.smack.SmackException.ConnectionException) Test(org.junit.Test)

Example 2 with ConnectionException

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

the class XMPPBOSHConnection method connectInternal.

@Override
protected void connectInternal() throws SmackException, InterruptedException {
    done = false;
    notified = false;
    try {
        // Ensure a clean starting state
        if (client != null) {
            client.close();
            client = null;
        }
        sessionID = null;
        // Initialize BOSH client
        BOSHClientConfig.Builder cfgBuilder = BOSHClientConfig.Builder.create(config.getURI(), config.getXMPPServiceDomain().toString());
        if (config.isProxyEnabled()) {
            cfgBuilder.setProxy(config.getProxyAddress(), config.getProxyPort());
        }
        client = BOSHClient.create(cfgBuilder.build());
        client.addBOSHClientConnListener(new BOSHConnectionListener());
        client.addBOSHClientResponseListener(new BOSHPacketReader());
        // Initialize the debugger
        if (config.isDebuggerEnabled()) {
            initDebugger();
            if (isFirstInitialization) {
                if (debugger.getReaderListener() != null) {
                    addAsyncStanzaListener(debugger.getReaderListener(), null);
                }
                if (debugger.getWriterListener() != null) {
                    addPacketSendingListener(debugger.getWriterListener(), null);
                }
            }
        }
        // Send the session creation request
        client.send(ComposableBody.builder().setNamespaceDefinition("xmpp", XMPP_BOSH_NS).setAttribute(BodyQName.createWithPrefix(XMPP_BOSH_NS, "version", "xmpp"), "1.0").build());
    } catch (Exception e) {
        throw new ConnectionException(e);
    }
    // Wait for the response from the server
    synchronized (this) {
        if (!connected) {
            final long deadline = System.currentTimeMillis() + getReplyTimeout();
            while (!notified) {
                final long now = System.currentTimeMillis();
                if (now >= deadline)
                    break;
                wait(deadline - now);
            }
        }
    }
    // If there is no feedback, throw an remote server timeout error
    if (!connected && !done) {
        done = true;
        String errorMessage = "Timeout reached for the connection to " + getHost() + ":" + getPort() + ".";
        throw new SmackException(errorMessage);
    }
    tlsHandled.reportSuccess();
    saslFeatureReceived.reportSuccess();
}
Also used : SmackException(org.jivesoftware.smack.SmackException) BOSHClientConfig(org.igniterealtime.jbosh.BOSHClientConfig) SmackException(org.jivesoftware.smack.SmackException) ConnectionException(org.jivesoftware.smack.SmackException.ConnectionException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) BOSHException(org.igniterealtime.jbosh.BOSHException) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionException(org.jivesoftware.smack.SmackException.ConnectionException)

Example 3 with ConnectionException

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

the class XMPPTCPConnection method connectUsingConfiguration.

private void connectUsingConfiguration() throws ConnectionException, IOException {
    List<HostAddress> failedAddresses = populateHostAddresses();
    SocketFactory socketFactory = config.getSocketFactory();
    ProxyInfo proxyInfo = config.getProxyInfo();
    int timeout = config.getConnectTimeout();
    if (socketFactory == null) {
        socketFactory = SocketFactory.getDefault();
    }
    for (HostAddress hostAddress : hostAddresses) {
        Iterator<InetAddress> inetAddresses = null;
        String host = hostAddress.getFQDN();
        int port = hostAddress.getPort();
        if (proxyInfo == null) {
            inetAddresses = hostAddress.getInetAddresses().iterator();
            assert (inetAddresses.hasNext());
            innerloop: while (inetAddresses.hasNext()) {
                // Create a *new* Socket before every connection attempt, i.e. connect() call, since Sockets are not
                // re-usable after a failed connection attempt. See also SMACK-724.
                socket = socketFactory.createSocket();
                final InetAddress inetAddress = inetAddresses.next();
                final String inetAddressAndPort = inetAddress + " at port " + port;
                LOGGER.finer("Trying to establish TCP connection to " + inetAddressAndPort);
                try {
                    socket.connect(new InetSocketAddress(inetAddress, port), timeout);
                } catch (Exception e) {
                    hostAddress.setException(inetAddress, e);
                    if (inetAddresses.hasNext()) {
                        continue innerloop;
                    } else {
                        break innerloop;
                    }
                }
                LOGGER.finer("Established TCP connection to " + inetAddressAndPort);
                // We found a host to connect to, return here
                this.host = host;
                this.port = port;
                return;
            }
            failedAddresses.add(hostAddress);
        } else {
            socket = socketFactory.createSocket();
            StringUtils.requireNotNullOrEmpty(host, "Host of HostAddress " + hostAddress + " must not be null when using a Proxy");
            final String hostAndPort = host + " at port " + port;
            LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort);
            try {
                proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout);
            } catch (IOException e) {
                hostAddress.setException(e);
                continue;
            }
            LOGGER.finer("Established TCP connection to " + hostAndPort);
            // We found a host to connect to, return here
            this.host = host;
            this.port = port;
            return;
        }
    }
    // HostAddresses in the exception
    throw ConnectionException.from(failedAddresses);
}
Also used : ProxyInfo(org.jivesoftware.smack.proxy.ProxyInfo) SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) HostAddress(org.jivesoftware.smack.util.dns.HostAddress) InetAddress(java.net.InetAddress) SynchronizationPoint(org.jivesoftware.smack.SynchronizationPoint) 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

ConnectionException (org.jivesoftware.smack.SmackException.ConnectionException)3 IOException (java.io.IOException)2 InetAddress (java.net.InetAddress)2 SmackException (org.jivesoftware.smack.SmackException)2 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)2 XMPPException (org.jivesoftware.smack.XMPPException)2 StreamErrorException (org.jivesoftware.smack.XMPPException.StreamErrorException)2 HostAddress (org.jivesoftware.smack.util.dns.HostAddress)2 InetSocketAddress (java.net.InetSocketAddress)1 UnknownHostException (java.net.UnknownHostException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 CertificateException (java.security.cert.CertificateException)1 LinkedList (java.util.LinkedList)1 SocketFactory (javax.net.SocketFactory)1 BOSHClientConfig (org.igniterealtime.jbosh.BOSHClientConfig)1 BOSHException (org.igniterealtime.jbosh.BOSHException)1