Java Code Examples for java.net.ConnectException#initCause()

The following examples show how to use java.net.ConnectException#initCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RealConnection.java    From styT with Apache License 2.0 6 votes vote down vote up
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */
private void connectSocket(int connectTimeout, int readTimeout) throws IOException {
  Proxy proxy = route.proxy();
  Address address = route.address();

  rawSocket = proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP
      ? address.socketFactory().createSocket()
      : new Socket(proxy);

  rawSocket.setSoTimeout(readTimeout);
  try {
    Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout);
  } catch (ConnectException e) {
    ConnectException ce = new ConnectException("Failed to connect to " + route.socketAddress());
    ce.initCause(e);
    throw ce;
  }
  source = Okio.buffer(Okio.source(rawSocket));
  sink = Okio.buffer(Okio.sink(rawSocket));
}
 
Example 2
Source File: RealConnection.java    From AndroidProjects with MIT License 6 votes vote down vote up
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */
private void connectSocket(int connectTimeout, int readTimeout) throws IOException {
  Proxy proxy = route.proxy();
  Address address = route.address();

  rawSocket = proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP
      ? address.socketFactory().createSocket()
      : new Socket(proxy);

  rawSocket.setSoTimeout(readTimeout);
  try {
    Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout);
  } catch (ConnectException e) {
    ConnectException ce = new ConnectException("Failed to connect to " + route.socketAddress());
    ce.initCause(e);
    throw ce;
  }
  source = Okio.buffer(Okio.source(rawSocket));
  sink = Okio.buffer(Okio.sink(rawSocket));
}
 
Example 3
Source File: Utils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isSecurePort(String hostname, int port, int depth) 
        throws IOException, ConnectException, SocketTimeoutException {
    // Open the socket with a short timeout for connects and reads.
    Socket socket = new Socket();
    try {
        Logger.getLogger("payara-socket-connect-diagnostic").log(Level.FINE, "Using socket.connect", new Exception());
        socket.connect(new InetSocketAddress(hostname, port), PORT_CHECK_TIMEOUT);
        socket.setSoTimeout(PORT_CHECK_TIMEOUT);
    } catch(SocketException ex) { // this could be bug 70020 due to SOCKs proxy not having localhost
        String socksNonProxyHosts = System.getProperty("socksNonProxyHosts");
        if(socksNonProxyHosts != null && socksNonProxyHosts.indexOf("localhost") < 0) {
            String localhost = socksNonProxyHosts.length() > 0 ? "|localhost" : "localhost";
            System.setProperty("socksNonProxyHosts",  socksNonProxyHosts + localhost);
            ConnectException ce = new ConnectException();
            ce.initCause(ex);
            throw ce; //status unknow at this point
            //next call, we'll be ok and it will really detect if we are secure or not
        }
    }
    //This is the test query used to ping the server in an attempt to
    //determine if it is secure or not.
    InputStream is = socket.getInputStream();        
    String testQuery = "GET / HTTP/1.0";
    PrintWriter pw = new PrintWriter(socket.getOutputStream());
    pw.println(testQuery);
    pw.println();
    pw.flush();
    byte[] respArr = new byte[1024];
    boolean isSecure = true;
    while (is.read(respArr) != -1) {
        String resp = new String(respArr);
        if (checkHelper(resp) == false) {
            isSecure = false;
            break;
        }
    }
    // Close the socket
    socket.close();
    return isSecure;
}
 
