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.fcrepo.test.api.TestRESTAPI.java

private HttpClient getClient(boolean followRedirects, boolean auth) {
    DefaultHttpClient result = s_client.getHttpClient(followRedirects, auth);
    if (auth) {//from w  ww. j a  va  2s  .co m
        String host = getHost();
        LOGGER.debug("credentials set for scope of {}:[ANY PORT]", host);
        result.getCredentialsProvider().setCredentials(
                new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
                new UsernamePasswordCredentials(getUsername(), getPassword()));
    } else {
        result.getCredentialsProvider().clear();
    }
    return result;
}

From source file:org.apache.hive.ptest.api.client.PTestClient.java

public PTestClient(String logsEndpoint, String apiEndPoint, String password, String testOutputDir)
        throws MalformedURLException {
    this.testOutputDir = testOutputDir;
    if (!Strings.isNullOrEmpty(testOutputDir)) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(logsEndpoint),
                "logsEndPoint must be specified if " + OUTPUT_DIR + " is specified");
        if (logsEndpoint.endsWith("/")) {
            this.mLogsEndpoint = logsEndpoint;
        } else {/*from   www  .j av  a 2  s  .c o  m*/
            this.mLogsEndpoint = logsEndpoint + "/";
        }
    } else {
        this.mLogsEndpoint = null;
    }
    if (apiEndPoint.endsWith("/")) {
        this.mApiEndPoint = apiEndPoint + "api/v1";
    } else {
        this.mApiEndPoint = apiEndPoint + "/api/v1";
    }
    URL apiURL = new URL(mApiEndPoint);
    mMapper = new ObjectMapper();
    mHttpClient = new DefaultHttpClient();
    mHttpClient.getCredentialsProvider().setCredentials(
            new AuthScope(apiURL.getHost(), apiURL.getPort(), AuthScope.ANY_REALM),
            new UsernamePasswordCredentials("hive", password));
}

From source file:org.apache.hive.ptest.execution.JIRAService.java

void publishComments(String comments) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {//from www. j a  va  2 s .co  m
        String url = String.format("%s/rest/api/2/issue/%s/comment", mUrl, mName);
        URL apiURL = new URL(mUrl);
        httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(apiURL.getHost(), apiURL.getPort(), AuthScope.ANY_REALM),
                new UsernamePasswordCredentials(mUser, mPassword));
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute("preemptive-auth", new BasicScheme());
        httpClient.addRequestInterceptor(new PreemptiveAuth(), 0);
        HttpPost request = new HttpPost(url);
        ObjectMapper mapper = new ObjectMapper();
        StringEntity params = new StringEntity(mapper.writeValueAsString(new Body(comments)));
        request.addHeader("Content-Type", "application/json");
        request.setEntity(params);
        HttpResponse httpResponse = httpClient.execute(request, localcontext);
        StatusLine statusLine = httpResponse.getStatusLine();
        if (statusLine.getStatusCode() != 201) {
            throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
        }
        mLogger.info("JIRA Response Metadata: " + httpResponse);
    } catch (Exception e) {
        mLogger.error("Encountered error attempting to post comment to " + mName, e);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.apache.http.client.protocol.RequestAuthCache.java

private void doPreemptiveAuth(final HttpHost host, final AuthScheme authScheme, final AuthState authState,
        final CredentialsProvider credsProvider) {
    final String schemeName = authScheme.getSchemeName();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Re-using cached '" + schemeName + "' auth scheme for " + host);
    }//  w ww  .  j a v  a2  s  .c  om

    final AuthScope authScope = new AuthScope(host, AuthScope.ANY_REALM, schemeName);
    final Credentials creds = credsProvider.getCredentials(authScope);

    if (creds != null) {
        if ("BASIC".equalsIgnoreCase(authScheme.getSchemeName())) {
            authState.setState(AuthProtocolState.CHALLENGED);
        } else {
            authState.setState(AuthProtocolState.SUCCESS);
        }
        authState.update(authScheme, creds);
    } else {
        this.log.debug("No credentials for preemptive authentication");
    }
}

From source file:org.datacleaner.user.UserPreferencesImpl.java

