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 mastering-java by Kingminghuang.
the class HttpClientCrawler method download.
public static HttpEntity download(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");
CloseableHttpResponse response = client.execute(httpGet);
return response.getEntity();
}
use of org.apache.http.client.methods.CloseableHttpResponse in project mastering-java by Kingminghuang.
the class HttpClientCrawler method testStatusCode.
private static void testStatusCode() throws IOException {
RequestConfig requestConfig = RequestConfig.custom().setProxy(new HttpHost(DEFAULT_PROXY, DEFAULT_PORT, DEFAULT_SCHEMA)).build();
config(requestConfig);
HttpGet httpGet = new HttpGet(EXAMPLE_URL);
CloseableHttpResponse response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode == HttpStatus.SC_OK);
}
use of org.apache.http.client.methods.CloseableHttpResponse in project libresonic by Libresonic.
the class ProxyController method handleRequest.
@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String url = ServletRequestUtils.getRequiredStringParameter(request, "url");
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000).setSocketTimeout(15000).build();
HttpGet method = new HttpGet(url);
method.setConfig(requestConfig);
InputStream in = null;
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpResponse resp = client.execute(method)) {
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
response.sendError(statusCode);
} else {
in = resp.getEntity().getContent();
IOUtils.copy(in, response.getOutputStream());
}
}
} finally {
IOUtils.closeQuietly(in);
}
return null;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project libresonic by Libresonic.
the class PodcastService method downloadImage.
private void downloadImage(PodcastChannel channel) {
InputStream in = null;
OutputStream out = null;
try (CloseableHttpClient client = HttpClients.createDefault()) {
String imageUrl = channel.getImageUrl();
if (imageUrl == null) {
return;
}
File dir = getChannelDirectory(channel);
MediaFile channelMediaFile = mediaFileService.getMediaFile(dir);
File existingCoverArt = mediaFileService.getCoverArt(channelMediaFile);
boolean imageFileExists = existingCoverArt != null && mediaFileService.getMediaFile(existingCoverArt) == null;
if (imageFileExists) {
return;
}
HttpGet method = new HttpGet(imageUrl);
try (CloseableHttpResponse response = client.execute(method)) {
in = response.getEntity().getContent();
out = new FileOutputStream(new File(dir, "cover." + getCoverArtSuffix(response)));
IOUtils.copy(in, out);
mediaFileService.refreshMediaFile(channelMediaFile);
}
} catch (Exception x) {
LOG.warn("Failed to download cover art for podcast channel '" + channel.getTitle() + "': " + x, x);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
Aggregations