public AuthScheme selectScheme(
final Map challenges,
final HttpResponse response,
final HttpContext context) throws AuthenticationException {
AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute(
HttpClientContext.AUTHSCHEME_REGISTRY);
if (registry == null) {
throw new IllegalStateException("AuthScheme registry not set in HTTP context");
}
HttpParams params = response.getParams();
Collection authPrefs = (Collection) params.getParameter(
HttpClientParams.AUTH_SCHEME_PRIORITY);
if (authPrefs == null || authPrefs.isEmpty()) {
authPrefs = DEFAULT_SCHEME_PRIORITY;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Supported authentication schemes in the order of preference: "
+ authPrefs);
}
AuthScheme authScheme = null;
for (Iterator it = authPrefs.iterator(); it.hasNext(); ) {
String id = (String) it.next();
Header challenge = (Header) challenges.get(id.toLowerCase());
if (challenge != null) {
if (LOG.isDebugEnabled()) {
LOG.debug(id + " authentication scheme selected");
}
try {
authScheme = registry.getAuthScheme(id, params);
} catch (IllegalStateException e) {
throw new AuthenticationException(e.getMessage());
}
break;
} else {