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);
}
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations