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