src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/HttpResponse.java
author chegar
Tue, 06 Feb 2018 11:15:40 +0000
branchhttp-client-branch
changeset 56077 3f6b75adcdc0
parent 56075 c76699d2e572
child 56078 6c11b48a0695
permissions -rw-r--r--
http-client-branch: move ResponseSubscribers to internal
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    26
package jdk.incubator.http;
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;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import java.nio.file.Paths;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
import java.nio.file.StandardOpenOption;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    40
import java.security.AccessControlContext;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    41
import java.util.List;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    42
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
import java.util.Optional;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
import java.util.concurrent.CompletionStage;
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
    46
import java.util.concurrent.ConcurrentMap;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
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
    48
import java.util.concurrent.Flow.Subscriber;
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
    49
import java.util.function.BiFunction;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
import java.util.function.Consumer;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
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
    52
import java.util.stream.Stream;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    53
import javax.net.ssl.SSLParameters;
56075
c76699d2e572 http-client-branch: move BufferingSubscriber to internal
chegar
parents: 56071
diff changeset
    54
import jdk.incubator.http.internal.BufferingSubscriber;
56077
3f6b75adcdc0 http-client-branch: move ResponseSubscribers to internal
chegar
parents: 56075
diff changeset
    55
import jdk.incubator.http.internal.ResponseSubscribers;
56075
c76699d2e572 http-client-branch: move BufferingSubscriber to internal
chegar
parents: 56071
diff changeset
    56
import static jdk.incubator.http.internal.common.Utils.unchecked;
c76699d2e572 http-client-branch: move BufferingSubscriber to internal
chegar
parents: 56071
diff changeset
    57
import static jdk.incubator.http.internal.common.Utils.charsetFrom;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
/**
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    60
 * Represents a response to a {@link HttpRequest}.
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    61
 * {@Incubating}
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    62
 *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    63
 * <p> A {@code HttpResponse} is available when the response status code and
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    64
 * headers have been received, and typically after the response body has also
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    65
 * been received. This depends on the response body handler provided when
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    66
 * sending the request. In all cases, the response body handler is invoked
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    67
 * before the body is read. This gives applications an opportunity to decide
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
    68
 * how to handle the body.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    69
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    70
 * <p> Methods are provided in this class for accessing the response headers,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
 * and response body.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    72
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    73
 * <p><b>Response handlers and subscribers</b>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    74
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    75
 * <p> Response bodies are handled at two levels. Application code supplies a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    76
 * response handler ({@link BodyHandler}) which may examine the response status
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    77
 * code and headers, and which then returns a {@link BodySubscriber} to actually
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    78
 * read (or discard) the body and convert it into some useful Java object type.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    79
 * The handler can return one of the pre-defined subscriber types, or a custom
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    80
 * subscriber, or if the body is to be discarded it can call {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    81
 * BodySubscriber#discard(Object) discard} and return a subscriber which
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    82
 * discards the response body. Static implementations of both handlers and
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    83
 * subscribers are provided in {@linkplain BodyHandler BodyHandler} and
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    84
 * {@linkplain BodySubscriber BodySubscriber} respectively. In all cases, the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    85
 * handler functions provided are convenience implementations which ignore the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    86
 * supplied status code and headers and return the relevant pre-defined {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    87
 * BodySubscriber}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
 * <p> See {@link BodyHandler} for example usage.
42460
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
 * @param <T> the response body type
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
 * @since 9
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
public abstract class HttpResponse<T> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
     * Creates an HttpResponse.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    98
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    99
    protected HttpResponse() { }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   100
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   102
     * 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
   103
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
     * @return the response code
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   105
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   106
    public abstract int statusCode();
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
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   109
     * Returns the {@link HttpRequest} corresponding to this response.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   110
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   111
     * <p> This may not be the original request provided by the caller,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   112
     * for example, if that request was redirected.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   113
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
     * @see #previousResponse()
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
     * @return the request
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   118
    public abstract HttpRequest request();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   119
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   121
     * Returns an {@code Optional} containing the previous intermediate response
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   122
     * if one was received. An intermediate response is one that is received
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   123
     * as a result of redirection or authentication. If no previous response
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   124
     * 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
   125
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
     * @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
   127
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
    public abstract Optional<HttpResponse<T>> previousResponse();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   129
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   130
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   131
     * Returns the received response headers.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
     * @return the response headers
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   135
    public abstract HttpHeaders headers();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   136
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
     * Returns the body. Depending on the type of {@code T}, the returned body
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
     * 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
   140
     * {@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
   141
     * 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
   142
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   143
     * <p> If this {@code HttpResponse} was returned from an invocation of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   144
     * {@link #previousResponse()} then this method returns {@code null}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   145
     *
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   146
     * @return the body
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   147
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   148
    public abstract T body();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   149
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   150
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   151
     * Returns the {@link javax.net.ssl.SSLParameters} in effect for this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   152
     * response. Returns {@code null} if this is not a HTTPS response.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   153
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   154
     * @return the SSLParameters associated with the response
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
    public abstract SSLParameters sslParameters();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   157
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   158
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   159
     * 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
   160
     * 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
   161
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   162
     * @return the URI of the response
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   163
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   164
    public abstract URI uri();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   165
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   166
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   167
     * 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
   168
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   169
     * @return HTTP protocol version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   170
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   171
    public abstract HttpClient.Version version();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   172
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   173
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   174
    private static String pathForSecurityCheck(Path path) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   175
        return path.toFile().getPath();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   176
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   177
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   178
    /** A body handler that is further restricted by a given ACC. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   179
    interface UntrustedBodyHandler<T> extends BodyHandler<T> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   180
        void setAccessControlContext(AccessControlContext acc);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   181
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   182
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   183
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   184
     * A Path body handler.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   185
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   186
     * Note: Exists mainly too allow setting of the senders ACC post creation of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   187
     * the handler.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   188
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   189
    static class PathBodyHandler implements UntrustedBodyHandler<Path> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   190
        private final Path file;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   191
        private final OpenOption[]openOptions;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
        private volatile AccessControlContext acc;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   194
        PathBodyHandler(Path file, OpenOption... openOptions) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
            this.file = file;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   196
            this.openOptions = openOptions;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   197
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   198
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   199
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   200
        public void setAccessControlContext(AccessControlContext acc) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   201
            this.acc = acc;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   202
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   203
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   204
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   205
        public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   206
            ResponseSubscribers.PathSubscriber bs = (ResponseSubscribers.PathSubscriber)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   207
                    BodySubscriber.asFileImpl(file, openOptions);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   208
            bs.setAccessControlContext(acc);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   209
            return bs;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   210
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   211
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   212
56031
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   213
    /* package-private with push promise Map implementation */
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   214
    static class PushPromisesHandlerWithMap<T> implements PushPromiseHandler<T> {
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   215
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   216
        private final ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   217
        private final Function<HttpRequest,BodyHandler<T>> pushPromiseHandler;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   218
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   219
        PushPromisesHandlerWithMap(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler,
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   220
                                   ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) {
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   221
            this.pushPromiseHandler = pushPromiseHandler;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   222
            this.pushPromisesMap = pushPromisesMap;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   223
        }
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   224
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   225
        @Override
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   226
        public void applyPushPromise(
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   227
            HttpRequest initiatingRequest, HttpRequest pushRequest,
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   228
            Function<BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor)
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   229
        {
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   230
            URI initiatingURI = initiatingRequest.uri();
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   231
            URI pushRequestURI = pushRequest.uri();
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   232
            if (!initiatingURI.getHost().equalsIgnoreCase(pushRequestURI.getHost()))
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   233
                return;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   234
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   235
            int initiatingPort = initiatingURI.getPort();
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   236
            if (initiatingPort == -1 ) {
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   237
                if ("https".equalsIgnoreCase(initiatingURI.getScheme()))
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   238
                    initiatingPort = 443;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   239
                else
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   240
                    initiatingPort = 80;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   241
            }
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   242
            int pushPort = pushRequestURI.getPort();
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   243
            if (pushPort == -1 ) {
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   244
                if ("https".equalsIgnoreCase(pushRequestURI.getScheme()))
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   245
                    pushPort = 443;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   246
                else
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   247
                    pushPort = 80;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   248
            }
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   249
            if (initiatingPort != pushPort)
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   250
                return;
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   251
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   252
            CompletableFuture<HttpResponse<T>> cf =
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   253
                    acceptor.apply(pushPromiseHandler.apply(pushRequest));
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   254
            pushPromisesMap.put(pushRequest, cf);
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   255
        }
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   256
    }
