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