1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.impl.auth;
28
29 import java.nio.charset.Charset;
30 import java.util.HashMap;
31 import java.util.Locale;
32 import java.util.Map;
33
34 import org.apache.http.Consts;
35 import org.apache.http.HeaderElement;
36 import org.apache.http.HttpRequest;
37 import org.apache.http.annotation.NotThreadSafe;
38 import org.apache.http.auth.ChallengeState;
39 import org.apache.http.auth.MalformedChallengeException;
40 import org.apache.http.auth.params.AuthPNames;
41 import org.apache.http.message.BasicHeaderValueParser;
42 import org.apache.http.message.HeaderValueParser;
43 import org.apache.http.message.ParserCursor;
44 import org.apache.http.util.CharArrayBuffer;
45
46
47
48
49
50
51
52
53 @SuppressWarnings("deprecation")
54 @NotThreadSafe
55 public abstract class RFC2617Scheme extends AuthSchemeBase {
56
57 private final Map<String, String> params;
58 private final Charset credentialsCharset;
59
60
61
62
63
64
65
66
67
68 @Deprecated
69 public RFC2617Scheme(final ChallengeState challengeState) {
70 super(challengeState);
71 this.params = new HashMap<String, String>();
72 this.credentialsCharset = Consts.ASCII;
73 }
74
75
76
77
78 public RFC2617Scheme(final Charset credentialsCharset) {
79 super();
80 this.params = new HashMap<String, String>();
81 this.credentialsCharset = credentialsCharset != null ? credentialsCharset : Consts.ASCII;
82 }
83
84 public RFC2617Scheme() {
85 this(Consts.ASCII);
86 }
87
88
89
90
91
92 public Charset getCredentialsCharset() {
93 return credentialsCharset;
94 }
95
96 String getCredentialsCharset(final HttpRequest request) {
97 String charset = (String) request.getParams().getParameter(AuthPNames.CREDENTIAL_CHARSET);
98 if (charset == null) {
99 charset = getCredentialsCharset().name();
100 }
101 return charset;
102 }
103
104 @Override
105 protected void parseChallenge(
106 final CharArrayBuffer buffer, final int pos, final int len) throws MalformedChallengeException {
107 final HeaderValueParser parser = BasicHeaderValueParser.INSTANCE;
108 final ParserCursor cursor = new ParserCursor(pos, buffer.length());
109 final HeaderElement[] elements = parser.parseElements(buffer, cursor);
110 if (elements.length == 0) {
111 throw new MalformedChallengeException("Authentication challenge is empty");
112 }
113 this.params.clear();
114 for (final HeaderElement element : elements) {
115 this.params.put(element.getName(), element.getValue());
116 }
117 }
118
119
120
121
122
123
124 protected Map<String, String> getParameters() {
125 return this.params;
126 }
127
128
129
130
131
132
133
134
135 public String getParameter(final String name) {
136 if (name == null) {
137 return null;
138 }
139 return this.params.get(name.toLowerCase(Locale.ENGLISH));
140 }
141
142
143
144
145
146
147 public String getRealm() {
148 return getParameter("realm");
149 }
150
151 }