Search in sources :

Example 76 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project alluxio by Alluxio.

the class HttpUtils method getInputStream.

/**
 * Uses the get method to send a url with arguments by http, this method can call RESTful Api.
 *
 * @param url the http url
 * @param timeout milliseconds to wait for the server to respond before giving up
 * @return the response body stream if response status is OK or CREATED
 */
public static InputStream getInputStream(String url, Integer timeout) throws IOException {
    Preconditions.checkNotNull(url, "url");
    Preconditions.checkNotNull(timeout, "timeout");
    RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build();
    CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
    HttpResponse response = client.execute(RequestBuilder.get(url).build());
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
        throw new IOException("Failed to perform GET request. Status code: " + statusCode);
    }
    InputStream inputStream = response.getEntity().getContent();
    return new BufferedInputStream(inputStream) {

        @Override
        public void close() throws IOException {
            client.close();
        }
    };
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Example 77 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project elasticsearch-analysis-ik by medcl.

the class Dictionary method getRemoteWordsUnprivileged.

/**
 * 从远程服务器上下载自定义词条
 */
private static List<String> getRemoteWordsUnprivileged(String location) {
    List<String> buffer = new ArrayList<String>();
    RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000).setSocketTimeout(60 * 1000).build();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response;
    BufferedReader in;
    HttpGet get = new HttpGet(location);
    get.setConfig(rc);
    try {
        response = httpclient.execute(get);
        if (response.getStatusLine().getStatusCode() == 200) {
            String charset = "UTF-8";
            // 获取编码,默认为utf-8
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                Header contentType = entity.getContentType();
                if (contentType != null && contentType.getValue() != null) {
                    String typeValue = contentType.getValue();
                    if (typeValue != null && typeValue.contains("charset=")) {
                        charset = typeValue.substring(typeValue.lastIndexOf("=") + 1);
                    }
                }
                if (entity.getContentLength() > 0 || entity.isChunked()) {
                    in = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
                    String line;
                    while ((line = in.readLine()) != null) {
                        buffer.add(line);
                    }
                    in.close();
                    response.close();
                    return buffer;
                }
            }
        }
        response.close();
    } catch (IllegalStateException | IOException e) {
        logger.error("getRemoteWords {} error", e, location);
    }
    return buffer;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader)

Example 78 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project elasticsearch-analysis-ik by medcl.

the class Monitor method runUnprivileged.

/**
 * 监控流程:
 *  ①向词库服务器发送Head请求
 *  ②从响应中获取Last-Modify、ETags字段值,判断是否变化
 *  ③如果未变化,休眠1min,返回第①步
 * 	④如果有变化,重新加载词典
 *  ⑤休眠1min,返回第①步
 */
public void runUnprivileged() {
    // 超时设置
    RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000).setSocketTimeout(15 * 1000).build();
    HttpHead head = new HttpHead(location);
    head.setConfig(rc);
    // 设置请求头
    if (last_modified != null) {
        head.setHeader("If-Modified-Since", last_modified);
    }
    if (eTags != null) {
        head.setHeader("If-None-Match", eTags);
    }
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(head);
        // 返回200 才做操作
        if (response.getStatusLine().getStatusCode() == 200) {
            if (((response.getLastHeader("Last-Modified") != null) && !response.getLastHeader("Last-Modified").getValue().equalsIgnoreCase(last_modified)) || ((response.getLastHeader("ETag") != null) && !response.getLastHeader("ETag").getValue().equalsIgnoreCase(eTags))) {
                // 远程词库有更新,需要重新加载词典,并修改last_modified,eTags
                Dictionary.getSingleton().reLoadMainDict();
                last_modified = response.getLastHeader("Last-Modified") == null ? null : response.getLastHeader("Last-Modified").getValue();
                eTags = response.getLastHeader("ETag") == null ? null : response.getLastHeader("ETag").getValue();
            }
        } else if (response.getStatusLine().getStatusCode() == 304) {
        // 没有修改,不做操作
        // noop
        } else {
            logger.info("remote_ext_dict {} return bad code {}", location, response.getStatusLine().getStatusCode());
        }
    } catch (Exception e) {
        logger.error("remote_ext_dict {} error!", e, location);
    } finally {
        try {
            if (response != null) {
                response.close();
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead) IOException(java.io.IOException)

Example 79 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project hbase by apache.

the class Client method initialize.

private void initialize(Cluster cluster, Configuration conf, boolean sslEnabled, Optional<KeyStore> trustStore) {
    this.cluster = cluster;
    this.conf = conf;
    this.sslEnabled = sslEnabled;
    extraHeaders = new ConcurrentHashMap<>();
    String clspath = System.getProperty("java.class.path");
    LOG.debug("classpath " + clspath);
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    int connTimeout = this.conf.getInt(Constants.REST_CLIENT_CONN_TIMEOUT, Constants.DEFAULT_REST_CLIENT_CONN_TIMEOUT);
    int socketTimeout = this.conf.getInt(Constants.REST_CLIENT_SOCKET_TIMEOUT, Constants.DEFAULT_REST_CLIENT_SOCKET_TIMEOUT);
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connTimeout).setSocketTimeout(socketTimeout).build();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);
    // Since HBASE-25267 we don't use the deprecated DefaultHttpClient anymore.
    // The new http client would decompress the gzip content automatically.
    // In order to keep the original behaviour of this public class, we disable
    // automatic content compression.
    httpClientBuilder.disableContentCompression();
    if (sslEnabled && trustStore.isPresent()) {
        try {
            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore.get(), null).build();
            httpClientBuilder.setSSLContext(sslcontext);
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            throw new ClientTrustStoreInitializationException("Error while processing truststore", e);
        }
    }
    this.httpClient = httpClientBuilder.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException)

Example 80 with RequestConfig

use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project indy by Commonjava.

the class ReplicationController method newGet.

private HttpGet newGet(final String url, final ReplicationDTO dto) {
    final HttpGet get = new HttpGet(url);
    final int proxyPort = dto.getProxyPort();
    HttpHost proxy;
    if (proxyPort < 1) {
        proxy = new HttpHost(dto.getProxyHost(), -1, "http");
    } else {
        proxy = new HttpHost(dto.getProxyHost(), dto.getProxyPort(), "http");
    }
    final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
    get.setConfig(config);
    return get;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)344 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)146 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)97 HttpGet (org.apache.http.client.methods.HttpGet)94 IOException (java.io.IOException)78 HttpEntity (org.apache.http.HttpEntity)67 HttpPost (org.apache.http.client.methods.HttpPost)65 HttpResponse (org.apache.http.HttpResponse)60 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)55 URI (java.net.URI)46 StringEntity (org.apache.http.entity.StringEntity)43 Map (java.util.Map)41 Test (org.junit.Test)41 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)33 HttpHost (org.apache.http.HttpHost)32 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)32 HttpClient (org.apache.http.client.HttpClient)31 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)27 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)24 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)24