Search in sources :

Example 46 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project lucene-solr by apache.

the class PeerSync method handleResponse.

private boolean handleResponse(ShardResponse srsp) {
    ShardRequest sreq = srsp.getShardRequest();
    if (srsp.getException() != null) {
        // redundantly asking other replicas for them).
        if (cantReachIsSuccess && sreq.purpose == 1 && srsp.getException() instanceof SolrServerException) {
            Throwable solrException = ((SolrServerException) srsp.getException()).getRootCause();
            boolean connectTimeoutExceptionInChain = connectTimeoutExceptionInChain(srsp.getException());
            if (connectTimeoutExceptionInChain || solrException instanceof ConnectException || solrException instanceof ConnectTimeoutException || solrException instanceof NoHttpResponseException || solrException instanceof SocketException) {
                log.warn(msg() + " couldn't connect to " + srsp.getShardAddress() + ", counting as success", srsp.getException());
                return true;
            }
        }
        if (cantReachIsSuccess && sreq.purpose == 1 && srsp.getException() instanceof SolrException && ((SolrException) srsp.getException()).code() == 503) {
            log.warn(msg() + " got a 503 from " + srsp.getShardAddress() + ", counting as success", srsp.getException());
            return true;
        }
        if (cantReachIsSuccess && sreq.purpose == 1 && srsp.getException() instanceof SolrException && ((SolrException) srsp.getException()).code() == 404) {
            log.warn(msg() + " got a 404 from " + srsp.getShardAddress() + ", counting as success. " + "Perhaps /get is not registered?", srsp.getException());
            return true;
        }
        // TODO: we should return the above information so that when we can request a recovery through zookeeper, we do
        // that for these nodes
        // TODO: at least log???
        // srsp.getException().printStackTrace(System.out);
        log.warn(msg() + " exception talking to " + srsp.getShardAddress() + ", failed", srsp.getException());
        return false;
    }
    if (sreq.purpose == 1) {
        return handleVersions(srsp);
    } else {
        return handleUpdates(srsp);
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) SocketException(java.net.SocketException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ShardRequest(org.apache.solr.handler.component.ShardRequest) SolrException(org.apache.solr.common.SolrException) ConnectException(java.net.ConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 47 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project xian by happyyangyuan.

the class BasicAuthApacheHttpClientGetUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String url = msg.get("url", String.class);
    String userName = msg.get("userName", String.class);
    String password = msg.get("password", String.class);
    Map<String, String> headers = msg.get("headers");
    try (IApacheHttpClient httpClient = BasicAuthApacheHttpClient.newInstance(url, userName, password, headers)) {
        JSONObject responseJSON = new JSONObject();
        HttpResponse httpResponse;
        try {
            httpResponse = httpClient.getHttpResponse();
        } catch (ConnectTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_CONNECT_TIMEOUT, null, "Connect timeout: " + url);
        } catch (SocketTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_SOCKET_TIMEOUT, null, "Read timeout: " + url);
        } catch (Throwable e) {
            return UnitResponse.exception(e);
        }
        responseJSON.put("statusLine", new JSONObject() {

            {
                put("statusCode", httpResponse.getStatusLine().getStatusCode());
                put("protocolVersion", httpResponse.getStatusLine().getProtocolVersion());
                put("reasonPhrase", httpResponse.getStatusLine().getReasonPhrase());
            }
        });
        responseJSON.put("allHeaders", httpResponse.getAllHeaders());
        try {
            responseJSON.put("entity", EntityUtils.toString(httpResponse.getEntity(), "UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return UnitResponse.success(responseJSON);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(com.alibaba.fastjson.JSONObject) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) IApacheHttpClient(info.xiancloud.httpclient.apache_http.IApacheHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 48 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project xian by happyyangyuan.

the class BasicAuthApacheHttpClientPostUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    String url = msg.get("url", String.class);
    String userName = msg.get("userName", String.class);
    String password = msg.get("password", String.class);
    Map<String, String> headers = msg.get("headers");
    String body = msg.get("body", String.class);
    try (IApacheHttpClient httpClient = BasicAuthApacheHttpClient.newInstance(url, userName, password, headers)) {
        JSONObject responseJSON = new JSONObject();
        String resPayload;
        try {
            resPayload = httpClient.post(body);
        } catch (ConnectTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_CONNECT_TIMEOUT, null, "Connect timeout: " + url);
        } catch (SocketTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_SOCKET_TIMEOUT, null, "Read timeout: " + url);
        } catch (Throwable e) {
            return UnitResponse.exception(e);
        }
        responseJSON.put("statusLine", new JSONObject() {

            {
                put("statusCode", "todo");
                put("protocolVersion", "todo");
                put("reasonPhrase", "todo");
            }
        });
        responseJSON.put("allHeaders", "todo");
        responseJSON.put("entity", resPayload);
        return UnitResponse.success(responseJSON);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(com.alibaba.fastjson.JSONObject) IOException(java.io.IOException) IApacheHttpClient(info.xiancloud.httpclient.apache_http.IApacheHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 49 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project xian by happyyangyuan.

the class ApacheHttpClient method post.

@Override
public String post(String body) throws ConnectTimeoutException, SocketTimeoutException {
    /*
         * LOG.info(String.format(
         * "httpClient获取到的header: %s  ;   httpClient获取到的body:%s ", headers,
         * body));
         */
    if (client == null) {
        return INIT_FAIL;
    }
    HttpPost httpPost = new HttpPost(url);
    httpPost.setProtocolVersion(HttpVersion.HTTP_1_1);
    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httpPost.setHeader(entry.getKey(), entry.getValue());
        }
    }
    String responseContent;
    try {
        StringEntity entity = new StringEntity(body == null ? "" : body, "utf-8");
        entity.setContentEncoding("utf-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        httpPost.setConfig(requestConfig);
        HttpResponse httpResponse = client.execute(httpPost);
        responseContent = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
    } catch (ConnectTimeoutException | SocketTimeoutException connectOrReadTimeout) {
        throw connectOrReadTimeout;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        httpPost.releaseConnection();
    }
    return responseContent;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) SocketTimeoutException(java.net.SocketTimeoutException) Map(java.util.Map) IOException(java.io.IOException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 50 with ConnectTimeoutException

use of org.apache.http.conn.ConnectTimeoutException in project xian by happyyangyuan.

the class ApacheHttpClient method get.

@Override
public String get() throws ConnectTimeoutException, SocketTimeoutException {
    if (client == null) {
        return INIT_FAIL;
    }
    HttpGet httpGet = new HttpGet(url);
    httpGet.setProtocolVersion(HttpVersion.HTTP_1_1);
    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httpGet.setHeader(entry.getKey(), entry.getValue());
        }
    }
    String responseContent;
    try {
        httpGet.setConfig(requestConfig);
        HttpResponse httpResponse = client.execute(httpGet);
        responseContent = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
    } catch (ConnectTimeoutException | SocketTimeoutException connectOrReadTimeout) {
        throw connectOrReadTimeout;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        httpGet.releaseConnection();
    }
    return responseContent;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) HttpGet(org.apache.http.client.methods.HttpGet) Map(java.util.Map) IOException(java.io.IOException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Aggregations

ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)69 SocketTimeoutException (java.net.SocketTimeoutException)38 IOException (java.io.IOException)32 HttpResponse (org.apache.http.HttpResponse)18 HashMap (java.util.HashMap)14 SocketException (java.net.SocketException)13 StatusLine (org.apache.http.StatusLine)13 MalformedURLException (java.net.MalformedURLException)11 NoHttpResponseException (org.apache.http.NoHttpResponseException)11 TimeoutError (com.android.volley.TimeoutError)10 HttpPost (org.apache.http.client.methods.HttpPost)10 AuthFailureError (com.android.volley.AuthFailureError)9 NetworkError (com.android.volley.NetworkError)9 NetworkResponse (com.android.volley.NetworkResponse)9 NoConnectionError (com.android.volley.NoConnectionError)9 ServerError (com.android.volley.ServerError)9 ConnectException (java.net.ConnectException)9 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)9 HttpEntity (org.apache.http.HttpEntity)8 UnknownHostException (java.net.UnknownHostException)7