Example usage for org.apache.commons.httpclient.auth AuthPolicy AUTH_SCHEME_PRIORITY

List of usage examples for org.apache.commons.httpclient.auth AuthPolicy AUTH_SCHEME_PRIORITY

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.auth AuthPolicy AUTH_SCHEME_PRIORITY.

Prototype

String AUTH_SCHEME_PRIORITY

To view the source code for org.apache.commons.httpclient.auth AuthPolicy AUTH_SCHEME_PRIORITY.

Click Source Link

Usage

From source file:com.zimbra.cs.fb.ExchangeFreeBusyProvider.java

private boolean basicAuth(HttpClient client, ServerInfo info) {
    HttpState state = new HttpState();
    Credentials cred = new UsernamePasswordCredentials(info.authUsername, info.authPassword);
    state.setCredentials(AuthScope.ANY, cred);
    client.setState(state);//from w ww. j  a  v a 2s  .c om
    ArrayList<String> authPrefs = new ArrayList<String>();
    authPrefs.add(AuthPolicy.BASIC);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    return true;
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Test if NTLM auth scheme is enabled./*w  w  w  .ja  va  2 s.  c o  m*/
 *
 * @param httpClient HttpClient instance
 * @return true if NTLM is enabled
 */
public static boolean hasNTLM(HttpClient httpClient) {
    Object authPrefs = httpClient.getParams().getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY);
    return authPrefs == null
            || (authPrefs instanceof List<?> && ((Collection) authPrefs).contains(AuthPolicy.NTLM));
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Enable NTLM authentication on http client
 *
 * @param httpClient HttpClient instance
 *//*  w  w w. ja  va  2s .c  o  m*/
public static void addNTLM(HttpClient httpClient) {
    // disable preemptive authentication
    httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, false);

    // register the jcifs based NTLMv2 implementation
    AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, NTLMv2Scheme.class);

    ArrayList<String> authPrefs = new ArrayList<String>();
    authPrefs.add(AuthPolicy.NTLM);
    authPrefs.add(AuthPolicy.DIGEST);
    authPrefs.add(AuthPolicy.BASIC);
    httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    // separate domain from username in credentials
    AuthScope authScope = new AuthScope(null, -1);
    NTCredentials credentials = (NTCredentials) httpClient.getState().getCredentials(authScope);
    String userName = credentials.getUserName();
    int backSlashIndex = userName.indexOf('\\');
    if (backSlashIndex >= 0) {
        String domain = userName.substring(0, backSlashIndex);
        userName = userName.substring(backSlashIndex + 1);
        credentials = new NTCredentials(userName, credentials.getPassword(), "UNKNOWN", domain);
        httpClient.getState().setCredentials(authScope, credentials);
    }

    // make sure NTLM is always active
    needNTLM = true;
}

From source file:no.sws.client.SwsClient.java

/**
 * Initialize a new HttpClient, storing the credentials for the client
 *
 * @param username The username to log on to SendRegning Web Services
 * @param password The password to log on to SendRegning Web Services
 * @return//from w w w. ja  va2 s  .  com
 */
private HttpClient initHttpClient(final String username, final String password) {

    HttpClient result = new HttpClient();

    List<String> authPrefs = new ArrayList<String>(1);
    authPrefs.add(AuthPolicy.BASIC);
    // This will exclude the NTLM and DIGEST authentication scheme
    result.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    // Preemptive authentication can be enabled within HttpClient. In this mode HttpClient will send the basic authentication response even before the
    // server gives an unauthorized response in certain situations, thus reducing the overhead of making the connection.
    result.getParams().setAuthenticationPreemptive(true);

    // set credentials
    result.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

    return result;
}

From source file:org.abstracthorizon.proximity.storage.remote.CommonsHttpClientRemotePeer.java

/**
 * Gets the http client./*from   w ww . j  a v  a  2 s . c  o  m*/
 * 
 * @return the http client
 */
