Search in sources :

Example 21 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project XobotOS by xamarin.

the class DefaultClientConnectionOperator method openConnection.

// non-javadoc, see interface ClientConnectionOperator
public void openConnection(OperatedClientConnection conn, HttpHost target, InetAddress local, HttpContext context, HttpParams params) throws IOException {
    if (conn == null) {
        throw new IllegalArgumentException("Connection must not be null.");
    }
    if (target == null) {
        throw new IllegalArgumentException("Target host must not be null.");
    }
    //@@@ is context allowed to be null?
    if (params == null) {
        throw new IllegalArgumentException("Parameters must not be null.");
    }
    if (conn.isOpen()) {
        throw new IllegalArgumentException("Connection must not be open.");
    }
    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    final SocketFactory sf = schm.getSocketFactory();
    final SocketFactory plain_sf;
    final LayeredSocketFactory layered_sf;
    if (sf instanceof LayeredSocketFactory) {
        plain_sf = staticPlainSocketFactory;
        layered_sf = (LayeredSocketFactory) sf;
    } else {
        plain_sf = sf;
        layered_sf = null;
    }
    InetAddress[] addresses = InetAddress.getAllByName(target.getHostName());
    for (int i = 0; i < addresses.length; ++i) {
        Socket sock = plain_sf.createSocket();
        conn.opening(sock, target);
        try {
            Socket connsock = plain_sf.connectSocket(sock, addresses[i].getHostAddress(), schm.resolvePort(target.getPort()), local, 0, params);
            if (sock != connsock) {
                sock = connsock;
                conn.opening(sock, target);
            }
            /*
                 * prepareSocket is called on the just connected
                 * socket before the creation of the layered socket to
                 * ensure that desired socket options such as
                 * TCP_NODELAY, SO_RCVTIMEO, SO_LINGER will be set
                 * before any I/O is performed on the socket. This
                 * happens in the common case as
                 * SSLSocketFactory.createSocket performs hostname
                 * verification which requires that SSL handshaking be
                 * performed.
                 */
            prepareSocket(sock, context, params);
            if (layered_sf != null) {
                Socket layeredsock = layered_sf.createSocket(sock, target.getHostName(), schm.resolvePort(target.getPort()), true);
                if (layeredsock != sock) {
                    conn.opening(layeredsock, target);
                }
                conn.openCompleted(sf.isSecure(layeredsock), params);
            } else {
                conn.openCompleted(sf.isSecure(sock), params);
            }
            break;
        // BEGIN android-changed
        //       catch SocketException to cover any kind of connect failure
        } catch (SocketException ex) {
            if (i == addresses.length - 1) {
                ConnectException cause = ex instanceof ConnectException ? (ConnectException) ex : new ConnectException(ex.getMessage(), ex);
                throw new HttpHostConnectException(target, cause);
            }
        // END android-changed
        } catch (ConnectTimeoutException ex) {
            if (i == addresses.length - 1) {
                throw ex;
            }
        }
    }
}
Also used : SocketException(java.net.SocketException) Scheme(org.apache.http.conn.scheme.Scheme) PlainSocketFactory(org.apache.http.conn.scheme.PlainSocketFactory) LayeredSocketFactory(org.apache.http.conn.scheme.LayeredSocketFactory) SocketFactory(org.apache.http.conn.scheme.SocketFactory) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) LayeredSocketFactory(org.apache.http.conn.scheme.LayeredSocketFactory) InetAddress(java.net.InetAddress) Socket(java.net.Socket) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 22 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project XobotOS by xamarin.

the class DefaultClientConnectionOperator method updateSecureConnection.

// openConnection
// non-javadoc, see interface ClientConnectionOperator
public void updateSecureConnection(OperatedClientConnection conn, HttpHost target, HttpContext context, HttpParams params) throws IOException {
    if (conn == null) {
        throw new IllegalArgumentException("Connection must not be null.");
    }
    if (target == null) {
        throw new IllegalArgumentException("Target host must not be null.");
    }
    //@@@ is context allowed to be null?
    if (params == null) {
        throw new IllegalArgumentException("Parameters must not be null.");
    }
    if (!conn.isOpen()) {
        throw new IllegalArgumentException("Connection must be open.");
    }
    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    if (!(schm.getSocketFactory() instanceof LayeredSocketFactory)) {
        throw new IllegalArgumentException("Target scheme (" + schm.getName() + ") must have layered socket factory.");
    }
    final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory();
    final Socket sock;
    try {
        sock = lsf.createSocket(conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), true);
    } catch (ConnectException ex) {
        throw new HttpHostConnectException(target, ex);
    }
    prepareSocket(sock, context, params);
    conn.update(sock, target, lsf.isSecure(sock), params);
