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