@Override
public CloseableHttpClient createHttpClient() {
    final HttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();

    if (isProxyEnabled()) {
        // set up HTTP proxy
        final String proxyHostname = getProxyHostname();
        final int proxyPort = getProxyPort();

        try {// w  w w. j  a va  2s .co m
            final HttpHost proxy = new HttpHost(proxyHostname, proxyPort);
            requestConfigBuilder.setProxy(proxy);

            if (isProxyAuthenticationEnabled()) {
                final AuthScope authScope = new AuthScope(proxyHostname, proxyPort);
                final String proxyUsername = getProxyUsername();
                final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(proxyUsername,
                        getProxyPassword());
                credentialsProvider.setCredentials(authScope, credentials);

                final int backslashIndex = proxyUsername.lastIndexOf('\\');
                final String ntUsername;
                final String ntDomain;
                if (backslashIndex != -1) {
                    ntUsername = proxyUsername.substring(backslashIndex + 1);
                    ntDomain = proxyUsername.substring(0, backslashIndex);
                } else {
                    ntUsername = proxyUsername;
                    ntDomain = System.getProperty("datacleaner.proxy.domain");
                }

                String workstation = System.getProperty("datacleaner.proxy.workstation");
                if (Strings.isNullOrEmpty(workstation)) {
                    String computername = InetAddress.getLocalHost().getHostName();
                    workstation = computername;
                }

                NTCredentials ntCredentials = new NTCredentials(ntUsername, getProxyPassword(), workstation,
                        ntDomain);
                AuthScope ntAuthScope = new AuthScope(proxyHostname, proxyPort, AuthScope.ANY_REALM, "ntlm");
                credentialsProvider.setCredentials(ntAuthScope, ntCredentials);
            }
        } catch (Exception e) {
            // ignore proxy creation and return http client without it
            logger.error("Unexpected error occurred while initializing HTTP proxy", e);
        }
    }

    final RequestConfig requestConfig = requestConfigBuilder.build();
    final CloseableHttpClient httpClient = HttpClients.custom().useSystemProperties()
            .setConnectionManager(connectionManager).setDefaultCredentialsProvider(credentialsProvider)
            .setDefaultRequestConfig(requestConfig).build();
    return httpClient;
}

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

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

    CloseableHttpClient httpClient = 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 = getCredentialsProvider(httpClient).getCredentials(authScope);
    assertEquals("[principal: bar][workstation: " + computername.toUpperCase() + "]", credentials.toString());

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

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

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

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

    CloseableHttpClient httpClient = 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 = getCredentialsProvider(httpClient).getCredentials(authScope);
    assertEquals("[principal: FOO/bar][workstation: " + computername.toUpperCase() + "]",
            credentials.toString().replaceAll("\\\\", "/"));

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

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

From source file:org.eclipse.ecf.provider.filetransfer.httpclient4.HttpClientRetrieveFileTransfer.java

protected void setupAuthentication(String urlString) throws UnsupportedCallbackException, IOException {
    Credentials credentials = null;//from www .  java  2 s. c o m
    if (username == null) {
        credentials = getFileRequestCredentials();
    }

    if (credentials != null && username != null) {
        final AuthScope authScope = new AuthScope(getHostFromURL(urlString), getPortFromURL(urlString),
                AuthScope.ANY_REALM);
        Trace.trace(Activator.PLUGIN_ID, "retrieve credentials=" + credentials); //$NON-NLS-1$
        httpClient.getCredentialsProvider().setCredentials(authScope, credentials);
    }
}

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

@Override
public HttpClient createHttpClient() {
    final ClientConnectionManager connectionManager = new PoolingClientConnectionManager();
    final DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);

    if (isProxyEnabled()) {
        // set up HTTP proxy
        final String proxyHostname = getProxyHostname();
        final int proxyPort = getProxyPort();

        try {//from www.j  ava  2 s  .  c  om
            final HttpHost proxy = new HttpHost(proxyHostname, proxyPort);
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

            if (isProxyAuthenticationEnabled()) {
                final AuthScope authScope = new AuthScope(proxyHostname, proxyPort);
                final String proxyUsername = getProxyUsername();
                final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(proxyUsername,
                        getProxyPassword());
                httpClient.getCredentialsProvider().setCredentials(authScope, credentials);

                final int backslashIndex = proxyUsername.lastIndexOf('\\');
                final String ntUsername;
                final String ntDomain;
                if (backslashIndex != -1) {
                    ntUsername = proxyUsername.substring(backslashIndex + 1);
                    ntDomain = proxyUsername.substring(0, backslashIndex);
                } else {
                    ntUsername = proxyUsername;
                    ntDomain = System.getProperty("datacleaner.proxy.domain");
                }

                String workstation = System.getProperty("datacleaner.proxy.workstation");
                if (Strings.isNullOrEmpty(workstation)) {
                    String computername = InetAddress.getLocalHost().getHostName();
                    workstation = computername;
                }

                NTCredentials ntCredentials = new NTCredentials(ntUsername, getProxyPassword(), workstation,
                        ntDomain);
                AuthScope ntAuthScope = new AuthScope(proxyHostname, proxyPort, AuthScope.ANY_REALM, "ntlm");
                httpClient.getCredentialsProvider().setCredentials(ntAuthScope, ntCredentials);
            }
        } catch (Exception e) {
            // ignore proxy creation and return http client without it
            logger.error("Unexpected error occurred while initializing HTTP proxy", e);
        }
    }

    return httpClient;
}

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

public void testCreateHttpClientWithoutNtCredentials() throws Exception {
    UserPreferencesImpl up = new UserPreferencesImpl(null);
    up.setProxyHostname("host");
    up.setProxyPort(1234);/*w w w . java  2s.  co m*/
    up.setProxyUsername("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: bar][workstation: " + computername.toUpperCase() + "]", credentials.toString());

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

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