use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project wildfly-swarm by wildfly-swarm.
the class SimpleHttp method getUrlContents.
protected Response getUrlContents(String theUrl, boolean useAuth, boolean followRedirects) {
StringBuilder content = new StringBuilder();
int code;
try {
CredentialsProvider provider = new BasicCredentialsProvider();
HttpClientBuilder builder = HttpClientBuilder.create();
if (!followRedirects) {
builder.disableRedirectHandling();
}
if (useAuth) {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "password");
provider.setCredentials(AuthScope.ANY, credentials);
builder.setDefaultCredentialsProvider(provider);
}
HttpClient client = builder.build();
HttpResponse response = client.execute(new HttpGet(theUrl));
code = response.getStatusLine().getStatusCode();
if (null == response.getEntity()) {
throw new RuntimeException("No response content present");
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
return new Response(code, content.toString());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project epp.mpc by eclipse.
the class CacheCredentialsAuthenticationStrategy method cacheCredentials.
private void cacheCredentials(HttpHost authhost, AuthScheme authScheme, HttpContext context) {
Credentials credentials = getCredentials(context);
if (credentials != null) {
CredentialsProvider credentialsCache = getCredentialsCache(context);
if (credentialsCache != null) {
AuthScope scope = createAuthScope(authhost, authScheme);
credentialsCache.setCredentials(scope, credentials);
}
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project epp.mpc by eclipse.
the class HttpClientFactory method createCredentialsProvider.
private static CredentialsProvider createCredentialsProvider(HttpClientBuilder clientBuilder) {
// TODO we should handle configured proxy passwords and dialogs to prompt for unknown credentials on our own...
CredentialsProvider credentialsProvider = new SystemCredentialsProvider();
credentialsProvider = customizeCredentialsProvider(credentialsProvider);
final CacheCredentialsProvider cacheProvider = new CacheCredentialsProvider();
credentialsProvider = new ChainedCredentialsProvider(cacheProvider, credentialsProvider);
credentialsProvider = new SynchronizedCredentialsProvider(credentialsProvider);
clientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
context.setAttribute(CacheCredentialsAuthenticationStrategy.CREDENTIALS_CACHE_ATTRIBUTE, cacheProvider);
}
});
return credentialsProvider;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project stdlib by petergeneric.
the class PreemptiveBearerAuthInterceptor method process.
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
final AuthState state = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
// Try to initialise an auth scheme if one is not already set
if (state.getAuthScheme() == null) {
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
HttpHost host = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
final Credentials credentials = credentialsProvider.getCredentials(new AuthScope(host));
if (credentials == null)
throw new HttpException("No credentials for preemptive authentication against: " + host);
else
state.update(new BearerAuthSchemeProvider().create(context), credentials);
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project stdlib by petergeneric.
the class ResteasyClientFactoryImpl method createHttpClientCustomiser.
/**
* N.B. This method signature may change in the future to add new parameters
*
* @param fastFail
* @param authScope
* @param credentials
* @param preemptiveAuth
* @param storeCookies
* @param customiser
*
* @return
*/
public Consumer<HttpClientBuilder> createHttpClientCustomiser(final boolean fastFail, final AuthScope authScope, final Credentials credentials, final boolean preemptiveAuth, final boolean storeCookies, Consumer<HttpClientBuilder> customiser) {
// Customise timeouts if fast fail mode is enabled
if (fastFail) {
customiser = concat(customiser, b -> {
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout((int) fastFailConnectionTimeout.getMilliseconds()).setSocketTimeout((int) fastFailSocketTimeout.getMilliseconds());
b.setDefaultRequestConfig(requestBuilder.build());
});
}
// If credentials were supplied then we should set them up
if (credentials != null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if (authScope != null)
credentialsProvider.setCredentials(authScope, credentials);
else
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
// Set up bearer auth scheme provider if we're using bearer credentials
if (credentials instanceof BearerCredentials) {
customiser = concat(customiser, b -> {
Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register("Bearer", new BearerAuthSchemeProvider()).build();
b.setDefaultAuthSchemeRegistry(authSchemeRegistry);
});
}
// Set up the credentials customisation
customiser = concat(customiser, b -> b.setDefaultCredentialsProvider(credentialsProvider));
if (preemptiveAuth && credentials instanceof BearerCredentials)
customiser = concat(customiser, b -> b.addInterceptorFirst(new PreemptiveBearerAuthInterceptor()));
else
customiser = concat(customiser, b -> b.addInterceptorLast(new PreemptiveBasicAuthInterceptor()));
}
// If cookies are enabled then set up a cookie store
if (storeCookies)
customiser = concat(customiser, b -> b.setDefaultCookieStore(new BasicCookieStore()));
return customiser;
}
Aggregations