use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.
the class DropwizardApacheConnectorTest method multiple_headers_with_the_same_name_are_processed_successfully.
@Test
public void multiple_headers_with_the_same_name_are_processed_successfully() throws Exception {
final CloseableHttpClient client = mock(CloseableHttpClient.class);
final DropwizardApacheConnector dropwizardApacheConnector = new DropwizardApacheConnector(client, null, false);
final Header[] apacheHeaders = { new BasicHeader("Set-Cookie", "test1"), new BasicHeader("Set-Cookie", "test2") };
final CloseableHttpResponse apacheResponse = mock(CloseableHttpResponse.class);
when(apacheResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
when(apacheResponse.getAllHeaders()).thenReturn(apacheHeaders);
when(client.execute(Mockito.any())).thenReturn(apacheResponse);
final ClientRequest jerseyRequest = mock(ClientRequest.class);
when(jerseyRequest.getUri()).thenReturn(URI.create("http://localhost"));
when(jerseyRequest.getMethod()).thenReturn("GET");
when(jerseyRequest.getHeaders()).thenReturn(new MultivaluedHashMap<>());
final ClientResponse jerseyResponse = dropwizardApacheConnector.apply(jerseyRequest);
assertThat(jerseyResponse.getStatus()).isEqualTo(apacheResponse.getStatusLine().getStatusCode());
}
use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.
the class HttpClientBuilderTest method usesTheDefaultRoutePlanner.
@Test
public void usesTheDefaultRoutePlanner() throws Exception {
final CloseableHttpClient httpClient = builder.using(configuration).createClient(apacheBuilder, connectionManager, "test").getClient();
assertThat(httpClient).isNotNull();
assertThat(spyHttpClientBuilderField("routePlanner", apacheBuilder)).isNull();
assertThat(spyHttpClientField("routePlanner", httpClient)).isInstanceOf(DefaultRoutePlanner.class);
}
use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.
the class HttpClientBuilderTest method usesProxyWithAuth.
@Test
public void usesProxyWithAuth() throws Exception {
HttpClientConfiguration config = new HttpClientConfiguration();
AuthConfiguration auth = new AuthConfiguration("secret", "stuff");
ProxyConfiguration proxy = new ProxyConfiguration("192.168.52.11", 8080, "http", auth);
config.setProxyConfiguration(proxy);
CloseableHttpClient httpClient = checkProxy(config, new HttpHost("dropwizard.io", 80), new HttpHost("192.168.52.11", 8080, "http"));
CredentialsProvider credentialsProvider = (CredentialsProvider) FieldUtils.getField(httpClient.getClass(), "credentialsProvider", true).get(httpClient);
assertThat(credentialsProvider.getCredentials(new AuthScope("192.168.52.11", 8080))).isEqualTo(new UsernamePasswordCredentials("secret", "stuff"));
}
use of org.apache.http.impl.client.CloseableHttpClient in project dropwizard by dropwizard.
the class HttpClientBuilderTest method checkProxy.
private CloseableHttpClient checkProxy(HttpClientConfiguration config, HttpHost target, HttpHost expectedProxy) throws Exception {
CloseableHttpClient httpClient = builder.using(config).build("test");
HttpRoutePlanner routePlanner = (HttpRoutePlanner) FieldUtils.getField(httpClient.getClass(), "routePlanner", true).get(httpClient);
HttpRoute route = routePlanner.determineRoute(target, new HttpGet(target.toURI()), new BasicHttpContext());
assertThat(route.getProxyHost()).isEqualTo(expectedProxy);
assertThat(route.getTargetHost()).isEqualTo(target);
assertThat(route.getHopCount()).isEqualTo(expectedProxy != null ? 2 : 1);
return httpClient;
}
use of org.apache.http.impl.client.CloseableHttpClient 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