use of org.apache.http.impl.client.CloseableHttpClient in project azure-tools-for-java by Microsoft.
the class SparkInteractiveSessions method getExecutionState.
/**
* get job execution status
*
* @param connectUrl eg http://localhost:8998/sessions
* @param sessionId session id
* @param statementId statement id
* @return response result
* @throws IOException
*/
public HttpResponse getExecutionState(String connectUrl, int sessionId, String statementId) throws IOException {
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
HttpGet httpGet = new HttpGet(connectUrl + "/" + sessionId + "/statements/" + statementId);
httpGet.addHeader("Content-Type", "application/json");
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
return StreamUtil.getResultFromHttpResponse(response);
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project azure-tools-for-java by Microsoft.
the class SparkInteractiveSessions method executeInSession.
/**
* @param connectUrl : eg http://localhost:8998/sessions
* @param sessionId : session id
* @param code scala or python code segment
* @return response result
* @throws IOException
*/
public HttpResponse executeInSession(String connectUrl, int sessionId, String code) throws IOException {
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
HttpPost httpPost = new HttpPost(connectUrl + "/" + sessionId + "/statements");
httpPost.addHeader("Content-Type", "application/json");
String jsonString = "{\"code\" : \"" + code + "\"}";
StringEntity postingString = new StringEntity(jsonString);
httpPost.setEntity(postingString);
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
return StreamUtil.getResultFromHttpResponse(response);
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project libresonic by Libresonic.
the class LyricsService method executeGetRequest.
private String executeGetRequest(String url) throws IOException {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000).setSocketTimeout(15000).build();
HttpGet method = new HttpGet(url);
method.setConfig(requestConfig);
try (CloseableHttpClient client = HttpClients.createDefault()) {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
return client.execute(method, responseHandler);
}
}
use of org.apache.http.impl.client.CloseableHttpClient 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);
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project libresonic by Libresonic.
the class PodcastService method doRefreshChannel.
@SuppressWarnings({ "unchecked" })
private void doRefreshChannel(PodcastChannel channel, boolean downloadEpisodes) {
InputStream in = null;
try (CloseableHttpClient client = HttpClients.createDefault()) {
channel.setStatus(PodcastStatus.DOWNLOADING);
channel.setErrorMessage(null);
podcastDao.updateChannel(channel);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(// 2 minutes
2 * 60 * 1000).setSocketTimeout(// 10 minutes
10 * 60 * 1000).build();
HttpGet method = new HttpGet(channel.getUrl());
method.setConfig(requestConfig);
try (CloseableHttpResponse response = client.execute(method)) {
in = response.getEntity().getContent();
Document document = new SAXBuilder().build(in);
Element channelElement = document.getRootElement().getChild("channel");
channel.setTitle(StringUtil.removeMarkup(channelElement.getChildTextTrim("title")));
channel.setDescription(StringUtil.removeMarkup(channelElement.getChildTextTrim("description")));
channel.setImageUrl(getChannelImageUrl(channelElement));
channel.setStatus(PodcastStatus.COMPLETED);
channel.setErrorMessage(null);
podcastDao.updateChannel(channel);
downloadImage(channel);
refreshEpisodes(channel, channelElement.getChildren("item"));
}
} catch (Exception x) {
LOG.warn("Failed to get/parse RSS file for Podcast channel " + channel.getUrl(), x);
channel.setStatus(PodcastStatus.ERROR);
channel.setErrorMessage(getErrorMessage(x));
podcastDao.updateChannel(channel);
} finally {
IOUtils.closeQuietly(in);
}
if (downloadEpisodes) {
for (final PodcastEpisode episode : getEpisodes(channel.getId())) {
if (episode.getStatus() == PodcastStatus.NEW && episode.getUrl() != null) {
downloadEpisode(episode);
}
}
}
}
Aggregations