c52c9a3a2046 http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents: 56025
diff changeset
   257
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   258
    // Similar to Path body handler, but for file download. Supports setting ACC.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   259
    static class FileDownloadBodyHandler implements UntrustedBodyHandler<Path> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   260
        private final Path directory;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   261
        private final OpenOption[]openOptions;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   262
        private volatile AccessControlContext acc;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   263
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   264
        FileDownloadBodyHandler(Path directory, OpenOption... openOptions) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   265
            this.directory = directory;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   266
            this.openOptions = openOptions;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   267
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   268
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   269
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   270
        public void setAccessControlContext(AccessControlContext acc) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
            this.acc = acc;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   272
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   273
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   274
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   275
        public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   276
            String dispoHeader = headers.firstValue("Content-Disposition")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   277
                    .orElseThrow(() -> unchecked(new IOException("No Content-Disposition")));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   278
            if (!dispoHeader.startsWith("attachment;")) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   279
                throw unchecked(new IOException("Unknown Content-Disposition type"));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   280
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   281
            int n = dispoHeader.indexOf("filename=");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   282
            if (n == -1) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   283
                throw unchecked(new IOException("Bad Content-Disposition type"));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   284
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   285
            int lastsemi = dispoHeader.lastIndexOf(';');
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   286
            String disposition;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   287
            if (lastsemi < n) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   288
                disposition = dispoHeader.substring(n + 9);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   289
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   290
                disposition = dispoHeader.substring(n + 9, lastsemi);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   291
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   292
            Path file = Paths.get(directory.toString(), disposition);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   293
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   294
            ResponseSubscribers.PathSubscriber bs = (ResponseSubscribers.PathSubscriber)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   295
                    BodySubscriber.asFileImpl(file, openOptions);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   296
            bs.setAccessControlContext(acc);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   297
            return bs;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   298
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   299
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   300
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   301
    /**
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
   302
     * A handler for response bodies.
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
   303
     * {@Incubating}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   304
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   305
     * <p> This is a function that takes two parameters: the response status code,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   306
     * and the response headers, and which returns a {@linkplain BodySubscriber}.
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
   307
     * The function is always called just before the response body is read. Its
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
   308
     * implementation may examine the status code or headers and must decide,
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
   309
     * whether to accept the response body or discard it, and if accepting it,
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
   310
     * exactly how to handle it.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   311
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   312
     * <p> Some pre-defined implementations which do not utilize the status code
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   313
     * or headers (meaning the body is always accepted) are defined:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   314
     * <ul><li>{@link #asByteArray() }</li>
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   315
     * <li>{@link #asByteArrayConsumer(java.util.function.Consumer)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   316
     * asByteArrayConsumer(Consumer)}</li>
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   317
     * <li>{@link #asString(java.nio.charset.Charset) asString(Charset)}</li>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   318
     * <li>{@link #asFile(Path, OpenOption...)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   319
     * asFile(Path,OpenOption...)}</li>
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   320
     * <li>{@link #asFileDownload(java.nio.file.Path,OpenOption...)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   321
     * asFileDownload(Path,OpenOption...)}</li>
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   322
     * <li>{@link #asInputStream() asInputStream()}</li>
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   323
     * <li>{@link #discard(Object) }</li>
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   324
     * <li>{@link #buffering(BodyHandler, int)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   325
     * buffering(BodyHandler,int)}</li>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   326
     * </ul>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   327
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   328
     * <p> These implementations return the equivalent {@link BodySubscriber}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   329
     * Alternatively, the handler can be used to examine the status code
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   330
     * or headers and return different body subscribers as appropriate.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   331
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   332
     * <p><b>Examples of handler usage</b>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   333
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   334
     * <p> The first example uses one of the predefined handler functions which
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   335
     * ignores the response headers and status, and always process the response
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   336
     * body in the same way.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   337
     * <pre>
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   338
     * {@code
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   339
     *      HttpResponse<Path> resp = HttpRequest
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   340
     *              .create(URI.create("http://www.foo.com"))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   341
     *              .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   342
     *              .response(BodyHandler.asFile(Paths.get("/tmp/f")));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   343
     * }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   344
     * </pre>
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   345
     * Note, that even though these pre-defined handlers ignore the status code
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   346
     * and headers, this information is still accessible from the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   347
     * {@code HttpResponse} when it is returned.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   348
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   349
     * <p> In the second example, the function returns a different subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   350
     * depending on the status code.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   351
     * <pre>
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   352
     * {@code
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   353
     *      HttpResponse<Path> resp1 = HttpRequest
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   354
     *              .create(URI.create("http://www.foo.com"))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   355
     *              .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   356
     *              .response(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   357
     *                  (status, headers) -> status == 200
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   358
     *                      ? BodySubscriber.asFile(Paths.get("/tmp/f"))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   359
     *                      : BodySubscriber.discard(Paths.get("/NULL")));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   360
     * }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   361
     * </pre>
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   362
     *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
     * @param <T> the response body type
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   364
     */
