jdk/src/java.httpclient/share/classes/java/net/http/ImmutableHeaders.java
author michaelm
Sat, 30 Apr 2016 00:30:31 +0100
changeset 37720 45cd7cc65382
child 39730 196f4e25d9f5
permissions -rw-r--r--
8087124: HTTP/2 implementation Reviewed-by: chegar
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
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    23
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    24
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    25
package java.net.http;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    26
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    27
import java.util.Collections;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    28
import java.util.List;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    29
import java.util.Map;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    30
import java.util.Optional;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    31
import java.util.TreeMap;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    32
import java.util.function.Predicate;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
/**
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
 * Immutable HttpHeaders constructed from mutable HttpHeadersImpl.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
class ImmutableHeaders implements HttpHeaders {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
    private final Map<String,List<String>> map;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
    @SuppressWarnings("unchecked")
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
    ImmutableHeaders() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
        map = (Map<String,List<String>>)Collections.EMPTY_MAP;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
    // 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
    47
    ImmutableHeaders(HttpHeadersImpl h, Predicate<String> keyAllowed) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
        Map<String,List<String>> src = h.directMap();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
        Map<String,List<String>> m = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
        src.forEach((key, value) -> {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
            if (keyAllowed.test(key))
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
                m.put(key, Collections.unmodifiableList(value));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
        });
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
        map = Collections.unmodifiableMap(m);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
    public Optional<String> firstValue(String name) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
        List<String> l = map.get(name);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
        String v = l == null ? null : l.get(0);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
        return Optional.ofNullable(v);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
    public Optional<Long> firstValueAsLong(String name) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
        return firstValue(name).map((v -> Long.parseLong(v)));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
    public List<String> allValues(String name) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    72
        return map.get(name);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    76
    public Map<String, List<String>> map() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
        return map;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
}