Search in sources :

Example 71 with CloseableHttpClient

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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 72 with CloseableHttpClient

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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 73 with CloseableHttpClient

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);
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Example 74 with CloseableHttpClient

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);
    }
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpGet(org.apache.http.client.methods.HttpGet) FileOutputStream(java.io.FileOutputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MediaFile(org.libresonic.player.domain.MediaFile) File(java.io.File)

Example 75 with CloseableHttpClient

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);
            }
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) SAXBuilder(org.jdom.input.SAXBuilder) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.jdom.Element) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.jdom.Document) PodcastEpisode(org.libresonic.player.domain.PodcastEpisode)

Aggregations

CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)430 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)238 HttpGet (org.apache.http.client.methods.HttpGet)212 Test (org.junit.Test)206 HttpResponse (org.apache.http.HttpResponse)108 HttpEntity (org.apache.http.HttpEntity)92 IOException (java.io.IOException)90 HttpPost (org.apache.http.client.methods.HttpPost)85 StringEntity (org.apache.http.entity.StringEntity)67 InputStream (java.io.InputStream)57 StatusLine (org.apache.http.StatusLine)41 HttpHost (org.apache.http.HttpHost)36 URI (java.net.URI)35 RequestConfig (org.apache.http.client.config.RequestConfig)32 Header (org.apache.http.Header)24 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)24 File (java.io.File)22 HttpPut (org.apache.http.client.methods.HttpPut)22 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19