use of org.apache.http.auth.UsernamePasswordCredentials 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.auth.UsernamePasswordCredentials in project citrus-samples by christophd.
the class EndpointConfig method basicAuthRequestFactoryBean.
@Bean
public BasicAuthClientHttpRequestFactory basicAuthRequestFactoryBean() {
BasicAuthClientHttpRequestFactory requestFactory = new BasicAuthClientHttpRequestFactory();
AuthScope authScope = new AuthScope("localhost", port, "", "basic");
requestFactory.setAuthScope(authScope);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("citrus", "secr3t");
requestFactory.setCredentials(credentials);
return requestFactory;
}
use of org.apache.http.auth.UsernamePasswordCredentials 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());
}
}
use of org.apache.http.auth.UsernamePasswordCredentials 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.apache.http.auth.UsernamePasswordCredentials in project build-info by JFrogDev.
the class PreemptiveHttpClient method createHttpClientBuilder.
private HttpClientBuilder createHttpClientBuilder(String userName, String password, int timeout, int connectionRetries) {
this.connectionRetries = connectionRetries;
int timeoutMilliSeconds = timeout * 1000;
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeoutMilliSeconds).setConnectTimeout(timeoutMilliSeconds).setCircularRedirectsAllowed(true).build();
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig);
if (StringUtils.isEmpty(userName)) {
userName = "anonymous";
password = "";
}
basicCredentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(userName, password));
localContext.setCredentialsProvider(basicCredentialsProvider);
// Add as the first request interceptor
builder.addInterceptorFirst(new PreemptiveAuth());
int retryCount = connectionRetries < 0 ? ArtifactoryHttpClient.DEFAULT_CONNECTION_RETRY : connectionRetries;
builder.setRetryHandler(new PreemptiveRetryHandler(retryCount));
builder.setServiceUnavailableRetryStrategy(new PreemptiveRetryStrategy());
builder.setRedirectStrategy(new PreemptiveRedirectStrategy());
// set the following user agent with each request
String userAgent = "ArtifactoryBuildClient/" + CLIENT_VERSION;
builder.setUserAgent(userAgent);
return builder;
}
Aggregations