author | chegar |
Thu, 21 Dec 2017 16:58:51 +0000 | |
changeset 48408 | 4f830b447edf |
parent 48379 | 5382baab8371 |
child 56008 | bbd688c6fbbb |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
44847 | 2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
48083 | 29 |
import java.io.InputStream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.net.URI; |
48083 | 31 |
import jdk.incubator.http.ResponseSubscribers.MultiSubscriberImpl; |
43730 | 32 |
import static jdk.incubator.http.internal.common.Utils.unchecked; |
33 |
import static jdk.incubator.http.internal.common.Utils.charsetFrom; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.nio.ByteBuffer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.nio.charset.Charset; |
48083 | 36 |
import java.nio.channels.FileChannel; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.nio.file.OpenOption; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.nio.file.Path; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.nio.file.Paths; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.nio.file.StandardOpenOption; |
48083 | 41 |
import java.security.AccessControlContext; |
42 |
import java.util.Arrays; |
|
43 |
import java.util.List; |
|
44 |
import java.util.Objects; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
import java.util.Optional; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.util.concurrent.CompletionStage; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
import java.util.function.Consumer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
import java.util.function.Function; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
import javax.net.ssl.SSLParameters; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
/** |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
55 |
* Represents a response to a {@link HttpRequest}. |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
56 |
* {@Incubating} |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
57 |
* |
48083 | 58 |
* <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
|
59 |
* 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
|
60 |
* 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
|
61 |
* 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
|
62 |
* 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
|
63 |
* how to handle the body. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
* <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
|
66 |
* and response body. |
48083 | 67 |
* |
68 |
* <p><b>Response handlers and subscribers</b> |
|
69 |
* |
|
70 |
* <p> Response bodies are handled at two levels. Application code supplies a |
|
71 |
* response handler ({@link BodyHandler}) which may examine the response status |
|
72 |
* code and headers, and which then returns a {@link BodySubscriber} to actually |
|
73 |
* read (or discard) the body and convert it into some useful Java object type. |
|
74 |
* The handler can return one of the pre-defined subscriber types, or a custom |
|
75 |
* subscriber, or if the body is to be discarded it can call {@link |
|
76 |
* BodySubscriber#discard(Object) discard} and return a subscriber which |
|
77 |
* discards the response body. Static implementations of both handlers and |
|
78 |
* subscribers are provided in {@linkplain BodyHandler BodyHandler} and |
|
79 |
* {@linkplain BodySubscriber BodySubscriber} respectively. In all cases, the |
|
80 |
* handler functions provided are convenience implementations which ignore the |
|
81 |
* supplied status code and headers and return the relevant pre-defined {@code |
|
82 |
* BodySubscriber}. |
|
83 |
* |
|
84 |
* <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
|
85 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
* @param <T> the response body type |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
* @since 9 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
public abstract class HttpResponse<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
* Creates an HttpResponse. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
protected HttpResponse() { } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
* 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
|
98 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
* @return the response code |
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 |
public abstract int statusCode(); |
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 |
/** |
48083 | 104 |
* Returns the {@link HttpRequest} corresponding to this response. |
105 |
* |
|
106 |
* <p> This may not be the original request provided by the caller, |
|
107 |
* for example, if that request was redirected. |
|
108 |
* |
|
109 |
* @see #previousResponse() |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
* @return the request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
public abstract HttpRequest request(); |
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 |
/** |
48083 | 116 |
* Returns an {@code Optional} containing the previous intermediate response |
117 |
* if one was received. An intermediate response is one that is received |
|
118 |
* as a result of redirection or authentication. If no previous response |
|
119 |
* 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
|
120 |
* |
48083 | 121 |
* @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
|
122 |
*/ |
48083 | 123 |
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
|
124 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
* Returns the received response headers. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
* @return the response headers |
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 |
public abstract HttpHeaders 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 |
/** |
48083 | 133 |
* Returns the body. Depending on the type of {@code T}, the returned body |
134 |
* 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
|
135 |
* {@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
|
136 |
* 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
|
137 |
* |
48083 | 138 |
* <p> If this {@code HttpResponse} was returned from an invocation of |
139 |
* {@link #previousResponse()} then this method returns {@code null} |
|
140 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
* @return the body |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
public abstract T body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
* 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
|
147 |
* 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
|
148 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
* @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
|
150 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
public abstract SSLParameters sslParameters(); |
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
* 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
|
155 |
* 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
|
156 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
* @return the URI of the response |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
public abstract URI uri(); |
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
162 |
* 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
|
163 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
* @return HTTP protocol version |
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 |
public abstract HttpClient.Version version(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
|
48083 | 168 |
|
169 |
private static String pathForSecurityCheck(Path path) { |
|
170 |
return path.toFile().getPath(); |
|
171 |
} |
|
172 |
||
173 |
/** A body handler that is further restricted by a given ACC. */ |
|
174 |
interface UntrustedBodyHandler<T> extends BodyHandler<T> { |
|
175 |
void setAccessControlContext(AccessControlContext acc); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* A Path body handler. |
|
180 |
* |
|
181 |
* Note: Exists mainly too allow setting of the senders ACC post creation of |
|
182 |
* the handler. |
|
183 |
*/ |
|
184 |
static class PathBodyHandler implements UntrustedBodyHandler<Path> { |
|
185 |
private final Path file; |
|
186 |
private final OpenOption[]openOptions; |
|
187 |
private volatile AccessControlContext acc; |
|
188 |
||
189 |
PathBodyHandler(Path file, OpenOption... openOptions) { |
|
190 |
this.file = file; |
|
191 |
this.openOptions = openOptions; |
|
192 |
} |
|
193 |
||
194 |
@Override |
|
195 |
public void setAccessControlContext(AccessControlContext acc) { |
|
196 |
this.acc = acc; |
|
197 |
} |
|
198 |
||
199 |
@Override |
|
200 |
public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) { |
|
201 |
ResponseSubscribers.PathSubscriber bs = (ResponseSubscribers.PathSubscriber) |
|
202 |
BodySubscriber.asFileImpl(file, openOptions); |
|
203 |
bs.setAccessControlContext(acc); |
|
204 |
return bs; |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
// Similar to Path body handler, but for file download. Supports setting ACC. |
|
209 |
static class FileDownloadBodyHandler implements UntrustedBodyHandler<Path> { |
|
210 |
private final Path directory; |
|
211 |
private final OpenOption[]openOptions; |
|
212 |
private volatile AccessControlContext acc; |
|
213 |
||
214 |
FileDownloadBodyHandler(Path directory, OpenOption... openOptions) { |
|
215 |
this.directory = directory; |
|
216 |
this.openOptions = openOptions; |
|
217 |
} |
|
218 |
||
219 |
@Override |
|
220 |
public void setAccessControlContext(AccessControlContext acc) { |
|
221 |
this.acc = acc; |
|
222 |
} |
|
223 |
||
224 |
@Override |
|
225 |
public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) { |
|
226 |
String dispoHeader = headers.firstValue("Content-Disposition") |
|
227 |
.orElseThrow(() -> unchecked(new IOException("No Content-Disposition"))); |
|
228 |
if (!dispoHeader.startsWith("attachment;")) { |
|
229 |
throw unchecked(new IOException("Unknown Content-Disposition type")); |
|
230 |
} |
|
231 |
int n = dispoHeader.indexOf("filename="); |
|
232 |
if (n == -1) { |
|
233 |
throw unchecked(new IOException("Bad Content-Disposition type")); |
|
234 |
} |
|
235 |
int lastsemi = dispoHeader.lastIndexOf(';'); |
|
236 |
String disposition; |
|
237 |
if (lastsemi < n) { |
|
238 |
disposition = dispoHeader.substring(n + 9); |
|
239 |
} else { |
|
240 |
disposition = dispoHeader.substring(n + 9, lastsemi); |
|
241 |
} |
|
242 |
Path file = Paths.get(directory.toString(), disposition); |
|
243 |
||
244 |
ResponseSubscribers.PathSubscriber bs = (ResponseSubscribers.PathSubscriber) |
|
245 |
BodySubscriber.asFileImpl(file, openOptions); |
|
246 |
bs.setAccessControlContext(acc); |
|
247 |
return bs; |
|
248 |
} |
|
249 |
} |
|
250 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
/** |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
252 |
* A handler for response bodies. |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
253 |
* {@Incubating} |
48083 | 254 |
* |
255 |
* <p> This is a function that takes two parameters: the response status code, |
|
256 |
* 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
|
257 |
* 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
|
258 |
* 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
|
259 |
* 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
|
260 |
* exactly how to handle it. |
48083 | 261 |
* |
262 |
* <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
|
263 |
* 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
|
264 |
* <ul><li>{@link #asByteArray() }</li> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
* <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
|
266 |
* asByteArrayConsumer(Consumer)}</li> |
48083 | 267 |
* <li>{@link #asString(java.nio.charset.Charset) asString(Charset)}</li> |
268 |
* <li>{@link #asFile(Path, OpenOption...) |
|
269 |
* asFile(Path,OpenOption...)}</li> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
* <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
|
271 |
* asFileDownload(Path,OpenOption...)}</li> |
48083 | 272 |
* <li>{@link #asInputStream() asInputStream()}</li> |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
* <li>{@link #discard(Object) }</li> |
48083 | 274 |
* <li>{@link #buffering(BodyHandler, int) |
275 |
* buffering(BodyHandler,int)}</li> |
|
276 |
* </ul> |
|
277 |
* |
|
278 |
* <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
|
279 |
* Alternatively, the handler can be used to examine the status code |
48083 | 280 |
* or headers and return different body subscribers as appropriate. |
281 |
* |
|
282 |
* <p><b>Examples of handler usage</b> |
|
283 |
* |
|
284 |
* <p> The first example uses one of the predefined handler functions which |
|
285 |
* 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
|
286 |
* body in the same way. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
* <pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
* {@code |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
* HttpResponse<Path> resp = HttpRequest |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
290 |
* .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
|
291 |
* .GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
292 |
* .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
|
293 |
* } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
294 |
* </pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
* Note, that even though these pre-defined handlers ignore the status code |
48083 | 296 |
* and headers, this information is still accessible from the |
297 |
* {@code HttpResponse} when it is returned. |
|
298 |
* |
|
299 |
* <p> In the second example, the function returns a different subscriber |
|
300 |
* depending on the status code. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
301 |
* <pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
* {@code |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
* HttpResponse<Path> resp1 = HttpRequest |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
* .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
|
305 |
* .GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
* .response( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
* (status, headers) -> status == 200 |
48083 | 308 |
* ? BodySubscriber.asFile(Paths.get("/tmp/f")) |
309 |
* : BodySubscriber.discard(Paths.get("/NULL"))); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
* } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
* </pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
* |
48083 | 313 |
* @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
|
314 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
@FunctionalInterface |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
public interface BodyHandler<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
318 |
/** |
48083 | 319 |
* Returns a {@link BodySubscriber BodySubscriber} considering the given |
320 |
* response status code and headers. This method is always called before |
|
321 |
* the body is read and its implementation can decide to keep the body |
|
322 |
* and store it somewhere, or else discard it by returning the {@code |
|
323 |
* BodySubscriber} returned from {@link BodySubscriber#discard(Object) |
|
324 |
* discard}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
* @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
|
327 |
* @param responseHeaders the response headers received |
48083 | 328 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
*/ |
48083 | 330 |
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
|
331 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
332 |
/** |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
333 |
* 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
|
334 |
* 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
|
335 |
* 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
|
336 |
* {@code subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
337 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
338 |
* <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
|
339 |
* 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
|
340 |
* 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
|
341 |
* 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
|
342 |
* database, etc. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
343 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
344 |
* @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
|
345 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
346 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
347 |
* <p> For example: |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
348 |
* <pre> {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
349 |
* TextSubscriber subscriber = new TextSubscriber(); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
350 |
* 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
|
351 |
* BodyHandler.fromSubscriber(subscriber)).join(); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
352 |
* System.out.println(response.statusCode()); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
353 |
* }</pre> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
354 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
355 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
356 |
* @return a response body handler |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
357 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
358 |
public static BodyHandler<Void> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
359 |
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
|
360 |
Objects.requireNonNull(subscriber); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
361 |
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
|
362 |
s -> null); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
363 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
364 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
365 |
/** |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
366 |
* 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
|
367 |
* 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
|
368 |
* 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
|
369 |
* 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
|
370 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
371 |
* <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
|
372 |
* 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
|
373 |
* 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
|
374 |
* 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
|
375 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
376 |
* @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
|
377 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
378 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
379 |
* <p> For example: |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
380 |
* <pre> {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
381 |
* 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
|
382 |
* 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
|
383 |
* 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
|
384 |
* String text = response.body(); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
385 |
* }</pre> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
386 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
387 |
* @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
|
388 |
* @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
|
389 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
390 |
* @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
|
391 |
* @return a response body handler |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
392 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
393 |
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
|
394 |
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
|
395 |
Objects.requireNonNull(subscriber); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
396 |
Objects.requireNonNull(finisher); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
397 |
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
|
398 |
finisher); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
399 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
400 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
401 |
/** |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
* 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
|
403 |
* 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
|
404 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
405 |
* @param <U> the response body type |
48083 | 406 |
* @param value the value of U to return as the body, may be {@code null} |
44847 | 407 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
408 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
409 |
public static <U> BodyHandler<U> discard(U value) { |
48083 | 410 |
return (status, headers) -> BodySubscriber.discard(value); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
411 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
412 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
413 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
414 |
* Returns a {@code BodyHandler<String>} that returns a |
48083 | 415 |
* {@link BodySubscriber BodySubscriber}{@code <String>} obtained from |
416 |
* {@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
|
417 |
* 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
|
418 |
* |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
419 |
* @param charset the character set to convert the body with |
44847 | 420 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
421 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
422 |
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
|
423 |
Objects.requireNonNull(charset); |
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
424 |
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
|
425 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
426 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
427 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
428 |
* Returns a {@code BodyHandler<Path>} that returns a |
48083 | 429 |
* {@link BodySubscriber BodySubscriber}{@code <Path>} obtained from |
430 |
* {@link BodySubscriber#asFile(Path, OpenOption...) |
|
431 |
* BodySubscriber.asFile(Path,OpenOption...)}. |
|
432 |
* |
|
433 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
434 |
* been completely written to the file, and {@link #body()} returns a |
|
435 |
* reference to its {@link Path}. |
|
436 |
* |
|
437 |
* @param file the filename to store the body in |
|
438 |
* @param openOptions any options to use when opening/creating the file |
|
439 |
* @return a response body handler |
|
440 |
* @throws SecurityException If a security manager has been installed |
|
441 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
442 |
* write access} to the file. The {@link |
|
443 |
* SecurityManager#checkDelete(String) checkDelete} method is |
|
444 |
* invoked to check delete access if the file is opened with |
|
445 |
* the {@code DELETE_ON_CLOSE} option. |
|
446 |
*/ |
|
447 |
public static BodyHandler<Path> asFile(Path file, OpenOption... openOptions) { |
|
448 |
Objects.requireNonNull(file); |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
449 |
List<OpenOption> opts = List.of(openOptions); |
48083 | 450 |
SecurityManager sm = System.getSecurityManager(); |
451 |
if (sm != null) { |
|
452 |
String fn = pathForSecurityCheck(file); |
|
453 |
sm.checkWrite(fn); |
|
454 |
if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE)) |
|
455 |
sm.checkDelete(fn); |
|
456 |
if (opts.contains(StandardOpenOption.READ)) |
|
457 |
sm.checkRead(fn); |
|
458 |
} |
|
459 |
return new PathBodyHandler(file, openOptions); |
|
460 |
} |
|
461 |
||
462 |
/** |
|
463 |
* Returns a {@code BodyHandler<Path>} that returns a |
|
464 |
* {@link BodySubscriber BodySubscriber}{@code <Path>} obtained from |
|
465 |
* {@link BodySubscriber#asFile(Path) BodySubscriber.asFile(Path)}. |
|
466 |
* |
|
467 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
468 |
* been completely written to the file, and {@link #body()} returns a |
|
469 |
* reference to its {@link Path}. |
|
470 |
* |
|
471 |
* @param file the file to store the body in |
|
472 |
* @return a response body handler |
|
473 |
* @throws SecurityException if a security manager has been installed |
|
474 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
475 |
* write access} to the file |
|
476 |
*/ |
|
477 |
public static BodyHandler<Path> asFile(Path file) { |
|
478 |
return BodyHandler.asFile(file, StandardOpenOption.CREATE, |
|
479 |
StandardOpenOption.WRITE); |
|
480 |
} |
|
481 |
||
482 |
/** |
|
483 |
* Returns a {@code BodyHandler<Path>} that returns a |
|
484 |
* {@link BodySubscriber BodySubscriber}<{@link Path}> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
485 |
* 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
|
486 |
* obtained from the {@code Content-Disposition} response header. The |
48083 | 487 |
* {@code Content-Disposition} header must specify the <i>attachment</i> |
488 |
* type and must also contain a <i>filename</i> parameter. If the |
|
489 |
* filename specifies multiple path components only the final component |
|
490 |
* is used as the filename (with the given directory name). |
|
491 |
* |
|
492 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
493 |
* been completely written to the file and {@link #body()} returns a |
|
494 |
* {@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
|
495 |
* 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
|
496 |
* 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
|
497 |
* 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
|
498 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
499 |
* @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
|
500 |
* @param openOptions open options |
44847 | 501 |
* @return a response body handler |
48083 | 502 |
* @throws SecurityException If a security manager has been installed |
503 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
504 |
* write access} to the file. The {@link |
|
505 |
* SecurityManager#checkDelete(String) checkDelete} method is |
|
506 |
* invoked to check delete access if the file is opened with |
|
507 |
* 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
|
508 |
*/ |
48083 | 509 |
//####: check if the dir exists and is writable?? |
510 |
public static BodyHandler<Path> asFileDownload(Path directory, |
|
511 |
OpenOption... openOptions) { |
|
512 |
Objects.requireNonNull(directory); |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
513 |
List<OpenOption> opts = List.of(openOptions); |
48083 | 514 |
SecurityManager sm = System.getSecurityManager(); |
515 |
if (sm != null) { |
|
516 |
String fn = pathForSecurityCheck(directory); |
|
517 |
sm.checkWrite(fn); |
|
518 |
if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE)) |
|
519 |
sm.checkDelete(fn); |
|
520 |
if (opts.contains(StandardOpenOption.READ)) |
|
521 |
sm.checkRead(fn); |
|
522 |
} |
|
523 |
return new FileDownloadBodyHandler(directory, openOptions); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
524 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
525 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
526 |
/** |
48083 | 527 |
* Returns a {@code BodyHandler<InputStream>} that returns a |
528 |
* {@link BodySubscriber BodySubscriber}{@code <InputStream>} obtained |
|
529 |
* 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
|
530 |
* |
48083 | 531 |
* <p> When the {@code HttpResponse} object is returned, the response |
532 |
* headers will have been completely read, but the body may not have |
|
533 |
* been fully received yet. The {@link #body()} method returns an |
|
534 |
* {@link InputStream} from which the body can be read as it is received. |
|
535 |
* |
|
536 |
* @apiNote See {@link BodySubscriber#asInputStream()} for more information. |
|
537 |
* |
|
44847 | 538 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
539 |
*/ |
48083 | 540 |
public static BodyHandler<InputStream> asInputStream() { |
541 |
return (status, headers) -> BodySubscriber.asInputStream(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
542 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
543 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
544 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
545 |
* Returns a {@code BodyHandler<Void>} that returns a |
48083 | 546 |
* {@link BodySubscriber BodySubscriber}{@code <Void>} obtained from |
547 |
* {@link BodySubscriber#asByteArrayConsumer(Consumer) |
|
548 |
* BodySubscriber.asByteArrayConsumer(Consumer)}. |
|
549 |
* |
|
550 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
551 |
* been completely written to the consumer. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
552 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
553 |
* @param consumer a Consumer to accept the response body |
44847 | 554 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
555 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
556 |
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
|
557 |
Objects.requireNonNull(consumer); |
48083 | 558 |
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
|
559 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
560 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
561 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
562 |
* Returns a {@code BodyHandler<byte[]>} that returns a |
48083 | 563 |
* {@link BodySubscriber BodySubscriber}<{@code byte[]}> obtained |
564 |
* from {@link BodySubscriber#asByteArray() BodySubscriber.asByteArray()}. |
|
565 |
* |
|
566 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
567 |
* 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
|
568 |
* |
44847 | 569 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
570 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
571 |
public static BodyHandler<byte[]> asByteArray() { |
48083 | 572 |
return (status, headers) -> BodySubscriber.asByteArray(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
573 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
574 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
575 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
576 |
* Returns a {@code BodyHandler<String>} that returns a |
48083 | 577 |
* {@link BodySubscriber BodySubscriber}{@code <String>} obtained from |
578 |
* {@link BodySubscriber#asString(java.nio.charset.Charset) |
|
579 |
* 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
|
580 |
* decoded using the character set specified in |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
581 |
* the {@code Content-encoding} response header. If there is no such |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
582 |
* 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
|
583 |
* {@link java.nio.charset.StandardCharsets#UTF_8 UTF_8} is used. |
48083 | 584 |
* |
585 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
586 |
* 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
|
587 |
* |
44847 | 588 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
589 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
590 |
public static BodyHandler<String> asString() { |
48083 | 591 |
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
|
592 |
} |
48083 | 593 |
|
594 |
/** |
|
595 |
* Returns a {@code BodyHandler} which, when invoked, returns a {@linkplain |
|
596 |
* BodySubscriber#buffering(BodySubscriber,int) buffering BodySubscriber} |
|
597 |
* that buffers data before delivering it to the downstream subscriber. |
|
598 |
* These {@code BodySubscriber} instances are created by calling |
|
599 |
* {@linkplain BodySubscriber#buffering(BodySubscriber,int) |
|
600 |
* BodySubscriber.buffering} with a subscriber obtained from the given |
|
601 |
* downstream handler and the {@code bufferSize} parameter. |
|
602 |
* |
|
603 |
* @param downstreamHandler the downstream handler |
|
604 |
* @param bufferSize the buffer size parameter passed to {@linkplain |
|
605 |
* BodySubscriber#buffering(BodySubscriber,int) BodySubscriber.buffering} |
|
606 |
* @return a body handler |
|
607 |
* @throws IllegalArgumentException if {@code bufferSize <= 0} |
|
608 |
*/ |
|
609 |
public static <T> BodyHandler<T> buffering(BodyHandler<T> downstreamHandler, |
|
610 |
int bufferSize) { |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
611 |
Objects.requireNonNull(downstreamHandler); |
48083 | 612 |
if (bufferSize <= 0) |
613 |
throw new IllegalArgumentException("must be greater than 0"); |
|
614 |
return (status, headers) -> BodySubscriber |
|
615 |
.buffering(downstreamHandler.apply(status, headers), |
|
616 |
bufferSize); |
|
617 |
} |
|
42460
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 |
/** |
48083 | 621 |
* A subscriber for response bodies. |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
622 |
* {@Incubating} |
48083 | 623 |
* |
624 |
* <p> The object acts as a {@link Flow.Subscriber}<{@link List}<{@link |
|
625 |
* ByteBuffer}>> to the HTTP client implementation, which publishes |
|
626 |
* unmodifiable lists of ByteBuffers containing the response body. The Flow |
|
627 |
* of data, as well as the order of ByteBuffers in the Flow lists, is a |
|
628 |
* strictly ordered representation of the response body. Both the Lists and |
|
629 |
* the ByteBuffers, once passed to the subscriber, are no longer used by the |
|
630 |
* HTTP client. The subscriber converts the incoming buffers of data to some |
|
631 |
* user-defined object type {@code T}. |
|
632 |
* |
|
633 |
* <p> The {@link #getBody()} method returns a {@link CompletionStage}{@code |
|
634 |
* <T>} that provides the response body object. The {@code CompletionStage} |
|
635 |
* must be obtainable at any time. When it completes depends on the nature |
|
636 |
* of type {@code T}. In many cases, when {@code T} represents the entire |
|
637 |
* body after being read then it completes after the body has been read. If |
|
638 |
* {@code T} is a streaming type such as {@link java.io.InputStream} then it |
|
639 |
* completes before the body has been read, because the calling code uses it |
|
640 |
* to consume the data. |
|
641 |
* |
|
642 |
* @apiNote To ensure that all resources associated with the |
|
643 |
* corresponding exchange are properly released, an implementation |
|
644 |
* of {@code BodySubscriber} must ensure to {@linkplain |
|
645 |
* Flow.Subscription#request request} more data until {@link |
|
646 |
* #onComplete() onComplete} or {@link #onError(Throwable) onError} |
|
647 |
* are signalled, or {@linkplain Flow.Subscription#request cancel} its |
|
648 |
* {@linkplain #onSubscribe(Flow.Subscription) subscription} |
|
649 |
* if unable or unwilling to do so. |
|
650 |
* Calling {@code cancel} before exhausting the data may cause |
|
651 |
* the underlying HTTP connection to be closed and prevent it |
|
652 |
* 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
|
653 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
654 |
* @param <T> the response body type |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
655 |
*/ |
48083 | 656 |
public interface BodySubscriber<T> |
657 |
extends Flow.Subscriber<List<ByteBuffer>> { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
658 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
659 |
/** |
48083 | 660 |
* Returns a {@code CompletionStage} which when completed will return |
661 |
* the response body object. |
|
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 |
* @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
|
664 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
665 |
public CompletionStage<T> getBody(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
666 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
667 |
/** |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
668 |
* Returns a body subscriber that forwards all response body to the |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
669 |
* given {@code Flow.Subscriber}. The {@linkplain #getBody()} completion |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
670 |
* 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
|
671 |
* 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
|
672 |
* invoked. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
673 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
674 |
* @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
|
675 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
676 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
677 |
* @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
|
678 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
679 |
* @return a body subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
680 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
681 |
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
|
682 |
fromSubscriber(S subscriber) { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
683 |
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
|
684 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
685 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
686 |
/** |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
687 |
* Returns a body subscriber that forwards all response body to the |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
688 |
* given {@code Flow.Subscriber}. The {@linkplain #getBody()} completion |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
689 |
* 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
|
690 |
* 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
|
691 |
* invoked. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
692 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
693 |
* <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
|
694 |
* 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
|
695 |
* 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
|
696 |
* 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
|
697 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
698 |
* @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
|
699 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
700 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
701 |
* @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
|
702 |
* @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
|
703 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
704 |
* @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
|
705 |
* completed |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
706 |
* @return a body subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
707 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
708 |
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
|
709 |
fromSubscriber(S subscriber, |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
710 |
Function<S,T> finisher) { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
711 |
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
|
712 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
713 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
714 |
/** |
48083 | 715 |
* 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
|
716 |
* String} converted using the given {@code Charset}. |
48083 | 717 |
* |
718 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
719 |
* 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
|
720 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
721 |
* @param charset the character set to convert the String with |
48083 | 722 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
723 |
*/ |
48083 | 724 |
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
|
725 |
Objects.requireNonNull(charset); |
48083 | 726 |
return new ResponseSubscribers.ByteArraySubscriber<>( |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
727 |
bytes -> new String(bytes, charset) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
728 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
729 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
730 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
731 |
/** |
48083 | 732 |
* 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
|
733 |
* byte array. |
48083 | 734 |
* |
735 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
736 |
* 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
|
737 |
* |
48083 | 738 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
739 |
*/ |
48083 | 740 |
public static BodySubscriber<byte[]> asByteArray() { |
741 |
return new ResponseSubscribers.ByteArraySubscriber<>( |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
742 |
Function.identity() // no conversion |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
743 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
744 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
745 |
|
48083 | 746 |
// no security check |
747 |
private static BodySubscriber<Path> asFileImpl(Path file, OpenOption... openOptions) { |
|
748 |
return new ResponseSubscribers.PathSubscriber(file, openOptions); |
|
749 |
} |
|
750 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
751 |
/** |
48083 | 752 |
* 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
|
753 |
* file opened with the given options and name. The file will be opened |
48083 | 754 |
* with the given options using {@link FileChannel#open(Path,OpenOption...) |
755 |
* FileChannel.open} just before the body is read. Any exception thrown |
|
756 |
* will be returned or thrown from {@link HttpClient#send(HttpRequest, |
|
757 |
* BodyHandler) HttpClient::send} or {@link HttpClient#sendAsync(HttpRequest, |
|
758 |
* BodyHandler) HttpClient::sendAsync} as appropriate. |
|
759 |
* |
|
760 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
761 |
* 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
|
762 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
763 |
* @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
|
764 |
* @param openOptions the list of options to open the file with |
48083 | 765 |
* @return a body subscriber |
766 |
* @throws SecurityException If a security manager has been installed |
|
767 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
768 |
* write access} to the file. The {@link |
|
769 |
* SecurityManager#checkDelete(String) checkDelete} method is |
|
770 |
* invoked to check delete access if the file is opened with the |
|
771 |
* {@code DELETE_ON_CLOSE} option. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
772 |
*/ |
48083 | 773 |
public static BodySubscriber<Path> asFile(Path file, OpenOption... openOptions) { |
774 |
Objects.requireNonNull(file); |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
775 |
List<OpenOption> opts = List.of(openOptions); |
48083 | 776 |
SecurityManager sm = System.getSecurityManager(); |
777 |
if (sm != null) { |
|
778 |
String fn = pathForSecurityCheck(file); |
|
779 |
sm.checkWrite(fn); |
|
780 |
if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE)) |
|
781 |
sm.checkDelete(fn); |
|
782 |
if (opts.contains(StandardOpenOption.READ)) |
|
783 |
sm.checkRead(fn); |
|
784 |
} |
|
785 |
return asFileImpl(file, openOptions); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
786 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
787 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
788 |
/** |
48083 | 789 |
* Returns a {@code BodySubscriber} which stores the response body in a |
790 |
* file opened with the given name. Has the same effect as calling |
|
791 |
* {@link #asFile(Path, OpenOption...) asFile} with the standard open |
|
792 |
* options {@code CREATE} and {@code WRITE} |
|
793 |
* |
|
794 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
795 |
* 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
|
796 |
* |
48083 | 797 |
* @param file the file to store the body in |
798 |
* @return a body subscriber |
|
799 |
* @throws SecurityException if a security manager has been installed |
|
800 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
801 |
* write access} to the file |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
802 |
*/ |
48083 | 803 |
public static BodySubscriber<Path> asFile(Path file) { |
804 |
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
|
805 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
806 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
807 |
/** |
48083 | 808 |
* Returns a {@code BodySubscriber} which provides the incoming body |
809 |
* data to the provided Consumer of {@code Optional<byte[]>}. Each |
|
810 |
* call to {@link Consumer#accept(java.lang.Object) Consumer.accept()} |
|
811 |
* will contain a non empty {@code Optional}, except for the final |
|
812 |
* invocation after all body data has been read, when the {@code |
|
813 |
* Optional} will be empty. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
814 |
* |
48083 | 815 |
* <p> The {@link HttpResponse} using this subscriber is available after |
816 |
* the entire response has been read. |
|
817 |
* |
|
818 |
* @param consumer a Consumer of byte arrays |
|
819 |
* @return a BodySubscriber |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
820 |
*/ |
48083 | 821 |
public static BodySubscriber<Void> asByteArrayConsumer(Consumer<Optional<byte[]>> consumer) { |
822 |
return new ResponseSubscribers.ConsumerSubscriber(consumer); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
823 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
824 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
825 |
/** |
48083 | 826 |
* Returns a {@code BodySubscriber} which streams the response body as |
827 |
* an {@link InputStream}. |
|
828 |
* |
|
829 |
* <p> The {@link HttpResponse} using this subscriber is available |
|
830 |
* immediately after the response headers have been read, without |
|
831 |
* requiring to wait for the entire body to be processed. The response |
|
832 |
* body can then be read directly from the {@link InputStream}. |
|
833 |
* |
|
834 |
* @apiNote To ensure that all resources associated with the |
|
835 |
* corresponding exchange are properly released the caller must |
|
836 |
* ensure to either read all bytes until EOF is reached, or call |
|
837 |
* {@link InputStream#close} if it is unable or unwilling to do so. |
|
838 |
* Calling {@code close} before exhausting the stream may cause |
|
839 |
* the underlying HTTP connection to be closed and prevent it |
|
840 |
* from being reused for subsequent operations. |
|
841 |
* |
|
842 |
* @return a body subscriber that streams the response body as an |
|
843 |
* {@link InputStream}. |
|
844 |
*/ |
|
845 |
public static BodySubscriber<InputStream> asInputStream() { |
|
846 |
return new ResponseSubscribers.HttpResponseInputStream(); |
|
847 |
} |
|
848 |
||
849 |
/** |
|
850 |
* 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
|
851 |
* 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
|
852 |
* {@link HttpResponse#body()}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
853 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
854 |
* @param <U> The type of the response body |
48083 | 855 |
* @param value the value to return from HttpResponse.body(), may be {@code null} |
856 |
* @return a {@code BodySubscriber} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
857 |
*/ |
48083 | 858 |
public static <U> BodySubscriber<U> discard(U value) { |
859 |
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
|
860 |
} |
48083 | 861 |
|
862 |
/** |
|
863 |
* Returns a {@code BodySubscriber} which buffers data before delivering |
|
864 |
* it to the given downstream subscriber. The subscriber guarantees to |
|
865 |
* deliver {@code buffersize} bytes of data to each invocation of the |
|
866 |
* downstream's {@linkplain #onNext(Object) onNext} method, except for |
|
867 |
* the final invocation, just before {@linkplain #onComplete() onComplete} |
|
868 |
* is invoked. The final invocation of {@code onNext} may contain fewer |
|
869 |
* than {@code buffersize} bytes. |
|
870 |
* |
|
871 |
* <p> The returned subscriber delegates its {@link #getBody()} method |
|
872 |
* to the downstream subscriber. |
|
873 |
* |
|
874 |
* @param downstream the downstream subscriber |
|
875 |
* @param bufferSize the buffer size |
|
876 |
* @return a buffering body subscriber |
|
877 |
* @throws IllegalArgumentException if {@code bufferSize <= 0} |
|
878 |
*/ |
|
879 |
public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream, |
|
880 |
int bufferSize) { |
|
881 |
if (bufferSize <= 0) |
|
882 |
throw new IllegalArgumentException("must be greater than 0"); |
|
883 |
return new BufferingSubscriber<T>(downstream, bufferSize); |
|
884 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
885 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
886 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
887 |
/** |
48083 | 888 |
* A response subscriber for a HTTP/2 multi response. |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
889 |
* {@Incubating} |
48083 | 890 |
* |
891 |
* <p> A multi response comprises a main response, and zero or more additional |
|
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
892 |
* responses. Each additional response is sent by the server in response to |
48083 | 893 |
* requests (PUSH_PROMISEs) that the server also generates. Additional responses are |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
894 |
* typically resources that the server expects the client will need which |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
895 |
* are related to the initial request. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
896 |
* <p> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
897 |
* Note. Instead of implementing this interface, applications should consider |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
898 |
* first using the mechanism (built on this interface) provided by |
48083 | 899 |
* {@link MultiSubscriber#asMap(java.util.function.Function, boolean) |
900 |
* MultiSubscriber.asMap()} which is a slightly simplified, but also |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
901 |
* general purpose interface. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
902 |
* <p> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
903 |
* The server generated requests are also known as <i>push promises</i>. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
904 |
* The server is permitted to send any number of these requests up to the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
905 |
* point where the main response is fully received. Therefore, after |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
906 |
* completion of the main response, the final number of additional |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
907 |
* responses is known. Additional responses may be canceled, but given that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
908 |
* the server does not wait for any acknowledgment before sending the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
909 |
* response, this must be done quickly to avoid unnecessary data transmission. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
910 |
* |
48083 | 911 |
* <p> {@code MultiSubscriber}s are parameterized with a type {@code U} which |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
912 |
* represents some meaningful aggregate of the responses received. This |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
913 |
* would typically be a collection of response or response body objects. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
914 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
915 |
* @param <U> a type representing the aggregated results |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
916 |
* @param <T> a type representing all of the response bodies |
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 |
* @since 9 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
919 |
*/ |
48083 | 920 |
public interface MultiSubscriber<U,T> { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
921 |
/** |
48083 | 922 |
* Called for the main request from the user. This {@link HttpRequest} |
923 |
* parameter is the request that was supplied to {@link |
|
924 |
* HttpClient#sendAsync(HttpRequest, MultiSubscriber)}. The |
|
925 |
* implementation must return an {@link BodyHandler} for the response |
|
926 |
* body. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
927 |
* |
48083 | 928 |
* @param request the request |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
929 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
930 |
* @return an optional body handler |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
931 |
*/ |
48083 | 932 |
BodyHandler<T> onRequest(HttpRequest request); |
933 |
||
934 |
/** |
|
935 |
* Called for each push promise that is received. The {@link HttpRequest} |
|
936 |
* parameter represents the PUSH_PROMISE. The implementation must return |
|
937 |
* an {@code Optional} of {@link BodyHandler} for the response body. |
|
938 |
* Different handlers (of the same type) can be returned for different |
|
939 |
* pushes within the same multi send. If no handler (an empty {@code |
|
940 |
* Optional}) is returned, then the push will be canceled. If required, |
|
941 |
* the {@code CompletableFuture<Void>} supplied to the {@code |
|
942 |
* onFinalPushPromise} parameter of {@link |
|
943 |
* #completion(CompletableFuture, CompletableFuture)} can be used to |
|
944 |
* determine when the final PUSH_PROMISE is received. |
|
945 |
* |
|
946 |
* @param pushPromise the push promise |
|
947 |
* |
|
948 |
* @return an optional body handler |
|
949 |
*/ |
|
950 |
Optional<BodyHandler<T>> onPushPromise(HttpRequest pushPromise); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
951 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
952 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
953 |
* Called for each response received. For each request either one of |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
954 |
* onResponse() or onError() is guaranteed to be called, but not both. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
955 |
* |
48083 | 956 |
* <p> Note: The reason for switching to this callback interface rather |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
957 |
* than using CompletableFutures supplied to onRequest() is that there |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
958 |
* is a subtle interaction between those CFs and the CF returned from |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
959 |
* completion() (or when onComplete() was called formerly). The completion() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
960 |
* CF will not complete until after all of the work done by the onResponse() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
961 |
* calls is done. Whereas if you just create CF's dependent on a supplied |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
962 |
* CF (to onRequest()) then the implementation has no visibility of the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
963 |
* dependent CFs and can't guarantee to call onComplete() (or complete |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
964 |
* the completion() CF) after the dependent CFs complete. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
965 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
966 |
* @param response the response received |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
967 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
968 |
void onResponse(HttpResponse<T> response); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
969 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
970 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
971 |
* Called if an error occurs receiving a response. For each request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
972 |
* either one of onResponse() or onError() is guaranteed to be called, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
973 |
* but not both. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
974 |
* |
44847 | 975 |
* @param request the main request or subsequent push promise |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
976 |
* @param t the Throwable that caused the error |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
977 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
978 |
void onError(HttpRequest request, Throwable t); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
979 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
980 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
981 |
* Returns a {@link java.util.concurrent.CompletableFuture}{@code <U>} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
982 |
* which completes when the aggregate result object itself is available. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
983 |
* It is expected that the returned {@code CompletableFuture} will depend |
48083 | 984 |
* on one of the given {@code CompletableFuture<Void}s which themselves |
985 |
* complete after all individual responses associated with the multi |
|
986 |
* response have completed, or after all push promises have been received. |
|
987 |
* This method is called after {@link #onRequest(HttpRequest)} but |
|
988 |
* before any other methods. |
|
46052 | 989 |
* |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
990 |
* @implNote Implementations might follow the pattern shown below |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
991 |
* <pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
992 |
* {@code |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
993 |
* CompletableFuture<U> completion( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
994 |
* CompletableFuture<Void> onComplete, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
995 |
* CompletableFuture<Void> onFinalPushPromise) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
996 |
* { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
997 |
* return onComplete.thenApply((v) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
998 |
* U u = ... instantiate and populate a U instance |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
999 |
* return u; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1000 |
* }); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1001 |
* } |
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 |
* </pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1004 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1005 |
* @param onComplete a CompletableFuture which completes after all |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1006 |
* responses have been received relating to this multi request. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1007 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1008 |
* @param onFinalPushPromise CompletableFuture which completes after all |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1009 |
* push promises have been received. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1010 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1011 |
* @return the aggregate CF response object |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1012 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1013 |
CompletableFuture<U> completion(CompletableFuture<Void> onComplete, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1014 |
CompletableFuture<Void> onFinalPushPromise); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1015 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1016 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1017 |
* Returns a general purpose handler for multi responses. The aggregated |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1018 |
* result object produced by this handler is a |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1019 |
* {@code Map<HttpRequest,CompletableFuture<HttpResponse<V>>>}. Each |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1020 |
* request (both the original user generated request and each server |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1021 |
* generated push promise) is returned as a key of the map. The value |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1022 |
* corresponding to each key is a |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1023 |
* {@code CompletableFuture<HttpResponse<V>>}. |
48083 | 1024 |
* |
1025 |
* <p> There are two ways to use these handlers, depending on the value |
|
1026 |
* of the <i>completion</I> parameter. If completion is true, then the |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1027 |
* aggregated result will be available after all responses have |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1028 |
* themselves completed. If <i>completion</i> is false, then the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1029 |
* aggregated result will be available immediately after the last push |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1030 |
* promise was received. In the former case, this implies that all the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1031 |
* CompletableFutures in the map values will have completed. In the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1032 |
* latter case, they may or may not have completed yet. |
48083 | 1033 |
* |
1034 |
* <p> The simplest way to use these handlers is to set completion to |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1035 |
* {@code true}, and then all (results) values in the Map will be |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1036 |
* accessible without blocking. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1037 |
* <p> |
48083 | 1038 |
* See {@link #asMap(java.util.function.Function, boolean)} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1039 |
* for a code sample of using this interface. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1040 |
* |
48083 | 1041 |
* <p> See {@link #asMap(Function, boolean)} for a code sample of using |
1042 |
* this interface. |
|
1043 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1044 |
* @param <V> the body type used for all responses |
48083 | 1045 |
* @param reqHandler a function invoked for the user's request and each |
1046 |
* push promise |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1047 |
* @param completion {@code true} if the aggregate CompletableFuture |
48083 | 1048 |
* completes after all responses have been received, |
1049 |
* or {@code false} after all push promises received |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1050 |
* |
48083 | 1051 |
* @return a MultiSubscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1052 |
*/ |
48083 | 1053 |
public static <V> MultiSubscriber<MultiMapResult<V>,V> asMap( |
1054 |
Function<HttpRequest, Optional<HttpResponse.BodyHandler<V>>> reqHandler, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1055 |
boolean completion) { |
48083 | 1056 |
return new MultiSubscriberImpl<V>(reqHandler.andThen(optv -> optv.get()), |
1057 |
reqHandler, |
|
1058 |
completion); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1059 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1060 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1061 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1062 |
* Returns a general purpose handler for multi responses. This is a |
48083 | 1063 |
* convenience method which invokes {@link #asMap(Function,boolean) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1064 |
* asMap(Function, true)} meaning that the aggregate result |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1065 |
* object completes after all responses have been received. |
48083 | 1066 |
* |
1067 |
* <p><b>Example usage:</b> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1068 |
* <br> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1069 |
* <pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1070 |
* {@code |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1071 |
* HttpRequest request = HttpRequest.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1072 |
* .uri(URI.create("https://www.foo.com/")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1073 |
* .GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1074 |
* .build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1075 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1076 |
* HttpClient client = HttpClient.newHttpClient(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1077 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1078 |
* Map<HttpRequest,CompletableFuture<HttpResponse<String>>> results = client |
48083 | 1079 |
* .sendAsync(request, MultiSubscriber.asMap( |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1080 |
* (req) -> Optional.of(HttpResponse.BodyHandler.asString()))) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1081 |
* .join(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1082 |
* }</pre> |
48083 | 1083 |
* |
1084 |
* <p> The lambda in this example is the simplest possible implementation, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1085 |
* where neither the incoming requests are examined, nor the response |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1086 |
* headers, and every push that the server sends is accepted. When the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1087 |
* join() call returns, all {@code HttpResponse}s and their associated |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1088 |
* body objects are available. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1089 |
* |
44847 | 1090 |
* @param <V> the body type used for all responses |
48083 | 1091 |
* @param reqHandler a function invoked for each push promise and the |
1092 |
* main request |
|
1093 |
* @return a MultiSubscriber |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1094 |
*/ |
48083 | 1095 |
public static <V> MultiSubscriber<MultiMapResult<V>,V> asMap( |
1096 |
Function<HttpRequest, Optional<HttpResponse.BodyHandler<V>>> reqHandler) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1097 |
|
48083 | 1098 |
return asMap(reqHandler, true); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1099 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1100 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1101 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1102 |
} |