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