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.io.IOException;
30 import java.io.ObjectInputStream;
31 import java.io.ObjectOutputStream;
32 import java.io.ObjectStreamException;
33 import java.io.Serializable;
34 import java.nio.charset.Charset;
35 import java.util.HashMap;
36 import java.util.Locale;
37 import java.util.Map;
38
39 import org.apache.http.Consts;
40 import org.apache.http.HeaderElement;
41 import org.apache.http.HttpRequest;
42 import org.apache.http.auth.ChallengeState;
43 import org.apache.http.auth.MalformedChallengeException;
44 import org.apache.http.auth.params.AuthPNames;
45 import org.apache.http.message.BasicHeaderValueParser;
46 import org.apache.http.message.HeaderValueParser;
47 import org.apache.http.message.ParserCursor;
48 import org.apache.http.util.CharArrayBuffer;
49 import org.apache.http.util.CharsetUtils;
50
51
52
53
54
55
56
57
58 @SuppressWarnings("deprecation")
59 public abstract class RFC2617Scheme extends AuthSchemeBase implements Serializable {
60
61 private static final long serialVersionUID = -2845454858205884623L;
62
63 private final Map<String, String> params;
64 private transient Charset credentialsCharset;
65
66
67
68
69
70
71
72
73
74 @Deprecated
75 public RFC2617Scheme(final ChallengeState challengeState) {
76 super(challengeState);
77 this.params = new HashMap<String, String>();
78 this.credentialsCharset = Consts.ASCII;
79 }
80
81
82
83
84 public RFC2617Scheme(final Charset credentialsCharset) {
85 super();
86 this.params = new HashMap<String, String>();
87 this.credentialsCharset = credentialsCharset != null ? credentialsCharset : Consts.ASCII;
88 }
89
90 public RFC2617Scheme() {
91 this(Consts.ASCII);
92 }
93
94
95
96
97
98 public Charset getCredentialsCharset() {
99 return credentialsCharset != null ? credentialsCharset : Consts.ASCII;
100 }
101
102 String getCredentialsCharset(final HttpRequest request) {
103 String charset = (String) request.getParams().getParameter(AuthPNames.CREDENTIAL_CHARSET);
104 if (charset == null) {
105 charset = getCredentialsCharset().name();
106 }
107 return charset;
108 }
109
110 @Override
111 protected void parseChallenge(
112 final CharArrayBuffer buffer, final int pos, final int len) throws MalformedChallengeException {
113 final HeaderValueParser parser = BasicHeaderValueParser.INSTANCE;
114 final ParserCursor cursor = new ParserCursor(pos, buffer.length());
115 final HeaderElement[] elements = parser.parseElements(buffer, cursor);
116 this.params.clear();
117 for (final HeaderElement element : elements) {
118 this.params.put(element.getName().toLowerCase(Locale.ROOT), element.getValue());
119 }
120 }
121
122
123
124
125
126
127 protected Map<String, String> getParameters() {
128 return this.params;
129 }
130
131
132
133
134
135
136
137
138 @Override
139 public String getParameter(final String name) {
140 if (name == null) {
141 return null;
142 }
143 return this.params.get(name.toLowerCase(Locale.ROOT));
144 }
145
146
147
148
149
150
151 @Override
152 public String getRealm() {
153 return getParameter("realm");
154 }
155
156 private void writeObject(final ObjectOutputStream out) throws IOException {
157 out.defaultWriteObject();
158 out.writeUTF(this.credentialsCharset.name());
159 out.writeObject(this.challengeState);
160 }
161
162 @SuppressWarnings("unchecked")
163 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
164 in.defaultReadObject();
165 this.credentialsCharset = CharsetUtils.get(in.readUTF());
166 if (this.credentialsCharset == null) {
167 this.credentialsCharset = Consts.ASCII;
168 }
169 this.challengeState = (ChallengeState) in.readObject();
170 }
171
172 private void readObjectNoData() throws ObjectStreamException {
173 }
174
175 }