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();
}
};
}
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;
}
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);
}
}
}
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();
}
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;
}
Aggregations