aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/HeaderFields.java
blob: ae2638115eae0a68b7137e26682434885704f600 (plain) (blame)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * This is an encapsulation of the header fields that belong to either a {@link Request} or a {@link Response}. It is
 * a multimap from String to String, with some additional methods for convenience. The keys of this map are compared by
 * ignoring their case, so that <code>get("foo")</code> returns the same entry as <code>get("FOO")</code>.
 *
 * @author Simon Thoresen Hult
 */
public class HeaderFields implements Map<String, List<String>> {

    private final ConcurrentSkipListMap<String, List<String>> content = new ConcurrentSkipListMap<>(String::compareToIgnoreCase);

    @Override
    public int size() {
        return content.size();
    }

    @Override
    public boolean isEmpty() {
        return content.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return content.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return content.containsValue(value);
    }

    /**
     * <p>Convenience method for checking whether or not a named header contains a specific value. If the named header
     * is not set, or if the given value is not contained within that header's value list, this method returns
     * <em>false</em>.</p>
     *
     * <p><em>NOTE:</em> This method is case-SENSITIVE.</p>
     *
     * @param key   The key whose values to search in.
     * @param value The values to search for.
     * @return True if the given value was found in the named header.
     * @see #containsIgnoreCase
     */
    public boolean contains(String key, String value) {
        List<String> lst = content.get(key);
        if (lst == null) {
            return false;
        }
        return lst.contains(value);
    }

    /**
     * <p>Convenience method for checking whether or not a named header contains a specific value, regardless of case.
     * If the named header is not set, or if the given value is not contained within that header's value list, this
     * method returns <em>false</em>.</p>
     *
     * <p><em>NOTE:</em> This method is case-INSENSITIVE.</p>
     *
     * @param key   The key whose values to search in.
     * @param value The values to search for, ignoring case.
     * @return True if the given value was found in the named header.
     * @see #contains
     */
    public boolean containsIgnoreCase(String key, String value) {
        List<String> lst = content.get(key);
        if (lst == null) {
            return false;
        }
        for (String val : lst) {
            if (value.equalsIgnoreCase(val)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Adds the given value to the entry of the specified key. If no entry exists for the given key, a new one is
     * created containing only the given value.
     *
     * @param key   The key with which the specified value is to be associated.
     * @param value The value to be added to the list associated with the specified key.
     */
    public void add(String key, String value) {
        List<String> lst = content.get(key);
        if (lst != null) {
            lst.add(value);
        } else {
            put(key, value);
        }
    }

    /**
     * Adds the given values to the entry of the specified key. If no entry exists for the given key, a new one is
     * created containing only the given values.
     *
     * @param key    the key with which the specified value is to be associated.
     * @param values the values to be added to the list associated with the specified key.
     */
    public void add(String key, List<String> values) {
        List<String> lst = content.get(key);
        if (lst != null) {
            lst.addAll(values);
        } else {
            put(key, values);
        }
    }

    /**
     * Adds all the entries of the given map to this. This is the same as calling {@link #add(String, List)} for each
     * entry in <code>values</code>.
     *
     * @param values the values to be added to this.
     */
    public void addAll(Map<? extends String, ? extends List<String>> values) {
        for (Entry<? extends String, ? extends List<String>> entry : values.entrySet()) {
            add(entry.getKey(), entry.getValue());
        }
    }

    /**
     * <p>Convenience method to call {@link #put(String, List)} with a singleton list that contains the specified
     * value.</p>
     *
     * @param key   The key of the entry to put.
     * @param value The value to put.
     * @return The previous value associated with <code>key</code>, or <code>null</code> if there was no mapping for
     *         <code>key</code>.
     */
    public List<String> put(String key, String value) {
        List<String> list = Collections.synchronizedList(new ArrayList<>(1));
        list.add(value);
        return content.put(key, list);
    }

    @Override
    public List<String> put(String key, List<String> value) {
        return content.put(key, Collections.synchronizedList(new ArrayList<>(value)));
    }

    @Override
    public void putAll(Map<? extends String, ? extends List<String>> values) {
        for (Entry<? extends String, ? extends List<String>> entry : values.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public List<String> remove(Object key) {
        return content.remove(key);
    }

    /**
     * <p>Removes the given value from the entry of the specified key.</p>
     *
     * @param key   The key of the entry to remove from.
     * @param value The value to remove from the entry.
     * @return True if the value was removed.
     */
    public boolean remove(String key, String value) {
        List<String> lst = content.get(key);
        if (lst == null) {
            return false;
        }
        if (!lst.remove(value)) {
            return false;
        }
        if (lst.isEmpty()) {
            content.remove(key);
        }
        return true;
    }

    @Override
    public void clear() {
        content.clear();
    }

    @Override
    public List<String> get(Object key) {
        return content.get(key);
    }

    /**
     * Convenience method for retrieving the first value of a named header field. If the header is not set, or if the
     * value list is empty, this method returns null.
     *
     * @param key the key whose first value to return
     * @return the first value of the named header, or null
     */
    public String getFirst(String key) {
        List<String> lst = get(key);
        if (lst == null || lst.isEmpty()) {
            return null;
        }
        return lst.get(0);
    }

    /**
     * Convenience method for checking whether a named header field is <em>true</em>. To satisfy this, the
     * header field needs to have at least 1 entry, and Boolean.valueOf() of all its values must parse as
     * <em>true</em>.
     *
     * @param key the key whose values to parse as a boolean.
     * @return the boolean value of the named header.
     */
    public boolean isTrue(String key) {
        List<String> lst = content.get(key);
        if (lst == null) {
            return false;
        }
        for (String value : lst) {
            if (!Boolean.valueOf(value)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Set<String> keySet() {
        return content.keySet();
    }

    @Override
    public Collection<List<String>> values() {
        return content.values();
    }

    @Override
    public Set<Entry<String, List<String>>> entrySet() {
        return content.entrySet();
    }

    @Override
    public String toString() {
        return content.toString();
    }

    /**
     * Returns an unmodifiable list of all key-value pairs of this. This provides a flattened view on the content of
     * this map.
     *
     * @return the collection of entries
     */
    public List<Entry<String, String>> entries() {
        List<Entry<String, String>> list = new ArrayList<>(content.size());
        for (Entry<String, List<String>> entry : content.entrySet()) {
            String key = entry.getKey();
            for (String value : entry.getValue()) {
                list.add(new MyEntry(key, value));
            }
        }
        return List.copyOf(list);
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof HeaderFields && content.equals(((HeaderFields)obj).content);
    }

    @Override
    public int hashCode() {
        return content.hashCode();
    }

    private static class MyEntry implements Map.Entry<String, String> {

        final String key;
        final String value;

        private MyEntry(String key, String value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public String getKey() {
            return key;
        }

        @Override
        public String getValue() {
            return value;
        }

        @Override
        public String setValue(String value) {
            throw new UnsupportedOperationException();
        }

        @Override
        public String toString() {
            return key + '=' + value;
        }

    }

}