1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/AbstractAuthenticationHandler.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,188 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.client; 1.32 + 1.33 +import java.util.Arrays; 1.34 +import java.util.Collection; 1.35 +import java.util.Collections; 1.36 +import java.util.HashMap; 1.37 +import java.util.List; 1.38 +import java.util.Locale; 1.39 +import java.util.Map; 1.40 + 1.41 +import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog; 1.42 +/* LogFactory removed by HttpClient for Android script. */ 1.43 +import ch.boye.httpclientandroidlib.FormattedHeader; 1.44 +import ch.boye.httpclientandroidlib.Header; 1.45 +import ch.boye.httpclientandroidlib.HttpResponse; 1.46 +import ch.boye.httpclientandroidlib.annotation.Immutable; 1.47 +import ch.boye.httpclientandroidlib.auth.AuthScheme; 1.48 +import ch.boye.httpclientandroidlib.auth.AuthSchemeRegistry; 1.49 +import ch.boye.httpclientandroidlib.auth.AuthenticationException; 1.50 +import ch.boye.httpclientandroidlib.auth.MalformedChallengeException; 1.51 +import ch.boye.httpclientandroidlib.client.AuthenticationHandler; 1.52 +import ch.boye.httpclientandroidlib.client.params.AuthPolicy; 1.53 +import ch.boye.httpclientandroidlib.client.protocol.ClientContext; 1.54 +import ch.boye.httpclientandroidlib.protocol.HTTP; 1.55 +import ch.boye.httpclientandroidlib.protocol.HttpContext; 1.56 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.57 + 1.58 +/** 1.59 + * Base class for {@link AuthenticationHandler} implementations. 1.60 + * 1.61 + * @since 4.0 1.62 + */ 1.63 +@Immutable 1.64 +public abstract class AbstractAuthenticationHandler implements AuthenticationHandler { 1.65 + 1.66 + public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass()); 1.67 + 1.68 + private static final List<String> DEFAULT_SCHEME_PRIORITY = 1.69 + Collections.unmodifiableList(Arrays.asList(new String[] { 1.70 + AuthPolicy.SPNEGO, 1.71 + AuthPolicy.NTLM, 1.72 + AuthPolicy.DIGEST, 1.73 + AuthPolicy.BASIC 1.74 + })); 1.75 + 1.76 + public AbstractAuthenticationHandler() { 1.77 + super(); 1.78 + } 1.79 + 1.80 + protected Map<String, Header> parseChallenges( 1.81 + final Header[] headers) throws MalformedChallengeException { 1.82 + 1.83 + Map<String, Header> map = new HashMap<String, Header>(headers.length); 1.84 + for (Header header : headers) { 1.85 + CharArrayBuffer buffer; 1.86 + int pos; 1.87 + if (header instanceof FormattedHeader) { 1.88 + buffer = ((FormattedHeader) header).getBuffer(); 1.89 + pos = ((FormattedHeader) header).getValuePos(); 1.90 + } else { 1.91 + String s = header.getValue(); 1.92 + if (s == null) { 1.93 + throw new MalformedChallengeException("Header value is null"); 1.94 + } 1.95 + buffer = new CharArrayBuffer(s.length()); 1.96 + buffer.append(s); 1.97 + pos = 0; 1.98 + } 1.99 + while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) { 1.100 + pos++; 1.101 + } 1.102 + int beginIndex = pos; 1.103 + while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) { 1.104 + pos++; 1.105 + } 1.106 + int endIndex = pos; 1.107 + String s = buffer.substring(beginIndex, endIndex); 1.108 + map.put(s.toLowerCase(Locale.ENGLISH), header); 1.109 + } 1.110 + return map; 1.111 + } 1.112 + 1.113 + /** 1.114 + * Returns default list of auth scheme names in their order of preference. 1.115 + * 1.116 + * @return list of auth scheme names 1.117 + */ 1.118 + protected List<String> getAuthPreferences() { 1.119 + return DEFAULT_SCHEME_PRIORITY; 1.120 + } 1.121 + 1.122 + /** 1.123 + * Returns default list of auth scheme names in their order of preference 1.124 + * based on the HTTP response and the current execution context. 1.125 + * 1.126 + * @param response HTTP response. 1.127 + * @param context HTTP execution context. 1.128 + * 1.129 + * @since 4.1 1.130 + */ 1.131 + protected List<String> getAuthPreferences( 1.132 + final HttpResponse response, 1.133 + final HttpContext context) { 1.134 + return getAuthPreferences(); 1.135 + } 1.136 + 1.137 + public AuthScheme selectScheme( 1.138 + final Map<String, Header> challenges, 1.139 + final HttpResponse response, 1.140 + final HttpContext context) throws AuthenticationException { 1.141 + 1.142 + AuthSchemeRegistry registry = (AuthSchemeRegistry) context.getAttribute( 1.143 + ClientContext.AUTHSCHEME_REGISTRY); 1.144 + if (registry == null) { 1.145 + throw new IllegalStateException("AuthScheme registry not set in HTTP context"); 1.146 + } 1.147 + 1.148 + Collection<String> authPrefs = getAuthPreferences(response, context); 1.149 + if (authPrefs == null) { 1.150 + authPrefs = DEFAULT_SCHEME_PRIORITY; 1.151 + } 1.152 + 1.153 + if (this.log.isDebugEnabled()) { 1.154 + this.log.debug("Authentication schemes in the order of preference: " 1.155 + + authPrefs); 1.156 + } 1.157 + 1.158 + AuthScheme authScheme = null; 1.159 + for (String id: authPrefs) { 1.160 + Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH)); 1.161 + 1.162 + if (challenge != null) { 1.163 + if (this.log.isDebugEnabled()) { 1.164 + this.log.debug(id + " authentication scheme selected"); 1.165 + } 1.166 + try { 1.167 + authScheme = registry.getAuthScheme(id, response.getParams()); 1.168 + break; 1.169 + } catch (IllegalStateException e) { 1.170 + if (this.log.isWarnEnabled()) { 1.171 + this.log.warn("Authentication scheme " + id + " not supported"); 1.172 + // Try again 1.173 + } 1.174 + } 1.175 + } else { 1.176 + if (this.log.isDebugEnabled()) { 1.177 + this.log.debug("Challenge for " + id + " authentication scheme not available"); 1.178 + // Try again 1.179 + } 1.180 + } 1.181 + } 1.182 + if (authScheme == null) { 1.183 + // If none selected, something is wrong 1.184 + throw new AuthenticationException( 1.185 + "Unable to respond to any of these challenges: " 1.186 + + challenges); 1.187 + } 1.188 + return authScheme; 1.189 + } 1.190 + 1.191 +}