Example usage for org.apache.commons.httpclient.auth AuthScheme authenticate

List of usage examples for org.apache.commons.httpclient.auth AuthScheme authenticate

Introduction

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

Prototype

public abstract String authenticate(Credentials paramCredentials, String paramString1, String paramString2)
            throws AuthenticationException;

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.j  av a  2s  .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();
    }

}