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:com.microsoft.azure.hdinsight.spark.common.SparkInteractiveSessions.java

/**
 * Set http request credential using username and password
 *
 * @param username : username/*from w  w  w. j  a  va  2s .co m*/
 * @param password : password
 */
public void setCredentialsProvider(String username, String password) {
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
}

From source file:com.marklogic.client.test.util.TestServerBootstrapper.java

private void invokeBootstrapExtension() throws ClientProtocolException, IOException {

    DefaultHttpClient client = new DefaultHttpClient();

    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", port),
            new UsernamePasswordCredentials(username, password));

    HttpPost post = new HttpPost("http://" + host + ":" + port + "/v1/resources/bootstrap");

    HttpResponse response = client.execute(post);
    @SuppressWarnings("unused")
    HttpEntity entity = response.getEntity();
    System.out.println("Invoked bootstrap extension.  Response is " + response.toString());
}

From source file:org.droidparts.http.wrapper.DefaultHttpClientWrapper.java

@Override
protected void setProxy(String protocol, String host, int port, String user, String password) {
    HttpHost proxyHost = new HttpHost(host, port, protocol);
    httpClient.getParams().setParameter(DEFAULT_PROXY, proxyHost);
    if (!isEmpty(user) && !isEmpty(password)) {
        AuthScope authScope = new AuthScope(host, port);
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, password);
        httpClient.getCredentialsProvider().setCredentials(authScope, credentials);
    }//from   ww w  .j  av a2 s. c  om
}

From source file:org.jasig.schedassist.impl.caldav.DefaultCredentialsProviderFactoryImpl.java

