private boolean processWWWAuthChallenge(final HttpMethod method)
      throws MalformedChallengeException, AuthenticationException {
    AuthState authstate = method.getHostAuthState();
    Map challenges =
        AuthChallengeParser.parseChallenges(method.getResponseHeaders(WWW_AUTH_CHALLENGE));
    if (challenges.isEmpty()) {
      LOG.debug("Authentication challenge(s) not found");
      return false;
    }
    AuthScheme authscheme = null;
    try {
      authscheme = this.authProcessor.processChallenge(authstate, challenges);
    } catch (AuthChallengeException e) {
      if (LOG.isWarnEnabled()) {
        LOG.warn(e.getMessage());
      }
    }
    if (authscheme == null) {
      return false;
    }
    String host = method.getParams().getVirtualHost();
    if (host == null) {
      host = conn.getHost();
    }
    int port = conn.getPort();
    AuthScope authscope =
        new AuthScope(host, port, authscheme.getRealm(), authscheme.getSchemeName());

    if (LOG.isDebugEnabled()) {
      LOG.debug("Authentication scope: " + authscope);
    }
    if (authstate.isAuthAttempted() && authscheme.isComplete()) {
      // Already tried and failed
      Credentials credentials = promptForCredentials(authscheme, method.getParams(), authscope);
      if (credentials == null) {
        if (LOG.isInfoEnabled()) {
          LOG.info("Failure authenticating with " + authscope);
        }
        return false;
      } else {
        return true;
      }
    } else {
      authstate.setAuthAttempted(true);
      Credentials credentials = this.state.getCredentials(authscope);
      if (credentials == null) {
        credentials = promptForCredentials(authscheme, method.getParams(), authscope);
      }
      if (credentials == null) {
        if (LOG.isInfoEnabled()) {
          LOG.info("No credentials available for " + authscope);
        }
        return false;
      } else {
        return true;
      }
    }
  }
 private void authenticateHost(final HttpMethod method) throws AuthenticationException {
   // Clean up existing authentication headers
   if (!cleanAuthHeaders(method, WWW_AUTH_RESP)) {
     // User defined authentication header(s) present
     return;
   }
   AuthState authstate = method.getHostAuthState();
   AuthScheme authscheme = authstate.getAuthScheme();
   if (authscheme == null) {
     return;
   }
   if (authstate.isAuthRequested() || !authscheme.isConnectionBased()) {
     String host = method.getParams().getVirtualHost();
     if (host == null) {
       host = conn.getHost();
     }
     int port = conn.getPort();
     AuthScope authscope =
         new AuthScope(host, port, authscheme.getRealm(), authscheme.getSchemeName());
     if (LOG.isDebugEnabled()) {
       LOG.debug("Authenticating with " + authscope);
     }
     Credentials credentials = this.state.getCredentials(authscope);
     if (credentials != null) {
       String authstring = authscheme.authenticate(credentials, method);
       if (authstring != null) {
         method.addRequestHeader(new Header(WWW_AUTH_RESP, authstring, true));
       }
     } else {
       if (LOG.isWarnEnabled()) {
         LOG.warn("Required credentials not available for " + authscope);
         if (method.getHostAuthState().isPreemptive()) {
           LOG.warn(
               "Preemptive authentication requested but no default " + "credentials available");
         }
       }
     }
   }
 }