use of org.apache.http.client.CredentialsProvider in project ddf by codice.
the class TestSecurity method createBasicAuth.
private CredentialsProvider createBasicAuth(String username, String password) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
return credentialsProvider;
}
use of org.apache.http.client.CredentialsProvider in project sling by apache.
the class SmokeIT method newClient.
private CloseableHttpClient newClient() {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
credsProvider.setCredentials(new AuthScope("localhost", LAUNCHPAD_PORT), creds);
return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
}
use of org.apache.http.client.CredentialsProvider in project cas by apereo.
the class HttpUtils method prepareCredentialsIfNeeded.
/**
* Prepare credentials if needed.
*
* @param builder the builder
* @param basicAuthUsername username for basic auth
* @param basicAuthPassword password for basic auth
* @return the http client builder
*/
private static HttpClientBuilder prepareCredentialsIfNeeded(final HttpClientBuilder builder, final String basicAuthUsername, final String basicAuthPassword) {
if (StringUtils.isNotBlank(basicAuthUsername) && StringUtils.isNotBlank(basicAuthPassword)) {
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(basicAuthUsername, basicAuthPassword);
provider.setCredentials(AuthScope.ANY, credentials);
return builder.setDefaultCredentialsProvider(provider);
}
return builder;
}
use of org.apache.http.client.CredentialsProvider in project syncany by syncany.
the class ApplicationLink method createHttpClient.
private CloseableHttpClient createHttpClient() {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).setRedirectsEnabled(false);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// do we use a https proxy?
String proxyHost = System.getProperty("https.proxyHost");
String proxyPortStr = System.getProperty("https.proxyPort");
String proxyUser = System.getProperty("https.proxyUser");
String proxyPassword = System.getProperty("https.proxyPassword");
if (proxyHost != null && proxyPortStr != null) {
try {
Integer proxyPort = Integer.parseInt(proxyPortStr);
requestConfigBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
logger.log(Level.INFO, "Using proxy: " + proxyHost + ":" + proxyPort);
if (proxyUser != null && proxyPassword != null) {
logger.log(Level.INFO, "Proxy required credentials; using '" + proxyUser + "' (username) and *** (hidden password)");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
}
} catch (NumberFormatException e) {
logger.log(Level.WARNING, "Invalid proxy settings found. Not using proxy.", e);
}
}
httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
return httpClientBuilder.build();
}
use of org.apache.http.client.CredentialsProvider in project SmartApplianceEnabler by camueller.
the class HttpTransactionExecutor method sendHttpRequest.
/**
* Send a HTTP request whose response has to be closed by the caller!
* @param url
* @return
*/
protected CloseableHttpResponse sendHttpRequest(String url, String data, ContentType contentType, String username, String password) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if (username != null && password != null) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClientBuilder.setDefaultCredentialsProvider(provider);
}
CloseableHttpClient client = httpClientBuilder.build();
logger.debug("{}: Sending HTTP request", applianceId);
logger.debug("{}: url={}", applianceId, url);
logger.debug("{}: data={}", applianceId, data);
logger.debug("{}: contentType={}", applianceId, contentType);
logger.debug("{}: username={}", applianceId, username);
logger.debug("{}: password={}", applianceId, password);
try {
HttpRequestBase request = null;
if (data != null) {
request = new HttpPost(url);
((HttpPost) request).setEntity(new StringEntity(data, contentType));
} else {
request = new HttpGet(url);
}
CloseableHttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
logger.debug("{}: Response code is {}", applianceId, responseCode);
return response;
} catch (IOException e) {
logger.error("{}: Error executing HTTP request.", applianceId, e);
return null;
}
}
Aggregations