use of org.apache.http.impl.client.BasicCredentialsProvider in project motech by motech.
the class SimpleHttpClient method createHttpClient.
private static DefaultHttpClient createHttpClient(String u, String p) {
DefaultHttpClient httpClient = new DefaultHttpClient();
if (!StringUtils.isBlank(u)) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(u, p);
provider.setCredentials(AuthScope.ANY, credentials);
httpClient.setCredentialsProvider(provider);
}
return httpClient;
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project ma-core-public by infiniteautomation.
the class Common method getHttpClient.
public static HttpClient getHttpClient(int timeout) {
// Create global request configuration
RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setExpectContinueEnabled(true).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setSocketTimeout(timeout).setConnectTimeout(timeout).build();
if (SystemSettingsDao.getBooleanValue(SystemSettingsDao.HTTP_CLIENT_USE_PROXY)) {
String proxyHost = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_SERVER);
int proxyPort = SystemSettingsDao.getIntValue(SystemSettingsDao.HTTP_CLIENT_PROXY_PORT);
String username = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_USERNAME, "");
String password = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_PASSWORD, "");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(username, password));
// Create an HttpClient with the given custom dependencies and configuration.
CloseableHttpClient httpclient = HttpClients.custom().setProxy(new HttpHost(proxyHost, proxyPort)).setDefaultRequestConfig(defaultRequestConfig).setDefaultCredentialsProvider(credentialsProvider).build();
return httpclient;
} else {
// Create an HttpClient with the given custom dependencies and configuration.
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
return httpclient;
}
// LEGACY CODE LEFT HERE UNTIL Testing of above code is confirmed as working
// DefaultHttpClient client = new DefaultHttpClient();
// client.getParams().setParameter("http.socket.timeout", timeout);
// client.getParams().setParameter("http.connection.timeout", timeout);
// client.getParams().setParameter("http.connection-manager.timeout", timeout);
// client.getParams().setParameter("http.protocol.head-body-timeout", timeout);
//
// if (SystemSettingsDao.getBooleanValue(SystemSettingsDao.HTTP_CLIENT_USE_PROXY)) {
// String proxyHost = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_SERVER);
// int proxyPort = SystemSettingsDao.getIntValue(SystemSettingsDao.HTTP_CLIENT_PROXY_PORT);
// String username = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_USERNAME, "");
// String password = SystemSettingsDao.getValue(SystemSettingsDao.HTTP_CLIENT_PROXY_PASSWORD, "");
//
// client.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
// new UsernamePasswordCredentials(username, password));
//
// }
//
// return client;
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project directory-fortress-core by apache.
the class RestUtils method getCredentialProvider.
private CredentialsProvider getCredentialProvider(String uid, String password) {
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(httpHost, Integer.valueOf(httpPort)), new UsernamePasswordCredentials(uid == null ? httpUid : uid, password == null ? httpPw : password));
return credentialsProvider;
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project LogHub by fbacchella.
the class AbstractHttpSender method configure.
@Override
public boolean configure(Properties properties) {
endPoints = Helpers.stringsToUrl(destinations, port, protocol, logger);
if (endPoints.length == 0) {
return false;
}
// Create the senders threads and the common queue
batch = new Batch();
threads = new Thread[publisherThreads];
for (int i = 1; i <= publisherThreads; i++) {
String tname = getPublishName() + "Publisher" + i;
threads[i - 1] = new Thread(publisher) {
{
setDaemon(false);
setName(tname);
start();
}
};
}
// The HTTP connection management
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setUserAgent(VersionInfo.getUserAgent("LogHub-HttpClient", "org.apache.http.client", HttpClientBuilder.class));
// Set the Configuration manager
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", new SSLConnectionSocketFactory(properties.ssl)).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.setDefaultMaxPerRoute(2);
cm.setMaxTotal(2 * publisherThreads);
cm.setValidateAfterInactivity(timeout * 1000);
builder.setConnectionManager(cm);
if (properties.ssl != null) {
builder.setSSLContext(properties.ssl);
}
builder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000).setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build());
builder.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true).setSoTimeout(timeout * 1000).build());
builder.setDefaultConnectionConfig(ConnectionConfig.custom().build());
builder.disableCookieManagement();
builder.setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
return false;
}
});
client = builder.build();
if (user != null && password != null) {
credsProvider = new BasicCredentialsProvider();
for (URL i : endPoints) {
credsProvider.setCredentials(new AuthScope(i.getHost(), i.getPort()), new UsernamePasswordCredentials(user, password));
}
}
// Schedule a task to flush every 5 seconds
Runnable flush = () -> {
synchronized (publisher) {
long now = new Date().getTime();
if ((now - lastFlush) > 5000) {
batches.add(batch);
batch = new Batch();
publisher.notify();
}
}
};
properties.registerScheduledTask(getPublishName() + "Flusher", flush, 5000);
return true;
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project structr by structr.
the class HttpHelper method configure.
private static void configure(final HttpRequestBase req, final String username, final String password, final String proxyUrlParameter, final String proxyUsernameParameter, final String proxyPasswordParameter, final String cookieParameter, final Map<String, String> headers, final boolean followRedirects) {
if (StringUtils.isBlank(proxyUrlParameter)) {
proxyUrl = Settings.HttpProxyUrl.getValue();
} else {
proxyUrl = proxyUrlParameter;
}
if (StringUtils.isBlank(proxyUsernameParameter)) {
proxyUsername = Settings.HttpProxyUser.getValue();
} else {
proxyUsername = proxyUsernameParameter;
}
if (StringUtils.isBlank(proxyPasswordParameter)) {
proxyPassword = Settings.HttpProxyPassword.getValue();
} else {
proxyPassword = proxyPasswordParameter;
}
if (!StringUtils.isBlank(cookieParameter)) {
cookie = cookieParameter;
}
// final HttpHost target = HttpHost.create(url.getHost());
HttpHost proxy = null;
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
if (StringUtils.isNoneBlank(username, password)) {
credsProvider.setCredentials(new AuthScope(new HttpHost(req.getURI().getHost())), new UsernamePasswordCredentials(username, password));
}
if (StringUtils.isNotBlank(proxyUrl)) {
proxy = HttpHost.create(proxyUrl);
if (StringUtils.isNoneBlank(proxyUsername, proxyPassword)) {
credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(proxyUsername, proxyPassword));
}
}
client = HttpClients.custom().setDefaultConnectionConfig(ConnectionConfig.DEFAULT).setUserAgent("curl/7.35.0").setDefaultCredentialsProvider(credsProvider).build();
reqConfig = RequestConfig.custom().setProxy(proxy).setRedirectsEnabled(followRedirects).setCookieSpec(CookieSpecs.DEFAULT).build();
req.setConfig(reqConfig);
if (StringUtils.isNotBlank(cookie)) {
req.addHeader("Cookie", cookie);
req.getParams().setParameter("http.protocol.single-cookie-header", true);
}
req.addHeader("Connection", "close");
// add request headers from context
for (final Map.Entry<String, String> header : headers.entrySet()) {
req.addHeader(header.getKey(), header.getValue());
}
}
Aggregations