|
1 /* |
|
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
|
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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 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. |
|
24 */ |
|
25 |
|
26 package java.net.http.internal; |
|
27 |
|
28 import java.util.ArrayList; |
|
29 import java.util.List; |
|
30 import java.util.Map; |
|
31 import java.util.TreeMap; |
|
32 import java.util.function.BiPredicate; |
|
33 import java.util.function.Predicate; |
|
34 import java.net.http.HttpHeaders; |
|
35 import static java.util.Collections.emptyMap; |
|
36 import static java.util.Collections.unmodifiableList; |
|
37 import static java.util.Collections.unmodifiableMap; |
|
38 import static java.util.Objects.requireNonNull; |
|
39 |
|
40 final class ImmutableHeaders extends HttpHeaders { |
|
41 |
|
42 private final Map<String, List<String>> map; |
|
43 |
|
44 public static ImmutableHeaders empty() { |
|
45 return of(emptyMap()); |
|
46 } |
|
47 |
|
48 public static ImmutableHeaders of(Map<String, List<String>> src) { |
|
49 return of(src, x -> true); |
|
50 } |
|
51 |
|
52 public static ImmutableHeaders of(HttpHeaders headers) { |
|
53 return (headers instanceof ImmutableHeaders) |
|
54 ? (ImmutableHeaders)headers |
|
55 : of(headers.map()); |
|
56 } |
|
57 |
|
58 public static ImmutableHeaders of(Map<String, List<String>> src, |
|
59 Predicate<? super String> keyAllowed) { |
|
60 requireNonNull(src, "src"); |
|
61 requireNonNull(keyAllowed, "keyAllowed"); |
|
62 return new ImmutableHeaders(src, headerAllowed(keyAllowed)); |
|
63 } |
|
64 |
|
65 public static ImmutableHeaders of(Map<String, List<String>> src, |
|
66 BiPredicate<? super String, ? super List<String>> headerAllowed) { |
|
67 requireNonNull(src, "src"); |
|
68 requireNonNull(headerAllowed, "headerAllowed"); |
|
69 return new ImmutableHeaders(src, headerAllowed); |
|
70 } |
|
71 |
|
72 private ImmutableHeaders(Map<String, List<String>> src, |
|
73 BiPredicate<? super String, ? super List<String>> headerAllowed) { |
|
74 Map<String, List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
|
75 src.entrySet().stream() |
|
76 .filter(e -> headerAllowed.test(e.getKey(), e.getValue())) |
|
77 .forEach(e -> |
|
78 { |
|
79 List<String> values = new ArrayList<>(e.getValue()); |
|
80 m.put(e.getKey(), unmodifiableList(values)); |
|
81 } |
|
82 ); |
|
83 this.map = unmodifiableMap(m); |
|
84 } |
|
85 |
|
86 private static BiPredicate<String, List<String>> headerAllowed(Predicate<? super String> keyAllowed) { |
|
87 return (n,v) -> keyAllowed.test(n); |
|
88 } |
|
89 |
|
90 @Override |
|
91 public Map<String, List<String>> map() { |
|
92 return map; |
|
93 } |
|
94 } |