Example usage for org.apache.http.auth AuthState getAuthScheme

List of usage examples for org.apache.http.auth AuthState getAuthScheme

Introduction

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

Prototype

public AuthScheme getAuthScheme() 

Source Link

Document

Returns actual AuthScheme .

Usage

From source file:com.dlmu.heipacker.crawler.client.ClientInteractiveAuthentication.java

public static void main(String[] args) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {//from  w  ww. ja  va  2 s  . c  o m
        // Create local execution context
        HttpContext localContext = new BasicHttpContext();

        HttpGet httpget = new HttpGet("http://localhost/test");

        boolean trying = true;
        while (trying) {
            System.out.println("executing request " + httpget.getRequestLine());
            HttpResponse response = httpclient.execute(httpget, localContext);

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());

            // Consume response content
            HttpEntity entity = response.getEntity();
            EntityUtils.consume(entity);

            int sc = response.getStatusLine().getStatusCode();

            AuthState authState = null;
            HttpHost authhost = null;
            if (sc == HttpStatus.SC_UNAUTHORIZED) {
                // Target host authentication required
                authState = (AuthState) localContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
                authhost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
            }
            if (sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
                // Proxy authentication required
                authState = (AuthState) localContext.getAttribute(ClientContext.PROXY_AUTH_STATE);
                authhost = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
            }

            if (authState != null) {
                System.out.println("----------------------------------------");
                AuthScheme authscheme = authState.getAuthScheme();
                System.out.println("Please provide credentials for " + authscheme.getRealm() + "@"
                        + authhost.toHostString());

                BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

                System.out.print("Enter username: ");
                String user = console.readLine();
                System.out.print("Enter password: ");
                String password = console.readLine();

                if (user != null && user.length() > 0) {
                    Credentials creds = new UsernamePasswordCredentials(user, password);
                    httpclient.getCredentialsProvider().setCredentials(new AuthScope(authhost), creds);
                    trying = true;
                } else {
                    trying = false;
                }
            } else {
                trying = false;
            }
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:net.homelinux.penecoptero.android.citybikes.app.RESTHelper.java

private static DefaultHttpClient setCredentials(DefaultHttpClient httpclient, String url, String username,
        String password) throws HttpException, IOException {
    HttpHost targetHost = new HttpHost(url);
    final UsernamePasswordCredentials access = new UsernamePasswordCredentials(username, password);

    httpclient.getCredentialsProvider()//from w  w  w.  jav  a  2s. c om
            .setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), access);

    httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
        @Override
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {

            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            if (authState.getAuthScheme() == null) {
                authState.setAuthScheme(new BasicScheme());
                authState.setCredentials(access);
            }
        }
    }, 0);
    return httpclient;
}

From source file:com.predic8.membrane.test.AssertUtils.java

private static DefaultHttpClient getAuthenticatingHttpClient(String host, int port, String user, String pass) {
    DefaultHttpClient hc = new DefaultHttpClient();
    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
        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.update(new BasicScheme(), creds);
                }// w  ww .  j  a  v a 2s. c  o m
            }
        }
    };
    hc.addRequestInterceptor(preemptiveAuth, 0);
    Credentials defaultcreds = new UsernamePasswordCredentials(user, pass);
    BasicCredentialsProvider bcp = new BasicCredentialsProvider();
    bcp.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
    hc.setCredentialsProvider(bcp);
    hc.setCookieStore(new BasicCookieStore());
    return hc;
}

From source file:org.sonatype.nexus.httpclient.PreemptiveAuthHttpRequestInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    HttpClientContext clientContext = HttpClientContext.adapt(context);
    AuthState authState = clientContext.getTargetAuthState();
    if (authState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        HttpHost targetHost = clientContext.getTargetHost();
        Credentials creds = credsProvider
                .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (creds != null) {
            authState.update(new BasicScheme(), creds);
        }// w  w w.java 2s  .  c o m
    }
}

From source file:org.sonatype.nexus.apachehttpclient.PreemptiveAuthHttpRequestInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    final HttpClientContext clientContext = HttpClientContext.adapt(context);
    final AuthState authState = clientContext.getTargetAuthState();
    if (authState.getAuthScheme() == null) {
        final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
        final HttpHost targetHost = clientContext.getTargetHost();
        final Credentials creds = credsProvider
                .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
        if (creds != null) {
            authState.update(new BasicScheme(), creds);
        }//from   w w  w  .j  ava 2  s.  c  om
    }
}

From source file:kuona.client.PreemptiveAuth.java

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

    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (authScheme != null) {
            Credentials creds = credsProvider
                    .getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*ww w .  j av  a 2  s .com*/
            authState.update(authScheme, creds);
        }
    }
}

From source file:com.github.avarabyeu.restendpoint.http.PreemptiveAuthInterceptor.java

/**
 * Adds provided auth scheme to the client if there are no another provided
 * auth schemes/* w  w  w  . j av  a2 s . c o  m*/
 */
@Override
public final void process(final HttpRequest request, final HttpContext context)
        throws HttpException, IOException {

    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {

        HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
        AuthCache authCache = new BasicAuthCache();
        authCache.put(targetHost, new BasicScheme());
        context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    }
}

From source file:ca.uhn.fhir.rest.client.HttpBasicAuthInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);

    if (authState.getAuthScheme() == null) {
        Credentials creds = new UsernamePasswordCredentials(myUsername, myPassword);
        authState.update(new BasicScheme(), creds);
    }/*from   w ww .j a va 2s.co m*/

}

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;/*from  ww w.j  ava  2 s  .  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:org.jasig.schedassist.impl.caldav.PreemptiveAuthInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute(PREEMPTIVE_AUTH);
        CredentialsProvider credsProvider = (CredentialsProvider) context
                .getAttribute(ClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(caldavAdminAuthScope);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }/*from   w  w w  .j av  a2s .  com*/
            authState.setAuthScheme(authScheme);
            authState.setCredentials(creds);
            if (log.isTraceEnabled()) {
                log.trace("successfully set credentials " + creds + " and authScheme " + authScheme
                        + " for request " + request);
            }
        } else {
            log.warn(PREEMPTIVE_AUTH
                    + " authScheme not found in context, failed to set scheme and credentials for " + request);
        }
    } else {
        log.warn("context's authState attribute (" + authState + ") has non-null AuthScheme for request "
                + request);
    }

}