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:io.adventurous.android.api.ApiHttpClient.java

private static void setApiCredentials(ApiCredentials credentials, HttpRequestBase request) {
    if (credentials == null || credentials instanceof NullApiCredentials) {
        mClient.getCredentialsProvider().clear();
    } else {/* w  w  w.  ja  v  a2  s. c  o m*/
        String host = request.getURI().getHost();
        int authPort = request.getURI().getPort();

        mClient.getCredentialsProvider().setCredentials(new AuthScope(host, authPort),
                new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword()));
    }
}

From source file:com.jayway.restassured.internal.http.AuthConfig.java

/**
 * Set authentication credentials to be used for the given host and port.
 *
 * @param host//from   ww  w  .  j a  v  a 2 s.c  o m
 * @param port
 * @param user
 * @param pass
 */
public void basic(String host, int port, String user, String pass) {
    builder.getClient().getCredentialsProvider().setCredentials(new AuthScope(host, port),
            new UsernamePasswordCredentials(user, pass));
}

From source file:org.apache.hadoop.gateway.hive.HiveHttpClientDispatch.java

protected void addCredentialsToRequest(HttpUriRequest request) {
    if (isBasicAuthPreemptive()) {
        Principal principal = getPrimaryPrincipal();
        if (principal != null) {

            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(principal.getName(),
                    PASSWORD_PLACEHOLDER);

            request.addHeader(BasicScheme.authenticate(credentials, "US-ASCII", false));
        }/*from   w  w w  . j a  v a  2 s  . co  m*/
    }
}

From source file:org.hawkular.client.RestFactory.java

public T createAPI(URI uri, String userName, String password) {
    HttpClient httpclient = null;/*w  ww. jav a2  s  .  c  om*/
    if (uri.toString().startsWith("https")) {
        httpclient = getHttpClient();
    } else {
        httpclient = HttpClientBuilder.create().build();
    }
    ResteasyClient client = null;
    if (userName != null) {
        HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials(userName, password));
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);
        // Add AuthCache to the execution context
        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);
        context.setAuthCache(authCache);
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpclient, context);

        client = new ResteasyClientBuilder().httpEngine(engine).build();
    } else {
        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(getHttpClient());
        client = new ResteasyClientBuilder().httpEngine(engine).build();
    }

    client.register(JacksonJaxbJsonProvider.class);
    client.register(JacksonObjectMapperProvider.class);
    client.register(RestRequestFilter.class);
    client.register(RestResponseFilter.class);
    client.register(HCJacksonJson2Provider.class);
    ProxyBuilder<T> proxyBuilder = client.target(uri).proxyBuilder(apiClassType);
    if (classLoader != null) {
        proxyBuilder = proxyBuilder.classloader(classLoader);
    }
    return proxyBuilder.build();
}

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

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

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

    try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
            .build()) {/*from ww w .j  a va 2s.  c o m*/
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code in HTTP response.", SC_FORBIDDEN, statusCode);
        }
    }
}

From source file:ca.ubc.ece.netsyslab.tagvalue.system.TaggingSystem.java

/**
 * A convenience constructor that allows initializing the base URL
 *//*from  w w  w  .jav  a 2  s  .c om*/
public TaggingSystem(String hostname, int port, String username, String password) {
    host = new HttpHost(hostname, port, "https");
    userId = username;
    userPassword = password;
    httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(host.getHostName(), host.getPort()),
            new UsernamePasswordCredentials(userId, userPassword));
}

From source file:org.opencastproject.loadtest.engage.util.TrustedHttpClient.java

/**
 * {@inheritDoc}// w w  w.jav a 2s .  c o  m
 * @see org.opencastproject.loadtest.engage.util.remotetest.util.security.api.TrustedHttpClient#execute(org.apache.http.client.methods.HttpUriRequest)
 */
public HttpResponse execute(HttpUriRequest httpUriRequest) {
    // Add the request header to elicit a digest auth response
    httpUriRequest.addHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);

    if ("GET".equalsIgnoreCase(httpUriRequest.getMethod())
            || "HEAD".equalsIgnoreCase(httpUriRequest.getMethod())) {
        // Set the user/pass
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

        // Run the request (the http client handles the multiple back-and-forth requests)
        try {
            return httpClient.execute(httpUriRequest);
        } catch (IOException e) {
            throw new TrustedHttpClientException(e);
        }
    }

    // HttpClient doesn't handle the request dynamics for other verbs (especially when sending a streamed multipart
    // request), so we need to handle the details of the digest auth back-and-forth manually
    HttpRequestBase digestRequest;
    try {
        digestRequest = (HttpRequestBase) httpUriRequest.getClass().newInstance();
    } catch (Exception e) {
        throw new IllegalStateException("Can not create a new " + httpUriRequest.getClass().getName());
    }
    digestRequest.setURI(httpUriRequest.getURI());
    digestRequest.addHeader(REQUESTED_AUTH_HEADER, DIGEST_AUTH);
    String[] realmAndNonce = getRealmAndNonce(digestRequest);

    if (realmAndNonce != null) {
        // Set the user/pass
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass);

        // Set up the digest authentication with the required values
        DigestScheme digestAuth = new DigestScheme();
        digestAuth.overrideParamter("realm", realmAndNonce[0]);
        digestAuth.overrideParamter("nonce", realmAndNonce[1]);

        // Add the authentication header
        try {
            httpUriRequest.addHeader(digestAuth.authenticate(creds, httpUriRequest));
        } catch (Exception e) {
            // close the http connection(s)
            httpClient.getConnectionManager().shutdown();
            throw new TrustedHttpClientException(e);
        }
    }
    try {
        return httpClient.execute(httpUriRequest);
    } catch (Exception e) {
        // close the http connection(s)
        httpClient.getConnectionManager().shutdown();
        throw new TrustedHttpClientException(e);
    }
}

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

@Override
public void authenticateBasic(String authUser, String authPassword) {
    AuthScope authScope = new AuthScope(ANY_HOST, ANY_PORT);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(authUser, authPassword);
    httpClient.getCredentialsProvider().setCredentials(authScope, credentials);
}

From source file:org.isisaddons.app.kitchensink.webapp.ro.tck.IsisWebServerRule.java

private RestfulClient getClient(final String path) {
    if (client == null) {
        final URI restfulUri = resolveUri(path);

        final ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager();
        final DefaultHttpClient httpClient = new DefaultHttpClient(threadSafeClientConnManager);

        if (user != null) {
            httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", port),
                    new UsernamePasswordCredentials(user, pass));
        }/*from   w  ww . j  ava2s  . c om*/

        client = new RestfulClient(restfulUri, httpClient);
    }
    return client;
}