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.twitter.hbc.httpclient.auth.BasicAuth.java

public void setupConnection(AbstractHttpClient client) {
    client.getCredentialsProvider().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(username, password));
}

From source file:be.benvd.mvforandroid.data.MVDataHelper.java

/**
 * Returns the GET response of the given url.
 * /*www  .  j ava  2  s . c om*/
 * @throws IOException
 * @return The response of the given URL. If no response was found, null is
 *         returned.
 */
public static String getResponse(String username, String password, String url) throws IOException {
    /*      DefaultHttpClient httpclient = new DefaultHttpClient();
          httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username + ":" + password));
          HttpGet httpget = new HttpGet(url);
          HttpResponse response = httpclient.execute(httpget);
    */
    DefaultHttpClient httpclient = new DefaultHttpClient();
    Credentials creds = new UsernamePasswordCredentials(username, password);
    httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), creds);

    String auth = android.util.Base64.encodeToString((username + ":" + password).getBytes("UTF-8"),
            android.util.Base64.NO_WRAP);

    HttpGet httpget = new HttpGet(url);
    httpget.addHeader("Authorization", "Basic " + auth);
    HttpResponse response = httpclient.execute(httpget);

    if (response.getEntity() != null) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        Log.v(MVDataHelper.class.getSimpleName(), "Response:" + sb.toString());
        return sb.toString();
    }

    return null;
}

From source file:v2.service.generic.library.utils.HttpClientUtil.java

public static HttpClient createHttpClientWithAuth(String credential, String principle) {
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(credential, principle);
    provider.setCredentials(AuthScope.ANY, credentials);
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10 * 1000).build();
    CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider)
            .setDefaultRequestConfig(requestConfig).build();
    return httpclient;
}

From source file:de.e7o.caldav.caldav.Connection.java

protected Connection(CaldavServer base) {
    this.base = base;
    if (base.username != null) {
        this.authInfo = new UsernamePasswordCredentials(base.username, base.password);
    } else {/*from   w ww .j  a v  a2s  .  c  o  m*/
        this.authInfo = null;
    }
}

From source file:com.ibm.ra.remy.web.utils.BusinessRulesUtils.java

/**
 * Invoke Business Rules with JSON content
 *
 * @param content The payload of itineraries to send to Business Rules.
 * @return A JSON string representing the output of Business Rules.
 *//*from   ww  w .  ja v a2  s.  com*/
public static String invokeRulesService(String json) throws Exception {
    PropertiesReader constants = PropertiesReader.getInstance();
    String username = constants.getStringProperty(USERNAME_KEY);
    String password = constants.getStringProperty(PASSWORD_KEY);
    String endpoint = constants.getStringProperty(EXECUTION_REST_URL_KEY)
            + constants.getStringProperty(RULE_APP_PATH_KEY);

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(credentialsProvider).build();

    String responseString = "";

    try {
        HttpPost httpPost = new HttpPost(endpoint);
        httpPost.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
        StringEntity jsonEntity = new StringEntity(json, MessageUtils.ENCODING);
        httpPost.setEntity(jsonEntity);
        CloseableHttpResponse response = httpClient.execute(httpPost);

        try {
            HttpEntity entity = response.getEntity();
            responseString = EntityUtils.toString(entity, MessageUtils.ENCODING);
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        httpClient.close();
    }
    return responseString;
}

From source file:org.surfnet.example.api.oauth.JsonPrincipalServiceTest.java

/**
 * Test method for//  ww w . j a v  a 2 s . c  o  m
 * {@link org.surfnet.example.api.oauth.JsonPrincipalService#getPrincipal(org.apache.http.auth.UsernamePasswordCredentials)}
 * .
 */
@Test
public void testGetPrincipal() {
    Student principal = service.getPrincipal(new UsernamePasswordCredentials("foo8", "doen-not-matter"));
    assertEquals("foo8", principal.getIdentifier());
}

From source file:org.lorislab.armonitor.client.MonitorClient.java

/**
 * Creates the monitor service.//  ww w.  ja  v a 2  s .c om
 *
 * @param url the URL.
 * @param username the user name.
 * @param password the password.
 * @return the monitor service.
 */
private static MonitorService createService(URL url, String username, String password, boolean auth) {
    ResteasyProviderFactory rpf = ResteasyProviderFactory.getInstance();
    RegisterBuiltin.register(rpf);

    String tmp = url.toString();
    if (!tmp.endsWith("/")) {
        tmp = tmp + "/";
    }
    tmp = tmp + APP_URL;

    if (auth) {
        Credentials credentials = new UsernamePasswordCredentials(username, password);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        BasicCredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(AuthScope.ANY, credentials);
        httpClient.setCredentialsProvider(provider);
        return ProxyFactory.create(MonitorService.class, tmp, new ApacheHttpClient4Executor(httpClient));
    }
    return ProxyFactory.create(MonitorService.class, tmp);
}

From source file:de.bytefish.fcmjava.client.tests.FakeFcmClientSettings.java

@Test
public void testFcmClientWithProxySettings() throws Exception {

    // Create Settings:
    IFcmClientSettings settings = new FakeFcmClientSettings();

    // Define the Credentials to be used:
    BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();

    // Set the Credentials (any auth scope used):
    basicCredentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("your_username", "your_password"));

    // Create the Apache HttpClientBuilder:
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
            // Set the Proxy Address:
            .setProxy(new HttpHost("your_hostname", 1234))
            // Set the Authentication Strategy:
            .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
            // Set the Credentials Provider we built above:
            .setDefaultCredentialsProvider(basicCredentialsProvider);

    // Create the DefaultHttpClient:
    DefaultHttpClient httpClient = new DefaultHttpClient(settings, httpClientBuilder);

    // Finally build the FcmClient:
    try (IFcmClient client = new FcmClient(settings, httpClient)) {
        // TODO Work with the Proxy ...
    }/* w w  w.j  av  a 2s.c om*/
}

From source file:org.lorislab.armonitor.util.RestClient.java

/**
 * Gets the rest-service client.//from www. jav a  2s .c  om
 *
 * @param <T> the rest-service client implementation.
 * @param clazz the rest-service class.
 * @param url the server URL.
 * @param username the username.
 * @param password the password.
 * @param auth the authentication flag.
 * @exception Exception if the method fails.
 *
 * @return the the rest-service client instance.
 */
public static <T> T getClient(final Class<T> clazz, String url, boolean auth, String username, char[] password)
        throws Exception {

    DefaultHttpClient httpClient = new DefaultHttpClient();
    if (url.startsWith(HTTPS)) {
        SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(),
                SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        httpClient.getConnectionManager().getSchemeRegistry()
                .register(new Scheme(HTTPS, 443, sslSocketFactory));
    }

    if (auth) {
        BasicCredentialsProvider provider = new BasicCredentialsProvider();
        Credentials credentials = new UsernamePasswordCredentials(username, new String(password));
        provider.setCredentials(AuthScope.ANY, credentials);
        httpClient.setCredentialsProvider(provider);
    }
    return ProxyFactory.create(clazz, url, new ApacheHttpClient4Executor(httpClient));
}