Example usage for org.apache.http.impl.client BasicCredentialsProvider BasicCredentialsProvider

List of usage examples for org.apache.http.impl.client BasicCredentialsProvider BasicCredentialsProvider

Introduction

In this page you can find the example usage for org.apache.http.impl.client BasicCredentialsProvider BasicCredentialsProvider.

Prototype

public BasicCredentialsProvider() 

Source Link

Document

Default constructor.

Usage

From source file:com.jimdo.graylog.net.Request.java

/**
 * Set Credentials for HTTPAuth/*from w  w  w.ja va2s. co  m*/
 */
public void setHttpAuth(String username, String password) {
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);

    httpClient.setCredentialsProvider(credentialsProvider);
}

From source file:org.fcrepo.camel.FcrepoHttpClientBuilder.java

/**
 *  Build an HttpClient/*from w w  w  . j av a2  s  . c o m*/
 *
 *  @return an HttpClient
 */
public CloseableHttpClient build() {

    if (isBlank(username) || isBlank(password)) {
        return HttpClients.createDefault();
    } else {
        LOGGER.debug("Accessing fcrepo with user credentials");

        final CredentialsProvider credsProvider = new BasicCredentialsProvider();
        AuthScope scope = null;

        if (isBlank(host)) {
            scope = new AuthScope(AuthScope.ANY);
        } else {
            scope = new AuthScope(new HttpHost(host));
        }
        credsProvider.setCredentials(scope, new UsernamePasswordCredentials(username, password));
        return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    }
}

From source file:org.fcrepo.client.FcrepoHttpClientBuilder.java

/**
 *  Build an HttpClient//from  w w w .ja  v  a 2 s .  c  om
 *
 *  @return an HttpClient
 */
public CloseableHttpClient build() {

    if (isBlank(username) || isBlank(password)) {
        return HttpClients.createSystem();
    } else {
        LOGGER.debug("Accessing fcrepo with user credentials");

        final CredentialsProvider credsProvider = new BasicCredentialsProvider();
        AuthScope scope = null;

        if (isBlank(host)) {
            scope = new AuthScope(AuthScope.ANY);
        } else {
            scope = new AuthScope(new HttpHost(host));
        }
        credsProvider.setCredentials(scope, new UsernamePasswordCredentials(username, password));
        return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).useSystemProperties().build();
    }
}

From source file:org.apache.olingo.client.core.http.NTLMAuthHttpClientFactory.java

@Override
public DefaultHttpClient create(final HttpMethod method, final URI uri) {
    final DefaultHttpClient httpclient = super.create(method, uri);

    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(username, password, workstation, domain));

    httpclient.setCredentialsProvider(credsProvider);

    return httpclient;
}

From source file:org.sonatype.nexus.ant.staging.deploy.ZapperImpl.java

@Override
public void deployDirectory(final ZapperRequest zapperRequest) throws IOException {
    try {/*  ww  w .j  av  a  2  s .  c  o m*/
        HttpHost proxyServer = null;
        BasicCredentialsProvider credentialsProvider = null;
        if (!StringUtils.isBlank(zapperRequest.getProxyProtocol())) {
            proxyServer = new HttpHost(zapperRequest.getProxyHost(), zapperRequest.getProxyPort(),
                    zapperRequest.getProxyProtocol());

            if (!StringUtils.isBlank(zapperRequest.getProxyUsername())) {
                UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(
                        zapperRequest.getProxyUsername(), zapperRequest.getProxyPassword());

                credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(new AuthScope(proxyServer.getHostName(),
                        proxyServer.getPort(), AuthScope.ANY_REALM, proxyServer.getSchemeName()),
                        proxyCredentials);
            }
        }

        if (!StringUtils.isBlank(zapperRequest.getRemoteUsername())) {
            UsernamePasswordCredentials remoteCredentials = new UsernamePasswordCredentials(
                    zapperRequest.getRemoteUsername(), zapperRequest.getRemotePassword());

            if (credentialsProvider == null) {
                credentialsProvider = new BasicCredentialsProvider();
            }

            credentialsProvider.setCredentials(AuthScope.ANY, remoteCredentials);
        }

        final Parameters parameters = ParametersBuilder.defaults().build();
        final Hc4ClientBuilder clientBuilder = new Hc4ClientBuilder(parameters, zapperRequest.getRemoteUrl());
        if (credentialsProvider != null) {
            clientBuilder.withPreemptiveRealm(credentialsProvider);
        }
        if (proxyServer != null) {
            clientBuilder.withProxy(proxyServer);
        }
        final Client client = clientBuilder.build();
        final IOSourceListable deployables = new DirectoryIOSource(zapperRequest.getStageRepository());

        try {
            client.upload(deployables);
        } finally {
            client.close();
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Unable to deploy!", e);
    }
}

From source file:org.wildfly.test.integration.elytron.http.PasswordMechTestBase.java

@Test
public void testSuccess() throws Exception {
    HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role1"));
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "password1");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, credentials);

    try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
            .build()) {/*  w  w  w .j a v  a 2 s.c  o  m*/
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code in HTTP response.", SC_OK, statusCode);
            assertEquals("Unexpected content of HTTP response.", SimpleServlet.RESPONSE_BODY,
                    EntityUtils.toString(response.getEntity()));
        }
    }
}

From source file:org.apache.sling.launchpad.SmokeIT.java

private CloseableHttpClient newClient() {

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
    credsProvider.setCredentials(new AuthScope("localhost", LAUNCHPAD_PORT), creds);

    return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
}

