use of org.apache.http.conn.ConnectTimeoutException in project elasticsearch by elastic.
the class RestClientMultipleHostsTests method createRestClient.
@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));
}
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);
}
use of org.apache.http.conn.ConnectTimeoutException in project elasticsearch by elastic.
the class RestClientSingleHostTests method createRestClient.
@Before
@SuppressWarnings("unchecked")
public void createRestClient() throws IOException {
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];
HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2];
assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class));
FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock.getArguments()[3];
HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest();
//return the desired status code or exception depending on the path
if (request.getURI().getPath().equals("/soe")) {
futureCallback.failed(new SocketTimeoutException());
} else if (request.getURI().getPath().equals("/coe")) {
futureCallback.failed(new ConnectTimeoutException());
} else {
int statusCode = Integer.parseInt(request.getURI().getPath().substring(1));
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), statusCode, "");
HttpResponse httpResponse = new BasicHttpResponse(statusLine);
//return the same body that was sent
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
assertTrue("the entity is not repeatable, cannot set it to the response directly", entity.isRepeatable());
httpResponse.setEntity(entity);
}
}
//return the same headers that were sent
httpResponse.setHeaders(request.getAllHeaders());
futureCallback.completed(httpResponse);
}
return null;
}
});
defaultHeaders = RestClientTestUtil.randomHeaders(getRandom(), "Header-default");
httpHost = new HttpHost("localhost", 9200);
failureListener = new HostsTrackingFailureListener();
restClient = new RestClient(httpClient, 10000, defaultHeaders, new HttpHost[] { httpHost }, null, failureListener);
}
use of org.apache.http.conn.ConnectTimeoutException in project UltimateAndroid by cymcsg.
the class HttpUtils method uploadFilesMPE.
/**
* Update Files via Multipart
*
* @param url
* @param paramsList
* @param fileParams
* @param file
* @param files
* @return status
* @deprecated
*/
public static String uploadFilesMPE(String url, List<NameValuePair> paramsList, String fileParams, File file, File... files) {
String result = "";
try {
DefaultHttpClient mHttpClient;
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
mHttpClient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost(url);
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
if (BasicUtils.judgeNotNull(paramsList)) {
for (NameValuePair nameValuePair : paramsList) {
multipartEntity.addPart(nameValuePair.getName(), new StringBody(nameValuePair.getValue()));
}
}
multipartEntity.addPart(fileParams, new FileBody(file));
for (File f : files) {
multipartEntity.addPart(fileParams, new FileBody(f));
}
httpPost.setEntity(multipartEntity);
HttpResponse httpResp = mHttpClient.execute(httpPost);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() == 200) {
// 获取返回的数据
result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Logs.d("HttpPost success :");
Logs.d(result);
} else {
Logs.d("HttpPost failed" + " " + httpResp.getStatusLine().getStatusCode() + " " + EntityUtils.toString(httpResp.getEntity(), "UTF-8"));
result = "HttpPost failed";
}
} catch (ConnectTimeoutException e) {
result = "ConnectTimeoutException";
Logs.e("HttpPost overtime: " + "");
} catch (Exception e) {
e.printStackTrace();
Logs.e(e, "");
result = "Exception";
}
return result;
}
use of org.apache.http.conn.ConnectTimeoutException in project jena by apache.
the class TestService method testStringTimeout2.
@Test
public void testStringTimeout2() {
BasicPattern basicPattern = new BasicPattern();
basicPattern.add(Triple.ANY);
Node serviceNode = NodeFactory.createURI(SERVICE);
OpService opService = new OpService(serviceNode, new OpBGP(basicPattern), false);
Context context = new Context();
ARQ.setNormalMode(context);
context.set(Service.queryTimeout, "10,10000");
try {
Service.exec(opService, context);
Assert.fail("Expected QueryExceptionHTTP");
} catch (QueryExceptionHTTP expected) {
Throwable thrown = expected.getCause();
if (thrown instanceof SocketException || thrown instanceof ConnectTimeoutException) {
// expected
} else {
Assert.fail(String.format("Expected SocketException or ConnectTimeoutException, instead got: %s %s", thrown.getClass().getName(), thrown.getMessage()));
}
}
}
use of org.apache.http.conn.ConnectTimeoutException in project jena by apache.
the class TestService method testNumericTimeout.
@Test
public void testNumericTimeout() {
BasicPattern basicPattern = new BasicPattern();
basicPattern.add(Triple.ANY);
Node serviceNode = NodeFactory.createURI(SERVICE);
OpService opService = new OpService(serviceNode, new OpBGP(basicPattern), false);
Context context = new Context();
ARQ.setNormalMode(context);
context.set(Service.queryTimeout, 10);
try {
Service.exec(opService, context);
Assert.fail("Expected QueryExceptionHTTP");
} catch (QueryExceptionHTTP expected) {
Throwable thrown = expected.getCause();
if (thrown instanceof SocketException || thrown instanceof ConnectTimeoutException) {
// expected
} else {
Assert.fail(String.format("Expected SocketException or ConnectTimeoutException, instead got: %s %s", thrown.getClass().getName(), thrown.getMessage()));
}
}
}
Aggregations