michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.impl.client;
michael@0:
michael@0: import java.util.Arrays;
michael@0: import java.util.Collection;
michael@0: import java.util.Collections;
michael@0: import java.util.HashMap;
michael@0: import java.util.List;
michael@0: import java.util.Locale;
michael@0: import java.util.Map;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
michael@0: /* LogFactory removed by HttpClient for Android script. */
michael@0: import ch.boye.httpclientandroidlib.FormattedHeader;
michael@0: import ch.boye.httpclientandroidlib.Header;
michael@0: import ch.boye.httpclientandroidlib.HttpResponse;
michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0: import ch.boye.httpclientandroidlib.auth.AuthScheme;
michael@0: import ch.boye.httpclientandroidlib.auth.AuthSchemeRegistry;
michael@0: import ch.boye.httpclientandroidlib.auth.AuthenticationException;
michael@0: import ch.boye.httpclientandroidlib.auth.MalformedChallengeException;
michael@0: import ch.boye.httpclientandroidlib.client.AuthenticationHandler;
michael@0: import ch.boye.httpclientandroidlib.client.params.AuthPolicy;
michael@0: import ch.boye.httpclientandroidlib.client.protocol.ClientContext;
michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP;
michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext;
michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0:
michael@0: /**
michael@0: * Base class for {@link AuthenticationHandler} implementations.
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @Immutable
michael@0: public abstract class AbstractAuthenticationHandler implements AuthenticationHandler {
michael@0:
michael@0: public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
michael@0:
michael@0: private static final List DEFAULT_SCHEME_PRIORITY =
michael@0: Collections.unmodifiableList(Arrays.asList(new String[] {
michael@0: AuthPolicy.SPNEGO,
michael@0: AuthPolicy.NTLM,
michael@0: AuthPolicy.DIGEST,
michael@0: AuthPolicy.BASIC
michael@0: }));
michael@0:
michael@0: public AbstractAuthenticationHandler() {
michael@0: super();
michael@0: }
michael@0:
michael@0: protected Map parseChallenges(
michael@0: final Header[] headers) throws MalformedChallengeException {
michael@0:
michael@0: Map map = new HashMap(headers.length);
michael@0: for (Header header : headers) {
michael@0: CharArrayBuffer buffer;
michael@0: int pos;
michael@0: if (header instanceof FormattedHeader) {
michael@0: buffer = ((FormattedHeader) header).getBuffer();
michael@0: pos = ((FormattedHeader) header).getValuePos();
michael@0: } else {
michael@0: String s = header.getValue();
michael@0: if (s == null) {
michael@0: throw new MalformedChallengeException("Header value is null");
michael@0: }
michael@0: buffer = new CharArrayBuffer(s.length());
michael@0: buffer.append(s);
michael@0: pos = 0;
michael@0: }
michael@0: while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
michael@0: pos++;
michael@0: }
michael@0: int beginIndex = pos;
michael@0: while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
michael@0: pos++;
michael@0: }
michael@0: int endIndex = pos;
michael@0: String s = buffer.substring(beginIndex, endIndex);
michael@0: map.put(s.toLowerCase(Locale.ENGLISH), header);
michael@0: }
michael@0: return map;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns default list of auth scheme names in their order of preference.
michael@0: *
michael@0: * @return list of auth scheme names
michael@0: */
michael@0: protected List getAuthPreferences() {
michael@0: return DEFAULT_SCHEME_PRIORITY;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns default list of auth scheme names in their order of preference
michael@0: * based on the HTTP response and the current execution context.
michael@0: *
michael@0: * @param response HTTP response.
michael@0: * @param context HTTP execution context.
michael@0: *
michael@0: * @since 4.1
michael@0: */
michael@0: protected List getAuthPreferences(
michael@0: final HttpResponse response,
michael@0: final HttpContext context) {
michael@0: return getAuthPreferences();
michael@0: }
michael@0:
michael@0: public AuthScheme selectScheme(
michael@0: final Map challenges,
michael@0: final HttpResponse response,
michael@0: final HttpContext context) throws AuthenticationException {
michael@0:
michael@0: AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute(
michael@0: ClientContext.AUTHSCHEME_REGISTRY);
michael@0: if (registry == null) {
michael@0: throw new IllegalStateException("AuthScheme registry not set in HTTP context");
michael@0: }
michael@0:
michael@0: Collection authPrefs = getAuthPreferences(response, context);
michael@0: if (authPrefs == null) {
michael@0: authPrefs = DEFAULT_SCHEME_PRIORITY;
michael@0: }
michael@0:
michael@0: if (this.log.isDebugEnabled()) {
michael@0: this.log.debug("Authentication schemes in the order of preference: "
michael@0: + authPrefs);
michael@0: }
michael@0:
michael@0: AuthScheme authScheme = null;
michael@0: for (String id: authPrefs) {
michael@0: Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
michael@0:
michael@0: if (challenge != null) {
michael@0: if (this.log.isDebugEnabled()) {
michael@0: this.log.debug(id + " authentication scheme selected");
michael@0: }
michael@0: try {
michael@0: authScheme = registry.getAuthScheme(id, response.getParams());
michael@0: break;
michael@0: } catch (IllegalStateException e) {
michael@0: if (this.log.isWarnEnabled()) {
michael@0: this.log.warn("Authentication scheme " + id + " not supported");
michael@0: // Try again
michael@0: }
michael@0: }
michael@0: } else {
michael@0: if (this.log.isDebugEnabled()) {
michael@0: this.log.debug("Challenge for " + id + " authentication scheme not available");
michael@0: // Try again
michael@0: }
michael@0: }
michael@0: }
michael@0: if (authScheme == null) {
michael@0: // If none selected, something is wrong
michael@0: throw new AuthenticationException(
michael@0: "Unable to respond to any of these challenges: "
michael@0: + challenges);
michael@0: }
michael@0: return authScheme;
michael@0: }
michael@0:
michael@0: }