michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * 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, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations 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.auth; michael@0: michael@0: import java.util.HashMap; michael@0: import java.util.Locale; michael@0: import java.util.Map; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; michael@0: michael@0: import ch.boye.httpclientandroidlib.HeaderElement; michael@0: import ch.boye.httpclientandroidlib.auth.MalformedChallengeException; michael@0: import ch.boye.httpclientandroidlib.message.BasicHeaderValueParser; michael@0: import ch.boye.httpclientandroidlib.message.HeaderValueParser; michael@0: import ch.boye.httpclientandroidlib.message.ParserCursor; michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer; michael@0: michael@0: /** michael@0: * Abstract authentication scheme class that lays foundation for all michael@0: * RFC 2617 compliant authentication schemes and provides capabilities common michael@0: * to all authentication schemes defined in RFC 2617. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @NotThreadSafe // AuthSchemeBase, params michael@0: public abstract class RFC2617Scheme extends AuthSchemeBase { michael@0: michael@0: /** michael@0: * Authentication parameter map. michael@0: */ michael@0: private Map params; michael@0: michael@0: /** michael@0: * Default constructor for RFC2617 compliant authentication schemes. michael@0: */ michael@0: public RFC2617Scheme() { michael@0: super(); michael@0: } michael@0: michael@0: @Override michael@0: protected void parseChallenge( michael@0: final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException { michael@0: HeaderValueParser parser = BasicHeaderValueParser.DEFAULT; michael@0: ParserCursor cursor = new ParserCursor(pos, buffer.length()); michael@0: HeaderElement[] elements = parser.parseElements(buffer, cursor); michael@0: if (elements.length == 0) { michael@0: throw new MalformedChallengeException("Authentication challenge is empty"); michael@0: } michael@0: michael@0: this.params = new HashMap(elements.length); michael@0: for (HeaderElement element : elements) { michael@0: this.params.put(element.getName(), element.getValue()); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns authentication parameters map. Keys in the map are lower-cased. michael@0: * michael@0: * @return the map of authentication parameters michael@0: */ michael@0: protected Map getParameters() { michael@0: if (this.params == null) { michael@0: this.params = new HashMap(); michael@0: } michael@0: return this.params; michael@0: } michael@0: michael@0: /** michael@0: * Returns authentication parameter with the given name, if available. michael@0: * michael@0: * @param name The name of the parameter to be returned michael@0: * michael@0: * @return the parameter with the given name michael@0: */ michael@0: public String getParameter(final String name) { michael@0: if (name == null) { michael@0: throw new IllegalArgumentException("Parameter name may not be null"); michael@0: } michael@0: if (this.params == null) { michael@0: return null; michael@0: } michael@0: return this.params.get(name.toLowerCase(Locale.ENGLISH)); michael@0: } michael@0: michael@0: /** michael@0: * Returns authentication realm. The realm may not be null. michael@0: * michael@0: * @return the authentication realm michael@0: */ michael@0: public String getRealm() { michael@0: return getParameter("realm"); michael@0: } michael@0: michael@0: }