author | prappo |
Tue, 21 Nov 2017 17:17:37 +0300 | |
branch | http-client-branch |
changeset 55847 | 3bac3bca4adb |
parent 55803 | 259cf67b22ec |
child 55858 | cd5eeec735fb |
permissions | -rw-r--r-- |
55763 | 1 |
/* |
2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.incubator.http; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.lang.System.Logger.Level; |
|
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.channels.FileChannel; |
|
33 |
import java.nio.file.OpenOption; |
|
34 |
import java.nio.file.Path; |
|
35 |
import java.security.AccessControlContext; |
|
36 |
import java.security.AccessController; |
|
37 |
import java.security.PrivilegedActionException; |
|
38 |
import java.security.PrivilegedExceptionAction; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Iterator; |
|
41 |
import java.util.List; |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
42 |
import java.util.Objects; |
55763 | 43 |
import java.util.Optional; |
44 |
import java.util.concurrent.ArrayBlockingQueue; |
|
45 |
import java.util.concurrent.BlockingQueue; |
|
46 |
import java.util.concurrent.CompletableFuture; |
|
47 |
import java.util.concurrent.CompletionStage; |
|
48 |
import java.util.concurrent.ConcurrentHashMap; |
|
49 |
import java.util.concurrent.Flow; |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
50 |
import java.util.concurrent.atomic.AtomicBoolean; |
55763 | 51 |
import java.util.function.Consumer; |
52 |
import java.util.function.Function; |
|
53 |
import jdk.incubator.http.internal.common.MinimalFuture; |
|
54 |
import jdk.incubator.http.internal.common.Utils; |
|
55 |
||
56 |
class ResponseSubscribers { |
|
57 |
||
58 |
static class ConsumerSubscriber implements HttpResponse.BodySubscriber<Void> { |
|
59 |
private final Consumer<Optional<byte[]>> consumer; |
|
60 |
private Flow.Subscription subscription; |
|
61 |
private final CompletableFuture<Void> result = new MinimalFuture<>(); |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
62 |
private final AtomicBoolean subscribed = new AtomicBoolean(); |
55763 | 63 |
|
64 |
ConsumerSubscriber(Consumer<Optional<byte[]>> consumer) { |
|
65 |
this.consumer = consumer; |
|
66 |
} |
|
67 |
||
68 |
@Override |
|
69 |
public CompletionStage<Void> getBody() { |
|
70 |
return result; |
|
71 |
} |
|
72 |
||
73 |
@Override |
|
74 |
public void onSubscribe(Flow.Subscription subscription) { |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
75 |
if (!subscribed.compareAndSet(false, true)) { |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
76 |
subscription.cancel(); |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
77 |
} else { |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
78 |
this.subscription = subscription; |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
79 |
subscription.request(1); |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
80 |
} |
55763 | 81 |
} |
82 |
||
83 |
@Override |
|
84 |
public void onNext(List<ByteBuffer> items) { |
|
85 |
for (ByteBuffer item : items) { |
|
86 |
byte[] buf = new byte[item.remaining()]; |
|
87 |
item.get(buf); |
|
88 |
consumer.accept(Optional.of(buf)); |
|
89 |
} |
|
90 |
subscription.request(1); |
|
91 |
} |
|
92 |
||
93 |
@Override |
|
94 |
public void onError(Throwable throwable) { |
|
95 |
result.completeExceptionally(throwable); |
|
96 |
} |
|
97 |
||
98 |
@Override |
|
99 |
public void onComplete() { |
|
100 |
consumer.accept(Optional.empty()); |
|
101 |
result.complete(null); |
|
102 |
} |
|
103 |
||
104 |
} |
|
105 |
||
106 |
static class PathSubscriber implements HttpResponse.BodySubscriber<Path> { |
|
107 |
||
108 |
private final Path file; |
|
109 |
private final CompletableFuture<Path> result = new MinimalFuture<>(); |
|
110 |
||
111 |
private volatile Flow.Subscription subscription; |
|
112 |
private volatile FileChannel out; |
|
113 |
private volatile AccessControlContext acc; |
|
114 |
private final OpenOption[] options; |
|
115 |
||
116 |
PathSubscriber(Path file, OpenOption... options) { |
|
117 |
this.file = file; |
|
118 |
this.options = options; |
|
119 |
} |
|
120 |
||
121 |
void setAccessControlContext(AccessControlContext acc) { |
|
122 |
this.acc = acc; |
|
123 |
} |
|
124 |
||
125 |
@Override |
|
126 |
public void onSubscribe(Flow.Subscription subscription) { |
|
127 |
if (System.getSecurityManager() != null && acc == null) |
|
128 |
throw new InternalError( |
|
129 |
"Unexpected null acc when security manager has been installed"); |
|
130 |
||
131 |
this.subscription = subscription; |
|
132 |
try { |
|
133 |
PrivilegedExceptionAction<FileChannel> pa = |
|
134 |
() -> FileChannel.open(file, options); |
|
135 |
out = AccessController.doPrivileged(pa, acc); |
|
136 |
} catch (PrivilegedActionException pae) { |
|
137 |
Throwable t = pae.getCause() != null ? pae.getCause() : pae; |
|
138 |
result.completeExceptionally(t); |
|
139 |
subscription.cancel(); |
|
140 |
return; |
|
141 |
} |
|
142 |
subscription.request(1); |
|
143 |
} |
|
144 |
||
145 |
@Override |
|
146 |
public void onNext(List<ByteBuffer> items) { |
|
147 |
try { |
|
55803 | 148 |
out.write(items.toArray(Utils.EMPTY_BB_ARRAY)); |
55763 | 149 |
} catch (IOException ex) { |
150 |
Utils.close(out); |
|
151 |
subscription.cancel(); |
|
152 |
result.completeExceptionally(ex); |
|
153 |
} |
|
154 |
subscription.request(1); |
|
155 |
} |
|
156 |
||
157 |
@Override |
|
158 |
public void onError(Throwable e) { |
|
159 |
result.completeExceptionally(e); |
|
160 |
Utils.close(out); |
|
161 |
} |
|
162 |
||
163 |
@Override |
|
164 |
public void onComplete() { |
|
165 |
Utils.close(out); |
|
166 |
result.complete(file); |
|
167 |
} |
|
168 |
||
169 |
@Override |
|
170 |
public CompletionStage<Path> getBody() { |
|
171 |
return result; |
|
172 |
} |
|
173 |
} |
|
174 |
||
175 |
static class ByteArraySubscriber<T> implements HttpResponse.BodySubscriber<T> { |
|
176 |
private final Function<byte[], T> finisher; |
|
177 |
private final CompletableFuture<T> result = new MinimalFuture<>(); |
|
178 |
private final List<ByteBuffer> received = new ArrayList<>(); |
|
179 |
||
180 |
private volatile Flow.Subscription subscription; |
|
181 |
||
182 |
ByteArraySubscriber(Function<byte[],T> finisher) { |
|
183 |
this.finisher = finisher; |
|
184 |
} |
|
185 |
||
186 |
@Override |
|
187 |
public void onSubscribe(Flow.Subscription subscription) { |
|
188 |
if (this.subscription != null) { |
|
189 |
subscription.cancel(); |
|
190 |
return; |
|
191 |
} |
|
192 |
this.subscription = subscription; |
|
193 |
// We can handle whatever you've got |
|
194 |
subscription.request(Long.MAX_VALUE); |
|
195 |
} |
|
196 |
||
197 |
@Override |
|
198 |
public void onNext(List<ByteBuffer> items) { |
|
199 |
// incoming buffers are allocated by http client internally, |
|
200 |
// and won't be used anywhere except this place. |
|
201 |
// So it's free simply to store them for further processing. |
|
202 |
assert Utils.hasRemaining(items); |
|
203 |
Utils.accumulateBuffers(received, items); |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public void onError(Throwable throwable) { |
|
208 |
received.clear(); |
|
209 |
result.completeExceptionally(throwable); |
|
210 |
} |
|
211 |
||
212 |
static private byte[] join(List<ByteBuffer> bytes) { |
|
213 |
int size = Utils.remaining(bytes, Integer.MAX_VALUE); |
|
214 |
byte[] res = new byte[size]; |
|
215 |
int from = 0; |
|
216 |
for (ByteBuffer b : bytes) { |
|
217 |
int l = b.remaining(); |
|
218 |
b.get(res, from, l); |
|
219 |
from += l; |
|
220 |
} |
|
221 |
return res; |
|
222 |
} |
|
223 |
||
224 |
@Override |
|
225 |
public void onComplete() { |
|
226 |
try { |
|
227 |
result.complete(finisher.apply(join(received))); |
|
228 |
received.clear(); |
|
229 |
} catch (IllegalArgumentException e) { |
|
230 |
result.completeExceptionally(e); |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
public CompletionStage<T> getBody() { |
|
236 |
return result; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* An InputStream built on top of the Flow API. |
|
242 |
*/ |
|
243 |
static class HttpResponseInputStream extends InputStream |
|
244 |
implements HttpResponse.BodySubscriber<InputStream> |
|
245 |
{ |
|
246 |
final static boolean DEBUG = Utils.DEBUG; |
|
247 |
final static int MAX_BUFFERS_IN_QUEUE = 1; // lock-step with the producer |
|
248 |
||
249 |
// An immutable ByteBuffer sentinel to mark that the last byte was received. |
|
250 |
private static final ByteBuffer LAST_BUFFER = ByteBuffer.wrap(new byte[0]); |
|
251 |
private static final List<ByteBuffer> LAST_LIST = List.of(LAST_BUFFER); |
|
252 |
private static final System.Logger DEBUG_LOGGER = |
|
253 |
Utils.getDebugLogger("HttpResponseInputStream"::toString, DEBUG); |
|
254 |
||
255 |
// A queue of yet unprocessed ByteBuffers received from the flow API. |
|
256 |
private final BlockingQueue<List<ByteBuffer>> buffers; |
|
257 |
private volatile Flow.Subscription subscription; |
|
258 |
private volatile boolean closed; |
|
259 |
private volatile Throwable failed; |
|
260 |
private volatile Iterator<ByteBuffer> currentListItr; |
|
261 |
private volatile ByteBuffer currentBuffer; |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
262 |
private final AtomicBoolean subscribed = new AtomicBoolean(); |
55763 | 263 |
|
264 |
HttpResponseInputStream() { |
|
265 |
this(MAX_BUFFERS_IN_QUEUE); |
|
266 |
} |
|
267 |
||
268 |
HttpResponseInputStream(int maxBuffers) { |
|
269 |
int capacity = (maxBuffers <= 0 ? MAX_BUFFERS_IN_QUEUE : maxBuffers); |
|
270 |
// 1 additional slot needed for LAST_LIST added by onComplete |
|
271 |
this.buffers = new ArrayBlockingQueue<>(capacity + 1); |
|
272 |
} |
|
273 |
||
274 |
@Override |
|
275 |
public CompletionStage<InputStream> getBody() { |
|
276 |
// Returns the stream immediately, before the |
|
277 |
// response body is received. |
|
278 |
// This makes it possible for senAsync().get().body() |
|
279 |
// to complete before the response body is received. |
|
280 |
return CompletableFuture.completedStage(this); |
|
281 |
} |
|
282 |
||
283 |
// Returns the current byte buffer to read from. |
|
284 |
// If the current buffer has no remaining data, this method will take the |
|
285 |
// next buffer from the buffers queue, possibly blocking until |
|
286 |
// a new buffer is made available through the Flow API, or the |
|
287 |
// end of the flow has been reached. |
|
288 |
private ByteBuffer current() throws IOException { |
|
289 |
while (currentBuffer == null || !currentBuffer.hasRemaining()) { |
|
290 |
// Check whether the stream is closed or exhausted |
|
291 |
if (closed || failed != null) { |
|
292 |
throw new IOException("closed", failed); |
|
293 |
} |
|
294 |
if (currentBuffer == LAST_BUFFER) break; |
|
295 |
||
296 |
try { |
|
297 |
if (currentListItr == null || !currentListItr.hasNext()) { |
|
298 |
// Take a new list of buffers from the queue, blocking |
|
299 |
// if none is available yet... |
|
300 |
||
301 |
DEBUG_LOGGER.log(Level.DEBUG, "Taking list of Buffers"); |
|
302 |
List<ByteBuffer> lb = buffers.take(); |
|
303 |
currentListItr = lb.iterator(); |
|
304 |
DEBUG_LOGGER.log(Level.DEBUG, "List of Buffers Taken"); |
|
305 |
||
306 |
// Check whether an exception was encountered upstream |
|
307 |
if (closed || failed != null) |
|
308 |
throw new IOException("closed", failed); |
|
309 |
||
310 |
// Check whether we're done. |
|
311 |
if (lb == LAST_LIST) { |
|
312 |
currentListItr = null; |
|
313 |
currentBuffer = LAST_BUFFER; |
|
314 |
break; |
|
315 |
} |
|
316 |
||
317 |
// Request another upstream item ( list of buffers ) |
|
318 |
Flow.Subscription s = subscription; |
|
319 |
if (s != null) { |
|
320 |
DEBUG_LOGGER.log(Level.DEBUG, "Increased demand by 1"); |
|
321 |
s.request(1); |
|
322 |
} |
|
323 |
} |
|
324 |
assert currentListItr != null; |
|
325 |
assert currentListItr.hasNext(); |
|
326 |
DEBUG_LOGGER.log(Level.DEBUG, "Next Buffer"); |
|
327 |
currentBuffer = currentListItr.next(); |
|
328 |
} catch (InterruptedException ex) { |
|
329 |
// continue |
|
330 |
} |
|
331 |
} |
|
332 |
assert currentBuffer == LAST_BUFFER || currentBuffer.hasRemaining(); |
|
333 |
return currentBuffer; |
|
334 |
} |
|
335 |
||
336 |
@Override |
|
337 |
public int read(byte[] bytes, int off, int len) throws IOException { |
|
338 |
// get the buffer to read from, possibly blocking if |
|
339 |
// none is available |
|
340 |
ByteBuffer buffer; |
|
341 |
if ((buffer = current()) == LAST_BUFFER) return -1; |
|
342 |
||
343 |
// don't attempt to read more than what is available |
|
344 |
// in the current buffer. |
|
345 |
int read = Math.min(buffer.remaining(), len); |
|
346 |
assert read > 0 && read <= buffer.remaining(); |
|
347 |
||
348 |
// buffer.get() will do the boundary check for us. |
|
349 |
buffer.get(bytes, off, read); |
|
350 |
return read; |
|
351 |
} |
|
352 |
||
353 |
@Override |
|
354 |
public int read() throws IOException { |
|
355 |
ByteBuffer buffer; |
|
356 |
if ((buffer = current()) == LAST_BUFFER) return -1; |
|
357 |
return buffer.get() & 0xFF; |
|
358 |
} |
|
359 |
||
360 |
@Override |
|
361 |
public void onSubscribe(Flow.Subscription s) { |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
362 |
if (!subscribed.compareAndSet(false, true)) { |
55763 | 363 |
s.cancel(); |
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
364 |
} else { |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
365 |
this.subscription = s; |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
366 |
assert buffers.remainingCapacity() > 1; // should contain at least 2 |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
367 |
DEBUG_LOGGER.log(Level.DEBUG, () -> "onSubscribe: requesting " |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
368 |
+ Math.max(1, buffers.remainingCapacity() - 1)); |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
369 |
s.request(Math.max(1, buffers.remainingCapacity() - 1)); |
55763 | 370 |
} |
371 |
} |
|
372 |
||
373 |
@Override |
|
374 |
public void onNext(List<ByteBuffer> t) { |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
375 |
Objects.requireNonNull(t); |
55763 | 376 |
try { |
377 |
DEBUG_LOGGER.log(Level.DEBUG, "next item received"); |
|
378 |
if (!buffers.offer(t)) { |
|
379 |
throw new IllegalStateException("queue is full"); |
|
380 |
} |
|
381 |
DEBUG_LOGGER.log(Level.DEBUG, "item offered"); |
|
382 |
} catch (Exception ex) { |
|
383 |
failed = ex; |
|
384 |
try { |
|
385 |
close(); |
|
386 |
} catch (IOException ex1) { |
|
387 |
// OK |
|
388 |
} |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
@Override |
|
393 |
public void onError(Throwable thrwbl) { |
|
394 |
subscription = null; |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
395 |
failed = Objects.requireNonNull(thrwbl); |
55776
9950bc2ee874
http-client-branch: ensure ResponseSubscribers.HttpResponseInputStreamTest::read will throw an exception when onError is called
dfuchs
parents:
55763
diff
changeset
|
396 |
// The client process that reads the input stream might |
9950bc2ee874
http-client-branch: ensure ResponseSubscribers.HttpResponseInputStreamTest::read will throw an exception when onError is called
dfuchs
parents:
55763
diff
changeset
|
397 |
// be blocked in queue.take(). |
9950bc2ee874
http-client-branch: ensure ResponseSubscribers.HttpResponseInputStreamTest::read will throw an exception when onError is called
dfuchs
parents:
55763
diff
changeset
|
398 |
// Tries to offer LAST_LIST to the queue. If the queue is |
9950bc2ee874
http-client-branch: ensure ResponseSubscribers.HttpResponseInputStreamTest::read will throw an exception when onError is called
dfuchs
parents:
55763
diff
changeset
|
399 |
// full we don't care if we can't insert this buffer, as |
9950bc2ee874
http-client-branch: ensure ResponseSubscribers.HttpResponseInputStreamTest::read will throw an exception when onError is called
dfuchs
parents:
55763
diff
changeset
|
400 |
// the client can't be blocked in queue.take() in that case. |
9950bc2ee874
http-client-branch: ensure ResponseSubscribers.HttpResponseInputStreamTest::read will throw an exception when onError is called
dfuchs
parents:
55763
diff
changeset
|
401 |
// Adding LAST_LIST to the queue is harmless, as the client |
9950bc2ee874
http-client-branch: ensure ResponseSubscribers.HttpResponseInputStreamTest::read will throw an exception when onError is called
dfuchs
parents:
55763
diff
changeset
|
402 |
// should find failed != null before handling LAST_LIST. |
9950bc2ee874
http-client-branch: ensure ResponseSubscribers.HttpResponseInputStreamTest::read will throw an exception when onError is called
dfuchs
parents:
55763
diff
changeset
|
403 |
buffers.offer(LAST_LIST); |
55763 | 404 |
} |
405 |
||
406 |
@Override |
|
407 |
public void onComplete() { |
|
408 |
subscription = null; |
|
409 |
onNext(LAST_LIST); |
|
410 |
} |
|
411 |
||
412 |
@Override |
|
413 |
public void close() throws IOException { |
|
414 |
synchronized (this) { |
|
415 |
if (closed) return; |
|
416 |
closed = true; |
|
417 |
} |
|
418 |
Flow.Subscription s = subscription; |
|
419 |
subscription = null; |
|
420 |
if (s != null) { |
|
421 |
s.cancel(); |
|
422 |
} |
|
423 |
super.close(); |
|
424 |
} |
|
425 |
||
426 |
} |
|
427 |
||
428 |
static class MultiSubscriberImpl<V> |
|
429 |
implements HttpResponse.MultiSubscriber<MultiMapResult<V>,V> |
|
430 |
{ |
|
431 |
private final MultiMapResult<V> results; |
|
432 |
private final Function<HttpRequest,Optional<HttpResponse.BodyHandler<V>>> pushHandler; |
|
433 |
private final Function<HttpRequest,HttpResponse.BodyHandler<V>> requestHandler; |
|
434 |
private final boolean completion; // aggregate completes on last PP received or overall completion |
|
435 |
||
436 |
MultiSubscriberImpl( |
|
437 |
Function<HttpRequest,HttpResponse.BodyHandler<V>> requestHandler, |
|
438 |
Function<HttpRequest,Optional<HttpResponse.BodyHandler<V>>> pushHandler, boolean completion) { |
|
439 |
this.results = new MultiMapResult<>(new ConcurrentHashMap<>()); |
|
440 |
this.requestHandler = requestHandler; |
|
441 |
this.pushHandler = pushHandler; |
|
442 |
this.completion = completion; |
|
443 |
} |
|
444 |
||
445 |
@Override |
|
446 |
public HttpResponse.BodyHandler<V> onRequest(HttpRequest request) { |
|
447 |
CompletableFuture<HttpResponse<V>> cf = MinimalFuture.newMinimalFuture(); |
|
448 |
results.put(request, cf); |
|
449 |
return requestHandler.apply(request); |
|
450 |
} |
|
451 |
||
452 |
@Override |
|
453 |
public Optional<HttpResponse.BodyHandler<V>> onPushPromise(HttpRequest push) { |
|
454 |
CompletableFuture<HttpResponse<V>> cf = MinimalFuture.newMinimalFuture(); |
|
455 |
results.put(push, cf); |
|
456 |
return pushHandler.apply(push); |
|
457 |
} |
|
458 |
||
459 |
@Override |
|
460 |
public void onResponse(HttpResponse<V> response) { |
|
461 |
CompletableFuture<HttpResponse<V>> cf = results.get(response.request()); |
|
462 |
cf.complete(response); |
|
463 |
} |
|
464 |
||
465 |
@Override |
|
466 |
public void onError(HttpRequest request, Throwable t) { |
|
467 |
CompletableFuture<HttpResponse<V>> cf = results.get(request); |
|
468 |
cf.completeExceptionally(t); |
|
469 |
} |
|
470 |
||
471 |
@Override |
|
472 |
public CompletableFuture<MultiMapResult<V>> completion( |
|
473 |
CompletableFuture<Void> onComplete, CompletableFuture<Void> onFinalPushPromise) { |
|
474 |
if (completion) |
|
475 |
return onComplete.thenApply((ignored)-> results); |
|
476 |
else |
|
477 |
return onFinalPushPromise.thenApply((ignored) -> results); |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
/** |
|
482 |
* Currently this consumes all of the data and ignores it |
|
483 |
*/ |
|
484 |
static class NullSubscriber<T> implements HttpResponse.BodySubscriber<T> { |
|
485 |
||
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
486 |
private final CompletableFuture<T> cf = new MinimalFuture<>(); |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
487 |
private final Optional<T> result; |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
488 |
private final AtomicBoolean subscribed = new AtomicBoolean(); |
55763 | 489 |
|
490 |
NullSubscriber(Optional<T> result) { |
|
491 |
this.result = result; |
|
492 |
} |
|
493 |
||
494 |
@Override |
|
495 |
public void onSubscribe(Flow.Subscription subscription) { |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
496 |
if (!subscribed.compareAndSet(false, true)) { |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
497 |
subscription.cancel(); |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
498 |
} else { |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
499 |
subscription.request(Long.MAX_VALUE); |
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
500 |
} |
55763 | 501 |
} |
502 |
||
503 |
@Override |
|
504 |
public void onNext(List<ByteBuffer> items) { |
|
55847
3bac3bca4adb
http-client-branch: removed RS TCK-incompatible publisher, updated subscribers so they'd pass TCK
prappo
parents:
55803
diff
changeset
|
505 |
Objects.requireNonNull(items); |
55763 | 506 |
} |
507 |
||
508 |
@Override |
|
509 |
public void onError(Throwable throwable) { |
|
510 |
cf.completeExceptionally(throwable); |
|
511 |
} |
|
512 |
||
513 |
@Override |
|
514 |
public void onComplete() { |
|
515 |
if (result.isPresent()) { |
|
516 |
cf.complete(result.get()); |
|
517 |
} else { |
|
518 |
cf.complete(null); |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
@Override |
|
523 |
public CompletionStage<T> getBody() { |
|
524 |
return cf; |
|
525 |
} |
|
526 |
} |
|
527 |
} |