Example usage for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials

List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials

Introduction

In this page you can find the example usage for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials.

Prototype

public UsernamePasswordCredentials(final String userName, final String password) 

Source Link

Document

The constructor with the username and password arguments.

Usage

From source file:org.apache.brooklyn.entity.brooklynnode.EntityHttpClientImpl.java

@Override
public HttpTool.HttpClientBuilder getHttpClientForBrooklynNode() {
    String baseUrl = getEntityUrl();
    HttpTool.HttpClientBuilder builder = HttpTool.httpClientBuilder().trustAll().laxRedirect(true).uri(baseUrl);
    if (entity.getConfig(BrooklynNode.MANAGEMENT_USER) != null) {
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
                entity.getConfig(BrooklynNode.MANAGEMENT_USER),
                entity.getConfig(BrooklynNode.MANAGEMENT_PASSWORD));
        builder.credentials(credentials);
    }/*from www .j a  v  a 2 s  .  c  o  m*/
    return builder;
}

From source file:com.blazemeter.bamboo.plugin.api.HttpUtility.java

public HttpUtility() {
    this.httpClient = HttpClients.createDefault();
    useProxy = Boolean.parseBoolean(System.getProperty(Constants.USE_PROXY));
    proxyHost = System.getProperty(Constants.PROXY_HOST);

    try {//from  w  w w .j a va  2 s  . co m
        this.proxyPort = Integer.parseInt(System.getProperty(Constants.PROXY_PORT));
    } catch (NumberFormatException nfe) {
        logger.error("Failed to read http.proxyPort: ", nfe);
    }
    if (useProxy && !org.apache.commons.lang3.StringUtils.isBlank(this.proxyHost)) {
        this.proxy = new HttpHost(proxyHost, proxyPort);

        this.proxyUser = System.getProperty(Constants.PROXY_USER);
        this.proxyPass = System.getProperty(Constants.PROXY_PASS);
        if (!org.apache.commons.lang3.StringUtils.isEmpty(this.proxyUser)
                && !org.apache.commons.lang3.StringUtils.isEmpty(this.proxyPass)) {
            Credentials cr = new UsernamePasswordCredentials(proxyUser, proxyPass);
            AuthScope aus = new AuthScope(proxyHost, proxyPort);
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(aus, cr);
            this.httpClient = HttpClients.custom().setProxy(proxy).setDefaultCredentialsProvider(credsProvider)
                    .build();
        } else {
            this.httpClient = HttpClients.custom().setProxy(proxy).build();
        }
    }
}

From source file:org.dspace.identifier.ezid.EZIDRequest.java

/**
 * Prepare a context for requests concerning a specific identifier or
 * authority prefix.//  w w w .  j a va 2  s  .c  om
 *
 * @param scheme URL scheme for access to the EZID service.
 * @param host Host name for access to the EZID service.
 * @param authority DOI authority prefix, e.g. "10.5072/FK2".
 * @param username an EZID user identity.
 * @param password user's password, or {@code null} for none.
 * @throws URISyntaxException if host or authority is bad.
 */
EZIDRequest(String scheme, String host, String authority, String username, String password)
        throws URISyntaxException {
    this.scheme = scheme;

    this.host = host;

    if (authority.charAt(authority.length() - 1) == '/') {
        this.authority = authority.substring(0, authority.length() - 1);
    } else {
        this.authority = authority;
    }

    client = new DefaultHttpClient();
    if (null != username) {
        URI uri = new URI(scheme, host, null, null);
        client.getCredentialsProvider().setCredentials(new AuthScope(uri.getHost(), uri.getPort()),
                new UsernamePasswordCredentials(username, password));
    }
}

From source file:com.cprassoc.solr.auth.SolrHttpHandler.java

protected SolrHttpHandler() {
    PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
    if (props == null) {
        props = SolrAuthManager.getProperties();
    }/*from www  .  j  a va2s  . co  m*/

    if (props.getProperty("solr.ssl.enabled").equals("true")) {
        solrBaseUrl = "https://";
    } else {
        solrBaseUrl = "http://";
    }
    solrBaseUrl += props.getProperty("solr.host.port");
    //  solr.crawler.cloud.server=localhost:9983
    //admin:password123@
    CredentialsProvider provider = new BasicCredentialsProvider();

    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            props.getProperty("solr.admin.user"), props.getProperty("solr.admin.pwd"));
    provider.setCredentials(AuthScope.ANY, credentials);

    // client = new DefaultHttpClient(cm);
    client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();

    zkCloudClient = new CloudSolrClient(props.getProperty("solr.zookeeper.port"), client);
    zkCloudClient.setDefaultCollection(props.getProperty("solr.default.collection"));

    //     solrCloudClient = new CloudSolrClient(props.getProperty("solr.host.port"), client);
    //    solrCloudClient.setDefaultCollection(props.getProperty("solr.default.collection"));

    System.out.println("Solr Base URL: " + solrBaseUrl);
}

