Home | History | Annotate | Download | only in client
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/AbstractAuthenticationHandler.java $
      3  * $Revision: 673450 $
      4  * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.impl.client;
     33 
     34 import java.util.Arrays;
     35 import java.util.HashMap;
     36 import java.util.List;
     37 import java.util.Locale;
     38 import java.util.Map;
     39 
     40 import org.apache.commons.logging.Log;
     41 import org.apache.commons.logging.LogFactory;
     42 import org.apache.http.FormattedHeader;
     43 import org.apache.http.Header;
     44 import org.apache.http.HttpResponse;
     45 import org.apache.http.auth.AuthScheme;
     46 import org.apache.http.auth.AuthSchemeRegistry;
     47 import org.apache.http.auth.AuthenticationException;
     48 import org.apache.http.auth.MalformedChallengeException;
     49 import org.apache.http.client.AuthenticationHandler;
     50 import org.apache.http.client.protocol.ClientContext;
     51 import org.apache.http.protocol.HTTP;
     52 import org.apache.http.protocol.HttpContext;
     53 import org.apache.http.util.CharArrayBuffer;
     54 
     55 /**
     56  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     57  *
     58  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     59  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     60  *     for further details.
     61  */
     62 @Deprecated
     63 public abstract class AbstractAuthenticationHandler implements AuthenticationHandler {
     64 
     65     private final Log log = LogFactory.getLog(getClass());
     66 
     67     private static final List<String> DEFAULT_SCHEME_PRIORITY = Arrays.asList(new String[] {
     68             "ntlm",
     69             "digest",
     70             "basic"
     71     });
     72 
     73     public AbstractAuthenticationHandler() {
     74         super();
     75     }
     76 
     77     protected Map<String, Header> parseChallenges(
     78             final Header[] headers) throws MalformedChallengeException {
     79 
     80         Map<String, Header> map = new HashMap<String, Header>(headers.length);
     81         for (Header header : headers) {
     82             CharArrayBuffer buffer;
     83             int pos;
     84             if (header instanceof FormattedHeader) {
     85                 buffer = ((FormattedHeader) header).getBuffer();
     86                 pos = ((FormattedHeader) header).getValuePos();
     87             } else {
     88                 String s = header.getValue();
     89                 if (s == null) {
     90                     throw new MalformedChallengeException("Header value is null");
     91                 }
     92                 buffer = new CharArrayBuffer(s.length());
     93                 buffer.append(s);
     94                 pos = 0;
     95             }
     96             while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
     97                 pos++;
     98             }
     99             int beginIndex = pos;
    100             while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
    101                 pos++;
    102             }
    103             int endIndex = pos;
    104             String s = buffer.substring(beginIndex, endIndex);
    105             map.put(s.toLowerCase(Locale.ENGLISH), header);
    106         }
    107         return map;
    108     }
    109 
    110     protected List<String> getAuthPreferences() {
    111         return DEFAULT_SCHEME_PRIORITY;
    112     }
    113 
    114     public AuthScheme selectScheme(
    115             final Map<String, Header> challenges,
    116             final HttpResponse response,
    117             final HttpContext context) throws AuthenticationException {
    118 
    119         AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute(
    120                 ClientContext.AUTHSCHEME_REGISTRY);
    121         if (registry == null) {
    122             throw new IllegalStateException("AuthScheme registry not set in HTTP context");
    123         }
    124 
    125         List<?> authPrefs = (List<?>) context.getAttribute(
    126                 ClientContext.AUTH_SCHEME_PREF);
    127         if (authPrefs == null) {
    128             authPrefs = getAuthPreferences();
    129         }
    130 
    131         if (this.log.isDebugEnabled()) {
    132             this.log.debug("Authentication schemes in the order of preference: "
    133                 + authPrefs);
    134         }
    135 
    136         AuthScheme authScheme = null;
    137         for (int i = 0; i < authPrefs.size(); i++) {
    138             String id = (String) authPrefs.get(i);
    139             Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
    140 
    141             if (challenge != null) {
    142                 if (this.log.isDebugEnabled()) {
    143                     this.log.debug(id + " authentication scheme selected");
    144                 }
    145                 try {
    146                     authScheme = registry.getAuthScheme(id, response.getParams());
    147                     break;
    148                 } catch (IllegalStateException e) {
    149                     if (this.log.isWarnEnabled()) {
    150                         this.log.warn("Authentication scheme " + id + " not supported");
    151                         // Try again
    152                     }
    153                 }
    154             } else {
    155                 if (this.log.isDebugEnabled()) {
    156                     this.log.debug("Challenge for " + id + " authentication scheme not available");
    157                     // Try again
    158                 }
    159             }
    160         }
    161         if (authScheme == null) {
    162             // If none selected, something is wrong
    163             throw new AuthenticationException(
    164                 "Unable to respond to any of these challenges: "
    165                     + challenges);
    166         }
    167         return authScheme;
    168     }
    169 
    170 }
    171