2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, Oracle and/or its affiliates. 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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.net.httpserver;
|
|
27 |
|
|
28 |
import java.util.*;
|
|
29 |
import java.io.*;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* HTTP request and response headers are represented by this class which implements
|
|
33 |
* the interface {@link java.util.Map}<
|
|
34 |
* {@link java.lang.String},{@link java.util.List}<{@link java.lang.String}>>.
|
|
35 |
* The keys are case-insensitive Strings representing the header names and
|
|
36 |
* the value associated with each key is a {@link List}<{@link String}> with one
|
|
37 |
* element for each occurence of the header name in the request or response.
|
|
38 |
* <p>
|
|
39 |
* For example, if a response header instance contains one key "HeaderName" with two values "value1 and value2"
|
|
40 |
* then this object is output as two header lines:
|
|
41 |
* <blockquote><pre>
|
|
42 |
* HeaderName: value1
|
|
43 |
* HeaderName: value2
|
|
44 |
* </blockquote></pre>
|
|
45 |
* <p>
|
|
46 |
* All the normal {@link java.util.Map} methods are provided, but the following
|
|
47 |
* additional convenience methods are most likely to be used:
|
|
48 |
* <ul>
|
|
49 |
* <li>{@link #getFirst(String)} returns a single valued header or the first value of
|
|
50 |
* a multi-valued header.</li>
|
|
51 |
* <li>{@link #add(String,String)} adds the given header value to the list for the given key</li>
|
|
52 |
* <li>{@link #set(String,String)} sets the given header field to the single value given
|
|
53 |
* overwriting any existing values in the value list.
|
|
54 |
* </ul><p>
|
|
55 |
* All methods in this class accept <code>null</code> values for keys and values. However, null
|
|
56 |
* keys will never will be present in HTTP request headers, and will not be output/sent in response headers.
|
|
57 |
* Null values can be represented as either a null entry for the key (i.e. the list is null) or
|
|
58 |
* where the key has a list, but one (or more) of the list's values is null. Null values are output
|
|
59 |
* as a header line containing the key but no associated value.
|
|
60 |
* @since 1.6
|
|
61 |
*/
|
|
62 |
public class Headers implements Map<String,List<String>> {
|
|
63 |
|
|
64 |
HashMap<String,List<String>> map;
|
|
65 |
|
|
66 |
public Headers () {map = new HashMap<String,List<String>>(32);}
|
|
67 |
|
|
68 |
/* Normalize the key by converting to following form.
|
|
69 |
* First char upper case, rest lower case.
|
|
70 |
* key is presumed to be ASCII
|
|
71 |
*/
|
|
72 |
private String normalize (String key) {
|
|
73 |
if (key == null) {
|
|
74 |
return null;
|
|
75 |
}
|
|
76 |
int len = key.length();
|
|
77 |
if (len == 0) {
|
|
78 |
return key;
|
|
79 |
}
|
|
80 |
char[] b = new char [len];
|
|
81 |
String s = null;
|
|
82 |
b = key.toCharArray();
|
|
83 |
if (b[0] >= 'a' && b[0] <= 'z') {
|
|
84 |
b[0] = (char)(b[0] - ('a' - 'A'));
|
|
85 |
}
|
|
86 |
for (int i=1; i<len; i++) {
|
|
87 |
if (b[i] >= 'A' && b[i] <= 'Z') {
|
|
88 |
b[i] = (char) (b[i] + ('a' - 'A'));
|
|
89 |
}
|
|
90 |
}
|
|
91 |
s = new String (b);
|
|
92 |
return s;
|
|
93 |
}
|
|
94 |
|
|
95 |
public int size() {return map.size();}
|
|
96 |
|
|
97 |
public boolean isEmpty() {return map.isEmpty();}
|
|
98 |
|
|
99 |
public boolean containsKey(Object key) {
|
|
100 |
if (key == null) {
|
|
101 |
return false;
|
|
102 |
}
|
|
103 |
if (!(key instanceof String)) {
|
|
104 |
return false;
|
|
105 |
}
|
|
106 |
return map.containsKey (normalize((String)key));
|
|
107 |
}
|
|
108 |
|
|
109 |
public boolean containsValue(Object value) {
|
|
110 |
return map.containsValue(value);
|
|
111 |
}
|
|
112 |
|
|
113 |
public List<String> get(Object key) {
|
|
114 |
return map.get(normalize((String)key));
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* returns the first value from the List of String values
|
|
119 |
* for the given key (if at least one exists).
|
|
120 |
* @param key the key to search for
|
|
121 |
* @return the first string value associated with the key
|
|
122 |
*/
|
|
123 |
public String getFirst (String key) {
|
|
124 |
List<String> l = map.get(normalize((String)key));
|
|
125 |
if (l == null) {
|
|
126 |
return null;
|
|
127 |
}
|
|
128 |
return l.get(0);
|
|
129 |
}
|
|
130 |
|
|
131 |
public List<String> put(String key, List<String> value) {
|
|
132 |
return map.put (normalize(key), value);
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* adds the given value to the list of headers
|
|
137 |
* for the given key. If the mapping does not
|
|
138 |
* already exist, then it is created
|
|
139 |
* @param key the header name
|
|
140 |
* @param value the header value to add to the header
|
|
141 |
*/
|
|
142 |
public void add (String key, String value) {
|
|
143 |
String k = normalize(key);
|
|
144 |
List<String> l = map.get(k);
|
|
145 |
if (l == null) {
|
|
146 |
l = new LinkedList<String>();
|
|
147 |
map.put(k,l);
|
|
148 |
}
|
|
149 |
l.add (value);
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* sets the given value as the sole header value
|
|
154 |
* for the given key. If the mapping does not
|
|
155 |
* already exist, then it is created
|
|
156 |
* @param key the header name
|
|
157 |
* @param value the header value to set.
|
|
158 |
*/
|
|
159 |
public void set (String key, String value) {
|
|
160 |
LinkedList<String> l = new LinkedList<String>();
|
|
161 |
l.add (value);
|
|
162 |
put (key, l);
|
|
163 |
}
|
|
164 |
|
|
165 |
|
|
166 |
public List<String> remove(Object key) {
|
|
167 |
return map.remove(normalize((String)key));
|
|
168 |
}
|
|
169 |
|
|
170 |
public void putAll(Map<? extends String,? extends List<String>> t) {
|
|
171 |
map.putAll (t);
|
|
172 |
}
|
|
173 |
|
|
174 |
public void clear() {map.clear();}
|
|
175 |
|
|
176 |
public Set<String> keySet() {return map.keySet();}
|
|
177 |
|
|
178 |
public Collection<List<String>> values() {return map.values();}
|
|
179 |
|
|
180 |
public Set<Map.Entry<String, List<String>>> entrySet() {
|
|
181 |
return map.entrySet();
|
|
182 |
}
|
|
183 |
|
|
184 |
public boolean equals(Object o) {return map.equals(o);}
|
|
185 |
|
|
186 |
public int hashCode() {return map.hashCode();}
|
|
187 |
}
|