From source file:org.jboss.as.test.integration.web.security.WebSecurityBASICTestCase.java

protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/*from w ww .  j  a v a  2  s  . c  om*/
        httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080),
                new UsernamePasswordCredentials(user, pass));

        HttpGet httpget = new HttpGet(URL);

        System.out.println("executing request" + httpget.getRequestLine());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        StatusLine statusLine = response.getStatusLine();
        System.out.println(statusLine);
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        assertEquals(expectedStatusCode, statusLine.getStatusCode());
        EntityUtils.consume(entity);
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:eu.esdihumboldt.util.http.client.ClientProxyUtil.java

/**
 * Create a credentials object./*from  w  ww.  j  av a 2 s  .c o m*/
 * 
 * @param user the user name
 * @param password the password
 * @return the created credentials
 */
public static Credentials createCredentials(String user, String password) {
    Credentials credentials;
    int sepIndex = user.indexOf('\\');
    if (sepIndex > 0 && sepIndex + 1 < user.length()) {
        // assume this is DOMAIN \ user for NTLM authentication
        String userName = user.substring(sepIndex + 1);
        String domain = user.substring(0, sepIndex);
        String workstation = null;
        credentials = new NTCredentials(userName, password, workstation, domain);
    } else {
        credentials = new UsernamePasswordCredentials(user, password);
    }
    return credentials;
}

From source file:org.dasein.security.joyent.AuthClientFactory.java

@Override
public @Nonnull HttpClient getClient(String endpoint) throws CloudException, InternalException {

    if (endpoint == null) {
        throw new CloudException("No cloud endpoint was defined");
    }//from   w  w w  .j ava  2 s.  c  om

    boolean ssl = endpoint.startsWith("https");
    int targetPort;
    URI uri;

    try {
        uri = new URI(endpoint);
        targetPort = uri.getPort();
        if (targetPort < 1) {
            targetPort = (ssl ? 443 : 80);
        }
    } catch (URISyntaxException e) {
        throw new CloudException(e);
    }
    HttpHost targetHost = new HttpHost(uri.getHost(), targetPort, uri.getScheme());

    DefaultHttpClient client = (DefaultHttpClient) super.getClient(endpoint);

    try {
        String userName = new String(getProviderContext().getAccessPublic(), "utf-8");
        String password = new String(getProviderContext().getAccessPrivate(), "utf-8");

        client.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials(userName, password));
    } catch (UnsupportedEncodingException e) {
        throw new InternalException(e);
    }
    return client;
}

From source file:com.jskj.asset.client.login.LoginTask.java

@Override
protected Object doInBackground() throws Exception {
    try {//from w  w w. j  a  v a 2  s  . com
        RestTemplate restTemplate = (RestTemplate) BeanFactory.instance().createBean(RestTemplate.class);

        if (logined) {
            ComResponse<String> com = restTemplate.getForObject(
                    java.net.URI.create(Constants.HTTP + Constants.APPID + "logout"), ComResponse.class);
            if (com.getResponseStatus() != ComResponse.STATUS_OK) {
                return new Exception("logout failed. ");
            } else {
                BaseTreePane.disTabCount.clear();
            }
        }

        Object userNameObj = map.get("userName");
        ;
        Object passwdObj = map.get("userPassword");

        HttpComponentsClientHttpRequestFactory httpRequestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate
                .getRequestFactory();
        DefaultHttpClient httpClient = (DefaultHttpClient) httpRequestFactory.getHttpClient();
        String unicodeStr = UnicodeConverter.toEncodedUnicode(userNameObj.toString(), false);
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(unicodeStr,
                passwdObj.toString());
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);

        UserSessionEntity session = restTemplate.getForObject(java.net.URI.create(URI),
                UserSessionEntity.class);

        return session;
    } catch (Exception e) {
        e.printStackTrace();
        return e;
    }
}

From source file:org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration.java

@Bean
@ConditionalOnMissingBean//from  w  w  w  .ja v  a 2s  . co  m
public RestClientBuilder restClientBuilder() {
    HttpHost[] hosts = this.properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new);
    RestClientBuilder builder = RestClient.builder(hosts);
    PropertyMapper map = PropertyMapper.get();
    map.from(this.properties::getUsername).whenHasText().to((username) -> {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        Credentials credentials = new UsernamePasswordCredentials(this.properties.getUsername(),
                this.properties.getPassword());
        credentialsProvider.setCredentials(AuthScope.ANY, credentials);
        builder.setHttpClientConfigCallback(
                (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    });
    this.builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
    return builder;
}