Example usage for javax.net SocketFactory createSocket

List of usage examples for javax.net SocketFactory createSocket

Introduction

In this page you can find the example usage for javax.net SocketFactory createSocket.

Prototype

public Socket createSocket() throws IOException 

Source Link

Document

Creates an unconnected socket.

Usage

From source file:SocketFetcher.java

/**
 * Create a socket with the given local address and connected to
 * the given host and port.  Use the specified connection timeout.
 * If a socket factory is specified, use it.  Otherwise, use the
 * SSLSocketFactory if useSSL is true.//from w  w  w  .j  a v  a  2  s .  c o  m
 */
private static Socket createSocket(InetAddress localaddr, int localport, String host, int port, int cto,
        SocketFactory sf, boolean useSSL) throws IOException {
    Socket socket;

    if (sf != null)
        socket = sf.createSocket();
    else if (useSSL)
        socket = SSLSocketFactory.getDefault().createSocket();
    else
        socket = new Socket();
    if (localaddr != null)
        socket.bind(new InetSocketAddress(localaddr, localport));
    if (cto >= 0)
        socket.connect(new InetSocketAddress(host, port), cto);
    else
        socket.connect(new InetSocketAddress(host, port));
    return socket;
}

From source file:org.apache.tajo.ha.HAServiceUtil.java

public static boolean isMasterAlive(String masterName, TajoConf conf) {
    boolean isAlive = true;

    try {/*w w w.  j a  va2s. c  o m*/
        // how to create sockets
        SocketFactory socketFactory = org.apache.hadoop.net.NetUtils.getDefaultSocketFactory(conf);

        int connectionTimeout = conf.getInt(CommonConfigurationKeys.IPC_CLIENT_CONNECT_TIMEOUT_KEY,
                CommonConfigurationKeys.IPC_CLIENT_CONNECT_TIMEOUT_DEFAULT);

        InetSocketAddress server = org.apache.hadoop.net.NetUtils.createSocketAddr(masterName);

        // connected socket
        Socket socket = socketFactory.createSocket();
        org.apache.hadoop.net.NetUtils.connect(socket, server, connectionTimeout);
    } catch (Exception e) {
        isAlive = false;
    }
    return isAlive;
}

From source file:org.apache.camel.itest.ftp.FtpInitialConnectTimeoutTest.java

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

From source file:com.thinkbiganalytics.nifi.security.ApplySecurityPolicy.java

protected void checkHdfsUriForTimeout(Configuration config) throws IOException {
    URI hdfsUri = FileSystem.getDefaultUri(config);
    String address = hdfsUri.getAuthority();
    int port = hdfsUri.getPort();
    if (address == null || address.isEmpty() || port < 0) {
        return;//from   w w  w .j  a  v  a 2  s .c o  m
    }
    InetSocketAddress namenode = NetUtils.createSocketAddr(address, port);
    SocketFactory socketFactory = NetUtils.getDefaultSocketFactory(config);
    Socket socket = null;
    try {
        socket = socketFactory.createSocket();
        NetUtils.connect(socket, namenode, 1000); // 1 second timeout
    } finally {
        IOUtils.closeQuietly(socket);
    }
}

From source file:org.eclipse.ecf.internal.provider.filetransfer.httpclient.ECFHttpClientProtocolSocketFactory.java

public ECFHttpClientProtocolSocketFactory(final SocketFactory socketFactory, ISocketEventSource source,
        ISocketListener socketConnectListener) {
    this(new INonconnectedSocketFactory() {
        public Socket createSocket() throws IOException {
            return socketFactory.createSocket();
        }//from w  w w  .j a v a2s .  c o m

    }, source, socketConnectListener);
}

From source file:org.eclipse.ecf.internal.provider.filetransfer.httpclient4.ECFHttpClientProtocolSocketFactory.java

public ECFHttpClientProtocolSocketFactory(final SocketFactory socketFactory, ISocketEventSource source,
        ISocketListener socketConnectListener) {
    this(new INonconnectedSocketFactory() {
        public Socket createSocket() throws IOException {
            return socketFactory.createSocket();
        }/* www . j av a 2  s. c  om*/
    }, source, socketConnectListener);
}