56038
e663f24e3020 http-client-branch: restore inadvertantly removed @FunctionalInterface
chegar
parents: 56034
diff changeset
   365
    @FunctionalInterface
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   366
    public interface BodyHandler<T> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   367
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   368
         * Returns a {@link BodySubscriber BodySubscriber} considering the given
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   369
         * response status code and headers. This method is always called before
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   370
         * the body is read and its implementation can decide to keep the body
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   371
         * and store it somewhere, or else discard it by returning the {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   372
         * BodySubscriber} returned from {@link BodySubscriber#discard(Object)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   373
         * discard}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   374
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   375
         * @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
   376
         * @param responseHeaders the response headers received
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   377
         * @return a body subscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   378
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   379
        public BodySubscriber<T> apply(int statusCode, HttpHeaders responseHeaders);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   380
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   381
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   382
        /**
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   383
         * 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
   384
         * BodySubscriber}{@code <Void>} obtained from {@linkplain
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   385
         * BodySubscriber#fromSubscriber(Subscriber)}, with the given
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   386
         * {@code subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   387
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   388
         * <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
   389
         * 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
   390
         * 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
   391
         * 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
   392
         * database, etc.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   393
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   394
         * @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
   395
         * BodySubscriber} and {@code Flow.Subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   396
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   397
         * <p> For example:
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   398
         * <pre> {@code
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   399
         *  TextSubscriber subscriber = new TextSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   400
         *  HttpResponse<Void> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   401
         *      BodyHandler.fromSubscriber(subscriber)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   402
         *  System.out.println(response.statusCode());
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   403
         * }</pre>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   404
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   405
         * @param subscriber the subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   406
         * @return a response body handler
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   407
         */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   408
        public static BodyHandler<Void>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   409
        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
   410
            Objects.requireNonNull(subscriber);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   411
            return (status, headers) -> BodySubscriber.fromSubscriber(subscriber,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   412
                                                                      s -> null);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   413
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   414
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   415
        /**
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   416
         * 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
   417
         * BodySubscriber}{@code <T>} obtained from {@link
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   418
         * BodySubscriber#fromSubscriber(Subscriber, Function)}, with the
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   419
         * 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
   420
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   421
         * <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
   422
         * 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
   423
         * 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
   424
         * 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
   425
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   426
         * @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
   427
         * BodySubscriber} and {@code Flow.Subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   428
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   429
         * <p> For example:
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   430
         * <pre> {@code
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   431
         * TextSubscriber subscriber = ...;  // accumulates bytes and transforms them into a String
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   432
         * HttpResponse<String> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   433
         *     BodyHandler.fromSubscriber(subscriber, TextSubscriber::getTextResult)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   434
         * String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   435
         * }</pre>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   436
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   437
         * @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
   438
         * @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
   439
         * @param subscriber the subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   440
         * @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
   441
         * @return a response body handler
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   442
         */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   443
        public static <S extends Subscriber<? super List<ByteBuffer>>,T> BodyHandler<T>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   444
        fromSubscriber(S subscriber, Function<S,T> finisher) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   445
            Objects.requireNonNull(subscriber);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   446
            Objects.requireNonNull(finisher);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   447
            return (status, headers) -> BodySubscriber.fromSubscriber(subscriber,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   448
                                                                      finisher);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   449
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   450
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   451
        /**
56009
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   452
         * 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
   453
         * BodySubscriber}{@code <Void>} obtained from {@link
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   454
         * BodySubscriber#fromLineSubscriber(Subscriber, Function, Charset, String)
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   455
         * BodySubscriber.fromLineSubscriber(subscriber, s -> null, charset, null)},
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   456
         * with the given {@code subscriber}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   457
         * The {@link Charset charset} used to decode the response body bytes is
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   458
         * obtained from the HTTP response headers as specified by {@link #asString()},
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   459
         * 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
   460
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   461
         * <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
   462
         * 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
   463
         * 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
   464
         * 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
   465
         * database, etc.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   466
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   467
         * @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
   468
         * BodySubscriber} and {@code Flow.Subscriber}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   469
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   470
         * <p> For example:
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   471
         * <pre> {@code
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   472
         *  TextSubscriber subscriber = new TextSubscriber();
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   473
         *  HttpResponse<Void> response = client.sendAsync(request,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   474
         *      BodyHandler.fromLineSubscriber(subscriber, "\n")).join();
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   475
         *  System.out.println(response.statusCode());
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   476
         * }</pre>
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   477
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   478
         * @param subscriber the subscriber
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   479
         * @return a response body handler
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   480
         */
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   481
        public static BodyHandler<Void>
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   482
        fromLineSubscriber(Subscriber<? super String> subscriber) {
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   483
            Objects.requireNonNull(subscriber);
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   484
            return (status, headers)
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   485
                    -> BodySubscriber.fromLineSubscriber(subscriber, s -> null,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   486
                    charsetFrom(headers), null);
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   487
        }
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   488
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   489
        /**
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   490
         * 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
   491
         * BodySubscriber}{@code <T>} obtained from {@link
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   492
         * BodySubscriber#fromLineSubscriber(Subscriber, Function, Charset, String)
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   493
         * BodySubscriber.fromLineSubscriber(subscriber, finisher, charset, lineSeparator)},
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   494
         * 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
   495
         * The {@link Charset charset} used to decode the response body bytes is
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   496
         * obtained from the HTTP response headers as specified by {@link #asString()}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   497
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   498
         * <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
   499
         * 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
   500
         * 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
   501
         * 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
   502
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   503
         * @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
   504
         * BodySubscriber} and {@code Flow.Subscriber}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   505
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   506
         * <p> For example:
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   507
         * <pre> {@code
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   508
         * TextSubscriber subscriber = ...;  // accumulates bytes and transforms them into a String
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   509
         * HttpResponse<String> response = client.sendAsync(request,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   510
         *     BodyHandler.fromSubscriber(subscriber, TextSubscriber::getTextResult, "\n")).join();
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   511
         * String text = response.body();
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   512
         * }</pre>
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   513
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   514
         * @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
   515
         * @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
   516
         * @param subscriber the subscriber
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   517
         * @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
   518
         * @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
   519
         *                      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
   520
         *                      {@link BufferedReader#readLine()}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   521
         * @return a response body handler
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   522
         * @throws IllegalArgumentException if the supplied {@code lineSeparator} is the empty string.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   523
         */
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   524
        public static <S extends Subscriber<? super String>,T> BodyHandler<T>
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   525
        fromLineSubscriber(S subscriber, Function<S,T> finisher, String lineSeparator) {
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   526
            Objects.requireNonNull(subscriber);
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   527
            Objects.requireNonNull(finisher);
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   528
            // implicit null check
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   529
            if (lineSeparator != null && lineSeparator.isEmpty())
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   530
                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
   531
            return (status, headers) ->
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   532
                    BodySubscriber.fromLineSubscriber(subscriber, finisher,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   533
                            charsetFrom(headers), lineSeparator);
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   534
        }
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   535
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   536
        /**
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   537
         * Returns a response body handler which discards the response body and
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   538
         * uses the given value as a replacement for it.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   539
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   540
         * @param <U> the response body type
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   541
         * @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
   542
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   543
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   544
        public static <U> BodyHandler<U> discard(U value) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   545
            return (status, headers) -> BodySubscriber.discard(value);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   546
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   547
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   548
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   549
         * Returns a {@code BodyHandler<String>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
         * {@link BodySubscriber BodySubscriber}{@code <String>} obtained from
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   551
         * {@link BodySubscriber#asString(Charset) BodySubscriber.asString(Charset)}.
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   552
         * 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
   553
         *
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   554
         * @param charset the character set to convert the body with
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   555
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   556
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   557
        public static BodyHandler<String> asString(Charset charset) {
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   558
            Objects.requireNonNull(charset);
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   559
            return (status, headers) -> BodySubscriber.asString(charset);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   560
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   561
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   562
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   563
         * Returns a {@code BodyHandler<Path>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
         * {@link BodySubscriber BodySubscriber}{@code <Path>} obtained from
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
         * {@link BodySubscriber#asFile(Path, OpenOption...)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   566
         * BodySubscriber.asFile(Path,OpenOption...)}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   568
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   569
         * been completely written to the file, and {@link #body()} returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   570
         * reference to its {@link Path}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   572
         * @param file the filename to store the body in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   573
         * @param openOptions any options to use when opening/creating the file
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   574
         * @return a response body handler
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   575
         * @throws SecurityException If a security manager has been installed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   576
         *          and it denies {@link SecurityManager#checkWrite(String)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   577
         *          write access} to the file. The {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   578
         *          SecurityManager#checkDelete(String) checkDelete} method is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   579
         *          invoked to check delete access if the file is opened with
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   580
         *          the {@code DELETE_ON_CLOSE} option.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   581
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   582
        public static BodyHandler<Path> asFile(Path file, OpenOption... openOptions) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   583
            Objects.requireNonNull(file);
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   584
            List<OpenOption> opts = List.of(openOptions);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   585
            SecurityManager sm = System.getSecurityManager();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   586
            if (sm != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   587
                String fn = pathForSecurityCheck(file);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   588
                sm.checkWrite(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   589
                if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   590
                    sm.checkDelete(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   591
                if (opts.contains(StandardOpenOption.READ))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   592
                    sm.checkRead(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   593
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   594
            return new PathBodyHandler(file, openOptions);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   595
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   596
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   597
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   598
         * Returns a {@code BodyHandler<Path>} that returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   599
         * {@link BodySubscriber BodySubscriber}{@code <Path>} obtained from
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   600
         * {@link BodySubscriber#asFile(Path) BodySubscriber.asFile(Path)}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   601
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   602
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   603
         * been completely written to the file, and {@link #body()} returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   604
         * reference to its {@link Path}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   605
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   606
         * @param file the file to store the body in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   607
         * @return a response body handler
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   608
         * @throws SecurityException if a security manager has been installed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   609
         *          and it denies {@link SecurityManager#checkWrite(String)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   610
         *          write access} to the file
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   611
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   612
        public static BodyHandler<Path> asFile(Path file) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   613
            return BodyHandler.asFile(file, StandardOpenOption.CREATE,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   614
                                            StandardOpenOption.WRITE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   615
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   616
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   617
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   618
         * Returns a {@code BodyHandler<Path>} that returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   619
         * {@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
   620
         * 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
   621
         * obtained from the {@code Content-Disposition} response header. The
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   622
         * {@code Content-Disposition} header must specify the <i>attachment</i>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   623
         * type and must also contain a <i>filename</i> parameter. If the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   624
         * filename specifies multiple path components only the final component
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   625
         * is used as the filename (with the given directory name).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   626
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   627
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   628
         * been completely written to the file and {@link #body()} returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   629
         * {@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
   630
         * 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
   631
         * 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
   632
         * 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
   633
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   634
         * @param directory the directory to store the file in
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   635
         * @param openOptions open options
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   636
         * @return a response body handler
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   637
         * @throws SecurityException If a security manager has been installed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   638
         *          and it denies {@link SecurityManager#checkWrite(String)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   639
         *          write access} to the file. The {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   640
         *          SecurityManager#checkDelete(String) checkDelete} method is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   641
         *          invoked to check delete access if the file is opened with
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   642
         *          the {@code DELETE_ON_CLOSE} option.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   643
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   644
         //####: check if the dir exists and is writable??
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   645
        public static BodyHandler<Path> asFileDownload(Path directory,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   646
                                                       OpenOption... openOptions) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   647
            Objects.requireNonNull(directory);
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   648
            List<OpenOption> opts = List.of(openOptions);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   649
            SecurityManager sm = System.getSecurityManager();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   650
            if (sm != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   651
                String fn = pathForSecurityCheck(directory);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   652
                sm.checkWrite(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   653
                if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   654
                    sm.checkDelete(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   655
                if (opts.contains(StandardOpenOption.READ))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   656
                    sm.checkRead(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   657
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   658
            return new FileDownloadBodyHandler(directory, openOptions);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   659
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   660
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   661
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   662
         * Returns a {@code BodyHandler<InputStream>} that returns a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   663
         * {@link BodySubscriber BodySubscriber}{@code <InputStream>} obtained
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   664
         * from {@link BodySubscriber#asInputStream() BodySubscriber.asInputStream}.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   665
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   666
         * <p> When the {@code HttpResponse} object is returned, the response
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   667
         * headers will have been completely read, but the body may not have
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   668
         * been fully received yet. The {@link #body()} method returns an
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   669
         * {@link InputStream} from which the body can be read as it is received.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   670
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   671
         * @apiNote See {@link BodySubscriber#asInputStream()} for more information.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   672
         *
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   673
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   674
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   675
        public static BodyHandler<InputStream> asInputStream() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   676
            return (status, headers) -> BodySubscriber.asInputStream();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   677
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   678
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   679
        /**
56009
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   680
         * Returns a {@code BodyHandler<Stream<String>>} that returns a
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   681
         * {@link BodySubscriber BodySubscriber}{@code <Stream<String>>} obtained from
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   682
         * {@link BodySubscriber#asLines(Charset)}
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   683
         * BodySubscriber.asLines(charset)}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   684
         * The {@link Charset charset} used to decode the response body bytes is
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   685
         * obtained from the HTTP response headers as specified by {@link #asString()},
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   686
         * 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
   687
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   688
         * <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
   689
         * not have been completely received.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   690
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   691
         * @return a response body handler
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   692
         */
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   693
        public static BodyHandler<Stream<String>> asLines() {
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   694
            return (status, headers) ->
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   695
                    BodySubscriber.asLines(charsetFrom(headers));
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   696
        }
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   697
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   698
        /**
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   699
         * Returns a {@code BodyHandler<Void>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   700
         * {@link BodySubscriber BodySubscriber}{@code <Void>} obtained from
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   701
         * {@link BodySubscriber#asByteArrayConsumer(Consumer)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   702
         * BodySubscriber.asByteArrayConsumer(Consumer)}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   703
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   704
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   705
         * been completely written to the consumer.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   706
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   707
         * @param consumer a Consumer to accept the response body
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   708
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   709
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   710
        public static BodyHandler<Void> asByteArrayConsumer(Consumer<Optional<byte[]>> consumer) {
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   711
            Objects.requireNonNull(consumer);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   712
            return (status, headers) -> BodySubscriber.asByteArrayConsumer(consumer);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   713
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   714
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   715
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   716
         * Returns a {@code BodyHandler<byte[]>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   717
         * {@link BodySubscriber BodySubscriber}&lt;{@code byte[]}&gt; obtained
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   718
         * from {@link BodySubscriber#asByteArray() BodySubscriber.asByteArray()}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   719
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   720
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   721
         * 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
   722
         *
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   723
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   724
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   725
        public static BodyHandler<byte[]> asByteArray() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   726
            return (status, headers) -> BodySubscriber.asByteArray();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   727
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   728
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   729
        /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   730
         * Returns a {@code BodyHandler<String>} that returns a
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   731
         * {@link BodySubscriber BodySubscriber}{@code <String>} obtained from
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   732
         * {@link BodySubscriber#asString(java.nio.charset.Charset)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   733
         * BodySubscriber.asString(Charset)}. The body is
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   734
         * decoded using the character set specified in
56009
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   735
         * 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
   736
         * header, or the character set is not supported, then
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   737
         * {@link java.nio.charset.StandardCharsets#UTF_8 UTF_8} is used.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   738
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   739
         * <p> When the {@code HttpResponse} object is returned, the body has
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   740
         * 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
   741
         *
44847
cf230b81a508 8179392: Fix warnings in the httpclient javadoc
chegar
parents: 43730
diff changeset
   742
         * @return a response body handler
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   743
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   744
        public static BodyHandler<String> asString() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   745
            return (status, headers) -> BodySubscriber.asString(charsetFrom(headers));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   746
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   747
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   748
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   749
         * Returns a {@code BodyHandler} which, when invoked, returns a {@linkplain
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   750
         * BodySubscriber#buffering(BodySubscriber,int) buffering BodySubscriber}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   751
         * that buffers data before delivering it to the downstream subscriber.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   752
         * These {@code BodySubscriber} instances are created by calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   753
         * {@linkplain BodySubscriber#buffering(BodySubscriber,int)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   754
         * BodySubscriber.buffering} with a subscriber obtained from the given
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   755
         * downstream handler and the {@code bufferSize} parameter.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   756
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   757
         * @param downstreamHandler the downstream handler
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   758
         * @param bufferSize the buffer size parameter passed to {@linkplain
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   759
         *        BodySubscriber#buffering(BodySubscriber,int) BodySubscriber.buffering}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   760
         * @return a body handler
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   761
         * @throws IllegalArgumentException if {@code bufferSize <= 0}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   762
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   763
         public static <T> BodyHandler<T> buffering(BodyHandler<T> downstreamHandler,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   764
                                                    int bufferSize) {
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
   765
             Objects.requireNonNull(downstreamHandler);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   766
             if (bufferSize <= 0)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   767
                 throw new IllegalArgumentException("must be greater than 0");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   768
             return (status, headers) -> BodySubscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   769
                     .buffering(downstreamHandler.apply(status, headers),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   770
                                bufferSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   771
         }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   772
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   773
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   774
    /**
56025
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   775
     * A handler for push promises.
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   776
     *
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   777
     * <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
   778
     * when retrieving an initiating client-sent request. The server has
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   779
     * determined, possibly through inspection of the initiating request, that
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   780
     * 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
   781
     * 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
   782
     * client can choose to accept or reject the push promise request.
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   783
     *
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   784
     * <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
   785
     * response body of the initiating client-sent request has been fully
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   786
     * received. The delivery of a push promise response, however, is not
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   787
     * coordinated with the delivery of the response to the initiating
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   788
     * client-sent request.
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   789
     *
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   790
     * @param <T> the push promise response body type
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   791
     */
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   792
    public interface PushPromiseHandler<T> {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   793
        /**
56025
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   794
         * Notification of an incoming push promise.
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   795
         *
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   796
         * <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
   797
         * 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
   798
         * request has been fully received.
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   799
         *
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   800
         * <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
   801
         * function. The {@code acceptor} function must be passed a non-null
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   802
         * {@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
   803
         * response body. The acceptor function will return a {@code
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   804
         * CompletableFuture} that completes with the promise's response.
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   805
         *
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   806
         * <p> If the {@code acceptor} function is not successfully invoked,
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   807
         * then the push promise is rejected. The {@code acceptor} function will
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   808
         * throw an {@code IllegalStateException} if invoked more than once.
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   809
         *
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   810
         * @param initiatingRequest the initiating client-send request
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   811
         * @param pushPromiseRequest the synthetic push request
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   812
         * @param acceptor the acceptor function that must be successfully
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   813
         *                 invoked to accept the push promise
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   814
         */
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   815
        public void applyPushPromise(
56025
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   816
            HttpRequest initiatingRequest,
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   817
            HttpRequest pushPromiseRequest,
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   818
            Function<HttpResponse.BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   819
        );
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   820
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   821
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   822
        /**
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   823
         * Returns a push promise handler that accumulates push promises, and
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   824
         * their responses, into the given map.
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   825
         *
56025
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   826
         * <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
   827
         * 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
   828
         * {@code CompletableFuture} that completes with the response
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   829
         * 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
   830
         * cancelled if there is already an entry in the map whose key is
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   831
         * {@linkplain HttpRequest#equals equal} to it. A push request is
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   832
         * 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
   833
         * initiating request.
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   834
         *
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   835
         * <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
   836
         * 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
   837
         * 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
   838
         * been issued by the server and avoid making, possibly, unnecessary
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   839
         * requests.
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   840
         *
56025
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   841
         * <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
   842
         * the delivery of the response to the initiating client-sent request.
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   843
         * However, when the response body for the initiating client-sent
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   844
         * 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
   845
         * populated, that is, no more entries will be added. The individual
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   846
         * {@code CompletableFutures} contained in the map may or may not
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   847
         * already be completed at this point.
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   848
         *
56025
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   849
         * @param <T> the push promise response body type
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   850
         * @param pushPromiseHandler t he body handler to use for push promises
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   851
         * @param pushPromisesMap a map to accumulate push promises into
56025
1f88e1587067 http-cient-branch: cleanup push promise spec
chegar
parents: 56010
diff changeset
   852
         * @return a push promise handler
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   853
         */
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   854
        public static <T> PushPromiseHandler<T>
56034
43b531ed872b http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents: 56031
diff changeset
   855
        of(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler,
43b531ed872b http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents: 56031
diff changeset
   856
           ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) {
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   857
            return new PushPromisesHandlerWithMap<>(pushPromiseHandler, pushPromisesMap);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   858
        }
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   859
    }
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   860
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56009
diff changeset
   861
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   862
     * A subscriber for response bodies.
