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