From source file:com.owncloud.android.network.AdvancedSslSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the
 * given time limit./*from w  ww  .j  ava 2 s  . co  m*/
 * 
 * @param host the host name/IP
 * @param port the port on the host
 * @param clientHost the local host name/IP to bind the socket to
 * @param clientPort 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 {
    Log_OC.d(TAG, "Creating SSL Socket with remote " + host + ":" + port + ", local " + localAddress + ":"
            + localPort + ", params: " + params);
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = mSslContext.getSocketFactory();
    Log_OC.d(TAG, " ... with connection timeout " + timeout + " and socket timeout " + params.getSoTimeout());
    Socket socket = socketfactory.createSocket();
    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.setSoTimeout(params.getSoTimeout());
    socket.bind(localaddr);
    socket.connect(remoteaddr, timeout);
    verifyPeerIdentity(host, port, socket);
    return socket;
}

From source file:ch.cyberduck.core.http.HttpConnectionPoolBuilder.java

protected HttpConnectionPoolBuilder(final Host host, final X509TrustManager trust, final X509KeyManager key,
        final ProxyFinder proxy, final SocketFactory socketFactory) {
    this(host, new PlainConnectionSocketFactory() {
        @Override/*from  w  w  w  . j  av a2  s  .c  o m*/
        public Socket createSocket(final HttpContext context) throws IOException {
            return socketFactory.createSocket();
        }
    }, new SSLConnectionSocketFactory(new CustomTrustSSLProtocolSocketFactory(trust, key),
            new DisabledX509HostnameVerifier()) {
        @Override
        public Socket createSocket(final HttpContext context) throws IOException {
            return socketFactory.createSocket();
        }

        @Override
        public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host,
                final InetSocketAddress remoteAddress, final InetSocketAddress localAddress,
                final HttpContext context) throws IOException {
            if (trust instanceof ThreadLocalHostnameDelegatingTrustManager) {
                ((ThreadLocalHostnameDelegatingTrustManager) trust).setTarget(remoteAddress.getHostName());
            }
            return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
        }
    }, proxy);
}

From source file:com.owncloud.android.oc_framework.network.AdvancedSslSocketFactory.java

/**
  * Attempts to get a new socket connection to the given host within the
  * given time limit./*from   ww w . j a  v a 2  s.c o m*/
  * 
  * @param host the host name/IP
  * @param port the port on the host
  * @param clientHost the local host name/IP to bind the socket to
  * @param clientPort 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 {
    Log.d(TAG, "Creating SSL Socket with remote " + host + ":" + port + ", local " + localAddress + ":"
            + localPort + ", params: " + params);
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();

    //logSslInfo();

    SocketFactory socketfactory = mSslContext.getSocketFactory();
    Log.d(TAG, " ... with connection timeout " + timeout + " and socket timeout " + params.getSoTimeout());
    Socket socket = socketfactory.createSocket();
    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.setSoTimeout(params.getSoTimeout());
    socket.bind(localaddr);
    ServerNameIndicator.setServerNameIndication(host, (SSLSocket) socket);
    socket.connect(remoteaddr, timeout);
    verifyPeerIdentity(host, port, socket);
    return socket;
}

From source file:com.owncloud.android.lib.common.network.AdvancedSslSocketFactory.java

/**
  * Attempts to get a new socket connection to the given host within the
  * given time limit.//from   w ww .  j a v  a2  s. c o m
  * 
  * @param host the host name/IP
  * @param port the port on the host
  * @param clientHost the local host name/IP to bind the socket to
  * @param clientPort 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 {
    Log_OC.d(TAG, "Creating SSL Socket with remote " + host + ":" + port + ", local " + localAddress + ":"
            + localPort + ", params: " + params);
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();

    //logSslInfo();

    SocketFactory socketfactory = mSslContext.getSocketFactory();
    Log_OC.d(TAG, " ... with connection timeout " + timeout + " and socket timeout " + params.getSoTimeout());
    Socket socket = socketfactory.createSocket();
    enableSecureProtocols(socket);
    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.setSoTimeout(params.getSoTimeout());
    socket.bind(localaddr);
    ServerNameIndicator.setServerNameIndication(host, (SSLSocket) socket);
    socket.connect(remoteaddr, timeout);
    verifyPeerIdentity(host, port, socket);
    return socket;
}