src/java.net.http/share/classes/java/net/http/HttpResponse.java
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 55402 b78af6d8a252
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
54096
acf36e8d61c6 8220480: Typo in java.net.http.HttpResponse.BodySubscriber documentation
chegar
parents: 53508
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    26
package java.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    28
import java.io.BufferedReader;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.io.IOException;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    30
import java.io.InputStream;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    31
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.nio.ByteBuffer;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import java.nio.charset.Charset;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    34
import java.nio.channels.FileChannel;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    35
import java.nio.charset.StandardCharsets;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.nio.file.OpenOption;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.nio.file.Path;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    38
import java.util.List;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    39
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.util.Optional;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
import java.util.concurrent.CompletionStage;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    43
import java.util.concurrent.ConcurrentMap;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
import java.util.concurrent.Flow;
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
    45
import java.util.concurrent.Flow.Subscriber;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    46
import java.util.concurrent.Flow.Publisher;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    47
import java.util.concurrent.Flow.Subscription;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    48
import java.util.function.Consumer;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
import java.util.function.Function;
53508
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
    50
import java.util.function.Supplier;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    51
import java.util.stream.Stream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    52
import javax.net.ssl.SSLSession;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    53
import jdk.internal.net.http.BufferingSubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    54
import jdk.internal.net.http.LineSubscriberAdapter;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    55
import jdk.internal.net.http.ResponseBodyHandlers.FileDownloadBodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    56
import jdk.internal.net.http.ResponseBodyHandlers.PathBodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    57
import jdk.internal.net.http.ResponseBodyHandlers.PushPromisesHandlerWithMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    58
import jdk.internal.net.http.ResponseSubscribers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    59
import jdk.internal.net.http.ResponseSubscribers.PathSubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    60
import static java.nio.file.StandardOpenOption.*;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    61
import static jdk.internal.net.http.common.Utils.charsetFrom;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
/**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    64
 * An HTTP response.
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    65
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    66
 * <p> An {@code HttpResponse} is not created directly, but rather returned as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    67
 * a result of sending an {@link HttpRequest}. An {@code HttpResponse} is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    68
 * made available when the response status code and headers have been received,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    69
 * and typically after the response body has also been completely received.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    70
 * Whether or not the {@code HttpResponse} is made available before the response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    71
 * body has been completely received depends on the {@link BodyHandler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    72
 * BodyHandler} provided when sending the {@code HttpRequest}.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    73
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    74
 * <p> This class provides methods for accessing the response status code,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    75
 * headers, the response body, and the {@code HttpRequest} corresponding
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    76
 * to this response.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    77
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    78
 * <p> The following is an example of retrieving a response as a String:
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    79
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    80
 * <pre>{@code    HttpResponse<String> response = client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    81
 *     .send(request, BodyHandlers.ofString()); }</pre>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    82
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    83
 * <p> The class {@link BodyHandlers BodyHandlers} provides implementations
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    84
 * of many common response handlers. Alternatively, a custom {@code BodyHandler}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    85
 * implementation can be used.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
 * @param <T> the response body type
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    88
 * @since 11
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
 */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    90
