use of org.apache.http.client.methods.CloseableHttpResponse in project apn-proxy by apn-proxy.
the class TestProxyWithHttpClient method test.
private void test(String uri, int exceptCode, String exceptHeaderName, String exceptHeaderValue) {
ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(2000);
cm.setDefaultMaxPerRoute(40);
cm.setDefaultConnectionConfig(connectionConfig);
CloseableHttpClient httpClient = HttpClients.custom().setUserAgent("Mozilla/5.0 xx-dev-web-common httpclient/4.x").setConnectionManager(cm).disableContentCompression().disableCookieManagement().build();
HttpHost proxy = new HttpHost("127.0.0.1", ApnProxyConfig.getConfig().getPort());
RequestConfig config = RequestConfig.custom().setProxy(proxy).setExpectContinueEnabled(true).setConnectionRequestTimeout(5000).setConnectTimeout(10000).setSocketTimeout(10000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
HttpGet request = new HttpGet(uri);
request.setConfig(config);
try {
CloseableHttpResponse httpResponse = httpClient.execute(request);
Assert.assertEquals(exceptCode, httpResponse.getStatusLine().getStatusCode());
if (StringUtils.isNotBlank(exceptHeaderName) && StringUtils.isNotBlank(exceptHeaderValue)) {
Assert.assertEquals(exceptHeaderValue, httpResponse.getFirstHeader(exceptHeaderName).getValue());
}
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseHandler.handleResponse(httpResponse);
httpResponse.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
use of org.apache.http.client.methods.CloseableHttpResponse in project YCSB by brianfrankcooper.
the class RestClient method httpExecute.
private int httpExecute(HttpEntityEnclosingRequestBase request, String data) throws IOException {
requestTimedout.setIsSatisfied(false);
Thread timer = new Thread(new Timer(execTimeout, requestTimedout));
timer.start();
int responseCode = 200;
for (int i = 0; i < headers.length; i = i + 2) {
request.setHeader(headers[i], headers[i + 1]);
}
InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(data.getBytes()), ContentType.APPLICATION_FORM_URLENCODED);
reqEntity.setChunked(true);
request.setEntity(reqEntity);
CloseableHttpResponse response = client.execute(request);
responseCode = response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
// If null entity don't bother about connection release.
if (responseEntity != null) {
InputStream stream = responseEntity.getContent();
if (compressedResponse) {
stream = new GZIPInputStream(stream);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
StringBuffer responseContent = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
if (requestTimedout.isSatisfied()) {
// Must avoid memory leak.
reader.close();
stream.close();
EntityUtils.consumeQuietly(responseEntity);
response.close();
client.close();
throw new TimeoutException();
}
responseContent.append(line);
}
timer.interrupt();
// Closing the input stream will trigger connection release.
stream.close();
}
EntityUtils.consumeQuietly(responseEntity);
response.close();
client.close();
return responseCode;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project YCSB by brianfrankcooper.
the class RestClient method httpDelete.
private int httpDelete(String endpoint) throws IOException {
requestTimedout.setIsSatisfied(false);
Thread timer = new Thread(new Timer(execTimeout, requestTimedout));
timer.start();
int responseCode = 200;
HttpDelete request = new HttpDelete(endpoint);
for (int i = 0; i < headers.length; i = i + 2) {
request.setHeader(headers[i], headers[i + 1]);
}
CloseableHttpResponse response = client.execute(request);
responseCode = response.getStatusLine().getStatusCode();
response.close();
client.close();
return responseCode;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project google-analytics-java by brsanthu.
the class GoogleAnalyticsThreadFactory method post.
@SuppressWarnings({ "rawtypes" })
public GoogleAnalyticsResponse post(GoogleAnalyticsRequest request) {
GoogleAnalyticsResponse response = new GoogleAnalyticsResponse();
if (!config.isEnabled()) {
return response;
}
CloseableHttpResponse httpResponse = null;
try {
List<NameValuePair> postParms = new ArrayList<NameValuePair>();
logger.debug("Processing " + request);
//Process the parameters
processParameters(request, postParms);
//Process custom dimensions
processCustomDimensionParameters(request, postParms);
//Process custom metrics
processCustomMetricParameters(request, postParms);
logger.debug("Processed all parameters and sending the request " + postParms);
HttpPost httpPost = new HttpPost(config.getUrl());
httpPost.setEntity(new UrlEncodedFormEntity(postParms, UTF8));
httpResponse = (CloseableHttpResponse) httpClient.execute(httpPost);
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
response.setPostedParms(postParms);
EntityUtils.consumeQuietly(httpResponse.getEntity());
if (config.isGatherStats()) {
gatherStats(request);
}
} catch (Exception e) {
if (e instanceof UnknownHostException) {
logger.warn("Coudln't connect to Google Analytics. Internet may not be available. " + e.toString());
} else {
logger.warn("Exception while sending the Google Analytics tracker request " + request, e);
}
} finally {
try {
httpResponse.close();
} catch (Exception e2) {
//ignore
}
}
return response;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project gocd by gocd.
the class ServerBinaryDownloaderTest method shouldReturnFalseIfTheServerDoesNotRespondWithEntity.
@Test
public void shouldReturnFalseIfTheServerDoesNotRespondWithEntity() throws Exception {
GoAgentServerHttpClientBuilder builder = mock(GoAgentServerHttpClientBuilder.class);
CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
when(builder.build()).thenReturn(closeableHttpClient);
CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class);
when(closeableHttpClient.execute(any(HttpRequestBase.class))).thenReturn(httpResponse);
ServerBinaryDownloader downloader = new ServerBinaryDownloader(builder, ServerUrlGeneratorMother.generatorFor("localhost", 9090));
assertThat(downloader.download(DownloadableFile.AGENT), is(false));
}
Aggregations