use of org.apache.http.conn.HttpHostConnectException in project jenkin-qtest-plugin by QASymphony.
the class HttpClientUtils method getHttpClient.
public static HttpClient getHttpClient(String hostUrl) throws Exception {
int timeout;
try {
timeout = Integer.parseInt(System.getenv("SOCKET_TIMEOUT"));
} catch (Exception e) {
timeout = DEFAULT_SOCKET_TIMEOUT;
}
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties();
setHttpProxy(httpClientBuilder, hostUrl);
SSLConnectionSocketFactory sslSocketFactory = getSslSocketFactory();
httpClientBuilder.setSSLSocketFactory(sslSocketFactory).setConnectionReuseStrategy(new NoConnectionReuseStrategy());
timeout = timeout * 1000;
httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).build());
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(RETRY_MAX_COUNT, RETRY_REQUEST_SEND_RETRY_ENABLED) {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount > this.getRetryCount())
return false;
if (exception instanceof HttpHostConnectException)
return true;
return super.retryRequest(exception, executionCount, context);
}
});
return httpClientBuilder.build();
}
use of org.apache.http.conn.HttpHostConnectException in project dropwizard by dropwizard.
the class DropwizardApacheConnectorTest method connect_timeout_override_changes_how_long_it_takes_for_a_connection_to_timeout.
/**
* <p>In first assertion we prove, that a request takes no longer than:
* <em>request_time < connect_timeout + error_margin</em> (1)</p>
* <p/>
* </p>In the second we show that if we set <b>connect_timeout</b> to
* <b>set_connect_timeout + increase + error_margin</b> then
* <em>request_time > connect_timeout + increase + error_margin</em> (2)</p>
* <p/>
* <p>Now, (1) and (2) can hold at the same time if then connect_timeout update was successful.</p>
*/
@Test
@Disabled("Flaky, timeout jumps over the threshold")
void connect_timeout_override_changes_how_long_it_takes_for_a_connection_to_timeout() {
// setUp override
WebTarget target = client.target(NON_ROUTABLE_ADDRESS);
// This can't be tested without a real connection
try {
target.request().get(Response.class);
} catch (ProcessingException e) {
if (e.getCause() instanceof HttpHostConnectException) {
return;
}
}
assertThatConnectionTimeoutFor(target).isLessThan(DEFAULT_CONNECT_TIMEOUT_IN_MILLIS + ERROR_MARGIN_IN_MILLIS);
// tearDown override
final int newTimeout = DEFAULT_CONNECT_TIMEOUT_IN_MILLIS + INCREASE_IN_MILLIS + ERROR_MARGIN_IN_MILLIS;
final WebTarget newTarget = target.property(ClientProperties.CONNECT_TIMEOUT, newTimeout);
assertThatConnectionTimeoutFor(newTarget).isGreaterThan(newTimeout);
}
use of org.apache.http.conn.HttpHostConnectException in project new-cloud by xie-summer.
the class HttpUtils method uploadFile.
/**
* @param url
* @param params
* @param uploadMap
* @param fileNameMap
* @param encode
* @return
*/
public static HttpResult uploadFile(String url, Map<String, String> params, Map<String, byte[]> uploadMap, Map<String, String> fileNameMap, String encode, int timeout) {
/**
* DefaultHttpClient client = new DefaultHttpClient();
* client.getParams().setIntParameter("http.socket.timeout",
* LONG_TIMEOUT); client.getParams().setBooleanParameter(
* "http.protocol.expect-continue", false);
* client.getParams().setIntParameter("http.connection.timeout",
* CONNECTION_TIMEOUT);
*/
CookieStore cookieStore = getCookieStore(null);
CloseableHttpClient client = getHttpClient(CONNECTION_TIMEOUT, timeout, cookieStore);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName(encode));
HttpPost request = new HttpPost(url);
// MultipartEntity reqEntity = new MultipartEntity();
for (String input : uploadMap.keySet()) {
ByteArrayBody isb = new ByteArrayBody(uploadMap.get(input), fileNameMap.get(input));
multipartEntityBuilder.addPart(input, isb);
}
try {
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
multipartEntityBuilder.addTextBody(key, params.get(key), ContentType.create("text/plain", Charset.forName(encode)));
}
}
request.setEntity(multipartEntityBuilder.build());
List<Cookie> reqcookie = cookieStore.getCookies();
CloseableHttpResponse response = client.execute(request);
try {
String result = "";
HttpEntity entity = getEntity(response);
if (entity != null) {
result = EntityUtils.toString(entity, encode);
}
if (isSuccess(response)) {
HttpResult ret = HttpResult.getSuccessReturn(result);
addHeader(ret, response);
List<Cookie> cookies = cookieStore.getCookies();
addCookie(ret, cookies, reqcookie);
return ret;
} else {
int statusCode = response.getStatusLine().getStatusCode();
String msg = "httpStatus:" + statusCode + response.getStatusLine().getReasonPhrase() + ", Header: ";
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
msg += header.getName() + ":" + header.getValue();
}
request.abort();
DB_LOGGER.error("ERROR HttpUtils:" + msg + request.getURI());
return HttpResult.getFailure("httpStatus:" + response.getStatusLine().getStatusCode(), statusCode, result);
}
} finally {
response.close();
}
} catch (HttpHostConnectException e) {
request.abort();
DB_LOGGER.error(request.getURI() + ":" + LoggerUtils.getExceptionTrace(e, 30));
return HttpResult.getFailure(request.getURI() + " exception:" + e.getClass().getCanonicalName(), HTTP_STATUSCODE_HTTP_HOST_CONNECT_EXCEPTION);
} catch (ConnectTimeoutException e) {
request.abort();
DB_LOGGER.error(request.getURI() + ":" + LoggerUtils.getExceptionTrace(e, 30));
return HttpResult.getFailure(request.getURI() + " exception:" + e.getClass().getCanonicalName(), HTTP_STATUSCODE_CONNECT_TIMEOUT_EXCEPTION);
} catch (SocketTimeoutException e) {
request.abort();
DB_LOGGER.error(request.getURI() + ":" + LoggerUtils.getExceptionTrace(e, 30));
return HttpResult.getFailure(request.getURI() + " exception:" + e.getClass().getCanonicalName(), HTTP_STATUSCODE_SOCKET_TIMEOUT_EXCEPTION);
} catch (Exception e) {
request.abort();
DB_LOGGER.error(request.getURI() + ":" + LoggerUtils.getExceptionTrace(e, 100));
return HttpResult.getFailure(request.getURI() + " exception:" + e.getClass().getCanonicalName(), EXCEPTION_HTTP_STATUSCODE);
}
}
use of org.apache.http.conn.HttpHostConnectException in project LogHub by fbacchella.
the class AbstractHttpSender method doRequest.
protected HttpResponse doRequest(HttpRequest therequest) {
CloseableHttpResponse response = null;
HttpClientContext context = HttpClientContext.create();
if (credsProvider != null) {
context.setCredentialsProvider(credsProvider);
}
HttpHost host;
RequestLine requestLine = new BasicRequestLine(therequest.verb, therequest.url.getPath(), therequest.httpVersion);
BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(requestLine);
if (therequest.content != null) {
request.setEntity(therequest.content);
}
therequest.headers.forEach((i, j) -> request.addHeader(i, j));
host = new HttpHost(therequest.url.getHost(), therequest.url.getPort(), therequest.url.getProtocol());
try {
response = client.execute(host, request, context);
} catch (ConnectionPoolTimeoutException e) {
logger.error("Connection to {} timed out", host);
return new HttpResponse(host, null, e, null);
} catch (HttpHostConnectException e) {
String message = "";
try {
throw e.getCause();
} catch (ConnectException e1) {
message = String.format("Connection to %s refused", host);
} catch (SocketTimeoutException e1) {
message = String.format("Slow response from %s", host);
} catch (Throwable e1) {
message = String.format("Connection to %s failed: %s", host, e1.getMessage());
logger.catching(Level.DEBUG, e1);
}
logger.error(message);
logger.catching(Level.DEBUG, e.getCause());
return new HttpResponse(host, null, e, null);
} catch (IOException e) {
Throwable rootCause = e;
while (rootCause.getCause() != null) {
rootCause = rootCause.getCause();
}
;
// A TLS exception, will not help to retry
if (rootCause instanceof GeneralSecurityException) {
logger.error("Secure comunication with {} failed: {}", host, rootCause.getMessage());
logger.catching(Level.DEBUG, rootCause);
return new HttpResponse(host, null, null, (GeneralSecurityException) rootCause);
} else {
logger.error("Comunication with {} failed: {}", host, e.getMessage());
logger.catching(Level.DEBUG, e);
return new HttpResponse(host, null, e, null);
}
}
if (response == null) {
logger.error("give up trying to connect to " + getPublishName());
return null;
}
;
return new HttpResponse(host, response, null, null);
}
use of org.apache.http.conn.HttpHostConnectException in project providence by morimekta.
the class HttpClientHandlerNetworkTest method testSimpleRequest_connectionRefused_apacheHttpTransport.
@Test
public void testSimpleRequest_connectionRefused_apacheHttpTransport() throws IOException, Failure {
HttpRequestFactory factory = new ApacheHttpTransport().createRequestFactory();
GenericUrl url = new GenericUrl("http://localhost:" + (port - 10) + "/" + ENDPOINT);
TestService.Iface client = new TestService.Client(new HttpClientHandler(() -> url, factory, provider));
try {
client.test(new Request("request"));
fail("No exception");
} catch (HttpHostConnectException ex) {
assertThat(ex.getMessage(), allOf(startsWith("Connect to localhost:" + (port - 10) + " failed: "), containsString("Connection refused")));
}
}
Aggregations