Example 4
Source File: Utils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isSecurePort(String hostname, int port, int depth) 
        throws IOException, ConnectException, SocketTimeoutException {
    // Open the socket with a short timeout for connects and reads.
    Socket socket = new Socket();
    try {
        Logger.getLogger("glassfish-socket-connect-diagnostic").log(Level.FINE, "Using socket.connect", new Exception());
        socket.connect(new InetSocketAddress(hostname, port), PORT_CHECK_TIMEOUT);
        socket.setSoTimeout(PORT_CHECK_TIMEOUT);
    } catch(SocketException ex) { // this could be bug 70020 due to SOCKs proxy not having localhost
        String socksNonProxyHosts = System.getProperty("socksNonProxyHosts");
        if(socksNonProxyHosts != null && socksNonProxyHosts.indexOf("localhost") < 0) {
            String localhost = socksNonProxyHosts.length() > 0 ? "|localhost" : "localhost";
            System.setProperty("socksNonProxyHosts",  socksNonProxyHosts + localhost);
            ConnectException ce = new ConnectException();
            ce.initCause(ex);
            throw ce; //status unknow at this point
            //next call, we'll be ok and it will really detect if we are secure or not
        }
    }
    //This is the test query used to ping the server in an attempt to
    //determine if it is secure or not.
    InputStream is = socket.getInputStream();        
    String testQuery = "GET / HTTP/1.0";
    PrintWriter pw = new PrintWriter(socket.getOutputStream());
    pw.println(testQuery);
    pw.println();
    pw.flush();
    byte[] respArr = new byte[1024];
    boolean isSecure = true;
    while (is.read(respArr) != -1) {
        String resp = new String(respArr);
        if (checkHelper(resp) == false) {
            isSecure = false;
            break;
        }
    }
    // Close the socket
    socket.close();
    return isSecure;
}
 
Example 5
Source File: NetUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Determine whether an HTTP listener is secure or not..
 * <p/>
 * This method accepts a host name and port #.  It uses this information
 * to attempt to connect to the port, send a test query, analyze the
 * result to determine if the port is secure or is not secure (currently
 * only HTTP / HTTPS is supported).
 * it might emit a warning in the server log for Payara cases.
 * No Harm, just an annoying warning, so we need to use this call only
 * when really needed.
 * <p/>
 * @param hostname The host for the HTTP listener.
 * @param port     The port for the HTTP listener.
 * @param depth     Method calling depth.
 * @throws IOException
 * @throws SocketTimeoutException
 * @throws ConnectException
 */
private static boolean isSecurePort(String hostname, int port, int depth) 
        throws IOException, ConnectException, SocketTimeoutException {
    final String METHOD = "isSecurePort";
    boolean isSecure = true;
    try (Socket socket = new Socket()) {
        try {
            LOGGER.log(Level.FINE, METHOD, "socket");
            socket.connect(new InetSocketAddress(hostname, port), PORT_CHECK_TIMEOUT);
            socket.setSoTimeout(PORT_CHECK_TIMEOUT);
        // This could be bug 70020 due to SOCKs proxy not having localhost
        } catch (SocketException ex) {
            String socksNonProxyHosts = System.getProperty("socksNonProxyHosts");
            if(socksNonProxyHosts != null && socksNonProxyHosts.indexOf("localhost") < 0) {
                String localhost = socksNonProxyHosts.length() > 0 ? "|localhost" : "localhost";
                System.setProperty("socksNonProxyHosts",  socksNonProxyHosts + localhost);
                ConnectException ce = new ConnectException();
                ce.initCause(ex);
                throw ce; //status unknow at this point
                //next call, we'll be ok and it will really detect if we are secure or not
            }
        }
        java.io.InputStream istream = socket.getInputStream();
        //This is the test query used to ping the server in an attempt to
        //determine if it is secure or not.
        String testQuery = "GET / HTTP/1.0";
        PrintWriter pw = new PrintWriter(socket.getOutputStream());
        pw.println(testQuery);
        pw.println();
        pw.flush();
        byte[] respArr = new byte[1024];
        while (istream.read(respArr) != -1) {
            String resp = new String(respArr);
            if (checkHelper(resp) == false) {
                isSecure = false;
                break;
            }
        }
    }
    return isSecure;
}
 
Example 6
Source File: PortDetector.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 *  This method accepts a hostname and port #.  It uses this information
 *  to attempt to connect to the port, send a test query, analyze the
 *  result to determine if the port is secure or unsecure (currently only
 *  http / https is supported).
 * it might emit a warning in the server log for GlassFish cases
 * No Harm, just an annoying warning, so we need to use this call only when really needed
 */
