Home | History | Annotate | Download | only in auth
      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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     52  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     53  *     for further details.
     54 */
     55 @Deprecated
     56 public abstract class RFC2617Scheme extends AuthSchemeBase {
     57 
     58     /**
     59      * Authentication parameter map.
     60      */
     61     private Map<String, String> params;
     62 
     63     /**
     64      * Default constructor for RFC2617 compliant authetication schemes.
     65      */
     66     public RFC2617Scheme() {
     67         super();
     68     }
     69 
     70     @Override
     71     protected void parseChallenge(
     72             final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException {
     73         HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
     74         ParserCursor cursor = new ParserCursor(pos, buffer.length());
     75         HeaderElement[] elements = parser.parseElements(buffer, cursor);
     76         if (elements.length == 0) {
     77             throw new MalformedChallengeException("Authentication challenge is empty");
     78         }
     79 
     80         this.params = new HashMap<String, String>(elements.length);
     81         for (HeaderElement element : elements) {
     82             this.params.put(element.getName(), element.getValue());
     83         }
     84     }
     85 
     86     /**
     87      * Returns authentication parameters map. Keys in the map are lower-cased.
     88      *
     89      * @return the map of authentication parameters
     90      */
     91     protected Map<String, String> getParameters() {
     92         if (this.params == null) {
     93             this.params = new HashMap<String, String>();
     94         }
     95         return this.params;
     96     }
     97 
     98     /**
     99      * Returns authentication parameter with the given name, if available.
    100      *
    101      * @param name The name of the parameter to be returned
    102      *
    103      * @return the parameter with the given name
    104      */
    105     public String getParameter(final String name) {
    106         if (name == null) {
    107             throw new IllegalArgumentException("Parameter name may not be null");
    108         }
    109         if (this.params == null) {
    110             return null;
    111         }
    112         return this.params.get(name.toLowerCase(Locale.ENGLISH));
    113     }
    114 
    115     /**
    116      * Returns authentication realm. The realm may not be null.
    117      *
    118      * @return the authentication realm
    119      */
    120     public String getRealm() {
    121         return getParameter("realm");
    122     }
    123 
    124 }
    125