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/AuthSchemeBase.java $
      3  * $Revision: 653867 $
      4  * $Date: 2008-05-06 11:17:29 -0700 (Tue, 06 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 org.apache.http.FormattedHeader;
     34 import org.apache.http.Header;
     35 import org.apache.http.auth.AUTH;
     36 import org.apache.http.auth.AuthScheme;
     37 import org.apache.http.auth.MalformedChallengeException;
     38 import org.apache.http.protocol.HTTP;
     39 import org.apache.http.util.CharArrayBuffer;
     40 
     41 /**
     42  * Abstract authentication scheme class that serves as a basis
     43  * for all authentication schemes supported by HttpClient. This class
     44  * defines the generic way of parsing an authentication challenge. It
     45  * does not make any assumptions regarding the format of the challenge
     46  * nor does it impose any specific way of responding to that challenge.
     47  *
     48  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
     49  *
     50  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     51  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     52  *     for further details.
     53 */
     54 @Deprecated
     55 public abstract class AuthSchemeBase implements AuthScheme {
     56 
     57     /**
     58      * Flag whether authenticating against a proxy.
     59      */
     60     private boolean proxy;
     61 
     62     public AuthSchemeBase() {
     63         super();
     64     }
     65 
     66     /**
     67      * Processes the given challenge token. Some authentication schemes
     68      * may involve multiple challenge-response exchanges. Such schemes must be able
     69      * to maintain the state information when dealing with sequential challenges
     70      *
     71      * @param header the challenge header
     72      *
     73      * @throws MalformedChallengeException is thrown if the authentication challenge
     74      * is malformed
     75      */
     76     public void processChallenge(final Header header) throws MalformedChallengeException {
     77         if (header == null) {
     78             throw new IllegalArgumentException("Header may not be null");
     79         }
     80         String authheader = header.getName();
     81         if (authheader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
     82             this.proxy = false;
     83         } else if (authheader.equalsIgnoreCase(AUTH.PROXY_AUTH)) {
     84             this.proxy = true;
     85         } else {
     86             throw new MalformedChallengeException("Unexpected header name: " + authheader);
     87         }
     88 
     89         CharArrayBuffer buffer;
     90         int pos;
     91         if (header instanceof FormattedHeader) {
     92             buffer = ((FormattedHeader) header).getBuffer();
     93             pos = ((FormattedHeader) header).getValuePos();
     94         } else {
     95             String s = header.getValue();
     96             if (s == null) {
     97                 throw new MalformedChallengeException("Header value is null");
     98             }
     99             buffer = new CharArrayBuffer(s.length());
    100             buffer.append(s);
    101             pos = 0;
    102         }
    103         while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
    104             pos++;
    105         }
    106         int beginIndex = pos;
    107         while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
    108             pos++;
    109         }
    110         int endIndex = pos;
    111         String s = buffer.substring(beginIndex, endIndex);
    112         if (!s.equalsIgnoreCase(getSchemeName())) {
    113             throw new MalformedChallengeException("Invalid scheme identifier: " + s);
    114         }
    115 
    116         parseChallenge(buffer, pos, buffer.length());
    117     }
    118 
    119     protected abstract void parseChallenge(
    120             CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException;
    121 
    122     /**
    123      * Returns <code>true</code> if authenticating against a proxy, <code>false</code>
    124      * otherwise.
    125      *
    126      * @return <code>true</code> if authenticating against a proxy, <code>false</code>
    127      * otherwise
    128      */
    129     public boolean isProxy() {
    130         return this.proxy;
    131     }
    132 
    133 }
    134