Search in sources :

Example 26 with RequestConfig

use of org.apache.http.client.config.RequestConfig 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 27 with RequestConfig

use of org.apache.http.client.config.RequestConfig 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)

Example 28 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project c4sg-services by Code4SocialGood.

the class SlackUtils method createHttpClient.

public static CloseableHttpClient createHttpClient(int timeout) {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
    return httpClient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 29 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project opennms by OpenNMS.

the class GrafanaDashletConfigurationWindow method getGrafanaDashboards.

private Map<String, String> getGrafanaDashboards() throws GrafanaDashletException {
    /**
         * Loading the required properties...
         */
    final String grafanaApiKey = System.getProperty("org.opennms.grafanaBox.apiKey", "");
    final String grafanaProtocol = System.getProperty("org.opennms.grafanaBox.protocol", "http");
    final String grafanaHostname = System.getProperty("org.opennms.grafanaBox.hostname", "localhost");
    final int grafanaPort = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.port", "3000"));
    final int grafanaConnectionTimeout = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.connectionTimeout", "500"));
    final int grafanaSoTimeout = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.soTimeout", "500"));
    if (!"".equals(grafanaApiKey) && !"".equals(grafanaHostname) && !"".equals(grafanaProtocol) && ("http".equals(grafanaProtocol) || "https".equals(grafanaProtocol))) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(grafanaConnectionTimeout).setSocketTimeout(grafanaSoTimeout).build();
            final URI uri = new URIBuilder().setScheme(grafanaProtocol).setHost(grafanaHostname).setPort(grafanaPort).setPath("/api/search/").build();
            final HttpGet httpGet = new HttpGet(uri);
            httpGet.setConfig(requestConfig);
            /**
                 * Adding the API key...
                 */
            httpGet.setHeader("Authorization", "Bearer " + grafanaApiKey);
            final Map<String, String> resultSet = new TreeMap<>();
            try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    /**
                         * Fill the result set...
                         */
                    final String responseString = IOUtils.toString(httpEntity.getContent(), StandardCharsets.UTF_8.name());
                    if (!Strings.isNullOrEmpty(responseString)) {
                        try {
                            final JSONArray arr = new JSONObject("{dashboards:" + responseString + "}").getJSONArray("dashboards");
                            for (int i = 0; i < arr.length(); i++) {
                                resultSet.put(arr.getJSONObject(i).getString("title"), arr.getJSONObject(i).getString("uri"));
                            }
                        } catch (JSONException e) {
                            throw new GrafanaDashletException(e.getMessage());
                        }
                    }
                }
            }
            return resultSet;
        } catch (Exception e) {
            throw new GrafanaDashletException(e.getMessage());
        }
    } else {
        throw new GrafanaDashletException("Invalid configuration");
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) TreeMap(java.util.TreeMap) URI(java.net.URI) JSONException(org.json.JSONException) URIBuilder(org.apache.http.client.utils.URIBuilder) JSONObject(org.json.JSONObject) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 30 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project Xponents by OpenSextant.

the class SharepointClient method getClient.

/**
     * Sharepoint requires NTLM. This client requires a non-null user/passwd/domain.
     *
     */
@Override
public HttpClient getClient() {
    if (currentConn != null) {
        return currentConn;
    }
    HttpClientBuilder clientHelper = HttpClientBuilder.create();
    if (proxyHost != null) {
        clientHelper.setProxy(proxyHost);
    }
    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    CredentialsProvider creds = new BasicCredentialsProvider();
    creds.setCredentials(AuthScope.ANY, new NTCredentials(user, passwd, server, domain));
    clientHelper.setDefaultCredentialsProvider(creds);
    HttpClient httpClient = clientHelper.setDefaultRequestConfig(globalConfig).build();
    return httpClient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) NTCredentials(org.apache.http.auth.NTCredentials)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)82 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)28 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)27 HttpGet (org.apache.http.client.methods.HttpGet)26 HttpPost (org.apache.http.client.methods.HttpPost)18 Test (org.junit.Test)15 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 StringEntity (org.apache.http.entity.StringEntity)13 IOException (java.io.IOException)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 URI (java.net.URI)9 HttpEntity (org.apache.http.HttpEntity)9 HttpResponse (org.apache.http.HttpResponse)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 InputStream (java.io.InputStream)8 HttpHost (org.apache.http.HttpHost)8 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)8 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 HttpClient (org.apache.http.client.HttpClient)6