Search in sources :

Example 31 with SocketFactory

use of javax.net.SocketFactory in project camel by apache.

the class FtpInitialConnectTimeoutTest method createSocketFactory.

private SocketFactory createSocketFactory() throws IOException {
    SocketFactory socketFactory = mock(SocketFactory.class);
    when(socketFactory.createSocket()).thenAnswer(new SocketAnswer());
    return socketFactory;
}
Also used : SocketFactory(javax.net.SocketFactory)

Example 32 with SocketFactory

use of javax.net.SocketFactory 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)

Example 33 with SocketFactory

use of javax.net.SocketFactory in project rest.li by linkedin.

the class TrustingSocketFactory method connectSocket.

public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException {
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (connectTimeout == 0) {
        return socketfactory.createSocket(host.getHostName(), host.getPort(), localAddress.getAddress(), localAddress.getPort());
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress.getAddress(), localAddress.getPort());
        SocketAddress remoteaddr = new InetSocketAddress(remoteAddress.getAddress(), remoteAddress.getPort());
        socket.bind(localaddr);
        socket.connect(remoteaddr, connectTimeout);
        return socket;
    }
}
Also used : SocketFactory(javax.net.SocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 34 with SocketFactory

use of javax.net.SocketFactory in project java-apns by notnoop.

the class MockingUtils method mockSocketFactory.

static SocketFactory mockSocketFactory(OutputStream out, InputStream in) {
    try {
        Socket socket = mock(Socket.class);
        when(socket.getOutputStream()).thenReturn(out);
        when(socket.getInputStream()).thenReturn(in);
        SocketFactory factory = mock(SocketFactory.class);
        when(factory.createSocket()).thenReturn(socket);
        when(factory.createSocket(anyString(), anyInt())).thenReturn(socket);
        return factory;
    } catch (Exception e) {
        e.printStackTrace();
        throw new AssertionError("Cannot be here!");
    }
}
Also used : SocketFactory(javax.net.SocketFactory) Socket(java.net.Socket) IOException(java.io.IOException)

Example 35 with SocketFactory

use of javax.net.SocketFactory in project java-apns by notnoop.

the class ApnsConnectionTest method errorOnce.

@Test
public void errorOnce() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    SocketFactory factory = mockClosedThenOpenSocket(baos, null, false, 1);
    packetSentRegardless(factory, baos);
}
Also used : SocketFactory(javax.net.SocketFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

SocketFactory (javax.net.SocketFactory)66 Socket (java.net.Socket)25 Test (org.junit.Test)25 IOException (java.io.IOException)18 InetSocketAddress (java.net.InetSocketAddress)14 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 SSLSocket (javax.net.ssl.SSLSocket)10 OutputStream (java.io.OutputStream)9 ServerSocket (java.net.ServerSocket)9 SocketAddress (java.net.SocketAddress)6 Configuration (org.apache.hadoop.conf.Configuration)5 ServerSocketFactory (javax.net.ServerSocketFactory)4 InputStream (java.io.InputStream)3 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)3 StandardSocketFactory (org.apache.hadoop.net.StandardSocketFactory)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 SocketException (java.net.SocketException)2