test/jdk/java/net/httpclient/offline/FixedHttpHeaders.java
author chegar
Wed, 07 Feb 2018 14:17:24 +0000
branchhttp-client-branch
changeset 56089 42208b2f224e
parent 56023 fa36a61f4cbf
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: move to standard package and module name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     1
/*
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     4
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     8
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    13
 * accompanied this code).
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    14
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    18
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    21
 * questions.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    22
 */
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    23
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    24
import java.util.ArrayList;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    25
import java.util.List;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    26
import java.util.Map;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    27
import java.util.Objects;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    28
import java.util.TreeMap;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    29
import java.net.http.HttpHeaders;
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    30
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    31
/**
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    32
 * An HttpHeaders consisting of the given name value pairs.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    33
 */
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    34
public class FixedHttpHeaders extends HttpHeaders {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    35
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    36
    private final Map<String, List<String>> map =
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    37
            new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    38
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    39
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    40
    public Map<String, List<String>> map() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    41
        return map;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    42
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    43
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    44
    /**
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    45
     * Creates an HttpHeaders of the given name value pairs.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    46
     */
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    47
    public static HttpHeaders of(String... params) {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    48
        Objects.requireNonNull(params);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    49
        if ((params.length & 0x1) != 0)
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    50
            throw new IllegalArgumentException("odd number of params");
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    51
        FixedHttpHeaders headers = new FixedHttpHeaders();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    52
        for (int i = 0; i < params.length; i += 2) {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    53
            String name = params[i];
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    54
            String value = params[i + 1];
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    55
            headers.map.computeIfAbsent(name, k -> new ArrayList<>(1))
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    56
                       .add(value);
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    57
        }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    58
        return headers;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    59
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    60
}