use of org.apache.http.client.methods.CloseableHttpResponse in project goci by EBISPOT.
the class SolrSearchController method dispatchDownloadSearch.
private void dispatchDownloadSearch(String searchString, OutputStream outputStream, boolean efo, String facet, boolean ancestry) throws IOException {
getLog().trace(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());
}
String file = null;
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) {
JsonProcessingService jsonProcessor = new JsonProcessingService(output, efo, facet, ancestry);
file = jsonProcessor.processJson();
}
EntityUtils.consume(entity);
}
if (file == null) {
//TO DO throw exception here and add error handler
file = "Some error occurred during your request. Please try again or contact the GWAS Catalog team for assistance";
}
PrintWriter outputWriter = new PrintWriter(outputStream);
outputWriter.write(file);
outputWriter.flush();
}
use of org.apache.http.client.methods.CloseableHttpResponse in project opennms by OpenNMS.
the class HttpUrlConnection method getInputStream.
/* (non-Javadoc)
* @see java.net.URLConnection#getInputStream()
*/
@Override
public InputStream getInputStream() throws IOException {
try {
if (m_clientWrapper == null) {
connect();
}
// Build URL
int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort();
URIBuilder ub = new URIBuilder();
ub.setPort(port);
ub.setScheme(m_url.getProtocol());
ub.setHost(m_url.getHost());
ub.setPath(m_url.getPath());
if (m_url.getQuery() != null && !m_url.getQuery().trim().isEmpty()) {
final List<NameValuePair> params = URLEncodedUtils.parse(m_url.getQuery(), StandardCharsets.UTF_8);
if (!params.isEmpty()) {
ub.addParameters(params);
}
}
// Build Request
HttpRequestBase request = null;
if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) {
final Content cnt = m_request.getContent();
HttpPost post = new HttpPost(ub.build());
ContentType contentType = ContentType.create(cnt.getType());
LOG.info("Processing POST request for {}", contentType);
if (contentType.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData());
post.setEntity(fields.getEntity());
} else {
StringEntity entity = new StringEntity(cnt.getData(), contentType);
post.setEntity(entity);
}
request = post;
} else {
request = new HttpGet(ub.build());
}
if (m_request != null) {
// Add Custom Headers
for (final Header header : m_request.getHeaders()) {
request.addHeader(header.getName(), header.getValue());
}
}
// Get Response
CloseableHttpResponse response = m_clientWrapper.execute(request);
return response.getEntity().getContent();
} catch (Exception e) {
throw new IOException("Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e);
}
}
use of org.apache.http.client.methods.CloseableHttpResponse in project opennms by OpenNMS.
the class MetadataUtils method fetchGeodata.
public static Map<String, String> fetchGeodata() {
final Map<String, String> ret = new HashMap<>();
final String url = "http://freegeoip.net/xml/";
final CloseableHttpClient httpclient = HttpClients.createDefault();
final HttpGet get = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(get);
final HttpEntity entity = response.getEntity();
final String xml = EntityUtils.toString(entity);
System.err.println("xml = " + xml);
final GeodataResponse geoResponse = JaxbUtils.unmarshal(GeodataResponse.class, xml);
ret.put("external-ip-address", InetAddressUtils.str(geoResponse.getIp()));
ret.put("country-code", geoResponse.getCountryCode());
ret.put("region-code", geoResponse.getRegionCode());
ret.put("city", geoResponse.getCity());
ret.put("zip-code", geoResponse.getZipCode());
ret.put("time-zone", geoResponse.getTimeZone());
ret.put("latitude", geoResponse.getLatitude() == null ? null : geoResponse.getLatitude().toString());
ret.put("longitude", geoResponse.getLongitude() == null ? null : geoResponse.getLongitude().toString());
EntityUtils.consumeQuietly(entity);
} catch (final Exception e) {
LOG.debug("Failed to get GeoIP data from " + url, e);
} finally {
IOUtils.closeQuietly(response);
}
return ret;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project api-snippets by TwilioDevEd.
the class Example method main.
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("lookups.twilio.com", 80), new UsernamePasswordCredentials("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "your_auth_token"));
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
try {
HttpGet httpget = new HttpGet("https://lookups.twilio.com/v1/PhoneNumbers/+16502530000/?AddOns=payfone_tcpa_compliance&AddOns.payfone_tcpa_compliance.RightPartyContactedDate=20160101");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
use of org.apache.http.client.methods.CloseableHttpResponse in project voltdb by VoltDB.
the class HTTPUtils method callProcOverJSONRaw.
public static String callProcOverJSONRaw(List<NameValuePair> vals, CloseableHttpClient httpclient, HttpPost httppost) throws Exception {
HttpEntity entity = null;
String entityStr = null;
httppost.setEntity(new UrlEncodedFormEntity(vals));
CloseableHttpResponse httpResponse = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
return response.toString();
}
Aggregations