use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider 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.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project opencast by opencast.
the class TrustedHttpClientImplTest method testAlreadySignedUrlIgnoredByUrlSigningService.
@Test
public void testAlreadySignedUrlIgnoredByUrlSigningService() throws IOException, UrlSigningException {
String acceptsUrl = "http://alreadysigned.com?signature=thesig&policy=thepolicy&keyId=thekey";
HttpHead headRequest = new HttpHead(acceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(acceptsUrl)).andReturn(true);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(headRequest);
assertTrue(request.hasCaptured());
assertEquals(acceptsUrl, request.getValue().getURI().toString());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project opencast by opencast.
the class TrustedHttpClientImplTest method testAcceptsUrlSigningService.
@Test
public void testAcceptsUrlSigningService() throws IOException, UrlSigningException {
String acceptsUrl = "http://accepts.com";
String signedUrl = "http://accepts.com?policy=testPolicy&signature=testSignature&keyId=testKeyId";
HttpGet get = new HttpGet(acceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(acceptsUrl)).andReturn(true);
EasyMock.expect(urlSigningService.sign(EasyMock.anyString(), EasyMock.anyLong(), EasyMock.anyLong(), EasyMock.anyString())).andReturn(signedUrl);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(get);
assertTrue(request.hasCaptured());
assertEquals(signedUrl, request.getValue().getURI().toString());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider 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.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider 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