Search in sources :

Example 1 with RemoteConnectionException

use of org.jivesoftware.smack.util.rce.RemoteConnectionException in project Smack by igniterealtime.

the class RemoteXmppTcpConnectionEndpointsTest method testConnectionException.

@Test
public void testConnectionException() {
    List<RemoteConnectionException<? extends RemoteConnectionEndpoint>> connectionExceptions = new ArrayList<>();
    {
        A aRr = new A("1.2.3.4");
        UInt16 port = UInt16.from(1234);
        String host = "example.org";
        IpTcpRemoteConnectionEndpoint<A> remoteConnectionEndpoint = new IpTcpRemoteConnectionEndpoint<>(host, port, aRr);
        Exception exception = new Exception("Failed for some reason");
        RemoteConnectionException<IpTcpRemoteConnectionEndpoint<A>> remoteConnectionException = RemoteConnectionException.from(remoteConnectionEndpoint, exception);
        connectionExceptions.add(remoteConnectionException);
    }
    {
        A aRr = new A("1.3.3.7");
        UInt16 port = UInt16.from(5678);
        String host = "other.example.org";
        IpTcpRemoteConnectionEndpoint<A> remoteConnectionEndpoint = new IpTcpRemoteConnectionEndpoint<>(host, port, aRr);
        Exception exception = new Exception("Failed for some other reason");
        RemoteConnectionException<IpTcpRemoteConnectionEndpoint<A>> remoteConnectionException = RemoteConnectionException.from(remoteConnectionEndpoint, exception);
        connectionExceptions.add(remoteConnectionException);
    }
    List<RemoteConnectionEndpointLookupFailure> lookupFailures = Collections.emptyList();
    SmackException.EndpointConnectionException endpointConnectionException = SmackException.EndpointConnectionException.from(lookupFailures, connectionExceptions);
    String message = endpointConnectionException.getMessage();
    assertEquals("The following addresses failed: " + "'RFC 6120 A/AAAA Endpoint + [example.org:1234] (/1.2.3.4:1234)' failed because: java.lang.Exception: Failed for some reason, " + "'RFC 6120 A/AAAA Endpoint + [other.example.org:5678] (/1.3.3.7:5678)' failed because: java.lang.Exception: Failed for some other reason", message);
}
Also used : A(org.minidns.record.A) RemoteConnectionException(org.jivesoftware.smack.util.rce.RemoteConnectionException) RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) SmackException(org.jivesoftware.smack.SmackException) ArrayList(java.util.ArrayList) SmackException(org.jivesoftware.smack.SmackException) RemoteConnectionException(org.jivesoftware.smack.util.rce.RemoteConnectionException) UInt16(org.jivesoftware.smack.datatypes.UInt16) RemoteConnectionEndpoint(org.jivesoftware.smack.util.rce.RemoteConnectionEndpoint) Test(org.junit.jupiter.api.Test)

Example 2 with RemoteConnectionException

use of org.jivesoftware.smack.util.rce.RemoteConnectionException in project Smack by igniterealtime.

the class XMPPTCPConnection method connectUsingConfiguration.

private void connectUsingConfiguration() throws ConnectionException, IOException, InterruptedException {
    RemoteXmppTcpConnectionEndpoints.Result<Rfc6120TcpRemoteConnectionEndpoint> result = RemoteXmppTcpConnectionEndpoints.lookup(config);
    List<RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint>> connectionExceptions = new ArrayList<>();
    SocketFactory socketFactory = config.getSocketFactory();
    ProxyInfo proxyInfo = config.getProxyInfo();
    int timeout = config.getConnectTimeout();
    if (socketFactory == null) {
        socketFactory = SocketFactory.getDefault();
    }
    for (Rfc6120TcpRemoteConnectionEndpoint endpoint : result.discoveredRemoteConnectionEndpoints) {
        Iterator<? extends InetAddress> inetAddresses;
        String host = endpoint.getHost().toString();
        UInt16 portUint16 = endpoint.getPort();
        int port = portUint16.intValue();
        if (proxyInfo == null) {
            inetAddresses = endpoint.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.
                SmackFuture.SocketFuture socketFuture = new SmackFuture.SocketFuture(socketFactory);
                final InetAddress inetAddress = inetAddresses.next();
                final InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, port);
                LOGGER.finer("Trying to establish TCP connection to " + inetSocketAddress);
                socketFuture.connectAsync(inetSocketAddress, timeout);
                try {
                    socket = socketFuture.getOrThrow();
                } catch (IOException e) {
                    RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint> rce = new RemoteConnectionException<>(endpoint, inetAddress, e);
                    connectionExceptions.add(rce);
                    if (inetAddresses.hasNext()) {
                        continue innerloop;
                    } else {
                        break innerloop;
                    }
                }
                LOGGER.finer("Established TCP connection to " + inetSocketAddress);
                // We found a host to connect to, return here
                this.host = host;
                this.port = portUint16;
                return;
            }
        } else {
            // TODO: Move this into the inner-loop above. There appears no reason why we should not try a proxy
            // connection to every inet address of each connection endpoint.
            socket = socketFactory.createSocket();
            StringUtils.requireNotNullNorEmpty(host, "Host of endpoint " + endpoint + " 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) {
                CloseableUtil.maybeClose(socket, LOGGER);
                RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint> rce = new RemoteConnectionException<>(endpoint, null, e);
                connectionExceptions.add(rce);
                continue;
            }
            LOGGER.finer("Established TCP connection to " + hostAndPort);
            // We found a host to connect to, return here
            this.host = host;
            this.port = portUint16;
            return;
        }
    }
    // HostAddresses in the exception
    throw EndpointConnectionException.from(result.lookupFailures, connectionExceptions);
}
Also used : RemoteConnectionException(org.jivesoftware.smack.util.rce.RemoteConnectionException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Rfc6120TcpRemoteConnectionEndpoint(org.jivesoftware.smack.tcp.rce.Rfc6120TcpRemoteConnectionEndpoint) SmackFuture(org.jivesoftware.smack.SmackFuture) ProxyInfo(org.jivesoftware.smack.proxy.ProxyInfo) RemoteXmppTcpConnectionEndpoints(org.jivesoftware.smack.tcp.rce.RemoteXmppTcpConnectionEndpoints) Rfc6120TcpRemoteConnectionEndpoint(org.jivesoftware.smack.tcp.rce.Rfc6120TcpRemoteConnectionEndpoint) UInt16(org.jivesoftware.smack.datatypes.UInt16) InetAddress(java.net.InetAddress)

Aggregations

ArrayList (java.util.ArrayList)2 UInt16 (org.jivesoftware.smack.datatypes.UInt16)2 RemoteConnectionException (org.jivesoftware.smack.util.rce.RemoteConnectionException)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketFactory (javax.net.SocketFactory)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 SmackException (org.jivesoftware.smack.SmackException)1 SmackFuture (org.jivesoftware.smack.SmackFuture)1 ProxyInfo (org.jivesoftware.smack.proxy.ProxyInfo)1 RemoteXmppTcpConnectionEndpoints (org.jivesoftware.smack.tcp.rce.RemoteXmppTcpConnectionEndpoints)1 Rfc6120TcpRemoteConnectionEndpoint (org.jivesoftware.smack.tcp.rce.Rfc6120TcpRemoteConnectionEndpoint)1 RemoteConnectionEndpoint (org.jivesoftware.smack.util.rce.RemoteConnectionEndpoint)1 RemoteConnectionEndpointLookupFailure (org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure)1 Test (org.junit.jupiter.api.Test)1 A (org.minidns.record.A)1