Search in sources :

Example 56 with SocketFactory

use of javax.net.SocketFactory in project mockito by mockito.

the class DeepStubbingTest method withSimplePrimitive.

/**
     * Test that deep stubbing work with primitive expected values
     */
@Test
public void withSimplePrimitive() throws Exception {
    int a = 32;
    SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
    when(sf.createSocket().getPort()).thenReturn(a);
    assertEquals(a, sf.createSocket().getPort());
}
Also used : SocketFactory(javax.net.SocketFactory) Test(org.junit.Test)

Example 57 with SocketFactory

use of javax.net.SocketFactory in project mockito by mockito.

the class DeepStubbingTest method withAnyPatternArguments.

/**
     * Test that deep stubbing work with argument patterns
     */
@Test
public void withAnyPatternArguments() throws Exception {
    OutputStream out = new ByteArrayOutputStream();
    //TODO: should not use javax in case it changes
    SocketFactory sf = mock(SocketFactory.class, RETURNS_DEEP_STUBS);
    when(sf.createSocket(anyString(), anyInt()).getOutputStream()).thenReturn(out);
    assertSame(out, sf.createSocket("google.com", 80).getOutputStream());
    assertSame(out, sf.createSocket("stackoverflow.com", 8080).getOutputStream());
}
Also used : SocketFactory(javax.net.SocketFactory) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 58 with SocketFactory

use of javax.net.SocketFactory in project keywhiz by square.

the class LdapConnectionFactory method getLDAPConnection.

public LDAPConnection getLDAPConnection(String userDN, String password) throws LDAPException, GeneralSecurityException {
    TrustStoreTrustManager trust = new TrustStoreTrustManager(trustStorePath, trustStorePassword.toCharArray(), trustStoreType, false);
    LDAPConnectionOptions options = new LDAPConnectionOptions();
    options.setSSLSocketVerifier(new HostNameSSLSocketVerifier(false));
    SSLUtil sslUtil = new SSLUtil(trust);
    SocketFactory factory = new EndpointIdentificationSocketFactory(sslUtil.createSSLSocketFactory("TLSv1.2"));
    LDAPConnection ldapConnection = new LDAPConnection(factory, options);
    // Connect, retrieve the DN of the user (if any)
    ldapConnection.connect(server, port);
    ldapConnection.bind(userDN, password);
    return ldapConnection;
}
Also used : LDAPConnectionOptions(com.unboundid.ldap.sdk.LDAPConnectionOptions) SSLUtil(com.unboundid.util.ssl.SSLUtil) HostNameSSLSocketVerifier(com.unboundid.util.ssl.HostNameSSLSocketVerifier) TrustStoreTrustManager(com.unboundid.util.ssl.TrustStoreTrustManager) SocketFactory(javax.net.SocketFactory) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection)

Example 59 with SocketFactory

use of javax.net.SocketFactory in project jdk8u_jdk by JetBrains.

the class SslRMIClientSocketFactory method createSocket.

/**
     * <p>Creates an SSL socket.</p>
     *
     * <p>If the system property
     * <code>javax.rmi.ssl.client.enabledCipherSuites</code> is
     * specified, this method will call {@link
     * SSLSocket#setEnabledCipherSuites(String[])} before returning
     * the socket. The value of this system property is a string that
     * is a comma-separated list of SSL/TLS cipher suites to
     * enable.</p>
     *
     * <p>If the system property
     * <code>javax.rmi.ssl.client.enabledProtocols</code> is
     * specified, this method will call {@link
     * SSLSocket#setEnabledProtocols(String[])} before returning the
     * socket. The value of this system property is a string that is a
     * comma-separated list of SSL/TLS protocol versions to
     * enable.</p>
     */
public Socket createSocket(String host, int port) throws IOException {
    // Retrieve the SSLSocketFactory
    //
    final SocketFactory sslSocketFactory = getDefaultClientSocketFactory();
    // Create the SSLSocket
    //
    final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
    // Set the SSLSocket Enabled Cipher Suites
    //
    final String enabledCipherSuites = System.getProperty("javax.rmi.ssl.client.enabledCipherSuites");
    if (enabledCipherSuites != null) {
        StringTokenizer st = new StringTokenizer(enabledCipherSuites, ",");
        int tokens = st.countTokens();
        String[] enabledCipherSuitesList = new String[tokens];
        for (int i = 0; i < tokens; i++) {
            enabledCipherSuitesList[i] = st.nextToken();
        }
        try {
            sslSocket.setEnabledCipherSuites(enabledCipherSuitesList);
        } catch (IllegalArgumentException e) {
            throw (IOException) new IOException(e.getMessage()).initCause(e);
        }
    }
    // Set the SSLSocket Enabled Protocols
    //
    final String enabledProtocols = System.getProperty("javax.rmi.ssl.client.enabledProtocols");
    if (enabledProtocols != null) {
        StringTokenizer st = new StringTokenizer(enabledProtocols, ",");
        int tokens = st.countTokens();
        String[] enabledProtocolsList = new String[tokens];
        for (int i = 0; i < tokens; i++) {
            enabledProtocolsList[i] = st.nextToken();
        }
        try {
            sslSocket.setEnabledProtocols(enabledProtocolsList);
        } catch (IllegalArgumentException e) {
            throw (IOException) new IOException(e.getMessage()).initCause(e);
        }
    }
    //
    return sslSocket;
}
Also used : StringTokenizer(java.util.StringTokenizer) SocketFactory(javax.net.SocketFactory) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException)

Example 60 with SocketFactory

use of javax.net.SocketFactory in project oxCore by GluuFederation.

the class EasySSLProtocolSocketFactory method createSocket.

/**
	 * Attempts to get a new socket connection to the given host within the
	 * given time limit.
	 * <p>
	 * To circumvent the limitations of older JREs that do not support connect
	 * timeout a controller thread is executed. The controller thread attempts
	 * to create a new socket within the given limit of time. If socket
	 * constructor does not return until the timeout expires, the controller
	 * terminates and throws an {@link ConnectTimeoutException}
	 * </p>
	 * 
	 * @param host
	 *            the host name/IP
	 * @param port
	 *            the port on the host
	 * @param localAddress
	 *            the local host name/IP to bind the socket to
	 * @param localPort
	 *            the port on the local machine
	 * @param params
	 *            {@link HttpConnectionParams Http connection parameters}
	 * 
	 * @return Socket a new socket
	 * 
	 * @throws IOException
	 *             if an I/O error occurs while creating the socket
	 * @throws UnknownHostException
	 *             if the IP address of the host cannot be determined
	 */
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}
Also used : SocketFactory(javax.net.SocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) SecureProtocolSocketFactory(org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

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