Example usage for org.apache.http.conn ConnectTimeoutException ConnectTimeoutException

List of usage examples for org.apache.http.conn ConnectTimeoutException ConnectTimeoutException

Introduction

In this page you can find the example usage for org.apache.http.conn ConnectTimeoutException ConnectTimeoutException.

Prototype

public ConnectTimeoutException(final String message) 

Source Link

Document

Creates a ConnectTimeoutException with the specified detail message.

Usage

From source file:com.flowzr.rates.AbstractRatesDownloaderTest.java

void givenExceptionWhileRequestingWebService() {
    client.error = new ConnectTimeoutException("Timeout");
}

From source file:net.java.sip.communicator.service.httputil.SSLSocketFactoryEx.java

/**
 * @since 4.1//from  w  ww.j  a v a2s.co  m
 */
@Override
public Socket connectSocket(final Socket socket, final InetSocketAddress remoteAddress,
        final InetSocketAddress localAddress, final HttpParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (remoteAddress == null) {
        throw new IllegalArgumentException("Remote address may not be null");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    Socket sock = socket != null ? socket : this.context.getSocketFactory().createSocket();
    if (localAddress != null) {
        sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
        sock.bind(localAddress);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    try {
        sock.setSoTimeout(soTimeout);
        sock.connect(remoteAddress, connTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
    }

    String hostname;
    if (remoteAddress instanceof HttpInetSocketAddress) {
        hostname = ((HttpInetSocketAddress) remoteAddress).getHttpHost().getHostName();
    } else {
        hostname = remoteAddress.getHostName();
    }

    SSLSocket sslsock;
    // Setup SSL layering if necessary
    if (sock instanceof SSLSocket) {
        sslsock = (SSLSocket) sock;
    } else {
        int port = remoteAddress.getPort();
        sslsock = (SSLSocket) this.context.getSocketFactory().createSocket(sock, hostname, port, true);
    }

    return sslsock;
}

From source file:org.apache.manifoldcf.connectorcommon.common.InterruptibleSocketFactory.java

protected Socket fireOffThread(InetAddress address, int port, InetAddress localHost, int localPort)
        throws IOException {
    SocketCreateThread thread = new SocketCreateThread(wrappedFactory, address, port, localHost, localPort);
    thread.start();//from   w w w.  jav  a2 s . c o m
    try {
        // Wait for thread to complete for only a certain amount of time!
        thread.join(connectTimeoutMilliseconds);
        // If join() times out, then the thread is going to still be alive.
        if (thread.isAlive()) {
            // Kill the thread - not that this will necessarily work, but we need to try
            thread.interrupt();
            throw new ConnectTimeoutException("Secure connection timed out");
        }
        // The thread terminated.  Throw an error if there is one, otherwise return the result.
        Throwable t = thread.getException();
        if (t != null) {
            if (t instanceof java.net.SocketTimeoutException)
                throw (java.net.SocketTimeoutException) t;
            else if (t instanceof ConnectTimeoutException)
                throw (ConnectTimeoutException) t;
            else if (t instanceof InterruptedIOException)
                throw (InterruptedIOException) t;
            else if (t instanceof IOException)
                throw (IOException) t;
            else if (t instanceof Error)
                throw (Error) t;
            else if (t instanceof RuntimeException)
                throw (RuntimeException) t;
            throw new Error("Received an unexpected exception: " + t.getMessage(), t);
        }
        return thread.getResult();
    } catch (InterruptedException e) {
        throw new InterruptedIOException("Interrupted: " + e.getMessage());
    }

}

From source file:org.openqa.selenium.remote.ReusingSocketSocketFactory.java

public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }/*from  w  w  w .  j  av a 2  s  .  c  o  m*/
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    if (sock == null)
        sock = createSocket();

    sock.setReuseAddress(true); // This is the 1 line added by kristian
    if ((localAddress != null) || (localPort > 0)) {

        // we need to bind explicitly
        if (localPort < 0)
            localPort = 0; // indicates "any"

        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
        sock.bind(isa);
    }

    int timeout = HttpConnectionParams.getConnectionTimeout(params);

    InetSocketAddress remoteAddress;
    if (this.nameResolver != null) {
        remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
    } else {
        remoteAddress = new InetSocketAddress(host, port);
    }
    try {
        sock.connect(remoteAddress, timeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
    }
    return sock;
}

From source file:org.silvertunnel.netlib.adapter.httpclient.NetlibSocketFactory.java

/**
 * @param sock    will be ignored; can be null
 *//*from   w w  w .ja  v  a2  s.  com*/
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    TcpipNetAddress localNetAddress = null;
    if ((localAddress != null) || (localPort > 0)) {
        // we need to bind explicitly
        if (localPort < 0) {
            localPort = 0; // indicates "any"
        }

        localNetAddress = new TcpipNetAddress(localAddress, localPort);
    }

    int timeoutInMs = HttpConnectionParams.getConnectionTimeout(params);

    // open connection without explicit DNS resolution
    Map<String, Object> localProperties = new HashMap<String, Object>();
    localProperties.put(TcpipNetLayer.TIMEOUT_IN_MS, new Integer(timeoutInMs));
    TcpipNetAddress remoteNetAddress = new TcpipNetAddress(host, port);

    try {
        NetSocket netSocket = lowerNetLayer.createNetSocket(localProperties, localNetAddress, remoteNetAddress);
        if (sock != null && sock instanceof NetSocket2Socket) {
            // change NetSocket of exiting wrapper 
            NetSocket2Socket netSocket2Socket = (NetSocket2Socket) sock;
            netSocket2Socket.setNetSocket(netSocket);
            return netSocket2Socket;
        } else {
            // create new wrapper
            return new NetSocket2Socket(netSocket);
        }

    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteNetAddress + " timed out");
    }
}

