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 jdk.internal.net.http; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.Map; 31 import java.util.Objects; 32 import java.util.TreeMap; 33 import java.util.function.BiPredicate; 34 import java.util.function.Predicate; 35 import java.net.http.HttpHeaders; 36 import jdk.internal.net.http.common.HttpHeadersImpl; 37 import jdk.internal.net.http.common.Utils; 38 import static java.util.Collections.emptyMap; 39 import static java.util.Collections.unmodifiableList; 40 import static java.util.Collections.unmodifiableMap; 41 import static java.util.Objects.requireNonNull; 42 43 final class ImmutableHeaders extends HttpHeaders { 44 45 private final Map<String, List<String>> map; 46 47 public static ImmutableHeaders empty() { 48 return of(emptyMap()); 49 } 50 51 public static ImmutableHeaders of(Map<String, List<String>> src) { 52 return of(src, x -> true); 53 } 54 55 public static ImmutableHeaders of(HttpHeaders headers) { 56 return (headers instanceof ImmutableHeaders) 57 ? (ImmutableHeaders)headers 58 : of(headers.map()); 59 } 60 61 static ImmutableHeaders validate(HttpHeaders headers) { 62 if (headers instanceof ImmutableHeaders) { 63 return of(headers); 64 } 65 if (headers instanceof HttpHeadersImpl) { 66 return of(headers); 67 } 68 Map<String, List<String>> map = headers.map(); 69 return new ImmutableHeaders(map, Utils.VALIDATE_USER_HEADER); 70 } 71 72 public static ImmutableHeaders of(Map<String, List<String>> src, 73 Predicate<? super String> keyAllowed) { 74 requireNonNull(src, "src"); 75 requireNonNull(keyAllowed, "keyAllowed"); 76 return new ImmutableHeaders(src, headerAllowed(keyAllowed)); 77 } 78 79 public static ImmutableHeaders of(Map<String, List<String>> src, 80 BiPredicate<? super String, ? super List<String>> headerAllowed) { 81 requireNonNull(src, "src"); 82 requireNonNull(headerAllowed, "headerAllowed"); 83 return new ImmutableHeaders(src, headerAllowed); 84 } 85 86 private ImmutableHeaders(Map<String, List<String>> src, 87 BiPredicate<? super String, ? super List<String>> headerAllowed) { 88 Map<String, List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); 89 src.entrySet().stream() 90 .forEach(e -> addIfAllowed(e, headerAllowed, m)); 91 this.map = unmodifiableMap(m); 92 } 93 94 private static void addIfAllowed(Map.Entry<String, List<String>> e, 95 BiPredicate<? super String, ? super List<String>> headerAllowed, 96 Map<String, List<String>> map) { 97 String key = e.getKey(); 98 List<String> values = unmodifiableValues(e.getValue()); 99 if (headerAllowed.test(key, values)) { 100 map.put(key, values); 101 } 102 } 103 104 private static List<String> unmodifiableValues(List<String> values) { 105 return unmodifiableList(new ArrayList<>(Objects.requireNonNull(values))); 106 } 107 108 private static BiPredicate<String, List<String>> headerAllowed(Predicate<? super String> keyAllowed) { 109 return (n,v) -> keyAllowed.test(n); 110 } 111 112 @Override 113 public Map<String, List<String>> map() { 114 return map; 115 } 116 }