src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/ImmutableHeaders.java
author chegar
Tue, 06 Feb 2018 14:10:28 +0000
branchhttp-client-branch
changeset 56079 d23b02f37fce
parent 56054 src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/ImmutableHeaders.java@352e845ae744
permissions -rw-r--r--
http-client-branch: more remaining impl types to internal
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 55973
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents: 56054
diff changeset
    26
package jdk.incubator.http.internal;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import java.util.ArrayList;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.util.List;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.util.Map;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    31
import java.util.TreeMap;
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    32
import java.util.function.BiPredicate;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import java.util.function.Predicate;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents: 56054
diff changeset
    34
import jdk.incubator.http.HttpHeaders;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import static java.util.Collections.emptyMap;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import static java.util.Collections.unmodifiableList;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import static java.util.Collections.unmodifiableMap;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import static java.util.Objects.requireNonNull;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    40
final class ImmutableHeaders extends HttpHeaders {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
    private final Map<String, List<String>> map;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
    public static ImmutableHeaders empty() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
        return of(emptyMap());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    46
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    48
    public static ImmutableHeaders of(Map<String, List<String>> src) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
        return of(src, x -> true);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 55973
diff changeset
    52
    public static ImmutableHeaders of(HttpHeaders headers) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 55973
diff changeset
    53
        return (headers instanceof ImmutableHeaders)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 55973
diff changeset
    54
                ? (ImmutableHeaders)headers
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 55973
diff changeset
    55
                : of(headers.map());
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 55973
diff changeset
    56
    }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 55973
diff changeset
    57
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
    public static ImmutableHeaders of(Map<String, List<String>> src,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
                                      Predicate<? super String> keyAllowed) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    60
        requireNonNull(src, "src");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
        requireNonNull(keyAllowed, "keyAllowed");
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    62
        return new ImmutableHeaders(src, headerAllowed(keyAllowed));
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    63
    }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    64
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    65
    public static ImmutableHeaders of(Map<String, List<String>> src,
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    66
                                      BiPredicate<? super String, ? super List<String>> headerAllowed) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    67
        requireNonNull(src, "src");
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    68
        requireNonNull(headerAllowed, "headerAllowed");
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    69
        return new ImmutableHeaders(src, headerAllowed);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    70
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    72
    private ImmutableHeaders(Map<String, List<String>> src,
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    73
                             BiPredicate<? super String, ? super List<String>> headerAllowed) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    74
        Map<String, List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    75
        src.entrySet().stream()
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    76
                .filter(e -> headerAllowed.test(e.getKey(), e.getValue()))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    77
                .forEach(e ->
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    78
                        {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    79
                            List<String> values = new ArrayList<>(e.getValue());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    80
                            m.put(e.getKey(), unmodifiableList(values));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    81
                        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    82
                );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    83
        this.map = unmodifiableMap(m);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    84
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    86
    private static BiPredicate<String, List<String>> headerAllowed(Predicate<? super String> keyAllowed) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    87
        return (n,v) -> keyAllowed.test(n);
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    88
    }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    89
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
    public Map<String, List<String>> map() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
        return map;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
}