Search in sources :

Example 1 with ProxyInfo

use of org.jivesoftware.smack.proxy.ProxyInfo 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

IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)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 SocketFactory (javax.net.SocketFactory)1 SmackException (org.jivesoftware.smack.SmackException)1 AlreadyConnectedException (org.jivesoftware.smack.SmackException.AlreadyConnectedException)1 AlreadyLoggedInException (org.jivesoftware.smack.SmackException.AlreadyLoggedInException)1 ConnectionException (org.jivesoftware.smack.SmackException.ConnectionException)1 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 SecurityRequiredByServerException (org.jivesoftware.smack.SmackException.SecurityRequiredByServerException)1 SynchronizationPoint (org.jivesoftware.smack.SynchronizationPoint)1 XMPPException (org.jivesoftware.smack.XMPPException)1 FailedNonzaException (org.jivesoftware.smack.XMPPException.FailedNonzaException)1