use of org.apache.http.client.methods.CloseableHttpResponse in project gocd by gocd.
the class HttpService method execute.
public CloseableHttpResponse execute(HttpRequestBase httpMethod) throws IOException {
GoAgentServerHttpClient client = httpClientFactory.httpClient();
CloseableHttpResponse response = client.execute(httpMethod);
LOGGER.info("Got back " + response.getStatusLine().getStatusCode() + " from server");
return response;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project gocd by gocd.
the class HttpServiceTest method shouldSetTheAcceptHeaderWhilePostingProperties.
@Test
public void shouldSetTheAcceptHeaderWhilePostingProperties() throws Exception {
HttpPost post = mock(HttpPost.class);
when(httpClientFactory.createPost("url")).thenReturn(post);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
when(httpClient.execute(post)).thenReturn(response);
ArgumentCaptor<UrlEncodedFormEntity> entityCaptor = ArgumentCaptor.forClass(UrlEncodedFormEntity.class);
service.postProperty("url", "value");
verify(post).setHeader("Confirm", "true");
verify(post).setEntity(entityCaptor.capture());
UrlEncodedFormEntity expected = new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("value", "value")));
UrlEncodedFormEntity actual = entityCaptor.getValue();
assertEquals(IOUtils.toString(expected.getContent()), IOUtils.toString(actual.getContent()));
assertEquals(expected.getContentLength(), expected.getContentLength());
assertEquals(expected.getContentType(), expected.getContentType());
assertEquals(expected.getContentEncoding(), expected.getContentEncoding());
assertEquals(expected.isChunked(), expected.isChunked());
}
use of org.apache.http.client.methods.CloseableHttpResponse in project gradle by gradle.
the class HttpClientHelper method executeGetOrHead.
protected CloseableHttpResponse executeGetOrHead(HttpRequestBase method) throws IOException {
final CloseableHttpResponse httpResponse = performHttpRequest(method);
// Consume content for non-successful, responses. This avoids the connection being left open.
if (!wasSuccessful(httpResponse)) {
CloseableHttpResponse response = new AutoClosedHttpResponse(httpResponse);
HttpClientUtils.closeQuietly(httpResponse);
return response;
}
return httpResponse;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project gradle by gradle.
the class HttpResourceAccessor method getRawResource.
/**
* Same as #getResource except that it always gives access to the response body,
* irrespective of the returned HTTP status code. Never returns {@code null}.
*/
public HttpResponseResource getRawResource(final URI uri, boolean revalidate) {
String location = uri.toString();
LOGGER.debug("Constructing external resource: {}", location);
CloseableHttpResponse response = http.performRawGet(location, revalidate);
return wrapResponse(uri, response);
}
use of org.apache.http.client.methods.CloseableHttpResponse in project goci by EBISPOT.
the class SolrQueryService method querySolr.
// public HttpEntity querySolr(String searchString) throws IOException{
public List<PublishedStudy> querySolr(String searchString) throws IOException {
System.out.println(searchString);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(searchString);
if (System.getProperty("http.proxyHost") != null) {
HttpHost proxy;
if (System.getProperty("http.proxyPort") != null) {
proxy = new HttpHost(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")));
} else {
proxy = new HttpHost(System.getProperty("http.proxyHost"));
}
httpGet.setConfig(RequestConfig.custom().setProxy(proxy).build());
}
// HttpEntity entity = null;
List<PublishedStudy> studies = new ArrayList<>();
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
getLog().debug("Received HTTP response: " + response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
String output;
while ((output = br.readLine()) != null) {
SolrDataProcessingService jsonProcessor = new SolrDataProcessingService(output);
studies = jsonProcessor.processJson();
}
EntityUtils.consume(entity);
}
// return entity;
return studies;
}
Aggregations