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 org.apache.http.impl.auth;
28
29 import java.util.HashMap;
30 import java.util.Locale;
31 import java.util.Map;
32
33 import org.apache.http.annotation.NotThreadSafe;
34
35 import org.apache.http.HeaderElement;
36 import org.apache.http.auth.ChallengeState;
37 import org.apache.http.auth.MalformedChallengeException;
38 import org.apache.http.message.BasicHeaderValueParser;
39 import org.apache.http.message.HeaderValueParser;
40 import org.apache.http.message.ParserCursor;
41 import org.apache.http.util.CharArrayBuffer;
42
43 /**
44 * Abstract authentication scheme class that lays foundation for all
45 * RFC 2617 compliant authentication schemes and provides capabilities common
46 * to all authentication schemes defined in RFC 2617.
47 *
48 * @since 4.0
49 */
50 @NotThreadSafe // AuthSchemeBase, params
51 public abstract class RFC2617Scheme extends AuthSchemeBase {
52
53 /**
54 * Authentication parameter map.
55 */
56 private final Map<String, String> params;
57
58 /**
59 * Creates an instance of <tt>RFC2617Scheme</tt> with the given challenge
60 * state.
61 *
62 * @since 4.2
63 */
64 public RFC2617Scheme(final ChallengeState challengeState) {
65 super(challengeState);
66 this.params = new HashMap<String, String>();
67 }
68
69 public RFC2617Scheme() {
70 this(null);
71 }
72
73 @Override
74 protected void parseChallenge(
75 final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException {
76 HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
77 ParserCursor cursor = new ParserCursor(pos, buffer.length());
78 HeaderElement[] elements = parser.parseElements(buffer, cursor);
79 if (elements.length == 0) {
80 throw new MalformedChallengeException("Authentication challenge is empty");
81 }
82 this.params.clear();
83 for (HeaderElement element : elements) {
84 this.params.put(element.getName(), element.getValue());
85 }
86 }
87
88 /**
89 * Returns authentication parameters map. Keys in the map are lower-cased.
90 *
91 * @return the map of authentication parameters
92 */
93 protected Map<String, String> getParameters() {
94 return this.params;
95 }
96
97 /**
98 * Returns authentication parameter with the given name, if available.
99 *
100 * @param name The name of the parameter to be returned
101 *
102 * @return the parameter with the given name
103 */
104 public String getParameter(final String name) {
105 if (name == null) {
106 return null;
107 }
108 return this.params.get(name.toLowerCase(Locale.ENGLISH));
109 }
110
111 /**
112 * Returns authentication realm. The realm may not be null.
113 *
114 * @return the authentication realm
115 */
116 public String getRealm() {
117 return getParameter("realm");
118 }
119
120 }