src/java.net.http/share/classes/java/net/http/HttpRequest.java
author chegar
Mon, 26 Feb 2018 16:17:03 +0000
branchhttp-client-branch
changeset 56183 ec8123e97fad
parent 56167 96fa4f49a9ff
child 56254 4b2272dfe720
permissions -rw-r--r--
http-client-branch: minor spec typos
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;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    31
import java.net.URLPermission;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.nio.ByteBuffer;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    33
import java.nio.charset.Charset;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    34
import java.nio.charset.StandardCharsets;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    35
import java.nio.file.Files;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.nio.file.Path;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    37
import java.security.AccessController;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    38
import java.security.PrivilegedAction;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
import java.time.Duration;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.util.Iterator;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    41
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
import java.util.Optional;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
import java.util.concurrent.CompletableFuture;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    44
import java.util.concurrent.Executor;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
import java.util.concurrent.Flow;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    46
import java.util.function.Supplier;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56088
diff changeset
    47
import java.net.http.HttpResponse.BodyHandler;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    48
import jdk.internal.net.http.HttpRequestBuilderImpl;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    49
import jdk.internal.net.http.RequestPublishers;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    50
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
    51
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
/**
56085
544869ae0b16 http-client-branch: remove remaining vestiges of MultiSubscriber and minor doc cleanup
chegar
parents: 56079
diff changeset
    53
 * An HTTP request.
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    54
 *
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    55
 * <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
    56
 * {@linkplain HttpRequest.Builder builder}. An {@code HttpRequest} builder
56099
41ba54ac9403 http-client-branch: revert overzealous use of @linkplain
chegar
parents: 56097
diff changeset
    57
 * 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
    58
 * 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
    59
 * bodies are provided through a {@link BodyPublisher BodyPublisher} supplied
41ba54ac9403 http-client-branch: revert overzealous use of @linkplain
chegar
parents: 56097
diff changeset
    60
 * to one of the {@link Builder#DELETE(BodyPublisher) DELETE},
41ba54ac9403 http-client-branch: revert overzealous use of @linkplain
chegar
parents: 56097
diff changeset
    61
 * {@link Builder#POST(BodyPublisher) POST} or
41ba54ac9403 http-client-branch: revert overzealous use of @linkplain
chegar
parents: 56097
diff changeset
    62
 * {@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
    63
 * 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
    64
 * 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
    65
 * 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
    66
 * that differ in some parameters.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
 *
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
    68
 * <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
    69
 * body as a String:
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
    70
 *
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    71
 * <pre>{@code    HttpClient client = HttpClient.newHttpClient();
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    72
 *   HttpRequest request = HttpRequest.newBuilder()
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    73
 *         .uri(URI.create("http://foo.com/"))
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    74
 *         .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
    75
 *   client.sendAsync(request, BodyHandlers.ofString())
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    76
 *         .thenApply(HttpResponse::body)
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    77
 *         .thenAccept(System.out::println)
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    78
 *         .join(); }</pre>
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    79
 *
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
    80
 * <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
    81
 * 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
    82
 * implementation can be used.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    83
 *
56086
ba42a59d3072 http-client-branch: update public API @since tags
chegar
parents: 56085
diff changeset
    84
 * @since 11
42460
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
public abstract class HttpRequest {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    88
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
     * Creates an HttpRequest.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
    protected HttpRequest() {}
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
    /**
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
    94
     * A builder of {@linkplain HttpRequest HTTP requests}.
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    95
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    96
     * <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
    97
     * 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
    98
     *
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
    99
     * <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
   100
     * 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
   101
     * 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
   102
     * 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
   103
     * {@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
   104
     * HttpRequest} is immutable, and can be sent multiple times.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   105
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   106
     * <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
   107
     * 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
   108
     * 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
   109
     * 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
   110
     *
56086
ba42a59d3072 http-client-branch: update public API @since tags
chegar
parents: 56085
diff changeset
   111
     * @since 11
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
     */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   113
    public interface Builder {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
         * 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
   117
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   118
         * @param uri the request URI
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   119
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
         * @throws IllegalArgumentException if the {@code URI} scheme is not
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   121
         *         supported
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   122
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   123
        public Builder uri(URI uri);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   124
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   125
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
         * Requests the server to acknowledge the request before sending the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   127
         * body. This is disabled by default. If enabled, the server is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
         * requested to send an error response or a {@code 100 Continue}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
         * response before the client sends the request body. This means the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   130
         * request publisher for the request will not be invoked until this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   131
         * interim response is received.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
         * @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
   134
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   135
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   136
        public Builder expectContinue(boolean enable);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   138
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
         * Sets the preferred {@link HttpClient.Version} for this request.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   140
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   141
         * <p> The corresponding {@link HttpResponse} should be checked for the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   142
         * version that was actually used. If the version is not set in a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   143
         * request, then the version requested will be that of the sending
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   144
         * {@link HttpClient}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   145
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   146
         * @param version the HTTP protocol version requested
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   147
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   148
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   149
        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
   150
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   151
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   152
         * 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
   153
         * 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
   154
         *
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents: 56030
diff changeset
   155
         * @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
   156
         *           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
   157
         *           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
   158
         *           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
   159
         *           {@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
   160
         *           {@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
   161
         *           to the builder.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents: 56030
diff changeset
   162
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   163
         * @param name the header name
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   164
         * @param value the header value
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   165
         * @return this builder
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   166
         * @throws IllegalArgumentException if the header name or value is not
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   167
         *         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
   168
         *         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
   169
         *         by the implementation.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   170
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   171
        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
   172
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   173
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   174
         * 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
   175
         * request. The supplied {@code String} instances must alternate as
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   176
         * header names and header values.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
         * To add several values to the same name then the same name must
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   178
         * 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
   179
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   180
         * @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
   181
         * @return this builder
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   182
         * @throws IllegalArgumentException if there are an odd number of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   183
         *         parameters, or if a header name or value is not valid, see
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   184
         *         <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
   185
         *         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
   186
         *         {@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
   187
         *         implementation.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   188
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   189
        public Builder headers(String... headers);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   190
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   191
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   192
         * 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
   193
         * 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
   194
         * 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
   195
         * java.net.http.HttpResponse.BodyHandler) HttpClient::send} or
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56088
diff changeset
   196
         * {@link HttpClient#sendAsync(java.net.http.HttpRequest,
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56088
diff changeset
   197
         * java.net.http.HttpResponse.BodyHandler) HttpClient::sendAsync}
56143
11e32a62803d http-client-branch: fix a few more typos in spec
chegar
parents: 56142
diff changeset
   198
         * 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
   199
         * 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
   200
         * block forever.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   201
         *
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43302
diff changeset
   202
         * @param duration the timeout duration
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   203
         * @return this builder
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   204
         * @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
   205
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   206
        public abstract Builder timeout(Duration duration);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   207
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   208
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   209
         * 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
   210
         * 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
   211
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   212
         * @param name the header name
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   213
         * @param value the header value
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   214
         * @return this builder
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   215
         * @throws IllegalArgumentException if the header name or value is not valid,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   216
         *         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
   217
         *         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
   218
         *         {@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
   219
         *         implementation.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   220
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   221
        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
   222
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
         * Sets the request method of this builder to GET.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   225
         * This is the default.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   226
         *
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   227
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   228
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   229
        public Builder GET();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   230
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   231
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   232
         * Sets the request method of this builder to POST and sets its
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   233
         * 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
   234
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   235
         * @param bodyPublisher the body publisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   236
         *
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   237
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   238
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   239
        public Builder POST(BodyPublisher bodyPublisher);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   240
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   241
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   242
         * Sets the request method of this builder to PUT and sets its
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   243
         * 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
   244
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   245
         * @param bodyPublisher the body publisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   246
         *
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   247
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   248
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   249
        public Builder PUT(BodyPublisher bodyPublisher);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
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
         * Sets the request method of this builder to DELETE and sets its
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   253
         * 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
   254
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   255
         * @param bodyPublisher the body publisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   256
         *
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   257
         * @return this builder
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   258
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   259
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   260
        public Builder DELETE(BodyPublisher bodyPublisher);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   261
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   262
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   263
         * 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
   264
         * given values.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   265
         *
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   266
         * @apiNote The {@link BodyPublishers#noBody() noBody} request
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   267
         * 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
   268
         * 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
   269
         * 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
   270
         * to restrict the {@code CONNECT} method.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   272
         * @param method the method to use
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   273
         * @param bodyPublisher the body publisher
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   274
         * @return this builder
56030
69fbcac27e35 http-client-branch: clarify behavior of restricted methods in the builder
chegar
parents: 56008
diff changeset
   275
         * @throws IllegalArgumentException if the method is restricted
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   276
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   277
        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
   278
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   279
        /**
56143
11e32a62803d http-client-branch: fix a few more typos in spec
chegar
parents: 56142
diff changeset
   280
         * 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
   281
         *
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   282
         * @return a new {@code HttpRequest}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   283
         * @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
   284
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   285
        public HttpRequest build();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   286
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   287
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   288
         * Returns an exact duplicate copy of this {@code Builder} based on
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   289
         * current state. The new builder can then be modified independently of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   290
         * this builder.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   291
         *
56142
571ba7474214 http-client-branch: fix a few more typos in spec
chegar
parents: 56141
diff changeset
   292
         * @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
   293
         */
56140
049b6940e9db http-client-branch: Builders as interfaces
chegar
parents: 56138
diff changeset
   294
        public Builder copy();
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   297
    /**
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   298
     * 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
   299
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   300
     * @param uri the request URI
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   301
     * @return a new request builder
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   302
     * @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
   303
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   304
    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
   305
        return new HttpRequestBuilderImpl(uri);
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   308
    /**
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   309
     * Creates an {@code HttpRequest} builder.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   310
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   311
     * @return a new request builder
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
    public static HttpRequest.Builder newBuilder() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   314
        return new HttpRequestBuilderImpl();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   315
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   316
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
     * Returns an {@code Optional} containing the {@link BodyPublisher} set on
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   319
     * this request. If no {@code BodyPublisher} was set in the requests's
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   320
     * 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
   321
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   322
     * @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
   323
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   324
    public abstract Optional<BodyPublisher> bodyPublisher();
42460
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
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   327
     * 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
   328
     * 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
   329
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   330
     * @return this request's method
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   331
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   332
    public abstract String method();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   333
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
     * Returns an {@code Optional} containing this request's timeout duration.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   336
     * 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
   337
     * {@code Optional} is empty.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   338
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
     * @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
   340
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
    public abstract Optional<Duration> timeout();
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
    /**
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   344
     * Returns this request's {@linkplain HttpRequest.Builder#expectContinue(boolean)
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   345
     * expect continue} setting.
42460
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
     * @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
   348
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   349
    public abstract boolean expectContinue();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   350
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   351
    /**
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   352
     * 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
   353
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   354
     * @return this request's URI
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   355
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   356
    public abstract URI uri();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   357
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   358
    /**
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44847
diff changeset
   359
     * 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
   360
     * 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
   361
     * 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
   362
     * 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
   363
     * {@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
   364
     * 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
   365
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   366
     * @return HTTP protocol version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   367
     */
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 44847
diff changeset
   368
    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
   369
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   370
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   371
     * 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
   372
     * sent with.
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
     * @return this request's HttpHeaders
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   375
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   376
    public abstract HttpHeaders headers();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   377
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   378
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   379
     * Tests this HTTP request instance for equality with the given object.
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> If the given object is not an {@code HttpRequest} then this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   382
     * method returns {@code false}. Two HTTP requests are equal if their URI,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
     * method, and headers fields are all equal.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
     * <p> This method satisfies the general contract of the {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
     * 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
   387
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   388
     * @param obj the object to which this object is to be compared
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   389
     * @return {@code true} if, and only if, the given object is an {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   390
     *         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
   391
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   392
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
    public final boolean equals(Object obj) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   394
       if (! (obj instanceof HttpRequest))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   395
           return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   396
       HttpRequest that = (HttpRequest)obj;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   397
       if (!that.method().equals(this.method()))
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
       if (!that.uri().equals(this.uri()))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   400
           return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   401
       if (!that.headers().equals(this.headers()))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   402
           return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   403
       return true;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   404
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   405
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   406
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   407
     * Computes a hash code for this HTTP request instance.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   408
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   409
     * <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
   410
     * header components, and satisfies the general contract of the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   411
     * {@link Object#hashCode Object.hashCode} method.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   412
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   413
     * @return the hash-code value for this HTTP request
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   414
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   415
    public final int hashCode() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   416
        return method().hashCode()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   417
                + uri().hashCode()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   418
                + headers().hashCode();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   419
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   420
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   421
    /**
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   422
     * 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
   423
     * 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
   424
     * {@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
   425
     * common publishers.
48083
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> The {@code BodyPublisher} interface extends {@link Flow.Publisher
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   428
     * Flow.Publisher&lt;ByteBuffer&gt;}, which means that a {@code BodyPublisher}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   429
     * acts as a publisher of {@linkplain ByteBuffer byte buffers}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   430
     *
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   431
     * <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
   432
     * 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
   433
     * 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
   434
     * 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
   435
     * Client and are expected from {@code BodyPublisher} implementations. Each
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   436
     * outgoing request results in one HTTP Client {@code Subscriber}
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   437
     * 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
   438
     * of byte buffers containing the request body. Instances of {@code
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   439
     * ByteBuffer} published by the publisher must be allocated by the
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   440
     * 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
   441
     * Client. These subscriptions complete normally when the request body is
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   442
     * 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
   443
     * 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
   444
     * created which is expected to generate the same data as before.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   445
     *
56097
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   446
     * <p> A {@code BodyPublisher} that reports a {@linkplain #contentLength()
15dc43936d39 http-client-branch: javadoc refresh and minor fixes
chegar
parents: 56092
diff changeset
   447
     * 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
   448
     * 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
   449
     *
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   450
     * @see BodyPublishers
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   451
     * @since 11
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   452
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   453
    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
   454
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   455
        /**
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   456
         * 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
   457
         * 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
   458
         * 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
   459
         *
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   460
         * <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
   461
         * 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
   462
         * 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
   463
         *
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   464
         * @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
   465
         */
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   466
        long contentLength();
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   467
    }
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   468
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
     * 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
   471
     * various useful publishers, such as publishing the request body from a
56183
ec8123e97fad http-client-branch: minor spec typos
chegar
parents: 56167
diff changeset
   472
     * String, or from a file.
56167
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
     * <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
   475
     * 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
   476
     * sending as a request body:
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   477
     *
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   478
     *  <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
   479
     *   HttpRequest request = HttpRequest.newBuilder()
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   480
     *        .uri(URI.create("https://foo.com/"))
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   481
     *        .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
   482
     *        .POST(BodyPublishers.ofString("some body text"))
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   483
     *        .build();
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   484
     *
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   485
     *   // Request body from a File
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   486
     *   HttpRequest request = HttpRequest.newBuilder()
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   487
     *        .uri(URI.create("https://foo.com/"))
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   488
     *        .header("Content-Type", "application/json")
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   489
     *        .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
   490
     *        .build();
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   491
     *
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   492
     *   // Request body from a byte array
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   493
     *   HttpRequest request = HttpRequest.newBuilder()
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   494
     *        .uri(URI.create("https://foo.com/"))
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   495
     *        .POST(BodyPublishers.ofByteArray(new byte[] { ... }))
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   496
     *        .build(); }</pre>
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
     * @since 11
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
    public static class BodyPublishers {
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   501
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   502
        private BodyPublishers() { }
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   503
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   504
        /**
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   505
         * 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
   506
         * 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
   507
         * has an unknown content length.
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
         * @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
   510
         * 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
   511
         * 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
   512
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   513
         * @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
   514
         * @return a BodyPublisher
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   515
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   516
        public static BodyPublisher
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   517
        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
   518
            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
   519
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   520
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
         * 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
   523
         * 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
   524
         * has the given content length.
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
         * <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
   527
         * 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
   528
         * publish.
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
         * @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
   531
         * 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
   532
         * 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
   533
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   534
         * @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
   535
         * @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
   536
         *                      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
   537
         * @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
   538
         *                                  non-positive
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   539
         * @return a BodyPublisher
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   540
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   541
        public static BodyPublisher
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   542
        fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher,
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   543
                      long contentLength) {
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   544
            if (contentLength < 1)
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   545
                throw new IllegalArgumentException("non-positive contentLength: "
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   546
                        + contentLength);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   547
            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
   548
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   549
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48083
diff changeset
   550
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   551
         * Returns a request body publisher whose body is the given {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   552
         * 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
   553
         * character set.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   554
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   555
         * @param body the String containing the body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   556
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   557
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   558
        public static BodyPublisher ofString(String body) {
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   559
            return ofString(body, UTF_8);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   560
        }
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
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   563
         * Returns a request body publisher whose body is the given {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
         * 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
   565
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   566
         * @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
   567
         * @param charset the character set to convert the string to bytes
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   568
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   569
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   570
        public static BodyPublisher ofString(String s, Charset charset) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
            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
   572
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   573
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   574
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   575
         * A request body publisher that reads its data from an {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   576
         * InputStream}. A {@link Supplier} of {@code InputStream} is used in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   577
         * case the request needs to be repeated, as the content is not buffered.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   578
         * The {@code Supplier} may return {@code null} on subsequent attempts,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   579
         * 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
   580
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   581
         * @param streamSupplier a Supplier of open InputStreams
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   582
         * @return a BodyPublisher
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
        // 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
   585
        public static BodyPublisher ofInputStream(Supplier<? extends InputStream> streamSupplier) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   586
            return new RequestPublishers.InputStreamPublisher(streamSupplier);
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   589
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   590
         * 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
   591
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   592
         * @param buf the byte array containing the body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   593
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   594
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   595
        public static BodyPublisher ofByteArray(byte[] buf) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   596
            return new RequestPublishers.ByteArrayPublisher(buf);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   597
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   598
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   599
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   600
         * Returns a request body publisher whose body is the content of the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   601
         * 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
   602
         * {@code offset}.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   603
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   604
         * @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
   605
         * @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
   606
         * @param length the number of bytes to use
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   607
         * @return a BodyPublisher
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   608
         * @throws IndexOutOfBoundsException if the sub-range is defined to be
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   609
         *                                   out-of-bounds
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   610
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   611
        public static BodyPublisher ofByteArray(byte[] buf, int offset, int length) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   612
            Objects.checkFromIndexSize(offset, length, buf.length);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   613
            return new RequestPublishers.ByteArrayPublisher(buf, offset, length);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   614
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   615
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   616
        private static String pathForSecurityCheck(Path path) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   617
            return path.toFile().getPath();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   618
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   619
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   620
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   621
         * 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
   622
         *
56138
4f92b988600e http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents: 56099
diff changeset
   623
         * <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
   624
         * 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
   625
         * 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
   626
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   627
         * @param path the path to the file containing the body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   628
         * @return a BodyPublisher
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   629
         * @throws java.io.FileNotFoundException if the path is not found
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   630
         * @throws SecurityException if a security manager has been installed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   631
         *          and it denies {@link SecurityManager#checkRead(String)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   632
         *          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
   633
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   634
        public static BodyPublisher ofFile(Path path) throws FileNotFoundException {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   635
            Objects.requireNonNull(path);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   636
            SecurityManager sm = System.getSecurityManager();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   637
            if (sm != null)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   638
                sm.checkRead(pathForSecurityCheck(path));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   639
            if (Files.notExists(path))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   640
                throw new FileNotFoundException(path + " not found");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   641
            return new RequestPublishers.FilePublisher(path);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   642
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   643
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   644
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   645
         * A request body publisher that takes data from an {@code Iterable}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   646
         * of byte arrays. An {@link Iterable} is provided which supplies
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   647
         * {@link Iterator} instances. Each attempt to send the request results
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   648
         * 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
   649
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   650
         * @param iter an Iterable of byte arrays
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   651
         * @return a BodyPublisher
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   652
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   653
        public static BodyPublisher ofByteArrays(Iterable<byte[]> iter) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   654
            return new RequestPublishers.IterablePublisher(iter);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   655
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   656
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   657
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   658
         * A request body publisher which sends no request body.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   659
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   660
         * @return a BodyPublisher which completes immediately and sends
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   661
         *         no request body.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   662
         */
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56143
diff changeset
   663
        public static BodyPublisher noBody() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   664
            return new RequestPublishers.EmptyPublisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   665
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   666
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   667
}