//@@@ error handling: close the layered socket in case of exception?
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) LayeredSocketFactory(org.apache.http.conn.scheme.LayeredSocketFactory) Socket(java.net.Socket) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectException(java.net.ConnectException)

Example 23 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project jackrabbit-oak by apache.

the class TomcatIT method testTomcat.

public void testTomcat() throws Exception {
    HtmlPage page = null;
    try {
        page = client.getPage(url);
    } catch (HttpHostConnectException e) {
        // sometimes on jenkins there are connections exceptions.
        // ignoring the rest of the test in this case
        LOG.error("Failed connecting to tomcat", e);
        return;
    }
    assertEquals("Content Repository Setup", page.getTitleText());
    page = submitNewRepositoryForm(page);
    assertEquals("Content Repository Ready", page.getTitleText());
    page = page.getAnchorByText("home").click();
    assertEquals("Apache Jackrabbit JCR Server", page.getTitleText());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException)

Example 24 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project tdi-studio-se by Talend.

the class RestClient method executePostRequest.

private int executePostRequest(String apiURI, UrlEncodedFormEntity entity) {
    try {
        HttpPost postRequest = new HttpPost(bonitaURI + apiURI);
        postRequest.setEntity(entity);
        HttpResponse response = httpClient.execute(postRequest, httpContext);
        return consumeResponse(response, true);
    } catch (HttpHostConnectException e) {
        throw new RuntimeException("Bonita bundle may not have been started, or the URL is invalid. Please verify hostname and port number. URL used is: " + BONITA_URI, e);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) HttpResponse(org.apache.http.HttpResponse) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) IOException(java.io.IOException)

Example 25 with HttpHostConnectException

use of org.apache.http.conn.HttpHostConnectException in project sling by apache.

the class SimpleHttpDistributionTransport method retrievePackage.

@Nullable
public RemoteDistributionPackage retrievePackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest distributionRequest, @Nonnull DistributionTransportContext distributionContext) throws DistributionException {
    log.debug("pulling from {}", distributionEndpoint.getUri());
    try {
        URI distributionURI = RequestUtils.appendDistributionRequest(distributionEndpoint.getUri(), distributionRequest);
        Executor executor = getExecutor(distributionContext);
        // TODO : add queue parameter
        InputStream inputStream = HttpTransportUtils.fetchNextPackage(executor, distributionURI, httpConfiguration);
        if (inputStream == null) {
            return null;
        }
        try {
            final DistributionPackage responsePackage = packageBuilder.readPackage(resourceResolver, inputStream);
            responsePackage.getInfo().put(PACKAGE_INFO_PROPERTY_ORIGIN_URI, distributionURI);
            log.debug("pulled package with info {}", responsePackage.getInfo());
            return new DefaultRemoteDistributionPackage(responsePackage, executor, distributionURI);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    } catch (HttpHostConnectException e) {
        log.debug("could not connect to {} - skipping", distributionEndpoint.getUri());
    } catch (Exception ex) {
        log.error("cannot retrieve packages", ex);
    }
    return null;
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) AbstractDistributionPackage(org.apache.sling.distribution.packaging.impl.AbstractDistributionPackage) Executor(org.apache.http.client.fluent.Executor) InputStream(java.io.InputStream) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) URI(java.net.URI) DistributionException(org.apache.sling.distribution.common.DistributionException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) HttpResponseException(org.apache.http.client.HttpResponseException) Nullable(javax.annotation.Nullable)

Aggregations

HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)37 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)11 IOException (java.io.IOException)10 ConnectException (java.net.ConnectException)10 SocketTimeoutException (java.net.SocketTimeoutException)10 Socket (java.net.Socket)7 HttpResponse (org.apache.http.HttpResponse)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)6 LayeredSocketFactory (org.apache.http.conn.scheme.LayeredSocketFactory)6 Scheme (org.apache.http.conn.scheme.Scheme)6 Test (org.junit.Test)5 InputStream (java.io.InputStream)4 GenericUrl (com.google.api.client.http.GenericUrl)3 InetAddress (java.net.InetAddress)3 NoRouteToHostException (java.net.NoRouteToHostException)3 SocketException (java.net.SocketException)3 UnknownHostException (java.net.UnknownHostException)3 HttpEntity (org.apache.http.HttpEntity)3 HttpHost (org.apache.http.HttpHost)3 ClientProtocolException (org.apache.http.client.ClientProtocolException)3