use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project spring-cloud-netflix by spring-cloud.
the class GZIPCompression method timeoutPropertiesAreApplied.
@Test
public void timeoutPropertiesAreApplied() {
addEnvironment(this.context, "zuul.host.socket-timeout-millis=11000", "zuul.host.connect-timeout-millis=2100");
setupContext();
CloseableHttpClient httpClient = getFilter().newClient();
Assertions.assertThat(httpClient).isInstanceOf(Configurable.class);
RequestConfig config = ((Configurable) httpClient).getConfig();
assertEquals(11000, config.getSocketTimeout());
assertEquals(2100, config.getConnectTimeout());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project neo-java by coranos.
the class GeoIPUtil method getLocation.
/**
* returns the location of a host.
*
* @param canonicalHostName
* the host name to use.
* @return the location, as JSON.
*/
public static JSONObject getLocation(final String canonicalHostName) {
try {
final String urlStr = "https://freegeoip.net/json/" + canonicalHostName;
final HttpGet get = new HttpGet(urlStr);
final RequestConfig requestConfig = RequestConfig.custom().build();
get.setConfig(requestConfig);
final CloseableHttpClient client = HttpClients.createDefault();
final CloseableHttpResponse response = client.execute(get);
final HttpEntity entity = response.getEntity();
final String str = EntityUtils.toString(entity);
final JSONObject outputJson = new JSONObject(str);
LOG.info("outputJson:{}", outputJson);
return outputJson;
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project ant-ivy by apache.
the class HttpClientHandler method upload.
@Override
public void upload(final File src, final URL dest, final CopyProgressListener listener, final TimeoutConstraint timeoutConstraint) throws IOException {
final int connectionTimeout = (timeoutConstraint == null || timeoutConstraint.getConnectionTimeout() < 0) ? 0 : timeoutConstraint.getConnectionTimeout();
final int readTimeout = (timeoutConstraint == null || timeoutConstraint.getReadTimeout() < 0) ? 0 : timeoutConstraint.getReadTimeout();
final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeout).setConnectTimeout(connectionTimeout).setAuthenticationEnabled(hasCredentialsConfigured(dest)).setTargetPreferredAuthSchemes(getAuthSchemePreferredOrder()).setProxyPreferredAuthSchemes(getAuthSchemePreferredOrder()).setExpectContinueEnabled(true).build();
final HttpPut put = new HttpPut(normalizeToString(dest));
put.setConfig(requestConfig);
put.setEntity(new FileEntity(src));
try (final CloseableHttpResponse response = this.httpClient.execute(put)) {
validatePutStatusCode(dest, response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project ovirt-engine by oVirt.
the class HttpClientBuilder method build.
public CloseableHttpClient build() throws IOException, GeneralSecurityException {
// Prepare the default configuration for all requests:
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout != null ? connectTimeout : 0).setSocketTimeout(readTimeout != null ? readTimeout : 0).build();
// Configure the trust manager:
TrustManager[] trustManager = null;
if (verifyChain) {
if (trustStore != null) {
try (InputStream is = new FileInputStream(trustStore)) {
KeyStore ks = KeyStore.getInstance(trustStoreType);
ks.load(is, StringUtils.isEmpty(trustStorePassword) ? null : trustStorePassword.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManagerAlgorithm);
tmf.init(ks);
trustManager = tmf.getTrustManagers();
}
}
} else {
trustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
}
// Create the SSL context:
SSLContext sslContext = SSLContext.getInstance(tlsProtocol);
sslContext.init(null, trustManager, null);
// Create the SSL host name verifier:
HostnameVerifier sslHostnameVerifier = null;
if (!verifyHost) {
sslHostnameVerifier = (hostname, session) -> true;
}
// Create the socket factory for HTTP:
ConnectionSocketFactory httpSocketFactory = new PlainConnectionSocketFactory();
// Create the socket factory for HTTPS:
ConnectionSocketFactory httpsSocketFactory = new SSLConnectionSocketFactory(sslContext, sslHostnameVerifier);
// Create the socket factory registry:
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", httpSocketFactory).register("https", httpsSocketFactory).build();
// Create the connection manager:
HttpClientConnectionManager connectionManager;
if (poolSize != null) {
PoolingHttpClientConnectionManager poolManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
poolManager.setDefaultMaxPerRoute(poolSize);
poolManager.setMaxTotal(poolSize);
poolManager.setValidateAfterInactivity(validateAfterInactivity == null ? 100 : validateAfterInactivity);
connectionManager = poolManager;
} else {
connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
}
// Create the client:
return org.apache.http.impl.client.HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setSSLHostnameVerifier(sslHostnameVerifier).setConnectionManager(connectionManager).setRetryHandler(new StandardHttpRequestRetryHandler(retryCount == null ? 1 : retryCount, true)).build();
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project ngtesting-platform by aaronchen2k.
the class HttpClientServiceImpl method post.
@Override
public String post(String url, String json) {
String resultJson = "";
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(url);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
httppost.setConfig(requestConfig);
CloseableHttpResponse response = null;
try {
StringEntity entity = new StringEntity(json, Encoding);
entity.setContentEncoding(Encoding);
entity.setContentType(ContentType);
httppost.setEntity(entity);
response = httpclient.execute(httppost);
HttpEntity result = response.getEntity();
resultJson = EntityUtils.toString(result, Encoding);
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
// 关闭连接,释放资源
if (response != null) {
try {
response.close();
} catch (IOException e) {
}
}
try {
httpclient.close();
} catch (IOException e) {
}
}
return resultJson;
}
Aggregations