public interface HttpResponse<T> {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
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
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
     * Returns the status code for this response.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
     * @return the response code
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
    98
    public int statusCode();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    99
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   100
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   101
     * Returns the {@link HttpRequest} corresponding to this response.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   102
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   103
     * <p> The returned {@code HttpRequest} may not be the initiating request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   104
     * provided when {@linkplain HttpClient#send(HttpRequest, BodyHandler)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   105
     * sending}. For example, if the initiating request was redirected, then the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   106
     * request returned by this method will have the redirected URI, which will
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   107
     * be different from the initiating request URI.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   109
     * @see #previousResponse()
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
     * @return the request
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   113
    public HttpRequest request();
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
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
     * Returns an {@code Optional} containing the previous intermediate response
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
     * if one was received. An intermediate response is one that is received
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
     * as a result of redirection or authentication. If no previous response
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
     * was received then an empty {@code Optional} is returned.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   121
     * @return an Optional containing the HttpResponse, if any.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   122
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   123
    public Optional<HttpResponse<T>> previousResponse();
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
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   126
     * Returns the received response headers.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   127
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   128
     * @return the response headers
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   129
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   130
    public HttpHeaders headers();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   131
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   133
     * Returns the body. Depending on the type of {@code T}, the returned body
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   134
     * may represent the body after it was read (such as {@code byte[]}, or
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   135
     * {@code String}, or {@code Path}) or it may represent an object with
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   136
     * which the body is read, such as an {@link java.io.InputStream}.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
     * <p> If this {@code HttpResponse} was returned from an invocation of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
     * {@link #previousResponse()} then this method returns {@code null}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   140
     *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   141
     * @return the body
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   142
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   143
    public T body();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   144
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   145
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   146
     * Returns an {@link Optional} containing the {@link SSLSession} in effect
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   147
     * for this response. Returns an empty {@code Optional} if this is not a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   148
     * <i>HTTPS</i> response.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   149
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   150
     * @return an {@code Optional} containing the {@code SSLSession} associated
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   151
     *         with the response
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   152
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   153
    public Optional<SSLSession> sslSession();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   154
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   155
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   156
     * Returns the {@code URI} that the response was received from. This may be
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   157
     * different from the request {@code URI} if redirection occurred.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   158
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   159
     * @return the URI of the response
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   160
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   161
     public URI uri();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   162
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   163
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   164
     * Returns the HTTP protocol version that was used for this response.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   165
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   166
     * @return HTTP protocol version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   167
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   168
    public HttpClient.Version version();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   169
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   170
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   171
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   172
     * Initial response information supplied to a {@link BodyHandler BodyHandler}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   173
     * when a response is initially received and before the body is processed.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   174
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   175
    public interface ResponseInfo {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   176
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   177
         * Provides the response status code.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   178
         * @return the response status code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   179
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   180
        public int statusCode();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   181
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   182
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   183
         * Provides the response headers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   184
         * @return the response headers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   185
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   186
        public HttpHeaders headers();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   187
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   188
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   189
         * Provides the response protocol version.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   190
         * @return the response protocol version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   191
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   192
        public HttpClient.Version version();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   194
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   196
     * A handler for response bodies.  The class {@link BodyHandlers BodyHandlers}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   197
     * provides implementations of many common body handlers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   198
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   199
     * <p> The {@code BodyHandler} interface allows inspection of the response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   200
     * code and headers, before the actual response body is received, and is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   201
     * responsible for creating the response {@link BodySubscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   202
     * BodySubscriber}. The {@code BodySubscriber} consumes the actual response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   203
     * body bytes and, typically, converts them into a higher-level Java type.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   204
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   205
     * <p> A {@code BodyHandler} is a function that takes a {@link ResponseInfo
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   206
     * ResponseInfo} object; and which returns a {@code BodySubscriber}. The
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   207
     * {@code BodyHandler} is invoked when the response status code and headers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   208
     * are available, but before the response  body bytes are received.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   209
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   210
     * <p> The following example uses one of the {@linkplain BodyHandlers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   211
     * predefined body handlers} that always process the response body in the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   212
     * same way ( streams the response body to a file ).
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   213
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   214
     * <pre>{@code   HttpRequest request = HttpRequest.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   215
     *        .uri(URI.create("http://www.foo.com/"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   216
     *        .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   217
     *  client.sendAsync(request, BodyHandlers.ofFile(Paths.get("/tmp/f")))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   218
     *        .thenApply(HttpResponse::body)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   219
     *        .thenAccept(System.out::println); }</pre>
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   220
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   221
     * Note, that even though the pre-defined handlers do not examine the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   222
     * response code, the response code and headers are always retrievable from
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   223
     * the {@link HttpResponse}, when it is returned.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   224
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   225
     * <p> In the second example, the function returns a different subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   226
     * depending on the status code.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   227
     * <pre>{@code   HttpRequest request = HttpRequest.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   228
     *        .uri(URI.create("http://www.foo.com/"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   229
     *        .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   230
     *  BodyHandler<Path> bodyHandler = (rspInfo) -> rspInfo.statusCode() == 200
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   231
     *                      ? BodySubscribers.ofFile(Paths.get("/tmp/f"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   232
     *                      : BodySubscribers.replacing(Paths.get("/NULL"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   233
     *  client.sendAsync(request, bodyHandler)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   234
     *        .thenApply(HttpResponse::body)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   235
     *        .thenAccept(System.out::println); }</pre>
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   236
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   237
     * @param <T> the response body type
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   238
     * @see BodyHandlers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   239
     * @since 11
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
    @FunctionalInterface
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   242
    public interface BodyHandler<T> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   243
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   244
        /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   245
         * Returns a {@link BodySubscriber BodySubscriber} considering the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   246
         * given response status code and headers. This method is invoked before
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   247
         * the actual response body bytes are read and its implementation must
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   248
         * return a {@link BodySubscriber BodySubscriber} to consume the response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   249
         * body bytes.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   251
         * <p> The response body can be discarded using one of {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   252
         * BodyHandlers#discarding() discarding} or {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   253
         * BodyHandlers#replacing(Object) replacing}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   254
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   255
         * @param responseInfo the response info
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   256
         * @return a body subscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   257
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   258
        public BodySubscriber<T> apply(ResponseInfo responseInfo);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   259
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   260
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   261
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   262
     * Implementations of {@link BodyHandler BodyHandler} that implement various
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   263
     * useful handlers, such as handling the response body as a String, or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   264
     * streaming the response body to a file.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   265
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   266
     * <p> These implementations do not examine the status code, meaning the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   267
     * body is always accepted. They typically return an equivalently named
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   268
     * {@code BodySubscriber}. Alternatively, a custom handler can be used to
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   269
     * examine the status code and headers, and return a different body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   270
     * subscriber, of the same type, as appropriate.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   271
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   272
     * <p>The following are examples of using the predefined body handlers to
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   273
     * convert a flow of response body data into common high-level Java objects:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   274
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   275
     * <pre>{@code    // Receives the response body as a String
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   276
     *   HttpResponse<String> response = client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   277
     *     .send(request, BodyHandlers.ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   278
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   279
     *   // Receives the response body as a file
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   280
     *   HttpResponse<Path> response = client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   281
     *     .send(request, BodyHandlers.ofFile(Paths.get("example.html")));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   282
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   283
     *   // Receives the response body as an InputStream
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   284
     *   HttpResponse<InputStream> response = client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   285
     *     .send(request, BodyHandlers.ofInputStream());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   286
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   287
     *   // Discards the response body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   288
     *   HttpResponse<Void> response = client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   289
     *     .send(request, BodyHandlers.discarding());  }</pre>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   290
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   291
     * @since 11
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   292
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   293
    public static class BodyHandlers {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   294
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   295
        private BodyHandlers() { }
42460
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
        /**
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   298
         * Returns a response body handler that returns a {@link BodySubscriber
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   299
         * BodySubscriber}{@code <Void>} obtained from {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   300
         * BodySubscribers#fromSubscriber(Subscriber)}, with the given
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   301
         * {@code subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   302
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   303
         * <p> The response body is not available through this, or the {@code
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   304
         * HttpResponse} API, but instead all response body is forwarded to the
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   305
         * given {@code subscriber}, which should make it available, if
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   306
         * appropriate, through some other mechanism, e.g. an entry in a
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   307
         * database, etc.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   308
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   309
         * @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: 48379
diff changeset
   310
         * BodySubscriber} and {@code Flow.Subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   311
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   312
         * <p> For example:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   313
         * <pre> {@code  TextSubscriber subscriber = new TextSubscriber();
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   314
         *  HttpResponse<Void> response = client.sendAsync(request,
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   315
         *      BodyHandlers.fromSubscriber(subscriber)).join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   316
         *  System.out.println(response.statusCode()); }</pre>
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   317
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   318
         * @param subscriber the subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   319
         * @return a response body handler
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   320
         */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   321
        public static BodyHandler<Void>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   322
        fromSubscriber(Subscriber<? super List<ByteBuffer>> subscriber) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   323
            Objects.requireNonNull(subscriber);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   324
            return (responseInfo) -> BodySubscribers.fromSubscriber(subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   325
                                                                       s -> null);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   326
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   327
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   328
        /**
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   329
         * Returns a response body handler that returns a {@link BodySubscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   330
         * BodySubscriber}{@code <T>} obtained from {@link
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   331
         * BodySubscribers#fromSubscriber(Subscriber, Function)}, with the
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   332
         * given {@code subscriber} and {@code finisher} function.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   333
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   334
         * <p> The given {@code finisher} function is applied after the given
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   335
         * subscriber's {@code onComplete} has been invoked. The {@code finisher}
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   336
         * function is invoked with the given subscriber, and returns a value
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   337
         * that is set as the response's body.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   338
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   339
         * @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: 48379
diff changeset
   340
         * BodySubscriber} and {@code Flow.Subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   341
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   342
         * <p> For example:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   343
         * <pre> {@code  TextSubscriber subscriber = ...;  // accumulates bytes and transforms them into a String
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   344
         *  HttpResponse<String> response = client.sendAsync(request,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   345
         *      BodyHandlers.fromSubscriber(subscriber, TextSubscriber::getTextResult)).join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   346
         *  String text = response.body(); }</pre>
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   347
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   348
         * @param <S> the type of the Subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   349
         * @param <T> the type of the response body
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   350
         * @param subscriber the subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   351
         * @param finisher a function to be applied after the subscriber has completed
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   352
         * @return a response body handler
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   353
         */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   354
        public static <S extends Subscriber<? super List<ByteBuffer>>,T> BodyHandler<T>
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   355
        fromSubscriber(S subscriber, Function<? super S,? extends T> finisher) {
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   356
            Objects.requireNonNull(subscriber);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   357
            Objects.requireNonNull(finisher);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   358
            return (responseInfo) -> BodySubscribers.fromSubscriber(subscriber,
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   359
                                                                      finisher);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   360
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   361
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   362
        /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   363
         * Returns a response body handler that returns a {@link BodySubscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   364
         * BodySubscriber}{@code <Void>} obtained from {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   365
         * BodySubscribers#fromLineSubscriber(Subscriber, Function, Charset, String)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   366
         * BodySubscribers.fromLineSubscriber(subscriber, s -> null, charset, null)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   367
         * with the given {@code subscriber}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   368
         * The {@link Charset charset} used to decode the response body bytes is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   369
         * obtained from the HTTP response headers as specified by {@link #ofString()},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   370
         * and lines are delimited in the manner of {@link BufferedReader#readLine()}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   371
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   372
         * <p> The response body is not available through this, or the {@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   373
         * HttpResponse} API, but instead all response body is forwarded to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   374
         * given {@code subscriber}, which should make it available, if
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   375
         * appropriate, through some other mechanism, e.g. an entry in a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   376
         * database, etc.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   377
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   378
         * @apiNote This method can be used as an adapter between a {@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   379
         * BodySubscriber} and a text based {@code Flow.Subscriber} that parses
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   380
         * text line by line.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   381
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   382
         * <p> For example:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   383
         * <pre> {@code  // A PrintSubscriber that implements Flow.Subscriber<String>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   384
         *  // and print lines received by onNext() on System.out
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   385
         *  PrintSubscriber subscriber = new PrintSubscriber(System.out);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   386
         *  client.sendAsync(request, BodyHandlers.fromLineSubscriber(subscriber))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   387
         *      .thenApply(HttpResponse::statusCode)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   388
         *      .thenAccept((status) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   389
         *          if (status != 200) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   390
         *              System.err.printf("ERROR: %d status received%n", status);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   391
         *          }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   392
         *      }); }</pre>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   393
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   394
         * @param subscriber the subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   395
         * @return a response body handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   396
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   397
        public static BodyHandler<Void>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   398
        fromLineSubscriber(Subscriber<? super String> subscriber) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   399
            Objects.requireNonNull(subscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   400
            return (responseInfo) ->
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   401
                        BodySubscribers.fromLineSubscriber(subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   402
                                                           s -> null,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   403
                                                           charsetFrom(responseInfo.headers()),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   404
                                                           null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   405
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   406
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   407
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   408
         * Returns a response body handler that returns a {@link BodySubscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   409
         * BodySubscriber}{@code <T>} obtained from {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   410
         * BodySubscribers#fromLineSubscriber(Subscriber, Function, Charset, String)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   411
         * BodySubscribers.fromLineSubscriber(subscriber, finisher, charset, lineSeparator)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   412
         * with the given {@code subscriber}, {@code finisher} function, and line separator.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   413
         * The {@link Charset charset} used to decode the response body bytes is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   414
         * obtained from the HTTP response headers as specified by {@link #ofString()}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   415
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   416
         * <p> The given {@code finisher} function is applied after the given
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   417
         * subscriber's {@code onComplete} has been invoked. The {@code finisher}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   418
         * function is invoked with the given subscriber, and returns a value
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   419
         * that is set as the response's body.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   420
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   421
         * @apiNote This method can be used as an adapter between a {@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   422
         * BodySubscriber} and a text based {@code Flow.Subscriber} that parses
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   423
         * text line by line.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   424
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   425
         * <p> For example:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   426
         * <pre> {@code  // A LineParserSubscriber that implements Flow.Subscriber<String>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   427
         *  // and accumulates lines that match a particular pattern
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   428
         *  Pattern pattern = ...;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   429
         *  LineParserSubscriber subscriber = new LineParserSubscriber(pattern);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   430
         *  HttpResponse<List<String>> response = client.send(request,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   431
         *      BodyHandlers.fromLineSubscriber(subscriber, s -> s.getMatchingLines(), "\n"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   432
         *  if (response.statusCode() != 200) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   433
         *      System.err.printf("ERROR: %d status received%n", response.statusCode());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   434
         *  } }</pre>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   435
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   436
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   437
         * @param <S> the type of the Subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   438
         * @param <T> the type of the response body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   439
         * @param subscriber the subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   440
         * @param finisher a function to be applied after the subscriber has completed
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   441
         * @param lineSeparator an optional line separator: can be {@code null},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   442
         *                      in which case lines will be delimited in the manner of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   443
         *                      {@link BufferedReader#readLine()}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   444
         * @return a response body handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   445
         * @throws IllegalArgumentException if the supplied {@code lineSeparator}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   446
         *         is the empty string
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   447
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   448
        public static <S extends Subscriber<? super String>,T> BodyHandler<T>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   449
        fromLineSubscriber(S subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   450
                           Function<? super S,? extends T> finisher,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   451
                           String lineSeparator) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   452
            Objects.requireNonNull(subscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   453
            Objects.requireNonNull(finisher);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   454
            // implicit null check
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   455
            if (lineSeparator != null && lineSeparator.isEmpty())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   456
                throw new IllegalArgumentException("empty line separator");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   457
            return (responseInfo) ->
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   458
                        BodySubscribers.fromLineSubscriber(subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   459
                                                           finisher,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   460
                                                           charsetFrom(responseInfo.headers()),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   461
                                                           lineSeparator);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   462
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   463
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   464
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   465
         * Returns a response body handler that discards the response body.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   466
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   467
         * @return a response body handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   468
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   469
        public static BodyHandler<Void> discarding() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   470
            return (responseInfo) -> BodySubscribers.discarding();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   471
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   472
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   473
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   474
         * Returns a response body handler that returns the given replacement
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   475
         * value, after discarding the response body.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   476
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   477
         * @param <U> the response body type
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   478
         * @param value the value of U to return as the body, may be {@code null}
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   479
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   480
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   481
        public static <U> BodyHandler<U> replacing(U value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   482
            return (responseInfo) -> BodySubscribers.replacing(value);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   483
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   484
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   485
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   486
         * Returns a {@code BodyHandler<String>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   487
         * {@link BodySubscriber BodySubscriber}{@code <String>} obtained from
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   488
         * {@link BodySubscribers#ofString(Charset) BodySubscribers.ofString(Charset)}.
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   489
         * The body is decoded using the given character set.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   490
         *
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   491
         * @param charset the character set to convert the body with
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   492
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   493
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   494
        public static BodyHandler<String> ofString(Charset charset) {
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   495
            Objects.requireNonNull(charset);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   496
            return (responseInfo) -> BodySubscribers.ofString(charset);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   497
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   498
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   499
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   500
         * Returns a {@code BodyHandler<Path>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   501
         * {@link BodySubscriber BodySubscriber}{@code <Path>} obtained from
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   502
         * {@link BodySubscribers#ofFile(Path, OpenOption...)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   503
         * BodySubscribers.ofFile(Path,OpenOption...)}.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   504
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   505
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   506
         * been completely written to the file, and {@link #body()} returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   507
         * reference to its {@link Path}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   509
         * <p> Security manager permission checks are performed in this factory
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   510
         * method, when the {@code BodyHandler} is created. Care must be taken
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   511
         * that the {@code BodyHandler} is not shared with untrusted code.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   512
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   513
         * @param file the file to store the body in
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   514
         * @param openOptions any options to use when opening/creating the file
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   515
         * @return a response body handler
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   516
         * @throws IllegalArgumentException if an invalid set of open options
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   517
         *          are specified
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
         * @throws SecurityException If a security manager has been installed
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
   519
         *          and it denies {@linkplain SecurityManager#checkWrite(String)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   520
         *          write access} to the file.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   521
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   522
        public static BodyHandler<Path> ofFile(Path file, OpenOption... openOptions) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   523
            Objects.requireNonNull(file);
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   524
            List<OpenOption> opts = List.of(openOptions);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   525
            if (opts.contains(DELETE_ON_CLOSE) || opts.contains(READ)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   526
                // these options make no sense, since the FileChannel is not exposed
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   527
                throw new IllegalArgumentException("invalid openOptions: " + opts);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   528
            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   529
            return PathBodyHandler.create(file, opts);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   530
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   531
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   532
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   533
         * Returns a {@code BodyHandler<Path>} that returns a
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   534
         * {@link BodySubscriber BodySubscriber}{@code <Path>}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   535
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   536
         * <p> Equivalent to: {@code ofFile(file, CREATE, WRITE)}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   537
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   538
         * <p> Security manager permission checks are performed in this factory
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   539
         * method, when the {@code BodyHandler} is created. Care must be taken
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   540
         * that the {@code BodyHandler} is not shared with untrusted code.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   541
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   542
         * @param file the file to store the body in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   543
         * @return a response body handler
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   544
         * @throws SecurityException If a security manager has been installed
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
   545
         *          and it denies {@linkplain SecurityManager#checkWrite(String)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   546
         *          write access} to the file.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   547
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   548
        public static BodyHandler<Path> ofFile(Path file) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   549
            return BodyHandlers.ofFile(file, CREATE, WRITE);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   551
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   552
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   553
         * Returns a {@code BodyHandler<Path>} that returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   554
         * {@link BodySubscriber BodySubscriber}&lt;{@link Path}&gt;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   555
         * where the download directory is specified, but the filename is
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   556
         * obtained from the {@code Content-Disposition} response header. The
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   557
         * {@code Content-Disposition} header must specify the <i>attachment</i>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   558
         * type and must also contain a <i>filename</i> parameter. If the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   559
         * filename specifies multiple path components only the final component
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   560
         * is used as the filename (with the given directory name).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   561
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   562
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   563
         * been completely written to the file and {@link #body()} returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
         * {@code Path} object for the file. The returned {@code Path} is the
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   565
         * combination of the supplied directory name and the file name supplied
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   566
         * by the server. If the destination directory does not exist or cannot
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   567
         * be written to, then the response will fail with an {@link IOException}.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   568
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   569
         * <p> Security manager permission checks are performed in this factory
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   570
         * method, when the {@code BodyHandler} is created. Care must be taken
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   571
         * that the {@code BodyHandler} is not shared with untrusted code.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   572
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   573
         * @param directory the directory to store the file in
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   574
         * @param openOptions open options used when opening the file
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   575
         * @return a response body handler
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   576
         * @throws IllegalArgumentException if the given path does not exist,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   577
         *          is not a directory, is not writable, or if an invalid set
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   578
         *          of open options are specified
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   579
         * @throws SecurityException If a security manager has been installed
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   580
         *          and it denies
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   581
         *          {@linkplain SecurityManager#checkRead(String) read access}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   582
         *          to the directory, or it denies
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   583
         *          {@linkplain SecurityManager#checkWrite(String) write access}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   584
         *          to the directory, or it denies
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   585
         *          {@linkplain SecurityManager#checkWrite(String) write access}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   586
         *          to the files within the directory.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   587
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   588
        public static BodyHandler<Path> ofFileDownload(Path directory,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   589
                                                       OpenOption... openOptions) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   590
            Objects.requireNonNull(directory);
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   591
            List<OpenOption> opts = List.of(openOptions);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   592
            if (opts.contains(DELETE_ON_CLOSE)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   593
                throw new IllegalArgumentException("invalid option: " + DELETE_ON_CLOSE);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   594
            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   595
            return FileDownloadBodyHandler.create(directory, opts);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   596
        }
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
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   599
         * Returns a {@code BodyHandler<InputStream>} that returns a
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   600
         * {@link BodySubscriber BodySubscriber}{@code <InputStream>} obtained from
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   601
         * {@link BodySubscribers#ofInputStream() BodySubscribers.ofInputStream}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   602
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   603
         * <p> When the {@code HttpResponse} object is returned, the response
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   604
         * headers will have been completely read, but the body may not have
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   605
         * been fully received yet. The {@link #body()} method returns an
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   606
         * {@link InputStream} from which the body can be read as it is received.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   607
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   608
         * @apiNote See {@link BodySubscribers#ofInputStream()} for more
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   609
         * information.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   610
         *
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   611
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   612
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   613
        public static BodyHandler<InputStream> ofInputStream() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   614
            return (responseInfo) -> BodySubscribers.ofInputStream();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   615
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   616
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   617
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   618
         * Returns a {@code BodyHandler<Stream<String>>} that returns a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   619
         * {@link BodySubscriber BodySubscriber}{@code <Stream<String>>} obtained
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   620
         * from {@link BodySubscribers#ofLines(Charset) BodySubscribers.ofLines(charset)}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   621
         * The {@link Charset charset} used to decode the response body bytes is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   622
         * obtained from the HTTP response headers as specified by {@link #ofString()},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   623
         * and lines are delimited in the manner of {@link BufferedReader#readLine()}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   624
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   625
         * <p> When the {@code HttpResponse} object is returned, the body may
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   626
         * not have been completely received.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   627
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   628
         * @return a response body handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   629
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   630
        public static BodyHandler<Stream<String>> ofLines() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   631
            return (responseInfo) ->
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   632
                    BodySubscribers.ofLines(charsetFrom(responseInfo.headers()));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   633
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   634
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   635
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   636
         * Returns a {@code BodyHandler<Void>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   637
         * {@link BodySubscriber BodySubscriber}{@code <Void>} obtained from
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   638
         * {@link BodySubscribers#ofByteArrayConsumer(Consumer)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   639
         * BodySubscribers.ofByteArrayConsumer(Consumer)}.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   640
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   641
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   642
         * been completely written to the consumer.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   643
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   644
         * @apiNote
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   645
         * The subscriber returned by this handler is not flow controlled.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   646
         * Therefore, the supplied consumer must be able to process whatever
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   647
         * amount of data is delivered in a timely fashion.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   648
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   649
         * @param consumer a Consumer to accept the response body
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   650
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   651
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   652
        public static BodyHandler<Void>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   653
        ofByteArrayConsumer(Consumer<Optional<byte[]>> consumer) {
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   654
            Objects.requireNonNull(consumer);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   655
            return (responseInfo) -> BodySubscribers.ofByteArrayConsumer(consumer);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   656
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   657
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   658
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   659
         * Returns a {@code BodyHandler<byte[]>} that returns a
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
   660
         * {@link BodySubscriber BodySubscriber}{@code <byte[]>} obtained
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   661
         * from {@link BodySubscribers#ofByteArray() BodySubscribers.ofByteArray()}.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   662
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   663
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   664
         * been completely written to the byte array.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   665
         *
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   666
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   667
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   668
        public static BodyHandler<byte[]> ofByteArray() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   669
            return (responseInfo) -> BodySubscribers.ofByteArray();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   670
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   671
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   672
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   673
         * Returns a {@code BodyHandler<String>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   674
         * {@link BodySubscriber BodySubscriber}{@code <String>} obtained from
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   675
         * {@link BodySubscribers#ofString(Charset) BodySubscribers.ofString(Charset)}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   676
         * The body is decoded using the character set specified in
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   677
         * the {@code Content-Type} response header. If there is no such
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   678
         * header, or the character set is not supported, then
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   679
         * {@link StandardCharsets#UTF_8 UTF_8} is used.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   680
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   681
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   682
         * been completely written to the string.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   683
         *
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   684
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   685
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   686
        public static BodyHandler<String> ofString() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   687
            return (responseInfo) -> BodySubscribers.ofString(charsetFrom(responseInfo.headers()));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   688
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   689
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   690
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   691
         * Returns a {@code BodyHandler<Publisher<List<ByteBuffer>>>} that creates a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   692
         * {@link BodySubscriber BodySubscriber}{@code <Publisher<List<ByteBuffer>>>}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   693
         * obtained from {@link BodySubscribers#ofPublisher()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   694
         * BodySubscribers.ofPublisher()}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   695
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   696
         * <p> When the {@code HttpResponse} object is returned, the response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   697
         * headers will have been completely read, but the body may not have
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   698
         * been fully received yet. The {@link #body()} method returns a
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
   699
         * {@link Publisher Publisher}{@code <List<ByteBuffer>>} from which the body
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   700
         * response bytes can be obtained as they are received. The publisher
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   701
         * can and must be subscribed to only once.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   702
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   703
         * @apiNote See {@link BodySubscribers#ofPublisher()} for more
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   704
         * information.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   705
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   706
         * @return a response body handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   707
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   708
        public static BodyHandler<Publisher<List<ByteBuffer>>> ofPublisher() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   709
            return (responseInfo) -> BodySubscribers.ofPublisher();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   710
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   711
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   712
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   713
         * Returns a {@code BodyHandler} which, when invoked, returns a {@linkplain
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   714
         * BodySubscribers#buffering(BodySubscriber,int) buffering BodySubscriber}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   715
         * that buffers data before delivering it to the downstream subscriber.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   716
         * These {@code BodySubscriber} instances are created by calling
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   717
         * {@link BodySubscribers#buffering(BodySubscriber,int)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   718
         * BodySubscribers.buffering} with a subscriber obtained from the given
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   719
         * downstream handler and the {@code bufferSize} parameter.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   720
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   721
         * @param <T> the response body type
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   722
         * @param downstreamHandler the downstream handler
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   723
         * @param bufferSize the buffer size parameter passed to {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   724
         *        BodySubscribers#buffering(BodySubscriber,int) BodySubscribers.buffering}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   725
         * @return a body handler
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   726
         * @throws IllegalArgumentException if {@code bufferSize <= 0}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   727
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   728
         public static <T> BodyHandler<T> buffering(BodyHandler<T> downstreamHandler,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   729
                                                    int bufferSize) {
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   730
             Objects.requireNonNull(downstreamHandler);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   731
             if (bufferSize <= 0)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   732
                 throw new IllegalArgumentException("must be greater than 0");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   733
             return (responseInfo) -> BodySubscribers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   734
                     .buffering(downstreamHandler.apply(responseInfo),
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   735
                                bufferSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   736
         }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   737
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   738
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   739
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   740
     * A handler for push promises.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   741
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   742
     * <p> A <i>push promise</i> is a synthetic request sent by an HTTP/2 server
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   743
     * when retrieving an initiating client-sent request. The server has
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   744
     * determined, possibly through inspection of the initiating request, that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   745
     * the client will likely need the promised resource, and hence pushes a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   746
     * synthetic push request, in the form of a push promise, to the client. The
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   747
     * client can choose to accept or reject the push promise request.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   748
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   749
     * <p> A push promise request may be received up to the point where the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   750
     * response body of the initiating client-sent request has been fully
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   751
     * received. The delivery of a push promise response, however, is not
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   752
     * coordinated with the delivery of the response to the initiating
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   753
     * client-sent request.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   754
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   755
     * @param <T> the push promise response body type
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   756
     * @since 11
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   757
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   758
    public interface PushPromiseHandler<T> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   759
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   760
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   761
         * Notification of an incoming push promise.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   762
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   763
         * <p> This method is invoked once for each push promise received, up
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   764
         * to the point where the response body of the initiating client-sent
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   765
         * request has been fully received.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   766
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   767
         * <p> A push promise is accepted by invoking the given {@code acceptor}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   768
         * function. The {@code acceptor} function must be passed a non-null
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   769
         * {@code BodyHandler}, that is to be used to handle the promise's
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   770
         * response body. The acceptor function will return a {@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   771
         * CompletableFuture} that completes with the promise's response.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   772
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   773
         * <p> If the {@code acceptor} function is not successfully invoked,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   774
         * then the push promise is rejected. The {@code acceptor} function will
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   775
         * throw an {@code IllegalStateException} if invoked more than once.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   776
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   777
         * @param initiatingRequest the initiating client-send request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   778
         * @param pushPromiseRequest the synthetic push request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   779
         * @param acceptor the acceptor function that must be successfully
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   780
         *                 invoked to accept the push promise
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   781
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   782
        public void applyPushPromise(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   783
            HttpRequest initiatingRequest,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   784
            HttpRequest pushPromiseRequest,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   785
            Function<HttpResponse.BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   786
        );
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   787
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   788
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   789
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   790
         * Returns a push promise handler that accumulates push promises, and
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   791
         * their responses, into the given map.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   792
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   793
         * <p> Entries are added to the given map for each push promise accepted.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   794
         * The entry's key is the push request, and the entry's value is a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   795
         * {@code CompletableFuture} that completes with the response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   796
         * corresponding to the key's push request. A push request is rejected /
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   797
         * cancelled if there is already an entry in the map whose key is
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
   798
         * {@linkplain HttpRequest#equals equal} to it. A push request is
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   799
         * rejected / cancelled if it  does not have the same origin as its
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   800
         * initiating request.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   801
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   802
         * <p> Entries are added to the given map as soon as practically
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   803
         * possible when a push promise is received and accepted. That way code,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   804
         * using such a map like a cache, can determine if a push promise has
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   805
         * been issued by the server and avoid making, possibly, unnecessary
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   806
         * requests.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   807
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   808
         * <p> The delivery of a push promise response is not coordinated with
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   809
         * the delivery of the response to the initiating client-sent request.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   810
         * However, when the response body for the initiating client-sent
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   811
         * request has been fully received, the map is guaranteed to be fully
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   812
         * populated, that is, no more entries will be added. The individual
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   813
         * {@code CompletableFutures} contained in the map may or may not
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   814
         * already be completed at this point.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   815
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   816
         * @param <T> the push promise response body type
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   817
         * @param pushPromiseHandler t he body handler to use for push promises
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   818
         * @param pushPromisesMap a map to accumulate push promises into
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   819
         * @return a push promise handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   820
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   821
        public static <T> PushPromiseHandler<T>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   822
        of(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   823
           ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   824
            return new PushPromisesHandlerWithMap<>(pushPromiseHandler, pushPromisesMap);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   825
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   826
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   827
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   828
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   829
     * A {@code BodySubscriber} consumes response body bytes and converts them
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   830
     * into a higher-level Java type.  The class {@link BodySubscribers
54096
acf36e8d61c6 8220480: Typo in java.net.http.HttpResponse.BodySubscriber documentation
chegar
parents: 53508
diff changeset
   831
     * BodySubscribers} provides implementations of many common body subscribers.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   832
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   833
     * <p> The object acts as a {@link Flow.Subscriber}&lt;{@link List}&lt;{@link
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   834
     * ByteBuffer}&gt;&gt; to the HTTP Client implementation, which publishes
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   835
     * lists of ByteBuffers containing the response body. The Flow of data, as
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   836
     * well as the order of ByteBuffers in the Flow lists, is a strictly ordered
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   837
     * representation of the response body. Both the Lists and the ByteBuffers,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   838
     * once passed to the subscriber, are no longer used by the HTTP Client. The
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   839
     * subscriber converts the incoming buffers of data to some higher-level
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   840
     * Java type {@code T}.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   841
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   842
     * <p> The {@link #getBody()} method returns a
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
   843
     * {@link CompletionStage}{@code <T>} that provides the response body
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   844
     * object. The {@code CompletionStage} must be obtainable at any time. When
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   845
     * it completes depends on the nature of type {@code T}. In many cases,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   846
     * when {@code T} represents the entire body after being consumed then
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   847
     * the {@code CompletionStage} completes after the body has been consumed.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   848
     * If  {@code T} is a streaming type, such as {@link java.io.InputStream
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   849
     * InputStream}, then it completes before the body has been read, because
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   850
     * the calling code uses the {@code InputStream} to consume the data.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   851
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   852
     * @apiNote To ensure that all resources associated with the corresponding
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   853
     * HTTP exchange are properly released, an implementation of {@code
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
   854
     * BodySubscriber} should ensure to {@linkplain Flow.Subscription#request
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   855
     * request} more data until one of {@link #onComplete() onComplete} or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   856
     * {@link #onError(Throwable) onError} are signalled, or {@link
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   857
     * Flow.Subscription#request cancel} its {@linkplain
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   858
     * #onSubscribe(Flow.Subscription) subscription} if unable or unwilling to
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   859
     * do so. Calling {@code cancel} before exhausting the response body data
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   860
     * may cause the underlying HTTP connection to be closed and prevent it
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   861
     * from being reused for subsequent operations.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   862
     *
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   863
     * @implNote The flow of data containing the response body is immutable.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   864
     * Specifically, it is a flow of unmodifiable lists of read-only ByteBuffers.
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   865
     *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   866
     * @param <T> the response body type
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   867
     * @see BodySubscribers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   868
     * @since 11
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   869
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   870
    public interface BodySubscriber<T>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   871
            extends Flow.Subscriber<List<ByteBuffer>> {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   872
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   873
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   874
         * Returns a {@code CompletionStage} which when completed will return
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   875
         * the response body object. This method can be called at any time
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   876
         * relative to the other {@link Flow.Subscriber} methods and is invoked
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   877
         * using the client's {@link HttpClient#executor() executor}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   878
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   879
         * @return a CompletionStage for the response body
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   880
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   881
        public CompletionStage<T> getBody();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   882
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   883
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   884
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   885
     * Implementations of {@link BodySubscriber BodySubscriber} that implement
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   886
     * various useful subscribers, such as converting the response body bytes
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   887
     * into a String, or streaming the bytes to a file.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   888
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   889
     * <p>The following are examples of using the predefined body subscribers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   890
     * to convert a flow of response body data into common high-level Java
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   891
     * objects:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   892
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   893
     * <pre>{@code    // Streams the response body to a File
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   894
     *   HttpResponse<byte[]> response = client
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   895
     *     .send(request, responseInfo -> BodySubscribers.ofByteArray());
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   896
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   897
     *   // Accumulates the response body and returns it as a byte[]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   898
     *   HttpResponse<byte[]> response = client
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   899
     *     .send(request, responseInfo -> BodySubscribers.ofByteArray());
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   900
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   901
     *   // Discards the response body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   902
     *   HttpResponse<Void> response = client
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   903
     *     .send(request, responseInfo -> BodySubscribers.discarding());
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   904
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   905
     *   // Accumulates the response body as a String then maps it to its bytes
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   906
     *   HttpResponse<byte[]> response = client
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   907
     *     .send(request, responseInfo ->
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   908
     *        BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), String::getBytes));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   909
     * }</pre>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   910
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   911
     * @since 11
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   912
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   913
    public static class BodySubscribers {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   914
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   915
        private BodySubscribers() { }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   916
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   917
        /**
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   918
         * Returns a body subscriber that forwards all response body to the
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   919
         * given {@code Flow.Subscriber}. The {@linkplain BodySubscriber#getBody()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   920
         * completion stage} of the returned body subscriber completes after one
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   921
         * of the given subscribers {@code onComplete} or {@code onError} has
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   922
         * been invoked.
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   923
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   924
         * @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: 48379
diff changeset
   925
         * BodySubscriber} and {@code Flow.Subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   926
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   927
         * @param subscriber the subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   928
         * @return a body subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   929
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   930
        public static BodySubscriber<Void>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   931
        fromSubscriber(Subscriber<? super List<ByteBuffer>> subscriber) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   932
            return new ResponseSubscribers.SubscriberAdapter<>(subscriber, s -> null);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   933
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   934
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   935
        /**
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   936
         * Returns a body subscriber that forwards all response body to the
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   937
         * given {@code Flow.Subscriber}. The {@linkplain BodySubscriber#getBody()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   938
         * completion stage} of the returned body subscriber completes after one
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   939
         * of the given subscribers {@code onComplete} or {@code onError} has
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   940
         * been invoked.
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   941
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   942
         * <p> The given {@code finisher} function is applied after the given
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   943
         * subscriber's {@code onComplete} has been invoked. The {@code finisher}
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   944
         * function is invoked with the given subscriber, and returns a value
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   945
         * that is set as the response's body.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   946
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   947
         * @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: 48379
diff changeset
   948
         * BodySubscriber} and {@code Flow.Subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   949
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   950
         * @param <S> the type of the Subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   951
         * @param <T> the type of the response body
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   952
         * @param subscriber the subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   953
         * @param finisher a function to be applied after the subscriber has
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   954
         *                 completed
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   955
         * @return a body subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   956
         */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   957
        public static <S extends Subscriber<? super List<ByteBuffer>>,T> BodySubscriber<T>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   958
        fromSubscriber(S subscriber,
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   959
                       Function<? super S,? extends T> finisher) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   960
            return new ResponseSubscribers.SubscriberAdapter<>(subscriber, finisher);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   961
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   962
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   963
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   964
         * Returns a body subscriber that forwards all response body to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   965
         * given {@code Flow.Subscriber}, line by line.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   966
         * The {@linkplain BodySubscriber#getBody() completion
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   967
         * stage} of the returned body subscriber completes after one of the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   968
         * given subscribers {@code onComplete} or {@code onError} has been
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   969
         * invoked.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   970
         * Bytes are decoded using the {@link StandardCharsets#UTF_8
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   971
         * UTF-8} charset, and lines are delimited in the manner of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   972
         * {@link BufferedReader#readLine()}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   973
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   974
         * @apiNote This method can be used as an adapter between {@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   975
         * BodySubscriber} and {@code Flow.Subscriber}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   976
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   977
         * @implNote This is equivalent to calling <pre>{@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   978
         *      fromLineSubscriber(subscriber, s -> null, StandardCharsets.UTF_8, null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   979
         * }</pre>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   980
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   981
         * @param subscriber the subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   982
         * @return a body subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   983
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   984
        public static BodySubscriber<Void>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   985
        fromLineSubscriber(Subscriber<? super String> subscriber) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   986
            return fromLineSubscriber(subscriber,  s -> null,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   987
                    StandardCharsets.UTF_8, null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   988
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   989
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   990
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   991
         * Returns a body subscriber that forwards all response body to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   992
         * given {@code Flow.Subscriber}, line by line. The {@linkplain
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   993
         * BodySubscriber#getBody() completion stage} of the returned body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   994
         * subscriber completes after one of the given subscribers
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   995
         * {@code onComplete} or {@code onError} has been invoked.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   996
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   997
         * <p> The given {@code finisher} function is applied after the given
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   998
         * subscriber's {@code onComplete} has been invoked. The {@code finisher}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
   999
         * function is invoked with the given subscriber, and returns a value
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1000
         * that is set as the response's body.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1001
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1002
         * @apiNote This method can be used as an adapter between {@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1003
         * BodySubscriber} and {@code Flow.Subscriber}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1004
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1005
         * @param <S> the type of the Subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1006
         * @param <T> the type of the response body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1007
         * @param subscriber the subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1008
         * @param finisher a function to be applied after the subscriber has
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1009
         *                 completed
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1010
         * @param charset a {@link Charset} to decode the bytes
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1011
         * @param lineSeparator an optional line separator: can be {@code null},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1012
         *                      in which case lines will be delimited in the manner of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1013
         *                      {@link BufferedReader#readLine()}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1014
         * @return a body subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1015
         * @throws IllegalArgumentException if the supplied {@code lineSeparator}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1016
         *         is the empty string
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1017
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1018
        public static <S extends Subscriber<? super String>,T> BodySubscriber<T>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1019
        fromLineSubscriber(S subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1020
                           Function<? super S,? extends T> finisher,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1021
                           Charset charset,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1022
                           String lineSeparator) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1023
            return LineSubscriberAdapter.create(subscriber,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1024
                    finisher, charset, lineSeparator);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
  1025
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
  1026
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
  1027
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1028
         * Returns a body subscriber which stores the response body as a {@code
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1029
         * String} converted using the given {@code Charset}.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1030
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1031
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1032
         * the entire response has been read.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1033
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1034
         * @param charset the character set to convert the String with
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1035
         * @return a body subscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1036
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1037
        public static BodySubscriber<String> ofString(Charset charset) {
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
  1038
            Objects.requireNonNull(charset);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1039
            return new ResponseSubscribers.ByteArraySubscriber<>(
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1040
                    bytes -> new String(bytes, charset)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1041
            );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1042
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1043
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1044
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1045
         * Returns a {@code BodySubscriber} which stores the response body as a
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1046
         * byte array.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1047
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1048
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1049
         * the entire response has been read.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1050
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1051
         * @return a body subscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1052
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1053
        public static BodySubscriber<byte[]> ofByteArray() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1054
            return new ResponseSubscribers.ByteArraySubscriber<>(
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1055
                    Function.identity() // no conversion
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1056
            );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1057
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1058
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1059
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1060
         * Returns a {@code BodySubscriber} which stores the response body in a
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1061
         * file opened with the given options and name. The file will be opened
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1062
         * with the given options using {@link FileChannel#open(Path,OpenOption...)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1063
         * FileChannel.open} just before the body is read. Any exception thrown
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1064
         * will be returned or thrown from {@link HttpClient#send(HttpRequest,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1065
         * BodyHandler) HttpClient::send} or {@link HttpClient#sendAsync(HttpRequest,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1066
         * BodyHandler) HttpClient::sendAsync} as appropriate.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1067
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1068
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1069
         * the entire response has been read.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1070
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1071
         * <p> Security manager permission checks are performed in this factory
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1072
         * method, when the {@code BodySubscriber} is created. Care must be taken
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1073
         * that the {@code BodyHandler} is not shared with untrusted code.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1074
         *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1075
         * @param file the file to store the body in
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1076
         * @param openOptions the list of options to open the file with
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1077
         * @return a body subscriber
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1078
         * @throws IllegalArgumentException if an invalid set of open options
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1079
         *          are specified
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1080
         * @throws SecurityException if a security manager has been installed
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
  1081
         *          and it denies {@linkplain SecurityManager#checkWrite(String)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1082
         *          write access} to the file
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1083
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1084
        public static BodySubscriber<Path> ofFile(Path file, OpenOption... openOptions) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1085
            Objects.requireNonNull(file);
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
  1086
            List<OpenOption> opts = List.of(openOptions);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1087
            if (opts.contains(DELETE_ON_CLOSE) || opts.contains(READ)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1088
                // these options make no sense, since the FileChannel is not exposed
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1089
                throw new IllegalArgumentException("invalid openOptions: " + opts);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1090
            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1091
            return PathSubscriber.create(file, opts);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1092
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1093
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1094
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1095
         * Returns a {@code BodySubscriber} which stores the response body in a
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1096
         * file opened with the given name.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1097
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1098
         * <p> Equivalent to: {@code ofFile(file, CREATE, WRITE)}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1099
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1100
         * <p> Security manager permission checks are performed in this factory
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1101
         * method, when the {@code BodySubscriber} is created. Care must be taken
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1102
         * that the {@code BodyHandler} is not shared with untrusted code.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1103
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1104
         * @param file the file to store the body in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1105
         * @return a body subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1106
         * @throws SecurityException if a security manager has been installed
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
  1107
         *          and it denies {@linkplain SecurityManager#checkWrite(String)
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1108
         *          write access} to the file
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1109
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1110
        public static BodySubscriber<Path> ofFile(Path file) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1111
            return ofFile(file, CREATE, WRITE);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1112
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1113
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1114
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1115
         * Returns a {@code BodySubscriber} which provides the incoming body
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1116
         * data to the provided Consumer of {@code Optional<byte[]>}. Each
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1117
         * call to {@link Consumer#accept(java.lang.Object) Consumer.accept()}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1118
         * will contain a non empty {@code Optional}, except for the final
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1119
         * invocation after all body data has been read, when the {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1120
         * Optional} will be empty.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1121
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1122
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1123
         * the entire response has been read.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1124
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1125
         * @apiNote
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1126
         * This subscriber is not flow controlled.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1127
         * Therefore, the supplied consumer must be able to process whatever
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1128
         * amount of data is delivered in a timely fashion.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1129
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1130
         * @param consumer a Consumer of byte arrays
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1131
         * @return a BodySubscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1132
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1133
        public static BodySubscriber<Void>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1134
        ofByteArrayConsumer(Consumer<Optional<byte[]>> consumer) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1135
            return new ResponseSubscribers.ConsumerSubscriber(consumer);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1136
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1137
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1138
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1139
         * Returns a {@code BodySubscriber} which streams the response body as
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1140
         * an {@link InputStream}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1141
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1142
         * <p> The {@link HttpResponse} using this subscriber is available
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1143
         * immediately after the response headers have been read, without
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1144
         * requiring to wait for the entire body to be processed. The response
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1145
         * body can then be read directly from the {@link InputStream}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1146
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1147
         * @apiNote To ensure that all resources associated with the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1148
         * corresponding exchange are properly released the caller must
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1149
         * ensure to either read all bytes until EOF is reached, or call
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1150
         * {@link InputStream#close} if it is unable or unwilling to do so.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1151
         * Calling {@code close} before exhausting the stream may cause
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1152
         * the underlying HTTP connection to be closed and prevent it
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1153
         * from being reused for subsequent operations.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1154
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1155
         * @return a body subscriber that streams the response body as an
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1156
         *         {@link InputStream}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1157
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1158
        public static BodySubscriber<InputStream> ofInputStream() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1159
            return new ResponseSubscribers.HttpResponseInputStream();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1160
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1161
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1162
        /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1163
         * Returns a {@code BodySubscriber} which streams the response body as
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
  1164
         * a {@link Stream Stream}{@code <String>}, where each string in the stream
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1165
         * corresponds to a line as defined by {@link BufferedReader#lines()}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1166
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1167
         * <p> The {@link HttpResponse} using this subscriber is available
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1168
         * immediately after the response headers have been read, without
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1169
         * requiring to wait for the entire body to be processed. The response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1170
         * body can then be read directly from the {@link Stream}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1171
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1172
         * @apiNote To ensure that all resources associated with the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1173
         * corresponding exchange are properly released the caller must
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1174
         * ensure to either read all lines until the stream is exhausted,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1175
         * or call {@link Stream#close} if it is unable or unwilling to do so.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1176
         * Calling {@code close} before exhausting the stream may cause
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1177
         * the underlying HTTP connection to be closed and prevent it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1178
         * from being reused for subsequent operations.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1179
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1180
         * @param charset the character set to use when converting bytes to characters
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1181
         * @return a body subscriber that streams the response body as a
51982
df8ae450fce3 8211349: Bad HTML in {@link} for HttpResponse.BodyHandlers.ofPublisher
dfuchs
parents: 51128
diff changeset
  1182
         *         {@link Stream Stream}{@code <String>}.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1183
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1184
         * @see BufferedReader#lines()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1185
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1186
        public static BodySubscriber<Stream<String>> ofLines(Charset charset) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1187
            return ResponseSubscribers.createLineStream(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1188
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1189
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1190
        /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1191
         * Returns a response subscriber which publishes the response body
51128
f0193a4828ef 8207265: Bad HTML in {@link} in HttpResponse.BodySubscribers.ofPublisher
chegar
parents: 50681
diff changeset
  1192
         * through a {@code Publisher<List<ByteBuffer>>}.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1193
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1194
         * <p> The {@link HttpResponse} using this subscriber is available
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1195
         * immediately after the response headers have been read, without
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1196
         * requiring to wait for the entire body to be processed. The response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1197
         * body bytes can then be obtained by subscribing to the publisher
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1198
         * returned by the {@code HttpResponse} {@link HttpResponse#body() body}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1199
         * method.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1200
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1201
         * <p>The publisher returned by the {@link HttpResponse#body() body}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1202
         * method can be subscribed to only once. The first subscriber will
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1203
         * receive the body response bytes if successfully subscribed, or will
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1204
         * cause the subscription to be cancelled otherwise.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1205
         * If more subscriptions are attempted, the subsequent subscribers will
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1206
         * be immediately subscribed with an empty subscription and their
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1207
         * {@link Subscriber#onError(Throwable) onError} method
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1208
         * will be invoked with an {@code IllegalStateException}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1209
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1210
         * @apiNote To ensure that all resources associated with the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1211
         * corresponding exchange are properly released the caller must
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1212
         * ensure that the provided publisher is subscribed once, and either
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1213
         * {@linkplain Subscription#request(long) requests} all bytes
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1214
         * until {@link Subscriber#onComplete() onComplete} or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1215
         * {@link Subscriber#onError(Throwable) onError} are invoked, or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1216
         * cancel the provided {@linkplain Subscriber#onSubscribe(Subscription)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1217
         * subscription} if it is unable or unwilling to do so.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1218
         * Note that depending on the actual HTTP protocol {@linkplain
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1219
         * HttpClient.Version version} used for the exchange, cancelling the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1220
         * subscription instead of exhausting the flow may cause the underlying
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1221
         * HTTP connection to be closed and prevent it from being reused for
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1222
         * subsequent operations.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1223
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1224
         * @return A {@code BodySubscriber} which publishes the response body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1225
         *         through a {@code Publisher<List<ByteBuffer>>}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1226
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1227
        public static BodySubscriber<Publisher<List<ByteBuffer>>> ofPublisher() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1228
            return ResponseSubscribers.createPublisher();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1229
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1230
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1231
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1232
         * Returns a response subscriber which discards the response body. The
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1233
         * supplied value is the value that will be returned from
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1234
         * {@link HttpResponse#body()}.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1235
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1236
         * @param <U> the type of the response body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1237
         * @param value the value to return from HttpResponse.body(), may be {@code null}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1238
         * @return a {@code BodySubscriber}
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1239
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1240
        public static <U> BodySubscriber<U> replacing(U value) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1241
            return new ResponseSubscribers.NullSubscriber<>(Optional.ofNullable(value));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1242
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1243
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1244
        /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1245
         * Returns a response subscriber which discards the response body.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1246
         *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1247
         * @return a response body subscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1248
         */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1249
        public static BodySubscriber<Void> discarding() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1250
            return new ResponseSubscribers.NullSubscriber<>(Optional.ofNullable(null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1251
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1252
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1253
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1254
         * Returns a {@code BodySubscriber} which buffers data before delivering
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1255
         * it to the given downstream subscriber. The subscriber guarantees to
55402
b78af6d8a252 8225583: Examine the HttpResponse.BodySubscribers for null handling
chegar
parents: 54096
diff changeset
  1256
         * deliver {@code bufferSize} bytes of data to each invocation of the
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1257
         * downstream's {@link BodySubscriber#onNext(Object) onNext} method,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1258
         * except for the final invocation, just before
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1259
         * {@link BodySubscriber#onComplete() onComplete} is invoked. The final
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1260
         * invocation of {@code onNext} may contain fewer than {@code bufferSize}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1261
         * bytes.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1262
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1263
         * <p> The returned subscriber delegates its {@link BodySubscriber#getBody()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1264
         * getBody()} method to the downstream subscriber.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1265
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1266
         * @param <T> the type of the response body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1267
         * @param downstream the downstream subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1268
         * @param bufferSize the buffer size
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1269
         * @return a buffering body subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1270
         * @throws IllegalArgumentException if {@code bufferSize <= 0}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1271
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1272
         public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1273
                                                       int bufferSize) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1274
             if (bufferSize <= 0)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1275
                 throw new IllegalArgumentException("must be greater than 0");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1276
             return new BufferingSubscriber<>(downstream, bufferSize);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1277
         }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1278
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1279
        /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1280
         * Returns a {@code BodySubscriber} whose response body value is that of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1281
         * the result of applying the given function to the body object of the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1282
         * given {@code upstream} {@code BodySubscriber}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1283
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1284
         * <p> The mapping function is executed using the client's {@linkplain
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1285
         * HttpClient#executor() executor}, and can therefore be used to map any
53508
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1286
         * response body type, including blocking {@link InputStream}.
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1287
         * However, performing any blocking operation in the mapper function
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1288
         * runs the risk of blocking the executor's thread for an unknown
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1289
         * amount of time (at least until the blocking operation finishes),
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1290
         * which may end up starving the executor of available threads.
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1291
         * Therefore, in the case where mapping to the desired type might
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1292
         * block (e.g. by reading on the {@code InputStream}), then mapping
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1293
         * to a {@link java.util.function.Supplier Supplier} of the desired
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1294
         * type and deferring the blocking operation until {@link Supplier#get()
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1295
         * Supplier::get} is invoked by the caller's thread should be preferred,
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1296
         * as shown in the following example which uses a well-known JSON parser to
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1297
         * convert an {@code InputStream} into any annotated Java type.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1298
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1299
         * <p>For example:
53508
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1300
         * <pre> {@code  public static <W> BodySubscriber<Supplier<W>> asJSON(Class<W> targetType) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1301
         *     BodySubscriber<InputStream> upstream = BodySubscribers.ofInputStream();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1302
         *
53508
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1303
         *     BodySubscriber<Supplier<W>> downstream = BodySubscribers.mapping(
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1304
         *           upstream,
53508
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1305
         *           (InputStream is) -> () -> {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1306
         *               try (InputStream stream = is) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1307
         *                   ObjectMapper objectMapper = new ObjectMapper();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1308
         *                   return objectMapper.readValue(stream, targetType);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1309
         *               } catch (IOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1310
         *                   throw new UncheckedIOException(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1311
         *               }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1312
         *           });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1313
         *    return downstream;
53508
04dcc65c9d58 8217627: HttpClient: The API documentation of BodySubscribers::mapping promotes bad behavior
dfuchs
parents: 51982
diff changeset
  1314
         *  } }</pre>
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1315
         *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1316
         * @param <T> the upstream body type
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1317
         * @param <U> the type of the body subscriber returned
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1318
         * @param upstream the body subscriber to be mapped
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1319
         * @param mapper the mapping function
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1320
         * @return a mapping body subscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1321
         */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1322
        public static <T,U> BodySubscriber<U> mapping(BodySubscriber<T> upstream,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1323
                                                      Function<? super T, ? extends U> mapper)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1324
        {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48408
diff changeset
  1325
            return new ResponseSubscribers.MappingSubscriber<>(upstream, mapper);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1326
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1327
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1328
}