Search in sources :

Example 66 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider 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 67 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider 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 68 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project directory-fortress-core by apache.

the class RestUtils method getCredentialProvider.

private CredentialsProvider getCredentialProvider(String uid, String password) {
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(httpHost, Integer.valueOf(httpPort)), new UsernamePasswordCredentials(uid == null ? httpUid : uid, password == null ? httpPw : password));
    return credentialsProvider;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthScope(org.apache.http.auth.AuthScope) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 69 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider 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;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpContext(org.apache.http.protocol.HttpContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) URL(java.net.URL) Date(java.util.Date) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) AuthScope(org.apache.http.auth.AuthScope) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler)

Example 70 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider 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

BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)192 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)162 CredentialsProvider (org.apache.http.client.CredentialsProvider)147 AuthScope (org.apache.http.auth.AuthScope)104 HttpHost (org.apache.http.HttpHost)76 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)53 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)41 HttpResponse (org.apache.http.HttpResponse)38 IOException (java.io.IOException)35 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)31 HttpGet (org.apache.http.client.methods.HttpGet)30 HttpClient (org.apache.http.client.HttpClient)29 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)29 AuthCache (org.apache.http.client.AuthCache)28 BasicScheme (org.apache.http.impl.auth.BasicScheme)27 RequestConfig (org.apache.http.client.config.RequestConfig)25 HttpPost (org.apache.http.client.methods.HttpPost)20 Credentials (org.apache.http.auth.Credentials)19 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)19 Test (org.junit.Test)16