/**
 * /* w w w .  ja  v a  2  s .  c  o  m*/
 * @return a {@link Credentials} made up of {@link #getCaldavAdminUsername()} and {@link #getCaldavAdminPassword()}.
 */
protected Credentials getAdminCredentials() {
    return new UsernamePasswordCredentials(getCaldavAdminUsername(), getCaldavAdminPassword());
}

From source file:read.taz.TazDownloader.java

private void downloadFile() throws ClientProtocolException, IOException {
    if (tazFile.file.exists()) {
        Log.w(getClass().getSimpleName(), "File " + tazFile + " exists.");
        return;//  w  w w .  ja  v  a2  s.  c o m
    }
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
    HttpConnectionParams.setSoTimeout(httpParams, 15000);

    DefaultHttpClient client = new DefaultHttpClient(httpParams);

    Credentials defaultcreds = new UsernamePasswordCredentials(uname, passwd);

    client.getCredentialsProvider()
            .setCredentials(new AuthScope(/* FIXME DRY */"dl.taz.de", 80, AuthScope.ANY_REALM), defaultcreds);

    URI uri = tazFile.getDownloadURI();
    Log.d(getClass().getSimpleName(), "Downloading taz from " + uri);
    HttpGet get = new HttpGet(uri);
    HttpResponse response = client.execute(get);
    if (response.getStatusLine().getStatusCode() != 200) {
        throw new IOException("Download failed with HTTP Error" + response.getStatusLine().getStatusCode());
    }

    String contentType = response.getEntity().getContentType().getValue();
    if (contentType.startsWith("text/html")) {
        Log.d(getClass().getSimpleName(),
                "Content type: " + contentType + " encountered. Assuming a non exisiting file");

        ByteArrayOutputStream htmlOut = new ByteArrayOutputStream();
        response.getEntity().writeTo(htmlOut);
        String resp = new String(htmlOut.toByteArray());
        Log.d(getClass().getSimpleName(), "Response: " + resp);
        throw new FileNotFoundException("No taz found for date " + tazFile.date.getTime());
    } else {
        FileOutputStream tazOut = new FileOutputStream(tazFile.file);
        try {
            response.getEntity().writeTo(tazOut);
        } finally {
            tazOut.close();
        }
    }

    // InputStream docStream = response.getEntity().getContent();
    // FileOutputStream out = null;
    // try {
    // out = tazOut;
    // byte[] buf = new byte[4096];
    // for (int read = docStream.read(buf); read != -1; read = docStream
    // .read(buf)) {
    // out.write(buf, 0, read);
    // }
    // } finally {
    // docStream.close();
    // if (out != null) {
    // out.close();
    // }
    // }

}

From source file:com.betfair.testing.utils.cougar.manager.HttpPageManager.java

public int getPage(HttpPageBean bean) {

    // Get bean properties
    String requestedProtocol = bean.getProtocol();
    String requestedHost = bean.getHost();
    int requestedPort = bean.getPort();
    String requestedLink = bean.getLink();
    String username = bean.getAuthusername();
    String password = bean.getAuthpassword();

    final SSLSocketFactory sf = new SSLSocketFactory(createEasySSLContext(),
            SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme https = new Scheme("https", 9999, sf);

    // Set up httpClient to use given auth details and protocol
    DefaultHttpClient client = new DefaultHttpClient();
    client.getConnectionManager().getSchemeRegistry().register(https);
    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(username, password));

    int status = -1;

    InputStream inputStream = null;
    // Make the request
    try {/*from w  ww. ja  v a 2 s  .c o  m*/
        final HttpGet httpget = new HttpGet(
                URIUtils.createURI(requestedProtocol, requestedHost, requestedPort, requestedLink, null, null));
        final HttpResponse httpResponse = client.execute(httpget);
        inputStream = httpResponse.getEntity().getContent();
        status = httpResponse.getStatusLine().getStatusCode();

        if (status == HttpStatus.SC_OK) {
            bean.setPageLoaded(true);

            byte[] buffer = new byte[(int) httpResponse.getEntity().getContentLength()];
            int read;
            int count = 0;
            while ((read = inputStream.read()) != -1) {
                buffer[count] = (byte) read;
                count++;
            }
            bean.setPageText(new String(buffer, "UTF-8"));
            bean.setBuffer(buffer);
        }
    } catch (IOException e1) {
        return -1;
    } catch (URISyntaxException e) {
        return -1;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    return status;
}

From source file:fr.pilato.elasticsearch.crawler.fs.client.ElasticsearchClient.java

private ElasticsearchClient(List<Node> nodes, String username, String password) {
    List<HttpHost> hosts = new ArrayList<>(nodes.size());
    for (Node node : nodes) {
        hosts.add(new HttpHost(node.getHost(), node.getPort(), node.getScheme().toLowerCase()));
    }//from   w  w  w  .  java2s.  co  m
    RestClientBuilder builder = RestClient.builder(hosts.toArray(new HttpHost[hosts.size()]));

    if (username != null) {
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        builder.setHttpClientConfigCallback(
                httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    }

    client = builder.build();
}

From source file:edu.emory.bmi.medicurator.tcia.TciaQuery.java

private HttpClient getInsecureClient() throws Exception {
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }/*from  w w  w.  ja  v a2  s .c o  m*/
    }).build();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setSSLSocketFactory(sslsf);
    if (Constants.PROXY_HOST != null && Constants.PROXY_PORT != null) {
        HttpHost proxy = new HttpHost(Constants.PROXY_HOST, Constants.PROXY_PORT, "http");
        builder.setProxy(proxy);
    }
    if (Constants.PROXY_USERNAME != null && Constants.PROXY_PASSWORD != null) {
        Credentials credentials = new UsernamePasswordCredentials(Constants.PROXY_USERNAME,
                Constants.PROXY_PASSWORD);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, credentials);
        builder.setDefaultCredentialsProvider(credsProvider);
    }
    return builder.build();
}

From source file:at.pagu.soldockr.core.HttpSolrServerFactoryTest.java

@Test
public void testInitFactoryWithAuthentication() {
    HttpSolrServerFactory factory = new HttpSolrServerFactory(solrServer, "core",
            new UsernamePasswordCredentials("username", "password"), "BASIC");

    AbstractHttpClient solrHttpClient = (AbstractHttpClient) ((HttpSolrServer) factory.getSolrServer())
            .getHttpClient();/*from   w w  w .  ja va  2  s  .  c  o m*/
    Assert.assertNotNull(solrHttpClient.getCredentialsProvider().getCredentials(AuthScope.ANY));
    Assert.assertNotNull(solrHttpClient.getParams().getParameter(AuthPNames.TARGET_AUTH_PREF));
    Assert.assertEquals("username", ((UsernamePasswordCredentials) solrHttpClient.getCredentialsProvider()
            .getCredentials(AuthScope.ANY)).getUserName());
    Assert.assertEquals("password", ((UsernamePasswordCredentials) solrHttpClient.getCredentialsProvider()
            .getCredentials(AuthScope.ANY)).getPassword());
}