43302
45de1dc92ecd 8173354: javadoc warning notice for types in Incubator Modules
chegar
parents: 42460
diff changeset
   863
     * {@Incubating}
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   864
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   865
     * <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
   866
     * ByteBuffer}&gt;&gt; to the HTTP client implementation, which publishes
56071
3353cb42b1b4 http-client-branch: immutable data Flow
chegar
parents: 56038
diff changeset
   867
     * unmodifiable lists of read-only ByteBuffers containing the response body.
3353cb42b1b4 http-client-branch: immutable data Flow
chegar
parents: 56038
diff changeset
   868
     * 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
   869
     * is a strictly ordered representation of the response body. Both the Lists
3353cb42b1b4 http-client-branch: immutable data Flow
chegar
parents: 56038
diff changeset
   870
     * 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
   871
     * the HTTP client. The subscriber converts the incoming buffers of data to
3353cb42b1b4 http-client-branch: immutable data Flow
chegar
parents: 56038
diff changeset
   872
     * some user-defined object type {@code T}.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   873
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   874
     * <p> The {@link #getBody()} method returns a {@link CompletionStage}{@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   875
     * <T>} that provides the response body object. The {@code CompletionStage}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   876
     * must be obtainable at any time. When it completes depends on the nature
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   877
     * of type {@code T}. In many cases, when {@code T} represents the entire
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   878
     * body after being read then it completes after the body has been read. If
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   879
     * {@code T} is a streaming type such as {@link java.io.InputStream} then it
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   880
     * completes before the body has been read, because the calling code uses it
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   881
     * to consume the data.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   882
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   883
     * @apiNote To ensure that all resources associated with the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   884
     * corresponding exchange are properly released, an implementation
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   885
     * of {@code BodySubscriber} must ensure to {@linkplain
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   886
     * Flow.Subscription#request request} more data until {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   887
     * #onComplete() onComplete} or {@link #onError(Throwable) onError}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   888
     * are signalled, or {@linkplain Flow.Subscription#request cancel} its
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   889
     * {@linkplain #onSubscribe(Flow.Subscription) subscription}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   890
     * if unable or unwilling to do so.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   891
     * Calling {@code cancel} before exhausting the data may cause
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   892
     * the underlying HTTP connection to be closed and prevent it
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   893
     * 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
   894
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   895
     * @param <T> the response body type
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   896
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   897
    public interface BodySubscriber<T>
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   898
            extends Flow.Subscriber<List<ByteBuffer>> {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   899
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   900
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   901
         * Returns a {@code CompletionStage} which when completed will return
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   902
         * the response body object.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   903
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   904
         * @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
   905
         */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   906
        public CompletionStage<T> getBody();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   907
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   908
        /**
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   909
         * Returns a body subscriber that forwards all response body to the
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   910
         * given {@code Flow.Subscriber}. The {@linkplain #getBody()} completion
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   911
         * stage} of the returned body subscriber completes after one of the
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   912
         * given subscribers {@code onComplete} or {@code onError} has been
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   913
         * invoked.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   914
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   915
         * @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
   916
         * BodySubscriber} and {@code Flow.Subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   917
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   918
         * @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
   919
         * @param subscriber the subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   920
         * @return a body subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   921
         */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   922
        public static <S extends Subscriber<? super List<ByteBuffer>>> BodySubscriber<Void>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   923
        fromSubscriber(S subscriber) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   924
            return new ResponseSubscribers.SubscriberAdapter<S,Void>(subscriber, s -> null);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   925
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   926
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   927
        /**
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   928
         * Returns a body subscriber that forwards all response body to the
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   929
         * given {@code Flow.Subscriber}. The {@linkplain #getBody()} completion
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   930
         * stage} of the returned body subscriber completes after one of the
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   931
         * given subscribers {@code onComplete} or {@code onError} has been
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   932
         * invoked.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   933
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   934
         * <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
   935
         * 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
   936
         * 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
   937
         * 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
   938
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   939
         * @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
   940
         * BodySubscriber} and {@code Flow.Subscriber}.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   941
         *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   942
         * @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
   943
         * @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
   944
         * @param subscriber the subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   945
         * @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
   946
         *                 completed
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   947
         * @return a body subscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   948
         */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   949
        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
   950
        fromSubscriber(S subscriber,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   951
                       Function<S,T> finisher) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   952
            return new ResponseSubscribers.SubscriberAdapter<S,T>(subscriber, finisher);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   953
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   954
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents: 48379
diff changeset
   955
        /**
56009
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   956
         * Returns a body subscriber that forwards all response body to the
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   957
         * given {@code Flow.Subscriber}, lines by lines.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   958
         * The {@linkplain #getBody()} completion
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   959
         * 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
   960
         * 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
   961
         * invoked.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   962
         * Bytes are decoded using the {@linkplain StandardCharsets#UTF_8
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   963
         * 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
   964
         * {@link BufferedReader#readLine()}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   965
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   966
         * @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
   967
         * BodySubscriber} and {@code Flow.Subscriber}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   968
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   969
         * @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
   970
         *      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
   971
         * }</pre>
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   972
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   973
         * @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
   974
         * @param subscriber the subscriber
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   975
         * @return a body subscriber
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
        public static <S extends Subscriber<? super String>> BodySubscriber<Void>
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   978
        fromLineSubscriber(S subscriber) {
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   979
            return fromLineSubscriber(subscriber, s -> null,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   980
                    StandardCharsets.UTF_8, null);
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   981
        }
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   982
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   983
        /**
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   984
         * Returns a body subscriber that forwards all response body to the
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   985
         * given {@code Flow.Subscriber}, lines by lines.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   986
         * The {@linkplain #getBody()} completion
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   987
         * 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
   988
         * 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
   989
         * invoked.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   990
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   991
         * <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
   992
         * 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
   993
         * 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
   994
         * 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
   995
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   996
         * @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
   997
         * BodySubscriber} and {@code Flow.Subscriber}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   998
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
   999
         * @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
  1000
         * @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
  1001
         * @param subscriber the subscriber
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1002
         * @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
  1003
         *                 completed
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1004
         * @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
  1005
         * @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
  1006
         *                      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
  1007
         *                      {@link BufferedReader#readLine()}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1008
         * @return a body subscriber
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1009
         * @throws IllegalArgumentException if the supplied {@code lineSeparator} is the empty string.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1010
         */
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1011
        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
  1012
        fromLineSubscriber(S subscriber,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1013
                           Function<S,T> finisher,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1014
                           Charset charset,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1015
                           String lineSeparator) {
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1016
            return LineSubscriberAdapter.create(subscriber,
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1017
                    finisher, charset, lineSeparator);
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1018
        }
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1019
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1020
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1021
         * 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
  1022
         * String} converted using the given {@code Charset}.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1023
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1024
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1025
         * 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
  1026
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1027
         * @param charset the character set to convert the String with
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1028
         * @return a body subscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1029
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1030
        public static BodySubscriber<String> asString(Charset charset) {
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
  1031
            Objects.requireNonNull(charset);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1032
            return new ResponseSubscribers.ByteArraySubscriber<>(
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1033
                    bytes -> new String(bytes, charset)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1034
            );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1035
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1036
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1037
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1038
         * 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
  1039
         * byte array.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1040
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1041
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1042
         * 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
  1043
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1044
         * @return a body subscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1045
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1046
        public static BodySubscriber<byte[]> asByteArray() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1047
            return new ResponseSubscribers.ByteArraySubscriber<>(
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1048
                    Function.identity() // no conversion
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1049
            );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1050
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1051
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1052
        // no security check
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1053
        private static BodySubscriber<Path> asFileImpl(Path file, OpenOption... openOptions) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1054
            return new ResponseSubscribers.PathSubscriber(file, openOptions);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1055
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1056
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1057
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1058
         * 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
  1059
         * 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
  1060
         * with the given options using {@link FileChannel#open(Path,OpenOption...)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1061
         * FileChannel.open} just before the body is read. Any exception thrown
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1062
         * will be returned or thrown from {@link HttpClient#send(HttpRequest,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1063
         * BodyHandler) HttpClient::send} or {@link HttpClient#sendAsync(HttpRequest,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1064
         * BodyHandler) HttpClient::sendAsync} as appropriate.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1065
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1066
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1067
         * 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
  1068
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1069
         * @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
  1070
         * @param openOptions the list of options to open the file with
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1071
         * @return a body subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1072
         * @throws SecurityException If a security manager has been installed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1073
         *          and it denies {@link SecurityManager#checkWrite(String)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1074
         *          write access} to the file. The {@link
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1075
         *          SecurityManager#checkDelete(String) checkDelete} method is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1076
         *          invoked to check delete access if the file is opened with the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1077
         *          {@code DELETE_ON_CLOSE} option.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1078
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1079
        public static BodySubscriber<Path> asFile(Path file, OpenOption... openOptions) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1080
            Objects.requireNonNull(file);
48379
5382baab8371 8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents: 48083
diff changeset
  1081
            List<OpenOption> opts = List.of(openOptions);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1082
            SecurityManager sm = System.getSecurityManager();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1083
            if (sm != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1084
                String fn = pathForSecurityCheck(file);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1085
                sm.checkWrite(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1086
                if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1087
                    sm.checkDelete(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1088
                if (opts.contains(StandardOpenOption.READ))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1089
                    sm.checkRead(fn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1090
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1091
            return asFileImpl(file, openOptions);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1092
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1093
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1094
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1095
         * Returns a {@code BodySubscriber} which stores the response body in a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1096
         * file opened with the given name. Has the same effect as calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1097
         * {@link #asFile(Path, OpenOption...) asFile} with the standard open
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1098
         * options {@code CREATE} and {@code WRITE}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1099
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1100
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1101
         * 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
  1102
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1103
         * @param file the file to store the body in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1104
         * @return a body subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1105
         * @throws SecurityException if a security manager has been installed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1106
         *          and it denies {@link SecurityManager#checkWrite(String)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1107
         *          write access} to the file
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1108
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1109
        public static BodySubscriber<Path> asFile(Path file) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1110
            return asFile(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1111
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1112
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1113
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1114
         * Returns a {@code BodySubscriber} which provides the incoming body
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1115
         * data to the provided Consumer of {@code Optional<byte[]>}. Each
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1116
         * call to {@link Consumer#accept(java.lang.Object) Consumer.accept()}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1117
         * will contain a non empty {@code Optional}, except for the final
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1118
         * invocation after all body data has been read, when the {@code
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1119
         * Optional} will be empty.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1120
         *
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1121
         * <p> The {@link HttpResponse} using this subscriber is available after
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1122
         * the entire response has been read.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1123
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1124
         * @param consumer a Consumer of byte arrays
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1125
         * @return a BodySubscriber
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1126
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1127
        public static BodySubscriber<Void> asByteArrayConsumer(Consumer<Optional<byte[]>> consumer) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1128
            return new ResponseSubscribers.ConsumerSubscriber(consumer);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1129
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1130
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1131
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1132
         * Returns a {@code BodySubscriber} which streams the response body as
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1133
         * an {@link InputStream}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1134
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1135
         * <p> The {@link HttpResponse} using this subscriber is available
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1136
         * immediately after the response headers have been read, without
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1137
         * requiring to wait for the entire body to be processed. The response
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1138
         * body can then be read directly from the {@link InputStream}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1139
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1140
         * @apiNote To ensure that all resources associated with the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1141
         * corresponding exchange are properly released the caller must
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1142
         * ensure to either read all bytes until EOF is reached, or call
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1143
         * {@link InputStream#close} if it is unable or unwilling to do so.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1144
         * Calling {@code close} before exhausting the stream may cause
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1145
         * the underlying HTTP connection to be closed and prevent it
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1146
         * from being reused for subsequent operations.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1147
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1148
         * @return a body subscriber that streams the response body as an
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1149
         *         {@link InputStream}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1150
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1151
        public static BodySubscriber<InputStream> asInputStream() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1152
            return new ResponseSubscribers.HttpResponseInputStream();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1153
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1154
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1155
        /**
56009
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1156
         * 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
  1157
         * 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
  1158
         * 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
  1159
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1160
         * <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
  1161
         * 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
  1162
         * 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
  1163
         * 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
  1164
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1165
         * @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
  1166
         * 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
  1167
         * 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
  1168
         * 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
  1169
         * 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
  1170
         * 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
  1171
         * 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
  1172
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1173
         * @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
  1174
         *         {@link Stream Stream<String>}.
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1175
         *
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1176
         * @see BufferedReader#lines()
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1177
         */
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1178
        public static BodySubscriber<Stream<String>> asLines(Charset charset) {
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1179
            return ResponseSubscribers.HttpLineStream.create(charset);
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1180
        }
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1181
cf8792f51dee http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents: 56008
diff changeset
  1182
        /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1183
         * 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
  1184
         * 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
  1185
         * {@link HttpResponse#body()}.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1186
         *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1187
         * @param <U> The type of the response body
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1188
         * @param value the value to return from HttpResponse.body(), may be {@code null}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1189
         * @return a {@code BodySubscriber}
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1190
         */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1191
        public static <U> BodySubscriber<U> discard(U value) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1192
            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
  1193
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1194
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1195
        /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1196
         * Returns a {@code BodySubscriber} which buffers data before delivering
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1197
         * it to the given downstream subscriber. The subscriber guarantees to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1198
         * deliver {@code buffersize} bytes of data to each invocation of the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1199
         * downstream's {@linkplain #onNext(Object) onNext} method, except for
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1200
         * the final invocation, just before {@linkplain #onComplete() onComplete}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1201
         * is invoked. The final invocation of {@code onNext} may contain fewer
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1202
         * than {@code buffersize} bytes.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1203
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1204
         * <p> The returned subscriber delegates its {@link #getBody()} method
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1205
         * to the downstream subscriber.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1206
         *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1207
         * @param downstream the downstream subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1208
         * @param bufferSize the buffer size
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1209
         * @return a buffering body subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1210
         * @throws IllegalArgumentException if {@code bufferSize <= 0}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1211
         */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1212
         public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1213
                                                       int bufferSize) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1214
             if (bufferSize <= 0)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1215
                 throw new IllegalArgumentException("must be greater than 0");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1216
             return new BufferingSubscriber<T>(downstream, bufferSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1217
         }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1218
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1219
}