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 String host, final int port) 

Source Link

Document

Creates a new credentials scope for the given host, port, any realm name, and any authentication scheme.

Usage

From source file:eu.esdihumboldt.util.http.client.ClientProxyUtil.java

/**
 * Set-up the given HTTP client to use the given proxy
 * //from ww  w . j a  va  2s  .  com
 * @param builder the HTTP client builder
 * @param proxy the proxy
 * @return the client builder adapted with the proxy settings
 */
public static HttpClientBuilder applyProxy(HttpClientBuilder builder, Proxy proxy) {
    ProxyUtil.init();

    // check if proxy shall be used
    if (proxy != null && proxy.type() == Type.HTTP) {
        InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();

        // set the proxy
        HttpHost proxyHost = new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort());
        builder = builder.setProxy(proxyHost);

        String user = System.getProperty("http.proxyUser"); //$NON-NLS-1$
        String password = System.getProperty("http.proxyPassword"); //$NON-NLS-1$
        boolean useProxyAuth = user != null && !user.isEmpty();

        if (useProxyAuth) {
            // set the proxy credentials
            CredentialsProvider credsProvider = new BasicCredentialsProvider();

            credsProvider.setCredentials(new AuthScope(proxyAddress.getHostName(), proxyAddress.getPort()),
                    createCredentials(user, password));
            builder = builder.setDefaultCredentialsProvider(credsProvider)
                    .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
        }

        _log.trace("Set proxy to " + proxyAddress.getHostName() + ":" + //$NON-NLS-1$ //$NON-NLS-2$
                proxyAddress.getPort() + ((useProxyAuth) ? (" as user " + user) : (""))); //$NON-NLS-1$ //$NON-NLS-2$
    }

    return builder;
}

From source file:at.bitfire.davdroid.webdav.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//from   w  w w  .j av  a  2s.c o  m
    }
}

From source file:com.xrl.chexian.http.CheXianHttpApiV1.java

/**
 * //  ww w.  java 2 s  . co m
 * @param domain
 * @param port
 * @param clientVersion
 */
public CheXianHttpApiV1(String domain, int port, String clientVersion) {
    mApiBaseUrl = "http://" + domain + ":" + port;
    mAuthScope = new AuthScope(domain, port);

    mHttpApi = new HttpApiWithOAuth(mHttpClient, clientVersion);
}

From source file:com.liferay.portal.search.solr.interceptor.PreemptiveAuthInterceptor.java

@Override
public void process(HttpRequest request, HttpContext httpContext) {
    AuthState authState = (AuthState) httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);

    if (authState.getAuthScheme() != null) {
        return;/*  www  .  jav  a2s.  c o m*/
    }

    CredentialsProvider credentialsProvider = (CredentialsProvider) httpContext
            .getAttribute(ClientContext.CREDS_PROVIDER);

    HttpHost targetHttpHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    AuthScope authScope = new AuthScope(targetHttpHost.getHostName(), targetHttpHost.getPort());

    Credentials credentials = credentialsProvider.getCredentials(authScope);

    if (credentials != null) {
        authState.update(new BasicScheme(), credentials);
    }
}

From source file:com.moarub.kipptapi.KipptAPIToken.java

@Override
protected StringBuilder doInBackground(String... u) {
    String username = u[0];/*from  w  w w  .  j  a v a 2s .  co m*/
    String password = u[1];
    DefaultHttpClient client = new DefaultHttpClient();
    Credentials creds = new UsernamePasswordCredentials(username, password);
    client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            creds);

    try {
        HttpGet request = new HttpGet(reqTokenUrl);

        HttpResponse response = client.execute(request);
        return ShareMoreUtils.getResponseString(response);
    } catch (ClientProtocolException e) {
        Log.d("ApiTokenFailure", "Can't fetch API Token " + e.getMessage());
        return null;
    } catch (IOException e) {
        Log.d("ApiTokenFailure", "Can't fetch API Token " + e.getMessage());
        return null;
    }
}

From source file:lyrics.crawler.webClient.ContentDownloader.java

public void setupProxyWithCredentials(String proxyHostname, int proxyPort, String username, String password) {
    setupProxy(proxyHostname, proxyPort);

    client.getCredentialsProvider().setCredentials(new AuthScope(proxyHostname, proxyPort),
            new UsernamePasswordCredentials(username, password));
}

From source file:org.dataconservancy.archive.impl.fcrepo.ri.RIClient.java

public RIClient(FedoraCredentials credentials, HttpClientConfig httpClientConfig) {
    this.riEndpoint = credentials.getBaseUrl() + "/risearch";
    this.httpClient = new MultiThreadedHttpClient(httpClientConfig);
    httpClient.getCredentialsProvider().setCredentials(
            new AuthScope(credentials.getBaseUrl().getHost(), AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword()));
}

From source file:org.torquebox.integration.arquillian.rack.AuthTest.java

@Test
public void testBasicAuth() {

    String creds = "bmcwhirt@redhat.com:swordfish";
    String encodedCreds = Base64.encodeBytes(creds.getBytes());

    AuthScope authScope = new AuthScope("localhost", 8080);
    Credentials credentials = new UsernamePasswordCredentials("bmcwhirt@redhat.com", "swordfish");
    driver.setCredentials(authScope, credentials);

    driver.get("http://localhost:8080/basic-auth");

    WebElement element = driver.findElementById("auth_header");
    assertNotNull(element);//from   w  w w. jav a2 s .c  om
    assertEquals("Basic " + encodedCreds, element.getText().trim());
}

From source file:edu.harvard.iq.dataverse.DataCiteRESTfullClient.java

public DataCiteRESTfullClient(String url, String username, String password) throws IOException {
    this.url = url;
    try {//from www. j a  v  a 2 s. c  o m
        context = HttpClientContext.create();
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(null, -1),
                new UsernamePasswordCredentials(username, password));
        context.setCredentialsProvider(credsProvider);

        httpClient = HttpClients.createDefault();
    } catch (Exception ioe) {
        close();
        logger.log(Level.SEVERE, "Fail to init Client", ioe);
        throw new RuntimeException("Fail to init Client", ioe);
    }
}

From source file:cn.com.loopj.android.http.PreemptiveAuthorizationHttpRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//  ww  w. j a  va 2  s . c o  m
    }
}