connManager.setMaxTotal(20);
ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();
connManager.setDefaultConnectionConfig(connectionConfig);
HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(connManager);
Integer timeout = new Long(Utils.parseTimeValue(config, CFG_TIMEOUT, 5, TimeUnit.SECONDS)).intValue();
if (timeout != null) {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
clientBuilder.setDefaultRequestConfig(requestConfig);
}
String remoteUsername = Utils.trimToNull(XContentMapValues.nodeStringValue(config.get(CFG_USERNAME), null));
String remotePassword = XContentMapValues.nodeStringValue(config.get(CFG_PASSWORD), null);
if (remoteUsername != null) {
if (remotePassword == null && pwdLoader != null)
remotePassword = pwdLoader.loadPassword(remoteUsername);
if (remotePassword != null) {
try {
URL urlParsed = new URL(url);
String host = urlParsed.getHost();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(host, AuthScope.ANY_PORT), new UsernamePasswordCredentials(
remoteUsername, remotePassword));
clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
isAuthConfigured = true;
} catch (MalformedURLException e) {
// this should never happen due validation before
}
} else {
logger.warn("Password not found so authentication is not used!");
remoteUsername = null;
}
} else {
remoteUsername = null;
}
httpclient = clientBuilder.build();
return remoteUsername;
}