1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/RFC2617Scheme.java $ 3 * $Revision: 659595 $ 4 * $Date: 2008-05-23 09:47:14 -0700 (Fri, 23 May 2008) $ 5 * 6 * ==================================================================== 7 * 8 * Licensed to the Apache Software Foundation (ASF) under one or more 9 * contributor license agreements. See the NOTICE file distributed with 10 * this work for additional information regarding copyright ownership. 11 * The ASF licenses this file to You under the Apache License, Version 2.0 12 * (the "License"); you may not use this file except in compliance with 13 * 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, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 * ==================================================================== 23 * 24 * This software consists of voluntary contributions made by many 25 * individuals on behalf of the Apache Software Foundation. For more 26 * information on the Apache Software Foundation, please see 27 * <http://www.apache.org/>. 28 * 29 */ 30 31 package org.apache.http.impl.auth; 32 33 import java.util.HashMap; 34 import java.util.Locale; 35 import java.util.Map; 36 37 import org.apache.http.HeaderElement; 38 import org.apache.http.auth.MalformedChallengeException; 39 import org.apache.http.message.BasicHeaderValueParser; 40 import org.apache.http.message.HeaderValueParser; 41 import org.apache.http.message.ParserCursor; 42 import org.apache.http.util.CharArrayBuffer; 43 44 /** 45 * Abstract authentication scheme class that lays foundation for all 46 * RFC 2617 compliant authetication schemes and provides capabilities common 47 * to all authentication schemes defined in RFC 2617. 48 * 49 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 50 */ 51 public abstract class RFC2617Scheme extends AuthSchemeBase { 52 53 /** 54 * Authentication parameter map. 55 */ 56 private Map<String, String> params; 57 58 /** 59 * Default constructor for RFC2617 compliant authetication schemes. 60 */ RFC2617Scheme()61 public RFC2617Scheme() { 62 super(); 63 } 64 65 @Override parseChallenge( final CharArrayBuffer buffer, int pos, int len)66 protected void parseChallenge( 67 final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException { 68 HeaderValueParser parser = BasicHeaderValueParser.DEFAULT; 69 ParserCursor cursor = new ParserCursor(pos, buffer.length()); 70 HeaderElement[] elements = parser.parseElements(buffer, cursor); 71 if (elements.length == 0) { 72 throw new MalformedChallengeException("Authentication challenge is empty"); 73 } 74 75 this.params = new HashMap<String, String>(elements.length); 76 for (HeaderElement element : elements) { 77 this.params.put(element.getName(), element.getValue()); 78 } 79 } 80 81 /** 82 * Returns authentication parameters map. Keys in the map are lower-cased. 83 * 84 * @return the map of authentication parameters 85 */ getParameters()86 protected Map<String, String> getParameters() { 87 if (this.params == null) { 88 this.params = new HashMap<String, String>(); 89 } 90 return this.params; 91 } 92 93 /** 94 * Returns authentication parameter with the given name, if available. 95 * 96 * @param name The name of the parameter to be returned 97 * 98 * @return the parameter with the given name 99 */ getParameter(final String name)100 public String getParameter(final String name) { 101 if (name == null) { 102 throw new IllegalArgumentException("Parameter name may not be null"); 103 } 104 if (this.params == null) { 105 return null; 106 } 107 return this.params.get(name.toLowerCase(Locale.ENGLISH)); 108 } 109 110 /** 111 * Returns authentication realm. The realm may not be null. 112 * 113 * @return the authentication realm 114 */ getRealm()115 public String getRealm() { 116 return getParameter("realm"); 117 } 118 119 } 120