From source file:com.rovemonteux.silvertunnel.netlib.adapter.httpclient.NetlibSocketFactory.java

/**
 * @param sock// w  w w  .j  a v  a  2s.  c  o  m
 *            will be ignored; can be null
 */
@Override
public Socket connectSocket(final Socket sock, final String host, final int port,
        final InetAddress localAddress, int localPort, final HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    TcpipNetAddress localNetAddress = null;
    if (localAddress != null || localPort > 0) {
        // we need to bind explicitly
        if (localPort < 0) {
            localPort = 0; // indicates "any"
        }

        localNetAddress = new TcpipNetAddress(localAddress, localPort);
    }

    final int timeoutInMs = HttpConnectionParams.getConnectionTimeout(params);

    // open connection without explicit DNS resolution
    final Map<String, Object> localProperties = new HashMap<String, Object>();
    localProperties.put(TcpipNetLayer.TIMEOUT_IN_MS, Integer.valueOf(timeoutInMs));
    final TcpipNetAddress remoteNetAddress = new TcpipNetAddress(host, port);

    try {
        final NetSocket netSocket = lowerNetLayer.createNetSocket(localProperties, localNetAddress,
                remoteNetAddress);
        if (sock != null && sock instanceof NetSocket2Socket) {
            // change NetSocket of exiting wrapper
            final NetSocket2Socket netSocket2Socket = (NetSocket2Socket) sock;
            netSocket2Socket.setNetSocket(netSocket);
            return netSocket2Socket;
        } else {
            // create new wrapper
            return new NetSocket2Socket(netSocket);
        }

    } catch (final SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteNetAddress + " timed out");
    }
}

From source file:org.elasticsearch.client.RestClientMultipleHostsTests.java

@Before
@SuppressWarnings("unchecked")
public void createRestClient() throws IOException {
    CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class)))
                    .thenAnswer(new Answer<Future<HttpResponse>>() {
                        @Override
                        public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
                            HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock
                                    .getArguments()[0];
                            HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest();
                            HttpHost httpHost = requestProducer.getTarget();
                            HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2];
                            assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class));
                            FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock
                                    .getArguments()[3];
                            //return the desired status code or exception depending on the path
                            if (request.getURI().getPath().equals("/soe")) {
                                futureCallback.failed(new SocketTimeoutException(httpHost.toString()));
                            } else if (request.getURI().getPath().equals("/coe")) {
                                futureCallback.failed(new ConnectTimeoutException(httpHost.toString()));
                            } else if (request.getURI().getPath().equals("/ioe")) {
                                futureCallback.failed(new IOException(httpHost.toString()));
                            } else {
                                int statusCode = Integer.parseInt(request.getURI().getPath().substring(1));
                                StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1),
                                        statusCode, "");
                                futureCallback.completed(new BasicHttpResponse(statusLine));
                            }//www  . ja  v  a  2 s . c  om
                            return null;
                        }
                    });
    int numHosts = RandomNumbers.randomIntBetween(getRandom(), 2, 5);
    httpHosts = new HttpHost[numHosts];
    for (int i = 0; i < numHosts; i++) {
        httpHosts[i] = new HttpHost("localhost", 9200 + i);
    }
    failureListener = new HostsTrackingFailureListener();
    restClient = new RestClient(httpClient, 10000, new Header[0], httpHosts, null, failureListener);
}

From source file:org.apache.chemistry.opencmis.client.bindings.spi.http.ApacheClientHttpInvoker.java

/**
 * Builds a SSL Socket Factory for the Apache HTTP Client.
 */// w  ww  .  j  a va 2s  .c om