public HttpClient getHttpClient() {
    if (httpClient == null) {
        logger.info("Creating CommonsHttpClient instance");
        httpRetryHandler = new DefaultHttpMethodRetryHandler(retrievalRetryCount, true);
        httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        httpClient.getParams().setConnectionManagerTimeout(getConnectionTimeout());

        httpConfiguration = httpClient.getHostConfiguration();

        // BASIC and DIGEST auth only
        if (getUsername() != null) {

            List authPrefs = new ArrayList(2);
            authPrefs.add(AuthPolicy.DIGEST);
            authPrefs.add(AuthPolicy.BASIC);

            if (getNtlmDomain() != null) {
                // Using NTLM auth, adding it as first in policies
                authPrefs.add(0, AuthPolicy.NTLM);

                logger.info("... authentication setup for NTLM domain {}, username {}", getNtlmDomain(),
                        getUsername());
                httpConfiguration.setHost(getNtlmHost());

                httpClient.getState().setCredentials(AuthScope.ANY,
                        new NTCredentials(getUsername(), getPassword(), getNtlmHost(), getNtlmDomain()));
            } else {

                // Using Username/Pwd auth, will not add NTLM
                logger.info("... setting authentication setup for remote peer {}, with username {}",
                        getRemoteUrl(), getUsername());

                httpClient.getState().setCredentials(AuthScope.ANY,
                        new UsernamePasswordCredentials(getUsername(), getPassword()));

            }
            httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
        }

        if (getProxyHost() != null) {
            logger.info("... proxy setup with host {}", getProxyHost());
            httpConfiguration.setProxy(getProxyHost(), getProxyPort());

            if (getProxyUsername() != null) {

                List authPrefs = new ArrayList(2);
                authPrefs.add(AuthPolicy.DIGEST);
                authPrefs.add(AuthPolicy.BASIC);

                if (getProxyNtlmDomain() != null) {

                    // Using NTLM auth, adding it as first in policies
                    authPrefs.add(0, AuthPolicy.NTLM);

                    if (getNtlmHost() != null) {
                        logger.warn("... CommonsHttpClient is unable to use NTLM auth scheme\n"
                                + " for BOTH server side and proxy side authentication!\n"
                                + " You MUST reconfigure server side auth and use BASIC/DIGEST scheme\n"
                                + " if you have to use NTLM proxy, since otherwise it will not work!\n"
                                + " *** SERVER SIDE AUTH OVERRIDDEN");
                    }
                    logger.info("... proxy authentication setup for NTLM domain {}, username {}",
                            getProxyNtlmDomain(), getProxyUsername());
                    httpConfiguration.setHost(getProxyNtlmHost());

                    httpClient.getState().setProxyCredentials(AuthScope.ANY, new NTCredentials(
                            getProxyUsername(), getProxyPassword(), getProxyNtlmHost(), getProxyNtlmDomain()));
                } else {

                    // Using Username/Pwd auth, will not add NTLM
                    logger.info("... proxy authentication setup for http proxy {}, username {}", getProxyHost(),
                            getProxyUsername());

                    httpClient.getState().setProxyCredentials(AuthScope.ANY,
                            new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword()));

                }
                httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
            }

        }
    }
    return httpClient;
}

From source file:org.anhonesteffort.flock.webdav.DavClient.java

protected void initClient() {
    HttpClientParams params = new HttpClientParams();
    List<String> authPrefs = new ArrayList<String>(2);

    authPrefs.add(AuthPolicy.DIGEST);/*from   w  w  w  . ja  v a2s. com*/
    authPrefs.add(AuthPolicy.BASIC);
    params.setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    params.setAuthenticationPreemptive(true);

    client = new HttpClient(params);
    hostConfiguration = client.getHostConfiguration();
    connectionManager = client.getHttpConnectionManager();

    hostConfiguration.setHost(davHost.getHost(), davHost.getPort(),
            Protocol.getProtocol(davHost.getProtocol()));

    Credentials credentials = new UsernamePasswordCredentials(davUsername, davPassword);
    client.getState().setCredentials(AuthScope.ANY, credentials);
}

From source file:org.apache.abdera.protocol.client.AbderaClient.java

/**
 * Configure the client to use the default authentication scheme settings
 *//*from w  w  w .jav a2s  . c  o m*/
public AbderaClient setAuthenticationSchemeDefaults() {
    List authPrefs = AuthPolicy.getDefaultAuthPrefs();
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    return this;
}

From source file:org.apache.abdera.protocol.client.AbderaClient.java

