public void authFailed(
      final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final AuthCache authCache = clientContext.getAuthCache();
    if (authCache != null) {
      if (Logger.isLoggable(TAG, Logger.DEBUG)) {
        Logger.d(TAG, "Clearing cached auth scheme for " + authhost);
      }
      authCache.remove(authhost);
    }
  }
 public Map<String, Header> getChallenges(
     final HttpHost authhost, final HttpResponse response, final HttpContext context)
     throws MalformedChallengeException {
   Args.notNull(response, "HTTP response");
   final Header[] headers = response.getHeaders(this.headerName);
   final Map<String, Header> map = new HashMap<String, Header>(headers.length);
   for (final Header header : headers) {
     final CharArrayBuffer buffer;
     int pos;
     if (header instanceof FormattedHeader) {
       buffer = ((FormattedHeader) header).getBuffer();
       pos = ((FormattedHeader) header).getValuePos();
     } else {
       final String s = header.getValue();
       if (s == null) {
         throw new MalformedChallengeException("Header value is null");
       }
       buffer = new CharArrayBuffer(s.length());
       buffer.append(s);
       pos = 0;
     }
     while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
       pos++;
     }
     final int beginIndex = pos;
     while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
       pos++;
     }
     final int endIndex = pos;
     final String s = buffer.substring(beginIndex, endIndex);
     map.put(s.toLowerCase(Locale.ENGLISH), header);
   }
   return map;
 }
  public void authSucceeded(
      final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
    Args.notNull(authhost, "Host");
    Args.notNull(authScheme, "Auth scheme");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    if (isCachable(authScheme)) {
      AuthCache authCache = clientContext.getAuthCache();
      if (authCache == null) {
        authCache = new BasicAuthCache();
        clientContext.setAuthCache(authCache);
      }
      if (Logger.isLoggable(TAG, Logger.DEBUG)) {
        Logger.d(TAG, "Caching '" + authScheme.getSchemeName() + "' auth scheme for " + authhost);
      }
      authCache.put(authhost, authScheme);
    }
  }
 public boolean isAuthenticationRequested(
     final HttpHost authhost, final HttpResponse response, final HttpContext context) {
   Args.notNull(response, "HTTP response");
   final int status = response.getStatusLine().getStatusCode();
   return status == this.challengeCode;
 }
  public Queue<AuthOption> select(
      final Map<String, Header> challenges,
      final HttpHost authhost,
      final HttpResponse response,
      final HttpContext context)
      throws MalformedChallengeException {
    Args.notNull(challenges, "Map of auth challenges");
    Args.notNull(authhost, "Host");
    Args.notNull(response, "HTTP response");
    Args.notNull(context, "HTTP context");
    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final Queue<AuthOption> options = new LinkedList<AuthOption>();
    final Lookup<AuthSchemeProvider> registry = clientContext.getAuthSchemeRegistry();
    if (registry == null) {
      if (Logger.isLoggable(TAG, Logger.DEBUG)) {
        Logger.d(TAG, "Auth scheme registry not set in the context");
      }
      return options;
    }
    final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
    if (credsProvider == null) {
      if (Logger.isLoggable(TAG, Logger.DEBUG)) {
        Logger.d(TAG, "Credentials provider not set in the context");
      }
      return options;
    }
    final RequestConfig config = clientContext.getRequestConfig();
    Collection<String> authPrefs = getPreferredAuthSchemes(config);
    if (authPrefs == null) {
      authPrefs = DEFAULT_SCHEME_PRIORITY;
    }
    if (Logger.isLoggable(TAG, Logger.DEBUG)) {
      Logger.d(TAG, "Authentication schemes in the order of preference: " + authPrefs);
    }

    for (final String id : authPrefs) {
      final Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
      if (challenge != null) {
        final AuthSchemeProvider authSchemeProvider = registry.lookup(id);
        if (authSchemeProvider == null) {
          if (Logger.isLoggable(TAG, Logger.WARN)) {
            Logger.w(TAG, "Authentication scheme " + id + " not supported");
            // Try again
          }
          continue;
        }
        final AuthScheme authScheme = authSchemeProvider.create(context);
        authScheme.processChallenge(challenge);

        final AuthScope authScope =
            new AuthScope(
                authhost.getHostName(),
                authhost.getPort(),
                authScheme.getRealm(),
                authScheme.getSchemeName());

        final Credentials credentials = credsProvider.getCredentials(authScope);
        if (credentials != null) {
          options.add(new AuthOption(authScheme, credentials));
        }
      } else {
        if (Logger.isLoggable(TAG, Logger.DEBUG)) {
          Logger.d(TAG, "Challenge for " + id + " authentication scheme not available");
          // Try again
        }
      }
    }
    return options;
  }