public static boolean isSecurePort(String hostname, int port) 
        throws IOException, ConnectException, SocketTimeoutException {
    // Open the socket with a short timeout for connects and reads.
    Socket socket = new Socket();
    try {
        socket.connect(new InetSocketAddress(hostname, port), PORT_CHECK_TIMEOUT);
        socket.setSoTimeout(PORT_CHECK_TIMEOUT);
    } catch(SocketException ex) { // this could be bug 70020 due to SOCKs proxy not having localhost
        String socksNonProxyHosts = System.getProperty("socksNonProxyHosts");
        if(socksNonProxyHosts != null && socksNonProxyHosts.indexOf("localhost") < 0) {
            String localhost = socksNonProxyHosts.length() > 0 ? "|localhost" : "localhost";
            System.setProperty("socksNonProxyHosts",  socksNonProxyHosts + localhost);
            ConnectException ce = new ConnectException();
            ce.initCause(ex);
            throw ce; //status unknow at this point
            //next call, we'll be ok and it will really detect if we are secure or not
        }
    }
    
    //This is the test query used to ping the server in an attempt to
    //determine if it is secure or not.
    String testQuery = "GET / HTTP/1.0";
    PrintWriter pw = new PrintWriter(socket.getOutputStream());
    pw.println(testQuery);
    pw.println();
    pw.flush();
    // Get the result
    InputStream is = socket.getInputStream();
    byte[] respArr = new byte[1024];
    boolean isSecure = true;
    while (is.read(respArr) != -1) {
        // Determine protocol from result
        // Can't read https response w/ OpenSSL (or equiv), so use as
        // default & try to detect an http response.
        String resp = new String(respArr);
        if (checkHelper(resp) == false) {
            isSecure = false;
            break;
        }
    }
    socket.close();
    return isSecure;
}
 
Example 7
Source File: NetUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Determine whether an HTTP listener is secure or not..
 * <p/>
 * This method accepts a host name and port #.  It uses this information
 * to attempt to connect to the port, send a test query, analyze the
 * result to determine if the port is secure or is not secure (currently
 * only HTTP / HTTPS is supported).
 * it might emit a warning in the server log for GlassFish cases.
 * No Harm, just an annoying warning, so we need to use this call only
 * when really needed.
 * <p/>
 * @param hostname The host for the HTTP listener.
 * @param port     The port for the HTTP listener.
 * @param depth     Method calling depth.
 * @throws IOException
 * @throws SocketTimeoutException
 * @throws ConnectException
 */
private static boolean isSecurePort(String hostname, int port, int depth) 
        throws IOException, ConnectException, SocketTimeoutException {
    final String METHOD = "isSecurePort";
    boolean isSecure = true;
    try (Socket socket = new Socket()) {
        try {
            LOGGER.log(Level.FINE, METHOD, "socket");
            socket.connect(new InetSocketAddress(hostname, port), PORT_CHECK_TIMEOUT);
            socket.setSoTimeout(PORT_CHECK_TIMEOUT);
        // This could be bug 70020 due to SOCKs proxy not having localhost
        } catch (SocketException ex) {
            String socksNonProxyHosts = System.getProperty("socksNonProxyHosts");
            if(socksNonProxyHosts != null && socksNonProxyHosts.indexOf("localhost") < 0) {
                String localhost = socksNonProxyHosts.length() > 0 ? "|localhost" : "localhost";
                System.setProperty("socksNonProxyHosts",  socksNonProxyHosts + localhost);
                ConnectException ce = new ConnectException();
                ce.initCause(ex);
                throw ce; //status unknow at this point
                //next call, we'll be ok and it will really detect if we are secure or not
            }
        }
        java.io.InputStream istream = socket.getInputStream();
        //This is the test query used to ping the server in an attempt to
        //determine if it is secure or not.
        String testQuery = "GET / HTTP/1.0";
        PrintWriter pw = new PrintWriter(socket.getOutputStream());
        pw.println(testQuery);
        pw.println();
        pw.flush();
        byte[] respArr = new byte[1024];
        while (istream.read(respArr) != -1) {
            String resp = new String(respArr);
            if (checkHelper(resp) == false) {
                isSecure = false;
                break;
            }
        }
    }
    return isSecure;
}
 
Example 8
Source File: UtilTest.java    From r2cloud with Apache License 2.0 4 votes vote down vote up
private static ConnectException createConnectException(Throwable e) {
	ConnectException result = new ConnectException("connect failed");
	result.initCause(e);
	return result;
}