src/java.net.http/share/classes/java/net/http/HttpRequest.java
author chegar
Wed, 20 Jun 2018 09:05:57 -0700
changeset 50681 4254bed3c09d
parent 50095 bf2f27b92064
child 52499 768b1c612100
child 56795 03ece2518428
permissions -rw-r--r--
8204679: HTTP Client refresh Reviewed-by: chegar, dfuchs, michaelm Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
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
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
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
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    26
package java.net.http;
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.io.FileNotFoundException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.io.InputStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    31
import java.nio.ByteBuffer;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    32
import java.nio.charset.Charset;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    33
import java.nio.charset.StandardCharsets;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.nio.file.Path;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.time.Duration;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.util.Iterator;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    37
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import java.util.Optional;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
import java.util.concurrent.Flow;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.util.function.Supplier;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    41
import jdk.internal.net.http.HttpRequestBuilderImpl;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    42
import jdk.internal.net.http.RequestPublishers;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    43
import static java.nio.charset.StandardCharsets.UTF_8;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
/**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    46
 * An HTTP request.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    47
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    48
 * <p> An {@code HttpRequest} instance is built through an {@code HttpRequest}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    49
 * {@linkplain HttpRequest.Builder builder}. An {@code HttpRequest} builder
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    50
 * is obtained from one of the {@link HttpRequest#newBuilder(URI) newBuilder}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    51
 * methods. A request's {@link URI}, headers, and body can be set. Request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    52
 * bodies are provided through a {@link BodyPublisher BodyPublisher} supplied
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    53
 * to one of the {@link Builder#POST(BodyPublisher) POST},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    54
 * {@link Builder#PUT(BodyPublisher) PUT} or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    55
 * {@link Builder#method(String,BodyPublisher) method} methods.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    56
 * Once all required parameters have been set in the builder, {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    57
 * Builder#build() build} will return the {@code HttpRequest}. Builders can be
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    58
 * copied and modified many times in order to build multiple related requests
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    59
 * that differ in some parameters.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    60
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    61
 * <p> The following is an example of a GET request that prints the response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    62
 * body as a String:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    64
 * <pre>{@code    HttpClient client = HttpClient.newHttpClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    65
 *   HttpRequest request = HttpRequest.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    66
 *         .uri(URI.create("http://foo.com/"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    67
 *         .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    68
 *   client.sendAsync(request, BodyHandlers.ofString())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    69
 *         .thenApply(HttpResponse::body)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    70
 *         .thenAccept(System.out::println)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    71
 *         .join(); }</pre>
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    72
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    73
 * <p>The class {@link BodyPublishers BodyPublishers} provides implementations
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    74
 * of many common publishers. Alternatively, a custom {@code BodyPublisher}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    75
 * implementation can be used.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    76
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    77
 * @since 11
42460
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
public abstract class HttpRequest {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    80
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
     * Creates an HttpRequest.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    83
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    84
    protected HttpRequest() {}
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    87
     * A builder of {@linkplain HttpRequest HTTP requests}.
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    88
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
     * <p> Instances of {@code HttpRequest.Builder} are created by calling {@link
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    90
     * HttpRequest#newBuilder(URI)} or {@link HttpRequest#newBuilder()}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
     *
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 50095
diff changeset
    92
     * <p> The builder can be used to configure per-request state, such as: the
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 50095
diff changeset
    93
     * request URI, the request method (default is GET unless explicitly set),
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 50095
diff changeset
    94
     * specific request headers, etc. Each of the setter methods modifies the
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 50095
diff changeset
    95
     * state of the builder and returns the same instance. The methods are not
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 50095
diff changeset
    96
     * synchronized and should not be called from multiple threads without
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 50095
diff changeset
    97
     * external synchronization. The {@link #build() build} method returns a new
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    98
     * {@code HttpRequest} each time it is invoked. Once built an {@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    99
     * HttpRequest} is immutable, and can be sent multiple times.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   100
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   101
     * <p> Note, that not all request headers may be set by user code. Some are
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   102
     * restricted for security reasons and others such as the headers relating
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   103
     * to authentication, redirection and cookie management may be managed by
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
     * specific APIs rather than through directly user set headers.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   105
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   106
     * @since 11
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   108
    public interface Builder {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
         * Sets this {@code HttpRequest}'s request {@code URI}.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
         * @param uri the request URI
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   114
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
         * @throws IllegalArgumentException if the {@code URI} scheme is not
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
         *         supported
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   118
        public Builder uri(URI uri);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   119
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   121
         * Requests the server to acknowledge the request before sending the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   122
         * body. This is disabled by default. If enabled, the server is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   123
         * requested to send an error response or a {@code 100 Continue}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   124
         * response before the client sends the request body. This means the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   125
         * request publisher for the request will not be invoked until this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
         * interim response is received.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   127
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   128
         * @param enable {@code true} if Expect continue to be sent
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   129
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   130
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   131
        public Builder expectContinue(boolean enable);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   134
         * Sets the preferred {@link HttpClient.Version} for this request.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   135
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   136
         * <p> The corresponding {@link HttpResponse} should be checked for the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   137
         * version that was actually used. If the version is not set in a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
         * request, then the version requested will be that of the sending
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
         * {@link HttpClient}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   140
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   141
         * @param version the HTTP protocol version requested
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   142
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   143
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   144
        public Builder version(HttpClient.Version version);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   145
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   146
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   147
         * Adds the given name value pair to the set of headers for this request.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   148
         * The given value is added to the list of values for that name.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   149
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   150
         * @implNote An implementation may choose to restrict some header names
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   151
         *           or values, as the HTTP Client may determine their value itself.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   152
         *           For example, "Content-Length", which will be determined by
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   153
         *           the request Publisher. In such a case, an implementation of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   154
         *           {@code HttpRequest.Builder} may choose to throw an
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   155
         *           {@code IllegalArgumentException} if such a header is passed
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   156
         *           to the builder.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   157
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   158
         * @param name the header name
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   159
         * @param value the header value
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   160
         * @return this builder
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   161
         * @throws IllegalArgumentException if the header name or value is not
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   162
         *         valid, see <a href="https://tools.ietf.org/html/rfc7230#section-3.2">
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   163
         *         RFC 7230 section-3.2</a>, or the header name or value is restricted
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   164
         *         by the implementation.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   165
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   166
        public Builder header(String name, String value);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   167
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   168
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   169
         * Adds the given name value pairs to the set of headers for this
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   170
         * request. The supplied {@code String} instances must alternate as
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   171
         * header names and header values.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
         * To add several values to the same name then the same name must
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   173
         * be supplied with each new value.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   174
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   175
         * @param headers the list of name value pairs
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   176
         * @return this builder
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
         * @throws IllegalArgumentException if there are an odd number of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   178
         *         parameters, or if a header name or value is not valid, see
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   179
         *         <a href="https://tools.ietf.org/html/rfc7230#section-3.2">
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   180
         *         RFC 7230 section-3.2</a>, or a header name or value is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   181
         *         {@linkplain #header(String, String) restricted} by the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   182
         *         implementation.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   183
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   184
        public Builder headers(String... headers);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   185
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   186
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   187
         * Sets a timeout for this request. If the response is not received
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   188
         * within the specified timeout then an {@link HttpTimeoutException} is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   189
         * thrown from {@link HttpClient#send(java.net.http.HttpRequest,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   190
         * java.net.http.HttpResponse.BodyHandler) HttpClient::send} or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   191
         * {@link HttpClient#sendAsync(java.net.http.HttpRequest,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   192
         * java.net.http.HttpResponse.BodyHandler) HttpClient::sendAsync}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   193
         * completes exceptionally with an {@code HttpTimeoutException}. The effect
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   194
         * of not setting a timeout is the same as setting an infinite Duration, ie.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   195
         * block forever.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   196
         *
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43302
diff changeset
   197
         * @param duration the timeout duration
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   198
         * @return this builder
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   199
         * @throws IllegalArgumentException if the duration is non-positive
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   200
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   201
        public abstract Builder timeout(Duration duration);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   202
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   203
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   204
         * Sets the given name value pair to the set of headers for this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   205
         * request. This overwrites any previously set values for name.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   206
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   207
         * @param name the header name
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   208
         * @param value the header value
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   209
         * @return this builder
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   210
         * @throws IllegalArgumentException if the header name or value is not valid,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   211
         *         see <a href="https://tools.ietf.org/html/rfc7230#section-3.2">
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   212
         *         RFC 7230 section-3.2</a>, or the header name or value is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   213
         *         {@linkplain #header(String, String) restricted} by the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   214
         *         implementation.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   215
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   216
        public Builder setHeader(String name, String value);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   217
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   218
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   219
         * Sets the request method of this builder to GET.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   220
         * This is the default.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   221
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   222
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   223
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   224
        public Builder GET();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   225
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   226
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   227
         * Sets the request method of this builder to POST and sets its
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   228
         * request body publisher to the given value.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   229
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   230
         * @param bodyPublisher the body publisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   231
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   232
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   233
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   234
        public Builder POST(BodyPublisher bodyPublisher);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   235
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   236
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   237
         * Sets the request method of this builder to PUT and sets its
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   238
         * request body publisher to the given value.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   239
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   240
         * @param bodyPublisher the body publisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   241
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   242
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   243
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   244
        public Builder PUT(BodyPublisher bodyPublisher);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   245
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   246
        /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   247
         * Sets the request method of this builder to DELETE.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   248
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   249
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   251
        public Builder DELETE();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   252
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   253
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   254
         * Sets the request method and request body of this builder to the
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   255
         * given values.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   256
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   257
         * @apiNote The {@link BodyPublishers#noBody() noBody} request
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   258
         * body publisher can be used where no request body is required or
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   259
         * appropriate. Whether a method is restricted, or not, is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   260
         * implementation specific. For example, some implementations may choose
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   261
         * to restrict the {@code CONNECT} method.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   262
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   263
         * @param method the method to use
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   264
         * @param bodyPublisher the body publisher
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   265
         * @return this builder
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   266
         * @throws IllegalArgumentException if the method name is not
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   267
         *         valid, see <a href="https://tools.ietf.org/html/rfc7230#section-3.1.1">
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   268
         *         RFC 7230 section-3.1.1</a>, or the method is restricted by the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   269
         *         implementation.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   270
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   271
        public Builder method(String method, BodyPublisher bodyPublisher);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   272
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   273
        /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   274
         * Builds and returns an {@link HttpRequest}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   275
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   276
         * @return a new {@code HttpRequest}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   277
         * @throws IllegalStateException if a URI has not been set
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   278
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   279
        public HttpRequest build();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   280
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   281
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   282
         * Returns an exact duplicate copy of this {@code Builder} based on
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   283
         * current state. The new builder can then be modified independently of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   284
         * this builder.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   285
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   286
         * @return an exact copy of this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   287
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   288
        public Builder copy();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   289
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   290
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   291
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   292
     * Creates an {@code HttpRequest} builder with the given URI.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   293
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   294
     * @param uri the request URI
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   295
     * @return a new request builder
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   296
     * @throws IllegalArgumentException if the URI scheme is not supported.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   297
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   298
    public static HttpRequest.Builder newBuilder(URI uri) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   299
        return new HttpRequestBuilderImpl(uri);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   300
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   301
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   302
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   303
     * Creates an {@code HttpRequest} builder.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   304
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   305
     * @return a new request builder
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   306
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   307
    public static HttpRequest.Builder newBuilder() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   308
        return new HttpRequestBuilderImpl();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   309
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   310
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   311
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   312
     * Returns an {@code Optional} containing the {@link BodyPublisher} set on
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   313
     * this request. If no {@code BodyPublisher} was set in the requests's
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   314
     * builder, then the {@code Optional} is empty.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   315
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   316
     * @return an {@code Optional} containing this request's {@code BodyPublisher}
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   317
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   318
    public abstract Optional<BodyPublisher> bodyPublisher();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   319
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   320
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   321
     * Returns the request method for this request. If not set explicitly,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   322
     * the default method for any request is "GET".
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   323
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   324
     * @return this request's method
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   325
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   326
    public abstract String method();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   327
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   328
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   329
     * Returns an {@code Optional} containing this request's timeout duration.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   330
     * If the timeout duration was not set in the request's builder, then the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   331
     * {@code Optional} is empty.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   332
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   333
     * @return an {@code Optional} containing this request's timeout duration
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   334
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   335
    public abstract Optional<Duration> timeout();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   336
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   337
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   338
     * Returns this request's {@linkplain HttpRequest.Builder#expectContinue(boolean)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   339
     * expect continue} setting.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   340
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   341
     * @return this request's expect continue setting
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   342
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   343
    public abstract boolean expectContinue();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   344
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   345
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   346
     * Returns this request's {@code URI}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   347
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   348
     * @return this request's URI
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   349
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   350
    public abstract URI uri();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   351
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   352
    /**
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44847
diff changeset
   353
     * Returns an {@code Optional} containing the HTTP protocol version that
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44847
diff changeset
   354
     * will be requested for this {@code HttpRequest}. If the version was not
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44847
diff changeset
   355
     * set in the request's builder, then the {@code Optional} is empty.
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44847
diff changeset
   356
     * In that case, the version requested will be that of the sending
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44847
diff changeset
   357
     * {@link HttpClient}. The corresponding {@link HttpResponse} should be
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   358
     * queried to determine the version that was actually used.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   359
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   360
     * @return HTTP protocol version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   361
     */
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44847
diff changeset
   362
    public abstract Optional<HttpClient.Version> version();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   363
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   364
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   365
     * The (user-accessible) request headers that this request was (or will be)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   366
     * sent with.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   367
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   368
     * @return this request's HttpHeaders
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   369
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   370
    public abstract HttpHeaders headers();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   371
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   372
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   373
     * Tests this HTTP request instance for equality with the given object.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   374
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
     * <p> If the given object is not an {@code HttpRequest} then this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   376
     * method returns {@code false}. Two HTTP requests are equal if their URI,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   377
     * method, and headers fields are all equal.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   378
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   379
     * <p> This method satisfies the general contract of the {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   380
     * Object#equals(Object) Object.equals} method.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   381
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   382
     * @param obj the object to which this object is to be compared
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
     * @return {@code true} if, and only if, the given object is an {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
     *         HttpRequest} that is equal to this HTTP request
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   385
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   387
    public final boolean equals(Object obj) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   388
       if (! (obj instanceof HttpRequest))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   389
           return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   390
       HttpRequest that = (HttpRequest)obj;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   391
       if (!that.method().equals(this.method()))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   392
           return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
       if (!that.uri().equals(this.uri()))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   394
           return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   395
       if (!that.headers().equals(this.headers()))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   396
           return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   397
       return true;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   398
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   399
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   400
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   401
     * Computes a hash code for this HTTP request instance.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   402
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   403
     * <p> The hash code is based upon the HTTP request's URI, method, and
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   404
     * header components, and satisfies the general contract of the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   405
     * {@link Object#hashCode Object.hashCode} method.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   406
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   407
     * @return the hash-code value for this HTTP request
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   408
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   409
    public final int hashCode() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   410
        return method().hashCode()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   411
                + uri().hashCode()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   412
                + headers().hashCode();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   413
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   414
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   415
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   416
     * A {@code BodyPublisher} converts high-level Java objects into a flow of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   417
     * byte buffers suitable for sending as a request body.  The class
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   418
     * {@link BodyPublishers BodyPublishers} provides implementations of many
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   419
     * common publishers.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   420
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   421
     * <p> The {@code BodyPublisher} interface extends {@link Flow.Publisher
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   422
     * Flow.Publisher&lt;ByteBuffer&gt;}, which means that a {@code BodyPublisher}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   423
     * acts as a publisher of {@linkplain ByteBuffer byte buffers}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   424
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   425
     * <p> When sending a request that contains a body, the HTTP Client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   426
     * subscribes to the request's {@code BodyPublisher} in order to receive the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   427
     * flow of outgoing request body data. The normal semantics of {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   428
     * Flow.Subscriber} and {@link Flow.Publisher} are implemented by the HTTP
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   429
     * Client and are expected from {@code BodyPublisher} implementations. Each
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   430
     * outgoing request results in one HTTP Client {@code Subscriber}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   431
     * subscribing to the {@code BodyPublisher} in order to provide the sequence
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   432
     * of byte buffers containing the request body. Instances of {@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   433
     * ByteBuffer} published by the publisher must be allocated by the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   434
     * publisher, and must not be accessed after being published to the HTTP
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   435
     * Client. These subscriptions complete normally when the request body is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   436
     * fully sent, and can be canceled or terminated early through error. If a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   437
     * request needs to be resent for any reason, then a new subscription is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   438
     * created which is expected to generate the same data as before.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   439
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   440
     * <p> A {@code BodyPublisher} that reports a {@linkplain #contentLength()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   441
     * content length} of {@code 0} may not be subscribed to by the HTTP Client,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   442
     * as it has effectively no data to publish.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   443
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   444
     * @see BodyPublishers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   445
     * @since 11
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   446
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   447
    public interface BodyPublisher extends Flow.Publisher<ByteBuffer> {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   448
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   449
        /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   450
         * Returns the content length for this request body. May be zero
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   451
         * if no request body being sent, greater than zero for a fixed
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   452
         * length content, or less than zero for an unknown content length.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   453
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   454
         * <p> This method may be invoked before the publisher is subscribed to.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   455
         * This method may be invoked more than once by the HTTP client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   456
         * implementation, and MUST return the same constant value each time.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   457
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   458
         * @return the content length for this request body, if known
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   459
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   460
        long contentLength();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   461
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   462
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   463
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   464
     * Implementations of {@link BodyPublisher BodyPublisher} that implement
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   465
     * various useful publishers, such as publishing the request body from a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   466
     * String, or from a file.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   467
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   468
     * <p> The following are examples of using the predefined body publishers to
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   469
     * convert common high-level Java objects into a flow of data suitable for
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   470
     * sending as a request body:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   471
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   472
     *  <pre>{@code    // Request body from a String
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   473
     *   HttpRequest request = HttpRequest.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   474
     *        .uri(URI.create("https://foo.com/"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   475
     *        .header("Content-Type", "text/plain; charset=UTF-8")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   476
     *        .POST(BodyPublishers.ofString("some body text"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   477
     *        .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   478
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   479
     *   // Request body from a File
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   480
     *   HttpRequest request = HttpRequest.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   481
     *        .uri(URI.create("https://foo.com/"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   482
     *        .header("Content-Type", "application/json")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   483
     *        .POST(BodyPublishers.ofFile(Paths.get("file.json")))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   484
     *        .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   485
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   486
     *   // Request body from a byte array
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   487
     *   HttpRequest request = HttpRequest.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   488
     *        .uri(URI.create("https://foo.com/"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   489
     *        .POST(BodyPublishers.ofByteArray(new byte[] { ... }))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   490
     *        .build(); }</pre>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   491
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   492
     * @since 11
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   493
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   494
    public static class BodyPublishers {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   495
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   496
        private BodyPublishers() { }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   497
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   498
        /**
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   499
         * Returns a request body publisher whose body is retrieved from the
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   500
         * given {@code Flow.Publisher}. The returned request body publisher
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   501
         * has an unknown content length.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   502
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   503
         * @apiNote This method can be used as an adapter between {@code
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   504
         * BodyPublisher} and {@code Flow.Publisher}, where the amount of
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   505
         * request body that the publisher will publish is unknown.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   506
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   507
         * @param publisher the publisher responsible for publishing the body
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   508
         * @return a BodyPublisher
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   509
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   510
        public static BodyPublisher
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   511
        fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher) {
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   512
            return new RequestPublishers.PublisherAdapter(publisher, -1L);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   513
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   514
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   515
        /**
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   516
         * Returns a request body publisher whose body is retrieved from the
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   517
         * given {@code Flow.Publisher}. The returned request body publisher
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   518
         * has the given content length.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   519
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   520
         * <p> The given {@code contentLength} is a positive number, that
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   521
         * represents the exact amount of bytes the {@code publisher} must
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   522
         * publish.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   523
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   524
         * @apiNote This method can be used as an adapter between {@code
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   525
         * BodyPublisher} and {@code Flow.Publisher}, where the amount of
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   526
         * request body that the publisher will publish is known.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   527
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   528
         * @param publisher the publisher responsible for publishing the body
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   529
         * @param contentLength a positive number representing the exact
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   530
         *                      amount of bytes the publisher will publish
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   531
         * @throws IllegalArgumentException if the content length is
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   532
         *                                  non-positive
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   533
         * @return a BodyPublisher
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   534
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   535
        public static BodyPublisher
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   536
        fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   537
                      long contentLength) {
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   538
            if (contentLength < 1)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   539
                throw new IllegalArgumentException("non-positive contentLength: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   540
                        + contentLength);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   541
            return new RequestPublishers.PublisherAdapter(publisher, contentLength);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   542
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   543
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   544
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   545
         * Returns a request body publisher whose body is the given {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   546
         * String}, converted using the {@link StandardCharsets#UTF_8 UTF_8}
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   547
         * character set.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   548
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   549
         * @param body the String containing the body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   551
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   552
        public static BodyPublisher ofString(String body) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   553
            return ofString(body, UTF_8);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   554
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   555
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   556
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   557
         * Returns a request body publisher whose body is the given {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   558
         * String}, converted using the given character set.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   559
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   560
         * @param s the String containing the body
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   561
         * @param charset the character set to convert the string to bytes
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   562
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   563
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   564
        public static BodyPublisher ofString(String s, Charset charset) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
            return new RequestPublishers.StringPublisher(s, charset);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   566
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   567
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   568
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   569
         * A request body publisher that reads its data from an {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   570
         * InputStream}. A {@link Supplier} of {@code InputStream} is used in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
         * case the request needs to be repeated, as the content is not buffered.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   572
         * The {@code Supplier} may return {@code null} on subsequent attempts,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   573
         * in which case the request fails.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   574
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   575
         * @param streamSupplier a Supplier of open InputStreams
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   576
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   577
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   578
        // TODO (spec): specify that the stream will be closed
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   579
        public static BodyPublisher ofInputStream(Supplier<? extends InputStream> streamSupplier) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   580
            return new RequestPublishers.InputStreamPublisher(streamSupplier);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   581
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   582
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   583
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   584
         * Returns a request body publisher whose body is the given byte array.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   585
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   586
         * @param buf the byte array containing the body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   587
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   588
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   589
        public static BodyPublisher ofByteArray(byte[] buf) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   590
            return new RequestPublishers.ByteArrayPublisher(buf);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   591
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   592
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   593
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   594
         * Returns a request body publisher whose body is the content of the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   595
         * given byte array of {@code length} bytes starting from the specified
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   596
         * {@code offset}.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   597
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   598
         * @param buf the byte array containing the body
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   599
         * @param offset the offset of the first byte
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   600
         * @param length the number of bytes to use
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   601
         * @return a BodyPublisher
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   602
         * @throws IndexOutOfBoundsException if the sub-range is defined to be
50095
bf2f27b92064 8202745: Remove hyphens from "out-of-bounds".
goetz
parents: 49765
diff changeset
   603
         *                                   out of bounds
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   604
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   605
        public static BodyPublisher ofByteArray(byte[] buf, int offset, int length) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   606
            Objects.checkFromIndexSize(offset, length, buf.length);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   607
            return new RequestPublishers.ByteArrayPublisher(buf, offset, length);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   608
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   609
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   610
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   611
         * A request body publisher that takes data from the contents of a File.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   612
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   613
         * <p> Security manager permission checks are performed in this factory
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   614
         * method, when the {@code BodyPublisher} is created. Care must be taken
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   615
         * that the {@code BodyPublisher} is not shared with untrusted code.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   616
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   617
         * @param path the path to the file containing the body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   618
         * @return a BodyPublisher
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   619
         * @throws java.io.FileNotFoundException if the path is not found
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   620
         * @throws SecurityException if a security manager has been installed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   621
         *          and it denies {@link SecurityManager#checkRead(String)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   622
         *          read access} to the given file
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   623
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   624
        public static BodyPublisher ofFile(Path path) throws FileNotFoundException {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   625
            Objects.requireNonNull(path);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   626
            return RequestPublishers.FilePublisher.create(path);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   627
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   628
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   629
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   630
         * A request body publisher that takes data from an {@code Iterable}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   631
         * of byte arrays. An {@link Iterable} is provided which supplies
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   632
         * {@link Iterator} instances. Each attempt to send the request results
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   633
         * in one invocation of the {@code Iterable}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   634
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   635
         * @param iter an Iterable of byte arrays
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   636
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   637
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   638
        public static BodyPublisher ofByteArrays(Iterable<byte[]> iter) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   639
            return new RequestPublishers.IterablePublisher(iter);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   640
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   641
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   642
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   643
         * A request body publisher which sends no request body.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   644
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   645
         * @return a BodyPublisher which completes immediately and sends
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   646
         *         no request body.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   647
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   648
        public static BodyPublisher noBody() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   649
            return new RequestPublishers.EmptyPublisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   650
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   651
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   652
}