Example usage for org.apache.commons.httpclient.auth DigestScheme DigestScheme

List of usage examples for org.apache.commons.httpclient.auth DigestScheme DigestScheme

Introduction

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

Prototype

public DigestScheme() 

Source Link

Usage

From source file:com.sipresponse.flibblecallmgr.internal.util.AuthenticationHelper.java

public static void processResponseAuthorization(CallManager callMgr, Line line, Response response,
        Request newRequest, boolean forRegister) {
    String headerName = "Authorization";
    FlibbleSipProvider flibbleProvider = InternalCallManager.getInstance().getProvider(callMgr);
    WWWAuthenticateHeader wwwAuthenticateHeader = (WWWAuthenticateHeader) response
            .getHeader(WWWAuthenticateHeader.NAME);
    SIPHeader sipHeader = (SIPHeader) wwwAuthenticateHeader;

    if (sipHeader == null) {
        headerName = "Proxy-Authorization";
        ProxyAuthenticateHeader proxyAuthenticateHeader = (ProxyAuthenticateHeader) response
                .getHeader(ProxyAuthenticateHeader.NAME);
        sipHeader = (SIPHeader) proxyAuthenticateHeader;
    }//  ww  w.  jav a2s.  c o m
    CSeqHeader cseqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
    String method = cseqHeader.getMethod();

    System.out.println("processResponseAuthorization()");
    // Proxy-Authorization header:

    UsernamePasswordCredentials cred = new UsernamePasswordCredentials(line.getUser(), line.getPassword());

    AuthScheme authscheme = new DigestScheme();
    try {
        authscheme.processChallenge(sipHeader.getHeaderValue());
    } catch (MalformedChallengeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String uriString = null;
    //        if (forRegister == true)
    //        {
    //            uriString = "sip:" + line.getHost(); 
    //        }
    //        else
    {
        uriString = newRequest.getRequestURI().toString();
    }
    String responseString = null;
    try {
        responseString = authscheme.authenticate(cred, method, uriString);
    } catch (AuthenticationException e) {
        e.printStackTrace();
    }

    try {
        Header header = flibbleProvider.headerFactory.createHeader(headerName, responseString);
        newRequest.addHeader(header);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

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

/**
 * @param method Method that got a 401./*from w ww.j a  v  a2  s  .c  o  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.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 . java2s  . 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;
}