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