Search in sources :

Example 76 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project intellij-community by JetBrains.

the class EduAdaptiveStepicConnector method setTimeout.

private static void setTimeout(HttpPost request) {
    final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).setSocketTimeout(CONNECTION_TIMEOUT).build();
    request.setConfig(requestConfig);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig)

Example 77 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.

the class PodcastService method doDownloadEpisode.

private void doDownloadEpisode(PodcastEpisode episode) {
    InputStream in = null;
    OutputStream out = null;
    if (isEpisodeDeleted(episode)) {
        LOG.info("Podcast " + episode.getUrl() + " was deleted. Aborting download.");
        return;
    }
    LOG.info("Starting to download Podcast from " + episode.getUrl());
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        PodcastChannel channel = getChannel(episode.getChannelId());
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(// 2 minutes
        2 * 60 * 1000).setSocketTimeout(// 10 minutes
        10 * 60 * 1000).build();
        HttpGet method = new HttpGet(episode.getUrl());
        method.setConfig(requestConfig);
        try (CloseableHttpResponse response = client.execute(method)) {
            in = response.getEntity().getContent();
            File file = getFile(channel, episode);
            out = new FileOutputStream(file);
            episode.setStatus(PodcastStatus.DOWNLOADING);
            episode.setBytesDownloaded(0L);
            episode.setErrorMessage(null);
            episode.setPath(file.getPath());
            podcastDao.updateEpisode(episode);
            byte[] buffer = new byte[4096];
            long bytesDownloaded = 0;
            int n;
            long nextLogCount = 30000L;
            while ((n = in.read(buffer)) != -1) {
                out.write(buffer, 0, n);
                bytesDownloaded += n;
                if (bytesDownloaded > nextLogCount) {
                    episode.setBytesDownloaded(bytesDownloaded);
                    nextLogCount += 30000L;
                    // Abort download if episode was deleted by user.
                    if (isEpisodeDeleted(episode)) {
                        break;
                    }
                    podcastDao.updateEpisode(episode);
                }
            }
            if (isEpisodeDeleted(episode)) {
                LOG.info("Podcast " + episode.getUrl() + " was deleted. Aborting download.");
                IOUtils.closeQuietly(out);
                file.delete();
            } else {
                addMediaFileIdToEpisodes(Arrays.asList(episode));
                episode.setBytesDownloaded(bytesDownloaded);
                podcastDao.updateEpisode(episode);
                LOG.info("Downloaded " + bytesDownloaded + " bytes from Podcast " + episode.getUrl());
                IOUtils.closeQuietly(out);
                updateTags(file, episode);
                episode.setStatus(PodcastStatus.COMPLETED);
                podcastDao.updateEpisode(episode);
                deleteObsoleteEpisodes(channel);
            }
        }
    } catch (Exception x) {
        LOG.warn("Failed to download Podcast from " + episode.getUrl(), x);
        episode.setStatus(PodcastStatus.ERROR);
        episode.setErrorMessage(getErrorMessage(x));
        podcastDao.updateEpisode(episode);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpGet(org.apache.http.client.methods.HttpGet) PodcastChannel(org.libresonic.player.domain.PodcastChannel) FileOutputStream(java.io.FileOutputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MediaFile(org.libresonic.player.domain.MediaFile) File(java.io.File)

Example 78 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project ats-framework by Axway.

the class HttpClient method createNewHttpClientWithCredentials.

private void createNewHttpClientWithCredentials(String hostname, String userName, String password) throws FileTransferException {
    log.debug("Creating new HTTP client to host " + hostname);
    httpBuilder = HttpClientBuilder.create();
    RequestConfig.Builder requestConfig = RequestConfig.custom();
    requestConfig.setConnectTimeout(this.timeout);
    httpBuilder.setDefaultRequestConfig(requestConfig.build());
    SocketConfig.Builder socket = SocketConfig.custom();
    socket.setRcvBufSize(this.socketBufferSize);
    socket.setSndBufSize(this.socketBufferSize);
    PoolingHttpClientConnectionManager conManager = new PoolingHttpClientConnectionManager();
    // set stale connection check
    conManager.setValidateAfterInactivity(-1);
    httpBuilder.setConnectionManager(conManager);
    Object socketTimeout = customProperties.get(HTTP_HTTPS_SOCKET_READ_TIMEOUT);
    if (socketTimeout != null) {
        // TODO this could be set either with the request config
        socket.setSoTimeout(Integer.parseInt(socketTimeout.toString()));
    }
    httpBuilder.setDefaultSocketConfig(socket.build());
    if (httpClient != null) {
        // cleanup previous client
        disconnect();
    }
    if (userName != null) {
        BasicCredentialsProvider credentials = new BasicCredentialsProvider();
        credentials.setCredentials(new AuthScope(hostname, this.port), new UsernamePasswordCredentials(userName, password));
        httpBuilder.setDefaultCredentialsProvider(credentials);
    }
    httpClient = httpBuilder.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SocketConfig(org.apache.http.config.SocketConfig) AuthScope(org.apache.http.auth.AuthScope) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 79 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project mastering-java by Kingminghuang.

the class CrawlerApp method main.

public static void main(String[] args) throws IOException {
    RequestConfig requestConfig = RequestConfig.custom().setProxy(new HttpHost(HttpClientCrawler.DEFAULT_PROXY, HttpClientCrawler.DEFAULT_PORT, HttpClientCrawler.DEFAULT_SCHEMA)).build();
    HttpClientCrawler.config(requestConfig);
    String html = EntityUtils.toString(HttpClientCrawler.download("https://www.zhihu.com/explore/recommendations"), "utf-8");
    Document document = Jsoup.parse(html);
    System.out.println(document.title());
    Elements elements = document.select("a.question_link");
    for (Element element : elements) {
        System.out.println(element.attr("href") + " -> " + element.text());
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 80 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.

the class CoverArtService method saveCoverArt.

private void saveCoverArt(String path, String url) throws Exception {
    InputStream input = null;
    OutputStream output = null;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(// 20 seconds
        20 * 1000).setSocketTimeout(// 20 seconds
        20 * 1000).build();
        HttpGet method = new HttpGet(url);
        method.setConfig(requestConfig);
        try (CloseableHttpResponse response = client.execute(method)) {
            input = response.getEntity().getContent();
            // Attempt to resolve proper suffix.
            String suffix = "jpg";
            if (url.toLowerCase().endsWith(".gif")) {
                suffix = "gif";
            } else if (url.toLowerCase().endsWith(".png")) {
                suffix = "png";
            }
            // Check permissions.
            File newCoverFile = new File(path, "cover." + suffix);
            if (!securityService.isWriteAllowed(newCoverFile)) {
                throw new Exception("Permission denied: " + StringUtil.toHtml(newCoverFile.getPath()));
            }
            // If file exists, create a backup.
            backup(newCoverFile, new File(path, "cover." + suffix + ".backup"));
            // Write file.
            output = new FileOutputStream(newCoverFile);
            IOUtils.copy(input, output);
            MediaFile dir = mediaFileService.getMediaFile(path);
            // Refresh database.
            mediaFileService.refreshMediaFile(dir);
            dir = mediaFileService.getMediaFile(dir.getId());
            // Rename existing cover files if new cover file is not the preferred.
            try {
                while (true) {
                    File coverFile = mediaFileService.getCoverArt(dir);
                    if (coverFile != null && !isMediaFile(coverFile) && !newCoverFile.equals(coverFile)) {
                        if (!coverFile.renameTo(new File(coverFile.getCanonicalPath() + ".old"))) {
                            LOG.warn("Unable to rename old image file " + coverFile);
                            break;
                        }
                        LOG.info("Renamed old image file " + coverFile);
                        // Must refresh again.
                        mediaFileService.refreshMediaFile(dir);
                        dir = mediaFileService.getMediaFile(dir.getId());
                    } else {
                        break;
                    }
                }
            } catch (Exception x) {
                LOG.warn("Failed to rename existing cover file.", x);
            }
        }
    } finally {
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);
    }
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) 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)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)91 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)33 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)31 HttpGet (org.apache.http.client.methods.HttpGet)29 HttpPost (org.apache.http.client.methods.HttpPost)19 Test (org.junit.Test)16 IOException (java.io.IOException)14 StringEntity (org.apache.http.entity.StringEntity)14 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 HttpResponse (org.apache.http.HttpResponse)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 HttpEntity (org.apache.http.HttpEntity)11 URI (java.net.URI)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 InputStream (java.io.InputStream)9 HttpHost (org.apache.http.HttpHost)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 Header (org.apache.http.Header)6