|
1 | 1 | /* |
2 | | - * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
@@ -275,119 +275,6 @@ public void nullBody(HttpResponse<T> resp, Throwable t) { |
275 | 275 | } |
276 | 276 | } |
277 | 277 |
|
278 | | - static final Flow.Subscription NOP = new Flow.Subscription() { |
279 | | - @Override |
280 | | - public void request(long n) { } |
281 | | - public void cancel() { } |
282 | | - }; |
283 | | - |
284 | | - /** |
285 | | - * The Http1AsyncReceiver ensures that all calls to |
286 | | - * the subscriber, including onSubscribe, occur sequentially. |
287 | | - * There could however be some race conditions that could happen |
288 | | - * in case of unexpected errors thrown at unexpected places, which |
289 | | - * may cause onError to be called multiple times. |
290 | | - * The Http1BodySubscriber will ensure that the user subscriber |
291 | | - * is actually completed only once - and only after it is |
292 | | - * subscribed. |
293 | | - * @param <U> The type of response. |
294 | | - */ |
295 | | - final static class Http1BodySubscriber<U> implements TrustedSubscriber<U> { |
296 | | - final HttpResponse.BodySubscriber<U> userSubscriber; |
297 | | - final AtomicBoolean completed = new AtomicBoolean(); |
298 | | - volatile Throwable withError; |
299 | | - volatile boolean subscribed; |
300 | | - Http1BodySubscriber(HttpResponse.BodySubscriber<U> userSubscriber) { |
301 | | - this.userSubscriber = userSubscriber; |
302 | | - } |
303 | | - |
304 | | - @Override |
305 | | - public boolean needsExecutor() { |
306 | | - return TrustedSubscriber.needsExecutor(userSubscriber); |
307 | | - } |
308 | | - |
309 | | - // propagate the error to the user subscriber, even if not |
310 | | - // subscribed yet. |
311 | | - private void propagateError(Throwable t) { |
312 | | - assert t != null; |
313 | | - try { |
314 | | - // if unsubscribed at this point, it will not |
315 | | - // get subscribed later - so do it now and |
316 | | - // propagate the error |
317 | | - if (subscribed == false) { |
318 | | - subscribed = true; |
319 | | - userSubscriber.onSubscribe(NOP); |
320 | | - } |
321 | | - } finally { |
322 | | - // if onError throws then there is nothing to do |
323 | | - // here: let the caller deal with it by logging |
324 | | - // and closing the connection. |
325 | | - userSubscriber.onError(t); |
326 | | - } |
327 | | - } |
328 | | - |
329 | | - // complete the subscriber, either normally or exceptionally |
330 | | - // ensure that the subscriber is completed only once. |
331 | | - private void complete(Throwable t) { |
332 | | - if (completed.compareAndSet(false, true)) { |
333 | | - t = withError = Utils.getCompletionCause(t); |
334 | | - if (t == null) { |
335 | | - assert subscribed; |
336 | | - try { |
337 | | - userSubscriber.onComplete(); |
338 | | - } catch (Throwable x) { |
339 | | - // Simply propagate the error by calling |
340 | | - // onError on the user subscriber, and let the |
341 | | - // connection be reused since we should have received |
342 | | - // and parsed all the bytes when we reach here. |
343 | | - // If onError throws in turn, then we will simply |
344 | | - // let that new exception flow up to the caller |
345 | | - // and let it deal with it. |
346 | | - // (i.e: log and close the connection) |
347 | | - // Note that rethrowing here could introduce a |
348 | | - // race that might cause the next send() operation to |
349 | | - // fail as the connection has already been put back |
350 | | - // into the cache when we reach here. |
351 | | - propagateError(t = withError = Utils.getCompletionCause(x)); |
352 | | - } |
353 | | - } else { |
354 | | - propagateError(t); |
355 | | - } |
356 | | - } |
357 | | - } |
358 | | - |
359 | | - @Override |
360 | | - public CompletionStage<U> getBody() { |
361 | | - return userSubscriber.getBody(); |
362 | | - } |
363 | | - |
364 | | - @Override |
365 | | - public void onSubscribe(Flow.Subscription subscription) { |
366 | | - if (!subscribed) { |
367 | | - subscribed = true; |
368 | | - userSubscriber.onSubscribe(subscription); |
369 | | - } else { |
370 | | - // could be already subscribed and completed |
371 | | - // if an unexpected error occurred before the actual |
372 | | - // subscription - though that's not supposed |
373 | | - // happen. |
374 | | - assert completed.get(); |
375 | | - } |
376 | | - } |
377 | | - @Override |
378 | | - public void onNext(List<ByteBuffer> item) { |
379 | | - assert !completed.get(); |
380 | | - userSubscriber.onNext(item); |
381 | | - } |
382 | | - @Override |
383 | | - public void onError(Throwable throwable) { |
384 | | - complete(throwable); |
385 | | - } |
386 | | - @Override |
387 | | - public void onComplete() { |
388 | | - complete(null); |
389 | | - } |
390 | | - } |
391 | 278 |
|
392 | 279 | public <U> CompletableFuture<U> readBody(HttpResponse.BodySubscriber<U> p, |
393 | 280 | boolean return2Cache, |
|
0 commit comments