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

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

Introduction

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

Prototype

public AuthScope(final AuthScope authscope) 

Source Link

Document

Creates a copy of the given credentials scope.

Usage

From source file:securitytools.veracode.VeracodeAsyncClient.java

/**
 * Constructs a new asynchronous VeracodeClient using the specified
 * configuration./*from   w w  w. ja va 2 s .c  o m*/
 *
 * @param credentials Credentials used to access Veracode services.   
 * @param clientConfiguration Client configuration for options such as proxy
 * settings, user-agent header, and network timeouts.
 */
public VeracodeAsyncClient(VeracodeCredentials credentials, ClientConfiguration clientConfiguration) {
    this.clientConfiguration = clientConfiguration;
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(TARGET), credentials);

    AuthCache authCache = new BasicAuthCache();
    authCache.put(TARGET, new BasicScheme());

    context = HttpClientContext.create();
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthCache(authCache);

    try {
        client = HttpClientFactory.buildAsync(clientConfiguration);
    } catch (NoSuchAlgorithmException nsae) {
        throw new VeracodeClientException(nsae.getMessage(), nsae);
    }
}

From source file:com.microsoft.azure.hdinsight.spark.common.SparkBatchSubmission.java

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

From source file:de.onyxbits.raccoon.gplay.PlayManager.java

/**
 * create a proxy client/* ww w .  ja v a  2s. c  o m*/
 * 
 * @return either a client or null if none is configured
 * @throws KeyManagementException
 * @throws NumberFormatException
 *           if that port could not be parsed.
 * @throws NoSuchAlgorithmException
 */
private static HttpClient createProxyClient(PlayProfile profile)
        throws KeyManagementException, NoSuchAlgorithmException {
    if (profile.getProxyAddress() == null) {
        return null;
    }

    PoolingClientConnectionManager connManager = new PoolingClientConnectionManager(
            SchemeRegistryFactory.createDefault());
    connManager.setMaxTotal(100);
    connManager.setDefaultMaxPerRoute(30);

    DefaultHttpClient client = new DefaultHttpClient(connManager);
    client.getConnectionManager().getSchemeRegistry().register(Utils.getMockedScheme());
    HttpHost proxy = new HttpHost(profile.getProxyAddress(), profile.getProxyPort());
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    if (profile.getProxyUser() != null && profile.getProxyPassword() != null) {
        client.getCredentialsProvider().setCredentials(new AuthScope(proxy),
                new UsernamePasswordCredentials(profile.getProxyUser(), profile.getProxyPassword()));
    }
    return client;
}

From source file:org.apache.olingo.client.core.http.ProxyWrappingHttpClientFactory.java

@Override
public HttpClient create(final HttpMethod method, final URI uri) {
    // Use wrapped factory to obtain an httpclient instance for given method and uri
    final DefaultHttpClient httpclient = wrapped.create(method, uri);

    final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort());

    // Sets usage of HTTP proxy
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);

    // Sets proxy authentication, if credentials were provided
    if (proxyUsername != null && proxyPassword != null) {
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost),
                new UsernamePasswordCredentials(proxyUsername, proxyPassword));
    }//from ww w  . j  a va 2 s.c  o m

    return httpclient;
}

From source file:com.jaeksoft.searchlib.crawler.web.spider.ProxyHandler.java

public void check(RequestConfig.Builder configBuilder, URI uri, CredentialsProvider credentialsProvider) {
    if (proxy == null || uri == null)
        return;/*from   w  w w . j a  v  a2  s  . c  o m*/
    if (exclusionSet.contains(uri.getHost()))
        return;
    configBuilder.setProxy(proxy);
    if (!StringUtils.isEmpty(username))
        credentialsProvider.setCredentials(new AuthScope(proxy),
                new UsernamePasswordCredentials(username, password));
}

From source file:com.katsu.springframework.security.authentication.dni.HttpDniAuthenticationDao.java

private Collection<? extends GrantedAuthority> doLogin(URL url, String username, String password, String dni)
        throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = null;/* w  ww.  j  a v a2s  .c o m*/
    HttpResponse httpResponse = null;
    HttpEntity entity = null;
    try {
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY),
                new UsernamePasswordCredentials(username, password));
        URIBuilder urib = new URIBuilder(url.toURI().toASCIIString());
        urib.addParameter("dni", dni);
        httpget = new HttpGet(urib.build());
        httpResponse = httpclient.execute(httpget);
        entity = httpResponse.getEntity();

        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            logger.trace(httpResponse.getStatusLine().toString());
            String body = new java.util.Scanner(new InputStreamReader(entity.getContent())).useDelimiter("\\A")
                    .next();
            List<? extends GrantedAuthority> roles = json.deserialize(body, new TypeToken<List<Role>>() {
            }.getType());
            if (roles != null && roles.size() > 0 && roles.get(0).getAuthority() == null) {
                roles = json.deserialize(body, new TypeToken<List<Role1>>() {
                }.getType());
            }
            return roles;
        } else {
            throw new Exception(httpResponse.getStatusLine().getStatusCode() + " Http code response.");
        }
    } catch (Exception e) {
        if (entity != null) {
            logger.error(log(entity.getContent()), e);
        } else {
            logger.error(e);
        }
        throw e;
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

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

private void appendAuthentication(Credentials credentials, String authPolicy, SolrServer solrServer) {
    if (assertSolrServerInstance(solrServer)) {
        HttpSolrServer httpSolrServer = (HttpSolrServer) solrServer;

        if (credentials != null && StringUtils.isNotBlank(authPolicy)
                && assertHttpClientInstance(httpSolrServer.getHttpClient())) {
            AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrServer.getHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
        }//ww w .j a va 2  s .c om
    }
}

From source file:org.springframework.data.solr.server.support.HttpSolrServerFactory.java

private void appendAuthentication(Credentials credentials, String authPolicy, SolrServer solrServer) {
    if (isHttpSolrServer(solrServer)) {
        HttpSolrServer httpSolrServer = (HttpSolrServer) solrServer;

        if (credentials != null && StringUtils.isNotBlank(authPolicy)
                && assertHttpClientInstance(httpSolrServer.getHttpClient())) {
            AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrServer.getHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
        }//from www .j a  va  2s  . c  o  m
    }
}

From source file:com.frank.search.solr.server.support.HttpSolrClientFactory.java

private void appendAuthentication(Credentials credentials, String authPolicy, SolrClient solrClient) {
    if (isHttpSolrClient(solrClient)) {
        HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient;

        if (credentials != null && StringUtils.isNotBlank(authPolicy)
                && assertHttpClientInstance(httpSolrClient.getHttpClient())) {
            AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrClient.getHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
            httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
        }/*from w w w .  j av a  2  s  .c om*/
    }
}

From source file:ch.sbb.releasetrain.utils.http.HttpUtilImpl.java

/**
 * authenticates the context if user and password are set
 *///from  w w  w.  ja v a2  s. c o  m
private HttpClientContext initAuthIfNeeded(String url) {

    HttpClientContext context = HttpClientContext.create();
    if (this.user.isEmpty() || this.password.isEmpty()) {
        log.debug(
                "http connection without autentication, because no user / password ist known to HttpUtilImpl ...");
        return context;
    }

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(user, password));
    URL url4Host;
    try {
        url4Host = new URL(url);
    } catch (MalformedURLException e) {
        log.error(e.getMessage(), e);
        return context;
    }

    HttpHost targetHost = new HttpHost(url4Host.getHost(), url4Host.getPort(), url4Host.getProtocol());
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);
    context.setCredentialsProvider(credsProvider);
    context.setAuthCache(authCache);
    return context;
}