1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/auth/RFC2617Scheme.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,118 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * 1.7 + * Licensed to the Apache Software Foundation (ASF) under one or more 1.8 + * contributor license agreements. See the NOTICE file distributed with 1.9 + * this work for additional information regarding copyright ownership. 1.10 + * The ASF licenses this file to You under the Apache License, Version 2.0 1.11 + * (the "License"); you may not use this file except in compliance with 1.12 + * 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, software 1.17 + * distributed under the License is distributed on an "AS IS" BASIS, 1.18 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.19 + * See the License for the specific language governing permissions and 1.20 + * limitations under the License. 1.21 + * ==================================================================== 1.22 + * 1.23 + * This software consists of voluntary contributions made by many 1.24 + * individuals on behalf of the Apache Software Foundation. For more 1.25 + * information on the Apache Software Foundation, please see 1.26 + * <http://www.apache.org/>. 1.27 + * 1.28 + */ 1.29 + 1.30 +package ch.boye.httpclientandroidlib.impl.auth; 1.31 + 1.32 +import java.util.HashMap; 1.33 +import java.util.Locale; 1.34 +import java.util.Map; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; 1.37 + 1.38 +import ch.boye.httpclientandroidlib.HeaderElement; 1.39 +import ch.boye.httpclientandroidlib.auth.MalformedChallengeException; 1.40 +import ch.boye.httpclientandroidlib.message.BasicHeaderValueParser; 1.41 +import ch.boye.httpclientandroidlib.message.HeaderValueParser; 1.42 +import ch.boye.httpclientandroidlib.message.ParserCursor; 1.43 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.44 + 1.45 +/** 1.46 + * Abstract authentication scheme class that lays foundation for all 1.47 + * RFC 2617 compliant authentication schemes and provides capabilities common 1.48 + * to all authentication schemes defined in RFC 2617. 1.49 + * 1.50 + * @since 4.0 1.51 + */ 1.52 +@NotThreadSafe // AuthSchemeBase, params 1.53 +public abstract class RFC2617Scheme extends AuthSchemeBase { 1.54 + 1.55 + /** 1.56 + * Authentication parameter map. 1.57 + */ 1.58 + private Map<String, String> params; 1.59 + 1.60 + /** 1.61 + * Default constructor for RFC2617 compliant authentication schemes. 1.62 + */ 1.63 + public RFC2617Scheme() { 1.64 + super(); 1.65 + } 1.66 + 1.67 + @Override 1.68 + protected void parseChallenge( 1.69 + final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException { 1.70 + HeaderValueParser parser = BasicHeaderValueParser.DEFAULT; 1.71 + ParserCursor cursor = new ParserCursor(pos, buffer.length()); 1.72 + HeaderElement[] elements = parser.parseElements(buffer, cursor); 1.73 + if (elements.length == 0) { 1.74 + throw new MalformedChallengeException("Authentication challenge is empty"); 1.75 + } 1.76 + 1.77 + this.params = new HashMap<String, String>(elements.length); 1.78 + for (HeaderElement element : elements) { 1.79 + this.params.put(element.getName(), element.getValue()); 1.80 + } 1.81 + } 1.82 + 1.83 + /** 1.84 + * Returns authentication parameters map. Keys in the map are lower-cased. 1.85 + * 1.86 + * @return the map of authentication parameters 1.87 + */ 1.88 + protected Map<String, String> getParameters() { 1.89 + if (this.params == null) { 1.90 + this.params = new HashMap<String, String>(); 1.91 + } 1.92 + return this.params; 1.93 + } 1.94 + 1.95 + /** 1.96 + * Returns authentication parameter with the given name, if available. 1.97 + * 1.98 + * @param name The name of the parameter to be returned 1.99 + * 1.100 + * @return the parameter with the given name 1.101 + */ 1.102 + public String getParameter(final String name) { 1.103 + if (name == null) { 1.104 + throw new IllegalArgumentException("Parameter name may not be null"); 1.105 + } 1.106 + if (this.params == null) { 1.107 + return null; 1.108 + } 1.109 + return this.params.get(name.toLowerCase(Locale.ENGLISH)); 1.110 + } 1.111 + 1.112 + /** 1.113 + * Returns authentication realm. The realm may not be null. 1.114 + * 1.115 + * @return the authentication realm 1.116 + */ 1.117 + public String getRealm() { 1.118 + return getParameter("realm"); 1.119 + } 1.120 + 1.121 +}