|
16 | 16 | import java.util.HashMap; |
17 | 17 | import java.util.List; |
18 | 18 | import java.util.Map; |
| 19 | +import java.util.function.Consumer; |
| 20 | +import java.util.stream.Stream; |
19 | 21 |
|
20 | 22 | /** |
21 | 23 | * REST client for the forge platform API ({@code GET /v1/projects}, {@code GET /v1/deployments}). |
@@ -457,39 +459,51 @@ public void streamSse( |
457 | 459 | SseHandler handler, |
458 | 460 | java.util.function.BooleanSupplier cancelled) |
459 | 461 | throws Exception { |
| 462 | + streamSse(accessToken, path, handler, cancelled, ignored -> {}); |
| 463 | + } |
| 464 | + |
| 465 | + public void streamSse( |
| 466 | + String accessToken, |
| 467 | + String path, |
| 468 | + SseHandler handler, |
| 469 | + java.util.function.BooleanSupplier cancelled, |
| 470 | + Consumer<AutoCloseable> closeOnCancel) |
| 471 | + throws Exception { |
460 | 472 | HttpRequest req = |
461 | 473 | HttpRequest.newBuilder(URI.create(baseUrl + path)) |
462 | 474 | .timeout(Duration.ofMinutes(10)) |
463 | 475 | .header("Authorization", "Bearer " + accessToken) |
464 | 476 | .header("Accept", "text/event-stream") |
465 | 477 | .GET() |
466 | 478 | .build(); |
467 | | - HttpResponse<java.util.stream.Stream<String>> res = |
468 | | - http.send(req, HttpResponse.BodyHandlers.ofLines()); |
| 479 | + HttpResponse<Stream<String>> res = http.send(req, HttpResponse.BodyHandlers.ofLines()); |
469 | 480 | if (res.statusCode() / 100 != 2) { |
470 | 481 | throw new ForgeApiException(res.statusCode(), "SSE " + path + " -> HTTP " + res.statusCode()); |
471 | 482 | } |
472 | 483 | String event = "message"; |
473 | 484 | StringBuilder data = new StringBuilder(); |
474 | | - java.util.Iterator<String> it = res.body().iterator(); |
475 | | - while (it.hasNext()) { |
476 | | - if (cancelled.getAsBoolean()) { |
477 | | - break; |
478 | | - } |
479 | | - String line = it.next(); |
480 | | - if (line.isEmpty()) { |
481 | | - if (data.length() > 0) { |
482 | | - handler.onEvent(event, data.toString()); |
| 485 | + try (Stream<String> lines = res.body()) { |
| 486 | + closeOnCancel.accept(lines); |
| 487 | + java.util.Iterator<String> it = lines.iterator(); |
| 488 | + while (it.hasNext()) { |
| 489 | + if (cancelled.getAsBoolean()) { |
| 490 | + break; |
483 | 491 | } |
484 | | - event = "message"; |
485 | | - data.setLength(0); |
486 | | - } else if (line.startsWith("event:")) { |
487 | | - event = line.substring(6).trim(); |
488 | | - } else if (line.startsWith("data:")) { |
489 | | - if (data.length() > 0) { |
490 | | - data.append('\n'); |
| 492 | + String line = it.next(); |
| 493 | + if (line.isEmpty()) { |
| 494 | + if (data.length() > 0) { |
| 495 | + handler.onEvent(event, data.toString()); |
| 496 | + } |
| 497 | + event = "message"; |
| 498 | + data.setLength(0); |
| 499 | + } else if (line.startsWith("event:")) { |
| 500 | + event = line.substring(6).trim(); |
| 501 | + } else if (line.startsWith("data:")) { |
| 502 | + if (data.length() > 0) { |
| 503 | + data.append('\n'); |
| 504 | + } |
| 505 | + data.append(line.substring(5).trim()); |
491 | 506 | } |
492 | | - data.append(line.substring(5).trim()); |
493 | 507 | } |
494 | 508 | } |
495 | 509 | } |
|
0 commit comments