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