From source file:com.samebug.clients.search.api.client.RawClient.java

RawClient(@NotNull final Config config) {
    HttpClientBuilder httpBuilder = HttpClientBuilder.create();
    requestConfigBuilder = RequestConfig.custom();
    CredentialsProvider provider = new BasicCredentialsProvider();

    requestConfigBuilder.setConnectTimeout(config.connectTimeout).setSocketTimeout(config.requestTimeout)
            .setConnectionRequestTimeout(500);
    try {/* ww w . j  a va 2  s  . c  om*/
        IdeHttpClientHelpers.ApacheHttpClient4.setProxyForUrlIfEnabled(requestConfigBuilder, config.serverRoot);
        IdeHttpClientHelpers.ApacheHttpClient4.setProxyCredentialsForUrlIfEnabled(provider, config.serverRoot);
    } catch (Throwable e) {
        // fallback to traditional proxy config for backward compatiblity
        try {
            final HttpConfigurable proxySettings = HttpConfigurable.getInstance();
            final ProxyCredentialsFacade facade = new ProxyCredentialsFacade(proxySettings);
            if (proxySettings != null && proxySettings.USE_HTTP_PROXY
                    && !StringUtil.isEmptyOrSpaces(proxySettings.PROXY_HOST)) {
                requestConfigBuilder.setProxy(new HttpHost(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT));
                if (proxySettings.PROXY_AUTHENTICATION) {
                    provider.setCredentials(AuthScope.ANY,
                            new UsernamePasswordCredentials(facade.getLogin(), facade.getPassword()));
                }
            }
        } catch (Throwable ignored) {
            // if even that fails, we cannot do more
        }
    }
    defaultRequestConfig = requestConfigBuilder.build();
    trackingConfig = requestConfigBuilder.setSocketTimeout(TrackingRequestTimeout_Millis).build();
    List<BasicHeader> defaultHeaders = new ArrayList<BasicHeader>();
    defaultHeaders.add(new BasicHeader("User-Agent", USER_AGENT));
    if (config.apiKey != null)
        defaultHeaders.add(new BasicHeader("X-Samebug-ApiKey", config.apiKey));
    if (config.workspaceId != null)
        defaultHeaders.add(new BasicHeader("X-Samebug-WorkspaceId", config.workspaceId.toString()));

    httpClient = httpBuilder.setDefaultRequestConfig(defaultRequestConfig).setMaxConnTotal(MaxConnections)
            .setMaxConnPerRoute(MaxConnections).setDefaultCredentialsProvider(provider)
            .setDefaultHeaders(defaultHeaders).build();
    if (config.isApacheLoggingEnabled)
        enableApacheLogging();
}

From source file:com.smartling.api.sdk.util.HttpProxyUtils.java

/**
 * Get an HttpClient given a proxy config if any
 * @param proxyConfiguration configuration of proxy to use
 * @return org.apache.http.impl.client.CloseableHttpClient
 *///from   w w  w .  j a v a2s.  c  o  m
public CloseableHttpClient getHttpClient(final ProxyConfiguration proxyConfiguration) {
    HttpClientBuilder httpClientBuilder = getHttpClientBuilder();

    if (proxyAuthenticationRequired(proxyConfiguration)) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort()),
                new UsernamePasswordCredentials(proxyConfiguration.getUsername(),
                        proxyConfiguration.getPassword()));

        httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }

    return httpClientBuilder.build();
}

From source file:org.sonatype.nexus.plugin.deploy.ZapperImpl.java

@Override
public void deployDirectory(final ZapperRequest zapperRequest) throws IOException {
    try {//from  www  . ja  va2s  .  c  om
        HttpHost proxyServer = null;
        BasicCredentialsProvider credentialsProvider = null;
        if (!StringUtils.isBlank(zapperRequest.getProxyProtocol())) {
            proxyServer = new HttpHost(zapperRequest.getProxyHost(), zapperRequest.getProxyPort(),
                    zapperRequest.getProxyProtocol());

            if (!StringUtils.isBlank(zapperRequest.getProxyUsername())) {
                UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(
                        zapperRequest.getProxyUsername(), zapperRequest.getProxyPassword());

                credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(new AuthScope(proxyServer.getHostName(),
                        proxyServer.getPort(), AuthScope.ANY_REALM, proxyServer.getSchemeName()),
                        proxyCredentials);
            }
        }

        if (!StringUtils.isBlank(zapperRequest.getRemoteUsername())) {
            UsernamePasswordCredentials remoteCredentials = new UsernamePasswordCredentials(
                    zapperRequest.getRemoteUsername(), zapperRequest.getRemotePassword());

            if (credentialsProvider == null) {
                credentialsProvider = new BasicCredentialsProvider();
            }

            credentialsProvider.setCredentials(AuthScope.ANY, remoteCredentials);
        }

        final Parameters parameters = ParametersBuilder.defaults().build();
        final Hc4ClientBuilder clientBuilder = new Hc4ClientBuilder(parameters, zapperRequest.getRemoteUrl());
        if (credentialsProvider != null) {
            clientBuilder.withRealm(credentialsProvider);
        }
        if (proxyServer != null) {
            clientBuilder.withProxy(proxyServer);
        }
        final Client client = clientBuilder.build();
        final IOSourceListable deployables = new DirectoryIOSource(zapperRequest.getStageRepository());

        try {
            client.upload(deployables);
        } finally {
            client.close();
        }
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Unable to deploy!", e);
    }
}