Search in sources :

Example 41 with CredentialsProvider

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;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 42 with CredentialsProvider

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());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) UrlSigningService(org.opencastproject.security.urlsigning.service.UrlSigningService) HttpParams(org.apache.http.params.HttpParams) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpClient(org.opencastproject.kernel.http.api.HttpClient) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpClientFactory(org.opencastproject.kernel.http.impl.HttpClientFactory) HttpHead(org.apache.http.client.methods.HttpHead) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 43 with CredentialsProvider

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());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) UrlSigningService(org.opencastproject.security.urlsigning.service.UrlSigningService) HttpParams(org.apache.http.params.HttpParams) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.opencastproject.kernel.http.api.HttpClient) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpClientFactory(org.opencastproject.kernel.http.impl.HttpClientFactory) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 44 with CredentialsProvider

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;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 45 with CredentialsProvider

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());
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HashMap(java.util.HashMap) Map(java.util.Map) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)273 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)225 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)203 AuthScope (org.apache.http.auth.AuthScope)138 HttpHost (org.apache.http.HttpHost)104 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)75 HttpGet (org.apache.http.client.methods.HttpGet)62 BasicScheme (org.apache.http.impl.auth.BasicScheme)49 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)48 HttpResponse (org.apache.http.HttpResponse)47 Test (org.junit.Test)44 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)41 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)40 IOException (java.io.IOException)39 URI (java.net.URI)36 Credentials (org.apache.http.auth.Credentials)36 AuthCache (org.apache.http.client.AuthCache)33 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)33 HttpClient (org.apache.http.client.HttpClient)31 RequestConfig (org.apache.http.client.config.RequestConfig)29