Example usage for org.apache.commons.httpclient.auth AuthChallengeParser parseChallenges

List of usage examples for org.apache.commons.httpclient.auth AuthChallengeParser parseChallenges

Introduction

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

Prototype

public static Map parseChallenges(Header[] paramArrayOfHeader) throws MalformedChallengeException 

Source Link

Usage

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

/**
 * @param method Method that got a 401.//w  w  w  . j a va  2s .c om
 * @param curi CrawlURI that got a 401.
 * @return Returns first wholesome authscheme found else null.
 */
protected AuthScheme getAuthScheme(final HttpMethod method, final CrawlURI curi) {
    Header[] headers = method.getResponseHeaders("WWW-Authenticate");
    if (headers == null || headers.length <= 0) {
        logger.info("We got a 401 but no WWW-Authenticate challenge: " + curi.toString());
        return null;
    }

    Map authschemes = null;
    try {
        authschemes = AuthChallengeParser.parseChallenges(headers);
    } catch (MalformedChallengeException e) {
        logger.info("Failed challenge parse: " + e.getMessage());
    }
    if (authschemes == null || authschemes.size() <= 0) {
        logger.info("We got a 401 and WWW-Authenticate challenge" + " but failed parse of the header "
                + curi.toString());
        return null;
    }

    AuthScheme result = null;
    // Use the first auth found.
    for (Iterator i = authschemes.keySet().iterator(); result == null && i.hasNext();) {
        String key = (String) i.next();
        String challenge = (String) authschemes.get(key);
        if (key == null || key.length() <= 0 || challenge == null || challenge.length() <= 0) {
            logger.warning("Empty scheme: " + curi.toString() + ": " + headers);
        }
        AuthScheme authscheme = null;
        if (key.equals("basic")) {
            authscheme = new BasicScheme();
        } else if (key.equals("digest")) {
            authscheme = new DigestScheme();
        } else {
            logger.info("Unsupported scheme: " + key);
            continue;
        }

        try {
            authscheme.processChallenge(challenge);
        } catch (MalformedChallengeException e) {
            logger.info(e.getMessage() + " " + curi + " " + headers);
            continue;
        }
        if (authscheme.isConnectionBased()) {
            logger.info("Connection based " + authscheme);
            continue;
        }

        if (authscheme.getRealm() == null || authscheme.getRealm().length() <= 0) {
            logger.info("Empty realm " + authscheme + " for " + curi);
            continue;
        }
        result = authscheme;
    }

    return result;
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * @param method Method that got a 401./*from w  w  w.  jav  a2s  . co  m*/
 * @param curi CrawlURI that got a 401.
 * @return Returns first wholesome authscheme found else null.
 */
protected AuthScheme getAuthScheme(final HttpMethod method, final CrawlURI curi) {
    Header[] headers = method.getResponseHeaders("WWW-Authenticate");
    if (headers == null || headers.length <= 0) {
        logger.info("We got a 401 but no WWW-Authenticate challenge: " + curi.toString());
        return null;
    }

    Map authschemes = null;
    try {
        authschemes = AuthChallengeParser.parseChallenges(headers);
    } catch (MalformedChallengeException e) {
        logger.info("Failed challenge parse: " + e.getMessage());
    }
    if (authschemes == null || authschemes.size() <= 0) {
        logger.info("We got a 401 and WWW-Authenticate challenge" + " but failed parse of the header "
                + curi.toString());
        return null;
    }

    AuthScheme result = null;
    // Use the first auth found.
    for (Iterator i = authschemes.keySet().iterator(); result == null && i.hasNext();) {
        String key = (String) i.next();
        String challenge = (String) authschemes.get(key);
        if (key == null || key.length() <= 0 || challenge == null || challenge.length() <= 0) {
            logger.warn("Empty scheme: " + curi.toString() + ": " + headers);
        }
        AuthScheme authscheme = null;
        if (key.equals("basic")) {
            authscheme = new BasicScheme();
        } else if (key.equals("digest")) {
            authscheme = new DigestScheme();
        } else {
            logger.info("Unsupported scheme: " + key);
            continue;
        }

        try {
            authscheme.processChallenge(challenge);
        } catch (MalformedChallengeException e) {
            logger.info(e.getMessage() + " " + curi + " " + headers);
            continue;
        }
        if (authscheme.isConnectionBased()) {
            logger.info("Connection based " + authscheme);
            continue;
        }

        if (authscheme.getRealm() == null || authscheme.getRealm().length() <= 0) {
            logger.info("Empty realm " + authscheme + " for " + curi);
            continue;
        }
        result = authscheme;
    }

    return result;
}