public HttpUtility() { this(null, false); }
public HttpUtility(UserPreferences userPrefs, boolean useAuth) {
HttpClientBuilder clientBuilder = HttpClients.custom();
if (useAuth) {
authHeader = getAuthHeader(userPrefs.getUsername(), userPrefs.getPassword());
appToken = userPrefs.getAPIKey();
}
authRequired = useAuth;
if(userPrefs != null) {
String proxyHost = userPrefs.getProxyHost();
String proxyPort = userPrefs.getProxyPort();
if (canUse(proxyHost) && canUse(proxyPort)) {
HttpHost proxy = new HttpHost(proxyHost, Integer.valueOf(proxyPort));
proxyConfig = RequestConfig.custom().setProxy(proxy).build();
if (canUse(userPrefs.getProxyUsername()) && canUse(userPrefs.getProxyPassword())) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyHost, Integer.valueOf(proxyPort)),
new UsernamePasswordCredentials(userPrefs.getProxyUsername(), userPrefs.getProxyPassword()));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
}
}
}
RequestConfig requestConfig = RequestConfig.custom().
setConnectTimeout(15000). // 15s
setSocketTimeout(60000). // 1m
build();
clientBuilder.setRetryHandler(datasyncDefaultHandler);
clientBuilder.setKeepAliveStrategy(datasyncDefaultKeepAliveStrategy);
clientBuilder.setDefaultRequestConfig(requestConfig);
httpClient = clientBuilder.build();
}