Example usage for org.apache.http.auth AuthScope ANY_REALM

List of usage examples for org.apache.http.auth AuthScope ANY_REALM

Introduction

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

Prototype

String ANY_REALM

To view the source code for org.apache.http.auth AuthScope ANY_REALM.

Click Source Link

Document

The null value represents any realm.

Usage

From source file:org.eobjects.datacleaner.user.UserPreferencesImplTest.java

public void testCreateHttpClientWithNtCredentials() throws Exception {
    UserPreferencesImpl up = new UserPreferencesImpl(null);
    up.setProxyHostname("host");
    up.setProxyPort(1234);//from   w  w  w  . ja v  a 2s . c  om
    up.setProxyUsername("FOO\\bar");
    up.setProxyPassword("baz");
    up.setProxyEnabled(true);
    up.setProxyAuthenticationEnabled(true);

    DefaultHttpClient httpClient = (DefaultHttpClient) up.createHttpClient();

    String computername = InetAddress.getLocalHost().getHostName();
    assertNotNull(computername);
    assertTrue(computername.length() > 1);

    AuthScope authScope;
    Credentials credentials;

    authScope = new AuthScope("host", 1234, AuthScope.ANY_REALM, "ntlm");
    credentials = httpClient.getCredentialsProvider().getCredentials(authScope);
    assertEquals("[principal: FOO/bar][workstation: " + computername.toUpperCase() + "]",
            credentials.toString());

    authScope = new AuthScope("host", 1234);
    credentials = httpClient.getCredentialsProvider().getCredentials(authScope);
    assertEquals("[principal: FOO\\bar]", credentials.toString());

    authScope = new AuthScope("anotherhost", AuthScope.ANY_PORT);
    credentials = httpClient.getCredentialsProvider().getCredentials(authScope);
    assertNull(credentials);
}

From source file:org.mule.modules.freshbooks.api.DefaultFreshBooksClient.java

/**
 * Constructor for Authentication Token mechanism
 * /* w w  w.j  a  v a 2s .  co m*/
 * @param apiUrl
 *            url for API
 * @param authenticationToken
 *            authentication token value
 * @param maxTotalConnection
 *            max total connections for client
 * @param defaultMaxConnectionPerRoute
 *            default max connection per route for client
 */
public DefaultFreshBooksClient(String apiUrl, String authenticationToken, int maxTotalConnection,
        int defaultMaxConnectionPerRoute) {
    Validate.notEmpty(apiUrl);
    Validate.notEmpty(authenticationToken);
    try {
        this.apiUrl = new URL(apiUrl);
    } catch (MalformedURLException e) {
        throw new FreshBooksException(e.getMessage());
    }

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    // max total connections
    cm.setMaxTotal(maxTotalConnection);
    // default max connection per route
    cm.setDefaultMaxPerRoute(defaultMaxConnectionPerRoute);

    client = new DefaultHttpClient(cm);
    this.client.getCredentialsProvider().setCredentials(
            new AuthScope(this.apiUrl.getHost(), 443, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(authenticationToken, ""));

    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {

        @Override
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            if (executionCount > 3) {
                logger.warn("Maximum tries reached for client http pool ");
                return false;
            }
            if (exception instanceof org.apache.http.NoHttpResponseException) {
                logger.warn("No response from server on " + executionCount + " call");
                return true;
            }
            return false;
        }
    });
}