src/java.net.http/share/classes/jdk/internal/net/http/ImmutableHttpRequest.java
author dfuchs
Tue, 15 Jan 2019 11:34:20 +0000
changeset 53300 54aa3ea04fe8
parent 50681 4254bed3c09d
permissions -rw-r--r--
8216974: HttpConnection not returned to the pool after 204 response Summary: MultiExchange now call nullBody() on Exchange after receiving 204 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     1
/*
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     4
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    10
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    15
 * accompanied this code).
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    16
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    20
 *
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    23
 * questions.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    24
 */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    25
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    26
package jdk.internal.net.http;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    27
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    28
import java.net.URI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    29
import java.net.http.HttpHeaders;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    30
import java.net.http.HttpRequest;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    31
import java.time.Duration;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    32
import java.util.Objects;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    33
import java.util.Optional;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    34
import java.net.http.HttpClient.Version;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    35
import static jdk.internal.net.http.common.Utils.ALLOWED_HEADERS;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    36
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    37
final class ImmutableHttpRequest extends HttpRequest {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    38
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    39
    private final String method;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    40
    private final URI uri;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    41
    private final HttpHeaders headers;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    42
    private final Optional<BodyPublisher> requestPublisher;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    43
    private final boolean expectContinue;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    44
    private final Optional<Duration> timeout;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    45
    private final Optional<Version> version;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    46
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    47
    /** Creates an ImmutableHttpRequest from the given builder. */
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    48
    ImmutableHttpRequest(HttpRequestBuilderImpl builder) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    49
        this.method = Objects.requireNonNull(builder.method());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    50
        this.uri = Objects.requireNonNull(builder.uri());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    51
        this.headers = HttpHeaders.of(builder.headersBuilder().map(), ALLOWED_HEADERS);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    52
        this.requestPublisher = Optional.ofNullable(builder.bodyPublisher());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    53
        this.expectContinue = builder.expectContinue();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    54
        this.timeout = Optional.ofNullable(builder.timeout());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    55
        this.version = Objects.requireNonNull(builder.version());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    56
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    57
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    58
    @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    59
    public String method() { return method; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    60
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    61
    @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    62
    public URI uri() { return uri; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    63
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    64
    @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    65
    public HttpHeaders headers() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    66
        return headers;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    67
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    68
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    69
    @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    70
    public Optional<BodyPublisher> bodyPublisher() { return requestPublisher; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    71
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    72
    @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    73
    public boolean expectContinue() { return expectContinue; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    74
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    75
    @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    76
    public Optional<Duration> timeout() { return timeout; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    77
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    78
    @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    79
    public Optional<Version> version() { return version; }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    80
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    81
    @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    82
    public String toString() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    83
        return uri.toString() + " " + method;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    84
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    85
}