private SchemeLayeredSocketFactory getSSLSocketFactory(final UrlBuilder url, final BindingSession session) {
    // get authentication provider
    AuthenticationProvider authProvider = CmisBindingsHelper.getAuthenticationProvider(session);

    // check SSL Socket Factory
    final SSLSocketFactory sf = authProvider.getSSLSocketFactory();
    if (sf == null) {
        // no custom factory -> return default factory
        return org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory();
    }

    // check hostame verifier and use default if not set
    final HostnameVerifier hv = (authProvider.getHostnameVerifier() == null
            ? new BrowserCompatHostnameVerifier()
            : authProvider.getHostnameVerifier());

    if (hv instanceof X509HostnameVerifier) {
        return new org.apache.http.conn.ssl.SSLSocketFactory(sf, (X509HostnameVerifier) hv);
    }

    // build new socket factory
    return new SchemeLayeredSocketFactory() {

        @Override
        public boolean isSecure(Socket sock) {
            return true;
        }

        @Override
        public Socket createSocket(HttpParams params) throws IOException {
            return sf.createSocket();
        }

        @Override
        public Socket connectSocket(final Socket socket, final InetSocketAddress remoteAddress,
                final InetSocketAddress localAddress, final HttpParams params) throws IOException {

            Socket sock = socket != null ? socket : createSocket(params);
            if (localAddress != null) {
                sock.setReuseAddress(HttpConnectionParams.getSoReuseaddr(params));
                sock.bind(localAddress);
            }

            int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
            int soTimeout = HttpConnectionParams.getSoTimeout(params);

            try {
                sock.setSoTimeout(soTimeout);
                sock.connect(remoteAddress, connTimeout);
            } catch (SocketTimeoutException ex) {
                closeSocket(sock);
                throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out!");
            }

            String host;
            if (remoteAddress instanceof HttpInetSocketAddress) {
                host = ((HttpInetSocketAddress) remoteAddress).getHttpHost().getHostName();
            } else {
                host = remoteAddress.getHostName();
            }

            SSLSocket sslSocket;
            if (sock instanceof SSLSocket) {
                sslSocket = (SSLSocket) sock;
            } else {
                int port = remoteAddress.getPort();
                sslSocket = (SSLSocket) sf.createSocket(sock, host, port, true);
            }
            verify(hv, host, sslSocket);

            return sslSocket;
        }

        @Override
        public Socket createLayeredSocket(final Socket socket, final String host, final int port,
                final HttpParams params) throws IOException {
            SSLSocket sslSocket = (SSLSocket) sf.createSocket(socket, host, port, true);
            verify(hv, host, sslSocket);

            return sslSocket;
        }
    };
}

From source file:com.example.mp_master.helper.UntrustedSSLSocketFactory.java

public Socket connectSocket(final Socket sock, final String host, final int port,
        final InetAddress localAddress, int localPort, final HttpParams params) throws IOException {

    if (host == null) {
        throw new IllegalArgumentException("Target host may not be null.");
    }//www . ja v a2 s  .com
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null.");
    }

    SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());

    if ((localAddress != null) || (localPort > 0)) {

        // we need to bind explicitly
        if (localPort < 0)
            localPort = 0; // indicates "any"

        InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
        sslsock.bind(isa);
    }

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
    int soTimeout = HttpConnectionParams.getSoTimeout(params);

    InetSocketAddress remoteAddress;
    if (this.nameResolver != null) {
        remoteAddress = new InetSocketAddress(this.nameResolver.resolve(host), port);
    } else {
        remoteAddress = new InetSocketAddress(host, port);
    }
    try {
        sslsock.connect(remoteAddress, connTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException("Connect to " + remoteAddress + " timed out");
    }
    sslsock.setSoTimeout(soTimeout);
    try {
        hostnameVerifier.verify(host, sslsock);
        // verifyHostName() didn't blowup - good!
    } catch (IOException iox) {
        // close the socket before re-throwing the exception
        try {
            sslsock.close();
        } catch (Exception x) {
            /*ignore*/ }
        throw iox;
    }

    return sslsock;
}

From source file:com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.java

/**
 * Connect via socket./*from   w ww .  ja va 2 s .  c  o m*/
 * @param connectTimeout the timeout
 * @param socket the socket
 * @param host the host
 * @param remoteAddress the remote address
 * @param localAddress the local address
 * @param context the context
 * @return the created/connected socket
 * @throws IOException in case of problems
 */
@Override
public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host,
        final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context)
        throws IOException {
    final HttpHost socksProxy = SocksConnectionSocketFactory.getSocksProxy(context);
    if (socksProxy != null) {
        final Socket underlying = SocksConnectionSocketFactory.createSocketWithSocksProxy(socksProxy);
        underlying.setReuseAddress(true);

        // TODO: commented out for HttpClient 4.3
        // final int soTimeout = HttpConnectionParams.getSoTimeout(params);

        final SocketAddress socksProxyAddress = new InetSocketAddress(socksProxy.getHostName(),
                socksProxy.getPort());
        try {
            //underlying.setSoTimeout(soTimeout);
            underlying.connect(remoteAddress, connectTimeout);
        } catch (final SocketTimeoutException ex) {
            throw new ConnectTimeoutException("Connect to " + socksProxyAddress + " timed out");
        }

        final Socket sslSocket = getSSLSocketFactory().createSocket(underlying, socksProxy.getHostName(),
                socksProxy.getPort(), true);
        configureSocket((SSLSocket) sslSocket, context);
        return sslSocket;
    }
    try {
        return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
    } catch (final IOException e) {
        if (useInsecureSSL_ && "handshake alert:  unrecognized_name".equals(e.getMessage())) {
            setEmptyHostname(host);

            return super.connectSocket(connectTimeout, createSocket(context), host, remoteAddress, localAddress,
                    context);
        }
        throw e;
    }
}