• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 public abstract class AuthSchemeBase implements AuthScheme {
51 
52     /**
53      * Flag whether authenticating against a proxy.
54      */
55     private boolean proxy;
56 
AuthSchemeBase()57     public AuthSchemeBase() {
58         super();
59     }
60 
61     /**
62      * Processes the given challenge token. Some authentication schemes
63      * may involve multiple challenge-response exchanges. Such schemes must be able
64      * to maintain the state information when dealing with sequential challenges
65      *
66      * @param header the challenge header
67      *
68      * @throws MalformedChallengeException is thrown if the authentication challenge
69      * is malformed
70      */
processChallenge(final Header header)71     public void processChallenge(final Header header) throws MalformedChallengeException {
72         if (header == null) {
73             throw new IllegalArgumentException("Header may not be null");
74         }
75         String authheader = header.getName();
76         if (authheader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
77             this.proxy = false;
78         } else if (authheader.equalsIgnoreCase(AUTH.PROXY_AUTH)) {
79             this.proxy = true;
80         } else {
81             throw new MalformedChallengeException("Unexpected header name: " + authheader);
82         }
83 
84         CharArrayBuffer buffer;
85         int pos;
86         if (header instanceof FormattedHeader) {
87             buffer = ((FormattedHeader) header).getBuffer();
88             pos = ((FormattedHeader) header).getValuePos();
89         } else {
90             String s = header.getValue();
91             if (s == null) {
92                 throw new MalformedChallengeException("Header value is null");
93             }
94             buffer = new CharArrayBuffer(s.length());
95             buffer.append(s);
96             pos = 0;
97         }
98         while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
99             pos++;
100         }
101         int beginIndex = pos;
102         while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
103             pos++;
104         }
105         int endIndex = pos;
106         String s = buffer.substring(beginIndex, endIndex);
107         if (!s.equalsIgnoreCase(getSchemeName())) {
108             throw new MalformedChallengeException("Invalid scheme identifier: " + s);
109         }
110 
111         parseChallenge(buffer, pos, buffer.length());
112     }
113 
parseChallenge( CharArrayBuffer buffer, int pos, int len)114     protected abstract void parseChallenge(
115             CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException;
116 
117     /**
118      * Returns <code>true</code> if authenticating against a proxy, <code>false</code>
119      * otherwise.
120      *
121      * @return <code>true</code> if authenticating against a proxy, <code>false</code>
122      * otherwise
123      */
isProxy()124     public boolean isProxy() {
125         return this.proxy;
126     }
127 
128 }
129