2
|
1 |
/*
|
715
|
2 |
* Copyright 1995-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*-
|
|
27 |
* news stream opener
|
|
28 |
*/
|
|
29 |
|
|
30 |
package sun.net.www;
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.util.Collections;
|
|
34 |
import java.util.Map;
|
|
35 |
import java.util.HashMap;
|
|
36 |
import java.util.List;
|
|
37 |
import java.util.ArrayList;
|
|
38 |
import java.util.Set;
|
|
39 |
import java.util.Iterator;
|
|
40 |
import java.util.NoSuchElementException;
|
|
41 |
|
|
42 |
/** An RFC 844 or MIME message header. Includes methods
|
|
43 |
for parsing headers from incoming streams, fetching
|
|
44 |
values, setting values, and printing headers.
|
|
45 |
Key values of null are legal: they indicate lines in
|
|
46 |
the header that don't have a valid key, but do have
|
|
47 |
a value (this isn't legal according to the standard,
|
|
48 |
but lines like this are everywhere). */
|
|
49 |
public
|
|
50 |
class MessageHeader {
|
|
51 |
private String keys[];
|
|
52 |
private String values[];
|
|
53 |
private int nkeys;
|
|
54 |
|
|
55 |
public MessageHeader () {
|
|
56 |
grow();
|
|
57 |
}
|
|
58 |
|
|
59 |
public MessageHeader (InputStream is) throws java.io.IOException {
|
|
60 |
parseHeader(is);
|
|
61 |
}
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Reset a message header (all key/values removed)
|
|
65 |
*/
|
|
66 |
public synchronized void reset() {
|
|
67 |
keys = null;
|
|
68 |
values = null;
|
|
69 |
nkeys = 0;
|
|
70 |
grow();
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Find the value that corresponds to this key.
|
|
75 |
* It finds only the first occurrence of the key.
|
|
76 |
* @param k the key to find.
|
|
77 |
* @return null if not found.
|
|
78 |
*/
|
|
79 |
public synchronized String findValue(String k) {
|
|
80 |
if (k == null) {
|
|
81 |
for (int i = nkeys; --i >= 0;)
|
|
82 |
if (keys[i] == null)
|
|
83 |
return values[i];
|
|
84 |
} else
|
|
85 |
for (int i = nkeys; --i >= 0;) {
|
|
86 |
if (k.equalsIgnoreCase(keys[i]))
|
|
87 |
return values[i];
|
|
88 |
}
|
|
89 |
return null;
|
|
90 |
}
|
|
91 |
|
|
92 |
// return the location of the key
|
|
93 |
public synchronized int getKey(String k) {
|
|
94 |
for (int i = nkeys; --i >= 0;)
|
|
95 |
if ((keys[i] == k) ||
|
|
96 |
(k != null && k.equalsIgnoreCase(keys[i])))
|
|
97 |
return i;
|
|
98 |
return -1;
|
|
99 |
}
|
|
100 |
|
|
101 |
public synchronized String getKey(int n) {
|
|
102 |
if (n < 0 || n >= nkeys) return null;
|
|
103 |
return keys[n];
|
|
104 |
}
|
|
105 |
|
|
106 |
public synchronized String getValue(int n) {
|
|
107 |
if (n < 0 || n >= nkeys) return null;
|
|
108 |
return values[n];
|
|
109 |
}
|
|
110 |
|
|
111 |
/** Deprecated: Use multiValueIterator() instead.
|
|
112 |
*
|
|
113 |
* Find the next value that corresponds to this key.
|
|
114 |
* It finds the first value that follows v. To iterate
|
|
115 |
* over all the values of a key use:
|
|
116 |
* <pre>
|
|
117 |
* for(String v=h.findValue(k); v!=null; v=h.findNextValue(k, v)) {
|
|
118 |
* ...
|
|
119 |
* }
|
|
120 |
* </pre>
|
|
121 |
*/
|
|
122 |
public synchronized String findNextValue(String k, String v) {
|
|
123 |
boolean foundV = false;
|
|
124 |
if (k == null) {
|
|
125 |
for (int i = nkeys; --i >= 0;)
|
|
126 |
if (keys[i] == null)
|
|
127 |
if (foundV)
|
|
128 |
return values[i];
|
|
129 |
else if (values[i] == v)
|
|
130 |
foundV = true;
|
|
131 |
} else
|
|
132 |
for (int i = nkeys; --i >= 0;)
|
|
133 |
if (k.equalsIgnoreCase(keys[i]))
|
|
134 |
if (foundV)
|
|
135 |
return values[i];
|
|
136 |
else if (values[i] == v)
|
|
137 |
foundV = true;
|
|
138 |
return null;
|
|
139 |
}
|
|
140 |
|
51
|
141 |
class HeaderIterator implements Iterator<String> {
|
2
|
142 |
int index = 0;
|
|
143 |
int next = -1;
|
|
144 |
String key;
|
|
145 |
boolean haveNext = false;
|
|
146 |
Object lock;
|
|
147 |
|
|
148 |
public HeaderIterator (String k, Object lock) {
|
|
149 |
key = k;
|
|
150 |
this.lock = lock;
|
|
151 |
}
|
|
152 |
public boolean hasNext () {
|
|
153 |
synchronized (lock) {
|
|
154 |
if (haveNext) {
|
|
155 |
return true;
|
|
156 |
}
|
|
157 |
while (index < nkeys) {
|
|
158 |
if (key.equalsIgnoreCase (keys[index])) {
|
|
159 |
haveNext = true;
|
|
160 |
next = index++;
|
|
161 |
return true;
|
|
162 |
}
|
|
163 |
index ++;
|
|
164 |
}
|
|
165 |
return false;
|
|
166 |
}
|
|
167 |
}
|
51
|
168 |
public String next() {
|
2
|
169 |
synchronized (lock) {
|
|
170 |
if (haveNext) {
|
|
171 |
haveNext = false;
|
|
172 |
return values [next];
|
|
173 |
}
|
|
174 |
if (hasNext()) {
|
|
175 |
return next();
|
|
176 |
} else {
|
|
177 |
throw new NoSuchElementException ("No more elements");
|
|
178 |
}
|
|
179 |
}
|
|
180 |
}
|
|
181 |
public void remove () {
|
|
182 |
throw new UnsupportedOperationException ("remove not allowed");
|
|
183 |
}
|
|
184 |
}
|
|
185 |
|
|
186 |
/**
|
|
187 |
* return an Iterator that returns all values of a particular
|
|
188 |
* key in sequence
|
|
189 |
*/
|
51
|
190 |
public Iterator<String> multiValueIterator (String k) {
|
2
|
191 |
return new HeaderIterator (k, this);
|
|
192 |
}
|
|
193 |
|
51
|
194 |
public synchronized Map<String, List<String>> getHeaders() {
|
2
|
195 |
return getHeaders(null);
|
|
196 |
}
|
|
197 |
|
51
|
198 |
public synchronized Map<String, List<String>> getHeaders(String[] excludeList) {
|
2
|
199 |
boolean skipIt = false;
|
51
|
200 |
Map<String, List<String>> m = new HashMap<String, List<String>>();
|
2
|
201 |
for (int i = nkeys; --i >= 0;) {
|
|
202 |
if (excludeList != null) {
|
|
203 |
// check if the key is in the excludeList.
|
|
204 |
// if so, don't include it in the Map.
|
|
205 |
for (int j = 0; j < excludeList.length; j++) {
|
|
206 |
if ((excludeList[j] != null) &&
|
|
207 |
(excludeList[j].equalsIgnoreCase(keys[i]))) {
|
|
208 |
skipIt = true;
|
|
209 |
break;
|
|
210 |
}
|
|
211 |
}
|
|
212 |
}
|
|
213 |
if (!skipIt) {
|
51
|
214 |
List<String> l = m.get(keys[i]);
|
2
|
215 |
if (l == null) {
|
51
|
216 |
l = new ArrayList<String>();
|
2
|
217 |
m.put(keys[i], l);
|
|
218 |
}
|
|
219 |
l.add(values[i]);
|
|
220 |
} else {
|
|
221 |
// reset the flag
|
|
222 |
skipIt = false;
|
|
223 |
}
|
|
224 |
}
|
|
225 |
|
51
|
226 |
for (String key : m.keySet()) {
|
|
227 |
m.put(key, Collections.unmodifiableList(m.get(key)));
|
2
|
228 |
}
|
|
229 |
|
|
230 |
return Collections.unmodifiableMap(m);
|
|
231 |
}
|
|
232 |
|
|
233 |
/** Prints the key-value pairs represented by this
|
|
234 |
header. Also prints the RFC required blank line
|
|
235 |
at the end. Omits pairs with a null key. */
|
|
236 |
public synchronized void print(PrintStream p) {
|
|
237 |
for (int i = 0; i < nkeys; i++)
|
|
238 |
if (keys[i] != null) {
|
|
239 |
p.print(keys[i] +
|
|
240 |
(values[i] != null ? ": "+values[i]: "") + "\r\n");
|
|
241 |
}
|
|
242 |
p.print("\r\n");
|
|
243 |
p.flush();
|
|
244 |
}
|
|
245 |
|
|
246 |
/** Adds a key value pair to the end of the
|
|
247 |
header. Duplicates are allowed */
|
|
248 |
public synchronized void add(String k, String v) {
|
|
249 |
grow();
|
|
250 |
keys[nkeys] = k;
|
|
251 |
values[nkeys] = v;
|
|
252 |
nkeys++;
|
|
253 |
}
|
|
254 |
|
|
255 |
/** Prepends a key value pair to the beginning of the
|
|
256 |
header. Duplicates are allowed */
|
|
257 |
public synchronized void prepend(String k, String v) {
|
|
258 |
grow();
|
|
259 |
for (int i = nkeys; i > 0; i--) {
|
|
260 |
keys[i] = keys[i-1];
|
|
261 |
values[i] = values[i-1];
|
|
262 |
}
|
|
263 |
keys[0] = k;
|
|
264 |
values[0] = v;
|
|
265 |
nkeys++;
|
|
266 |
}
|
|
267 |
|
|
268 |
/** Overwrite the previous key/val pair at location 'i'
|
|
269 |
* with the new k/v. If the index didn't exist before
|
|
270 |
* the key/val is simply tacked onto the end.
|
|
271 |
*/
|
|
272 |
|
|
273 |
public synchronized void set(int i, String k, String v) {
|
|
274 |
grow();
|
|
275 |
if (i < 0) {
|
|
276 |
return;
|
|
277 |
} else if (i >= nkeys) {
|
|
278 |
add(k, v);
|
|
279 |
} else {
|
|
280 |
keys[i] = k;
|
|
281 |
values[i] = v;
|
|
282 |
}
|
|
283 |
}
|
|
284 |
|
|
285 |
|
|
286 |
/** grow the key/value arrays as needed */
|
|
287 |
|
|
288 |
private void grow() {
|
|
289 |
if (keys == null || nkeys >= keys.length) {
|
|
290 |
String[] nk = new String[nkeys + 4];
|
|
291 |
String[] nv = new String[nkeys + 4];
|
|
292 |
if (keys != null)
|
|
293 |
System.arraycopy(keys, 0, nk, 0, nkeys);
|
|
294 |
if (values != null)
|
|
295 |
System.arraycopy(values, 0, nv, 0, nkeys);
|
|
296 |
keys = nk;
|
|
297 |
values = nv;
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
/**
|
|
302 |
* Remove the key from the header. If there are multiple values under
|
|
303 |
* the same key, they are all removed.
|
|
304 |
* Nothing is done if the key doesn't exist.
|
|
305 |
* After a remove, the other pairs' order are not changed.
|
|
306 |
* @param k the key to remove
|
|
307 |
*/
|
|
308 |
public synchronized void remove(String k) {
|
|
309 |
if(k == null) {
|
|
310 |
for (int i = 0; i < nkeys; i++) {
|
|
311 |
while (keys[i] == null && i < nkeys) {
|
|
312 |
for(int j=i; j<nkeys-1; j++) {
|
|
313 |
keys[j] = keys[j+1];
|
|
314 |
values[j] = values[j+1];
|
|
315 |
}
|
|
316 |
nkeys--;
|
|
317 |
}
|
|
318 |
}
|
|
319 |
} else {
|
|
320 |
for (int i = 0; i < nkeys; i++) {
|
|
321 |
while (k.equalsIgnoreCase(keys[i]) && i < nkeys) {
|
|
322 |
for(int j=i; j<nkeys-1; j++) {
|
|
323 |
keys[j] = keys[j+1];
|
|
324 |
values[j] = values[j+1];
|
|
325 |
}
|
|
326 |
nkeys--;
|
|
327 |
}
|
|
328 |
}
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
/** Sets the value of a key. If the key already
|
|
333 |
exists in the header, it's value will be
|
|
334 |
changed. Otherwise a new key/value pair will
|
|
335 |
be added to the end of the header. */
|
|
336 |
public synchronized void set(String k, String v) {
|
|
337 |
for (int i = nkeys; --i >= 0;)
|
|
338 |
if (k.equalsIgnoreCase(keys[i])) {
|
|
339 |
values[i] = v;
|
|
340 |
return;
|
|
341 |
}
|
|
342 |
add(k, v);
|
|
343 |
}
|
|
344 |
|
|
345 |
/** Set's the value of a key only if there is no
|
|
346 |
* key with that value already.
|
|
347 |
*/
|
|
348 |
|
|
349 |
public synchronized void setIfNotSet(String k, String v) {
|
|
350 |
if (findValue(k) == null) {
|
|
351 |
add(k, v);
|
|
352 |
}
|
|
353 |
}
|
|
354 |
|
|
355 |
/** Convert a message-id string to canonical form (strips off
|
|
356 |
leading and trailing <>s) */
|
|
357 |
public static String canonicalID(String id) {
|
|
358 |
if (id == null)
|
|
359 |
return "";
|
|
360 |
int st = 0;
|
|
361 |
int len = id.length();
|
|
362 |
boolean substr = false;
|
|
363 |
int c;
|
|
364 |
while (st < len && ((c = id.charAt(st)) == '<' ||
|
|
365 |
c <= ' ')) {
|
|
366 |
st++;
|
|
367 |
substr = true;
|
|
368 |
}
|
|
369 |
while (st < len && ((c = id.charAt(len - 1)) == '>' ||
|
|
370 |
c <= ' ')) {
|
|
371 |
len--;
|
|
372 |
substr = true;
|
|
373 |
}
|
|
374 |
return substr ? id.substring(st, len) : id;
|
|
375 |
}
|
|
376 |
|
|
377 |
/** Parse a MIME header from an input stream. */
|
|
378 |
public void parseHeader(InputStream is) throws java.io.IOException {
|
|
379 |
synchronized (this) {
|
|
380 |
nkeys = 0;
|
|
381 |
}
|
|
382 |
mergeHeader(is);
|
|
383 |
}
|
|
384 |
|
|
385 |
/** Parse and merge a MIME header from an input stream. */
|
|
386 |
public void mergeHeader(InputStream is) throws java.io.IOException {
|
|
387 |
if (is == null)
|
|
388 |
return;
|
|
389 |
char s[] = new char[10];
|
|
390 |
int firstc = is.read();
|
|
391 |
while (firstc != '\n' && firstc != '\r' && firstc >= 0) {
|
|
392 |
int len = 0;
|
|
393 |
int keyend = -1;
|
|
394 |
int c;
|
|
395 |
boolean inKey = firstc > ' ';
|
|
396 |
s[len++] = (char) firstc;
|
|
397 |
parseloop:{
|
|
398 |
while ((c = is.read()) >= 0) {
|
|
399 |
switch (c) {
|
|
400 |
case ':':
|
|
401 |
if (inKey && len > 0)
|
|
402 |
keyend = len;
|
|
403 |
inKey = false;
|
|
404 |
break;
|
|
405 |
case '\t':
|
|
406 |
c = ' ';
|
|
407 |
case ' ':
|
|
408 |
inKey = false;
|
|
409 |
break;
|
|
410 |
case '\r':
|
|
411 |
case '\n':
|
|
412 |
firstc = is.read();
|
|
413 |
if (c == '\r' && firstc == '\n') {
|
|
414 |
firstc = is.read();
|
|
415 |
if (firstc == '\r')
|
|
416 |
firstc = is.read();
|
|
417 |
}
|
|
418 |
if (firstc == '\n' || firstc == '\r' || firstc > ' ')
|
|
419 |
break parseloop;
|
|
420 |
/* continuation */
|
|
421 |
c = ' ';
|
|
422 |
break;
|
|
423 |
}
|
|
424 |
if (len >= s.length) {
|
|
425 |
char ns[] = new char[s.length * 2];
|
|
426 |
System.arraycopy(s, 0, ns, 0, len);
|
|
427 |
s = ns;
|
|
428 |
}
|
|
429 |
s[len++] = (char) c;
|
|
430 |
}
|
|
431 |
firstc = -1;
|
|
432 |
}
|
|
433 |
while (len > 0 && s[len - 1] <= ' ')
|
|
434 |
len--;
|
|
435 |
String k;
|
|
436 |
if (keyend <= 0) {
|
|
437 |
k = null;
|
|
438 |
keyend = 0;
|
|
439 |
} else {
|
|
440 |
k = String.copyValueOf(s, 0, keyend);
|
|
441 |
if (keyend < len && s[keyend] == ':')
|
|
442 |
keyend++;
|
|
443 |
while (keyend < len && s[keyend] <= ' ')
|
|
444 |
keyend++;
|
|
445 |
}
|
|
446 |
String v;
|
|
447 |
if (keyend >= len)
|
|
448 |
v = new String();
|
|
449 |
else
|
|
450 |
v = String.copyValueOf(s, keyend, len - keyend);
|
|
451 |
add(k, v);
|
|
452 |
}
|
|
453 |
}
|
|
454 |
|
|
455 |
public synchronized String toString() {
|
|
456 |
String result = super.toString() + nkeys + " pairs: ";
|
|
457 |
for (int i = 0; i < keys.length && i < nkeys; i++) {
|
|
458 |
result += "{"+keys[i]+": "+values[i]+"}";
|
|
459 |
}
|
|
460 |
return result;
|
|
461 |
}
|
|
462 |
}
|