jdk/src/java.httpclient/share/classes/java/net/http/ImmutableHeaders.java
author vinnie
Mon, 15 Aug 2016 14:25:51 +0100
changeset 40276 2217f830ca7b
parent 39730 196f4e25d9f5
permissions -rw-r--r--
8163503: PKCS12 keystore cannot store non-X.509 certificates Reviewed-by: weijun, xuelei, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     4
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    10
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    15
 * accompanied this code).
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    16
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    20
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 37720
diff changeset
    23
 * questions.
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    24
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    25
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    26
package java.net.http;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    27
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    28
import java.util.Collections;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    29
import java.util.List;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    30
import java.util.Map;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    31
import java.util.Optional;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    32
import java.util.TreeMap;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
import java.util.function.Predicate;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
/**
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
 * Immutable HttpHeaders constructed from mutable HttpHeadersImpl.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
class ImmutableHeaders implements HttpHeaders {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
    private final Map<String,List<String>> map;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
    @SuppressWarnings("unchecked")
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
    ImmutableHeaders() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
        map = (Map<String,List<String>>)Collections.EMPTY_MAP;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
    // TODO: fix lower case issue. Must be lc for http/2 compares ignoreCase for http/1
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
    ImmutableHeaders(HttpHeadersImpl h, Predicate<String> keyAllowed) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
        Map<String,List<String>> src = h.directMap();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
        Map<String,List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
        src.forEach((key, value) -> {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
            if (keyAllowed.test(key))
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
                m.put(key, Collections.unmodifiableList(value));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
        });
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
        map = Collections.unmodifiableMap(m);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
    public Optional<String> firstValue(String name) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
        List<String> l = map.get(name);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
        String v = l == null ? null : l.get(0);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
        return Optional.ofNullable(v);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
    public Optional<Long> firstValueAsLong(String name) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
        return firstValue(name).map((v -> Long.parseLong(v)));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    72
    public List<String> allValues(String name) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
        return map.get(name);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    76
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
    public Map<String, List<String>> map() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
        return map;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
}