use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project JFramework by gugumall.
the class JHttp method createClient.
/**
* @param timeout in milliseconds
* @return
*/
public HttpClient createClient(int timeout, String host, int port, String scheme, String username, String password) {
HttpHost proxy = new HttpHost(host, port, scheme == null ? "http" : scheme);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
CloseableHttpClient client = HttpClients.custom().setConnectionManager(poolingmgr).setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
int retries = default_retries;
if (JUtilMath.isInt(AppConfig.getPara("HTTP", "retries"))) {
retries = Integer.parseInt(AppConfig.getPara("HTTP", "retries"));
}
RequestConfig requestConfig = RequestConfig.custom().setMaxRedirects(retries).setSocketTimeout(timeout).setConnectTimeout(timeout).build();
configOfClients.put(client.toString(), requestConfig);
return client;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project commons by terran4j.
the class ApacheHttpClientBuilder method build.
public static final HttpClient build(int timeout, String charset) {
SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build();
List<Header> defaultHeaders = Lists.newArrayList();
defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, charset));
return HttpClients.custom().setDefaultHeaders(defaultHeaders).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).build();
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project tutorials by eugenp.
the class HttpClientTimeoutLiveTest method givenUsingNewApi_whenSettingTimeoutViaHighLevelApi_thenCorrect.
@Test
public final void givenUsingNewApi_whenSettingTimeoutViaHighLevelApi_thenCorrect() throws IOException {
final int timeout = 5;
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
final HttpGet request = new HttpGet("http://www.github.com");
response = client.execute(request);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project tutorials by eugenp.
the class HttpClientTimeoutLiveTest method givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrect.
// tests
@Test
public final void givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrect() throws IOException {
final int timeout = 2;
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
final HttpGet request = new HttpGet("http://www.github.com");
// httpParams.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000)); // https://issues.apache.org/jira/browse/HTTPCLIENT-1418
response = client.execute(request);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project acs-aem-commons by Adobe-Consulting-Services.
the class HttpClientFactoryImpl method activate.
@Activate
protected void activate(Map<String, Object> config) throws Exception {
boolean useSSL = PropertiesUtil.toBoolean(config.get(PROP_USE_SSL), DEFAULT_USE_SSL);
String scheme = useSSL ? "https" : "http";
String hostname = PropertiesUtil.toString(config.get(PROP_HOST_DOMAIN), null);
int port = PropertiesUtil.toInteger(config.get(PROP_GATEWAY_PORT), 0);
if (hostname == null || port == 0) {
throw new IllegalArgumentException("Configuration not valid. Both host and port must be provided.");
}
baseUrl = String.format("%s://%s:%s", scheme, hostname, port);
int connectTimeout = PropertiesUtil.toInteger(config.get(PROP_CONNECT_TIMEOUT), DEFAULT_CONNECT_TIMEOUT);
int soTimeout = PropertiesUtil.toInteger(config.get(PROP_SO_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
HttpClientBuilder builder = httpClientBuilderFactory.newBuilder();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout).setSocketTimeout(soTimeout).build();
builder.setDefaultRequestConfig(requestConfig);
boolean disableCertCheck = PropertiesUtil.toBoolean(config.get(PROP_DISABLE_CERT_CHECK), DEFAULT_DISABLE_CERT_CHECK);
if (useSSL && disableCertCheck) {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
builder.setHostnameVerifier(new AllowAllHostnameVerifier()).setSslcontext(sslContext);
}
httpClient = builder.build();
executor = Executor.newInstance(httpClient);
String username = PropertiesUtil.toString(config.get(PROP_USERNAME), null);
String password = PropertiesUtil.toString(config.get(PROP_PASSWORD), null);
if (username != null && password != null) {
HttpHost httpHost = new HttpHost(hostname, port, useSSL ? "https" : "http");
executor.auth(httpHost, username, password).authPreemptive(httpHost);
}
}
Aggregations