/**
 * When multiple authentication schemes are supported by a server, the client will automatically select a scheme
 * based on the configured priority. For instance, to tell the client to prefer "digest" over "basic", set the
 * priority by calling setAuthenticationSchemePriority("digest","basic")
 *//*  w  ww .  j a  v a 2s  .c  o  m*/
public AbderaClient setAuthenticationSchemePriority(String... scheme) {
    List authPrefs = java.util.Arrays.asList(scheme);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
    return this;
}

From source file:org.apache.abdera.protocol.client.AbderaClient.java

/**
 * Returns the current listing of preferred authentication schemes, in order of preference
 * //from   w ww.  ja v a 2  s .c  o  m
 * @see setAuthenticationSchemePriority
 */
public String[] getAuthenticationSchemePriority() {
    List list = (List) client.getParams().getParameter(AuthPolicy.AUTH_SCHEME_PRIORITY);
    return (String[]) list.toArray(new String[list.size()]);
}

From source file:org.apache.axis2.transport.http.AbstractHTTPSender.java

protected void setAuthenticationInfo(HttpClient agent, MessageContext msgCtx, HostConfiguration config)
        throws AxisFault {
    HttpTransportProperties.Authenticator authenticator;
    Object obj = msgCtx.getProperty(HTTPConstants.AUTHENTICATE);
    if (obj != null) {
        if (obj instanceof HttpTransportProperties.Authenticator) {
            authenticator = (HttpTransportProperties.Authenticator) obj;

            String username = authenticator.getUsername();
            String password = authenticator.getPassword();
            String host = authenticator.getHost();
            String domain = authenticator.getDomain();

            int port = authenticator.getPort();
            String realm = authenticator.getRealm();

            /* If retrying is available set it first */
            isAllowedRetry = authenticator.isAllowedRetry();

            Credentials creds;//  w  ww  .j  a v  a  2s  .  c  o m

            HttpState tmpHttpState = null;
            HttpState httpState = (HttpState) msgCtx.getProperty(HTTPConstants.CACHED_HTTP_STATE);
            if (httpState != null) {
                tmpHttpState = httpState;
            } else {
                tmpHttpState = agent.getState();
            }

            agent.getParams().setAuthenticationPreemptive(authenticator.getPreemptiveAuthentication());

            if (host != null) {
                if (domain != null) {
                    /*Credentials for NTLM Authentication*/
                    creds = new NTCredentials(username, password, host, domain);
                } else {
                    /*Credentials for Digest and Basic Authentication*/
                    creds = new UsernamePasswordCredentials(username, password);
                }
                tmpHttpState.setCredentials(new AuthScope(host, port, realm), creds);
            } else {
                if (domain != null) {
                    /*Credentials for NTLM Authentication when host is ANY_HOST*/
                    creds = new NTCredentials(username, password, AuthScope.ANY_HOST, domain);
                    tmpHttpState.setCredentials(new AuthScope(AuthScope.ANY_HOST, port, realm), creds);
                } else {
                    /*Credentials only for Digest and Basic Authentication*/
                    creds = new UsernamePasswordCredentials(username, password);
                    tmpHttpState.setCredentials(new AuthScope(AuthScope.ANY), creds);
                }
            }
            /* Customizing the priority Order */
            List schemes = authenticator.getAuthSchemes();
            if (schemes != null && schemes.size() > 0) {
                List authPrefs = new ArrayList(3);
                for (int i = 0; i < schemes.size(); i++) {
                    if (schemes.get(i) instanceof AuthPolicy) {
                        authPrefs.add(schemes.get(i));
                        continue;
                    }
                    String scheme = (String) schemes.get(i);
                    if (HttpTransportProperties.Authenticator.BASIC.equals(scheme)) {
                        authPrefs.add(AuthPolicy.BASIC);
                    } else if (HttpTransportProperties.Authenticator.NTLM.equals(scheme)) {
                        authPrefs.add(AuthPolicy.NTLM);
                    } else if (HttpTransportProperties.Authenticator.DIGEST.equals(scheme)) {
                        authPrefs.add(AuthPolicy.DIGEST);
                    }
                }
                agent.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
            }

        } else {
            throw new AxisFault("HttpTransportProperties.Authenticator class cast exception");
        }
    }

}