Example usage for javax.net.ssl SSLSocket connect

List of usage examples for javax.net.ssl SSLSocket connect

Introduction

In this page you can find the example usage for javax.net.ssl SSLSocket connect.

Prototype

public void connect(SocketAddress endpoint, int timeout) throws IOException 

Source Link

Document

Connects this socket to the server with a specified timeout value.

Usage

From source file:org.ovirt.engine.core.utils.ssl.AuthSSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>// w  w w .j a  v  a 2 s  .  c o  m
 * 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 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 {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = sslcontext.getSocketFactory();
    if (timeout == 0) {
        SSLSocket socket = (SSLSocket) socketfactory.createSocket(host, port, localAddress, localPort);
        socket.setEnabledProtocols(new String[] { "SSLv3" });
        return socket;
    } else {
        SSLSocket socket = (SSLSocket) socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        socket.setEnabledProtocols(new String[] { "SSLv3" });
        return socket;
    }
}

From source file:spade.resolver.Recursive.java

/**
 * Computes a result, or throws an exception if unable to do so.
 *
 * @return computed result//from   w ww  . j av  a 2 s.c o m
 * @throws Exception if unable to compute a result
 */
@Override
public Graph call() throws Exception {
    Graph resultGraph = null;
    try {
        // Establish a connection to the remote host
        String host = networkVertex.getAnnotation(OPMConstants.ARTIFACT_REMOTE_ADDRESS);
        int port = Integer.parseInt(Settings.getProperty("commandline_query_port"));
        logger.log(Level.INFO, "network Vertex: " + networkVertex);
        SSLSocket remoteSocket = (SSLSocket) Kernel.sslSocketFactory.createSocket();
        int connectTimeOut = 5000; // 5 sec
        remoteSocket.connect(new InetSocketAddress(host, port), connectTimeOut);
        //            SSLSocket remoteSocket = (SSLSocket) Kernel.sslSocketFactory.createSocket(host, port);

        OutputStream outStream = remoteSocket.getOutputStream();
        InputStream inStream = remoteSocket.getInputStream();
        ObjectInputStream graphInputStream = new ObjectInputStream(inStream);
        PrintWriter remoteSocketOut = new PrintWriter(outStream, true);

        String networkVertexQuery = "GetVertex(" + OPMConstants.ARTIFACT_LOCAL_ADDRESS
                + AbstractQuery.OPERATORS.EQUALS
                + networkVertex.getAnnotation(OPMConstants.ARTIFACT_REMOTE_ADDRESS) + " AND "
                + OPMConstants.ARTIFACT_LOCAL_PORT + AbstractQuery.OPERATORS.EQUALS
                + networkVertex.getAnnotation(OPMConstants.ARTIFACT_REMOTE_PORT) + " AND "
                + OPMConstants.ARTIFACT_REMOTE_ADDRESS + AbstractQuery.OPERATORS.EQUALS
                + networkVertex.getAnnotation(OPMConstants.ARTIFACT_LOCAL_ADDRESS) + " AND "
                + OPMConstants.ARTIFACT_REMOTE_PORT + AbstractQuery.OPERATORS.EQUALS
                + networkVertex.getAnnotation(OPMConstants.ARTIFACT_LOCAL_PORT) + " AND " + OPMConstants.SOURCE
                + AbstractQuery.OPERATORS.EQUALS + OPMConstants.SOURCE_AUDIT_NETFILTER + ")";

        remoteSocketOut.println(networkVertexQuery);
        logger.log(Level.INFO, "remote vertex query: " + networkVertexQuery);
        String returnType = (String) graphInputStream.readObject();
        // Check whether the remote query server returned a vertex set in response
        Set<AbstractVertex> vertexSet;
        if (returnType.equals(Set.class.getName())) {
            vertexSet = (Set<AbstractVertex>) graphInputStream.readObject();
        } else {
            logger.log(Level.INFO, "Return type not Set!");
            return null;
        }
        AbstractVertex targetNetworkVertex;
        if (!CollectionUtils.isEmpty(vertexSet)) {
            targetNetworkVertex = vertexSet.iterator().next();
        } else {
            logger.log(Level.INFO, "TargetNetworkVertex empty!");
            return null;
        }
        String targetNetworkVertexHash = targetNetworkVertex.bigHashCode();

        String lineageQuery = "GetLineage(" + PRIMARY_KEY + AbstractQuery.OPERATORS.EQUALS
                + targetNetworkVertexHash + ", " + depth + ", " + direction + ")";
        remoteSocketOut.println(lineageQuery);
        logger.log(Level.INFO, "remote lineage query: " + lineageQuery);

        returnType = (String) graphInputStream.readObject();
        if (returnType.equals(Graph.class.getName())) {
            AbstractEdge localToRemoteEdge = new Edge(networkVertex, targetNetworkVertex);
            localToRemoteEdge.addAnnotation("type", "WasDerivedFrom");
            AbstractEdge remoteToLocalEdge = new Edge(targetNetworkVertex, networkVertex);
            remoteToLocalEdge.addAnnotation("type", "WasDerivedFrom");
            resultGraph = (Graph) graphInputStream.readObject();
            resultGraph.putVertex(networkVertex);
            resultGraph.putEdge(localToRemoteEdge);
            resultGraph.putEdge(remoteToLocalEdge);
        } else {
            logger.log(Level.INFO, "Return type not Graph!");
        }

        remoteSocketOut.println("exit");
        remoteSocketOut.close();
        graphInputStream.close();
        inStream.close();
        outStream.close();
        remoteSocket.close();
    } catch (NumberFormatException | IOException | ClassNotFoundException exception) {
        logger.log(Level.SEVERE, "Remote resolution unsuccessful!", exception);
        return null;
    }

    logger.log(Level.INFO, "Remote resolution successful!");
    return resultGraph;
}