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

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

Introduction

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

Prototype

public String getScheme() 

Source Link

Usage

From source file:org.jets3t.apps.uploader.Uploader.java

/**
 * Implementation method for the CredentialsProvider interface.
 * <p>//from ww w. j a v a 2 s . c o m
 * Based on sample code:
 * <a href="http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/examples/InteractiveAuthenticationExample.java?view=markup">InteractiveAuthenticationExample</a>
 *
 */
public Credentials getCredentials(AuthScope scope) {
    if (scope == null || scope.getScheme() == null) {
        return null;
    }
    Credentials credentials = mCredentialProvider.getCredentials(scope);
    if (credentials != null) {
        return credentials;
    }

    try {
        if (scope.getScheme().equals("ntlm")) {
            //if (authscheme instanceof NTLMScheme) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html>Host <b>" + scope.getHost() + ":" + scope.getPort()
                            + "</b> requires Windows authentication</html>",
                    true);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new NTCredentials(pwDialog.getUser(), pwDialog.getPassword(), scope.getHost(),
                        pwDialog.getDomain());
            }
            pwDialog.dispose();
        } else if (scope.getScheme().equals("basic") || scope.getScheme().equals("digest")) {
            //if (authscheme instanceof RFC2617Scheme) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html><center>Host <b>" + scope.getHost() + ":" + scope.getPort() + "</b>"
                            + " requires authentication for the realm:<br><b>" + scope.getRealm()
                            + "</b></center></html>",
                    false);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new UsernamePasswordCredentials(pwDialog.getUser(), pwDialog.getPassword());
            }
            pwDialog.dispose();
        } else {
            throw new IllegalArgumentException("Unsupported authentication scheme: " + scope.getScheme());
        }
        if (credentials != null) {
            mCredentialProvider.setCredentials(scope, credentials);
        }
        return credentials;
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}

From source file:org.jets3t.apps.cockpit.Cockpit.java

/**
 * Implementation method for the CredentialsProvider interface.
 * <p>//from   ww  w  .j a  va 2 s.  c  o  m
 * Based on sample code:
 * <a href="http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/examples/InteractiveAuthenticationExample.java?view=markup">InteractiveAuthenticationExample</a>
 *
 */
public Credentials getCredentials(AuthScope scope) {
    if (scope == null || scope.getScheme() == null) {
        return null;
    }
    Credentials credentials = mCredentialProvider.getCredentials(scope);
    if (credentials != null) {
        return credentials;
    }
    try {
        if (scope.getScheme().equals("ntlm")) {
            //if (authscheme instanceof NTLMScheme) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html>Host <b>" + scope.getHost() + ":" + scope.getPort()
                            + "</b> requires Windows authentication</html>",
                    true);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new NTCredentials(pwDialog.getUser(), pwDialog.getPassword(), scope.getHost(),
                        pwDialog.getDomain());
            }
            pwDialog.dispose();
        } else if (scope.getScheme().equals("basic") || scope.getScheme().equals("digest")) {
            //if (authscheme instanceof RFC2617Scheme) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html><center>Host <b>" + scope.getHost() + ":" + scope.getPort() + "</b>"
                            + " requires authentication for the realm:<br><b>" + scope.getRealm()
                            + "</b></center></html>",
                    false);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new UsernamePasswordCredentials(pwDialog.getUser(), pwDialog.getPassword());
            }
            pwDialog.dispose();
        } else {
            throw new IllegalArgumentException("Unsupported authentication scheme: " + scope.getScheme());
        }
        if (credentials != null) {
            mCredentialProvider.setCredentials(scope, credentials);
        }
        return credentials;
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}

From source file:org.apache.http.osgi.impl.OSGiCredentialsProvider.java

/**
 * {@inheritDoc}/*  w  ww . jav a2 s.  com*/
 */
@Override
public Credentials getCredentials(final AuthScope authScope) {
    // iterate over all active proxy configurations at the moment of getting the credential
    for (final ProxyConfiguration config : proxyConfigurations) {
        if (config.isEnabled() && isSuitable(config, authScope)) {
            final String scheme = authScope.getScheme();
            if (BASIC_SCHEME_NAME.equals(scheme)) {
                return new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
            } else if (NTLM_SCHEME_NAME.equals(scheme)) {
                return createNTCredentials(config);
            } else {
                log.debug("credentials requested for unsupported authentication scheme " + scheme);
            }
        }
    }
    // credentials no longer available!
    return null;
}

From source file:org.jets3t.apps.cockpitlite.CockpitLite.java

/**
 * Implementation method for the CredentialsProvider interface.
 * <p>/*from  www  .ja  v  a 2s  .  com*/
 * Based on sample code:
 * <a href="http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/examples/InteractiveAuthenticationExample.java?view=markup">InteractiveAuthenticationExample</a>
 *
 */
public Credentials getCredentials(AuthScope scope) {
    if (scope == null || scope.getScheme() == null) {
        return null;
    }
    Credentials credentials = mCredentialProvider.getCredentials(scope);
    if (credentials != null) {
        return credentials;
    }
    try {
        if (scope.getScheme().equals("ntlm")) {
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html>Host <b>" + scope.getHost() + ":" + scope.getPort()
                            + "</b> requires Windows authentication</html>",
                    true);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new NTCredentials(pwDialog.getUser(), pwDialog.getPassword(), scope.getHost(),
                        pwDialog.getDomain());
            }
            pwDialog.dispose();
        } else if (scope.getScheme().equals("basic") || scope.getScheme().equals("digest")) {
            //authscheme instanceof RFC2617Scheme
            AuthenticationDialog pwDialog = new AuthenticationDialog(ownerFrame, "Authentication Required",
                    "<html><center>Host <b>" + scope.getHost() + ":" + scope.getPort() + "</b>"
                            + " requires authentication for the realm:<br><b>" + scope.getRealm()
                            + "</b></center></html>",
                    false);
            pwDialog.setVisible(true);
            if (pwDialog.getUser().length() > 0) {
                credentials = new UsernamePasswordCredentials(pwDialog.getUser(), pwDialog.getPassword());
            }
            pwDialog.dispose();
        } else {
            throw new InvalidCredentialsException("Unsupported authentication scheme: " + scope.getScheme());
        }
        if (credentials != null) {
            mCredentialProvider.setCredentials(scope, credentials);
        }
        return credentials;
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}