private CompletableFuture<JsonArray> mapResponseToJsonArray(final HttpResponse response) { final CompletionStage<JsonObject> body = response.entity().getDataBytes().fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .map(JsonFactory::readFrom) .map(JsonValue::asObject) .runWith(Sink.head(), httpClient.getActorMaterializer()); final JsonPointer keysPointer = JsonPointer.of("keys"); return body.toCompletableFuture() .thenApply(jsonObject -> jsonObject.getValue(keysPointer).map(JsonValue::asArray) .orElseThrow(() -> new JsonMissingFieldException(keysPointer))) .exceptionally(t -> { throw new IllegalStateException("Failed to extract public keys from JSON response: " + body, t); }); }
private void completeWithResult(final HttpResponse response) { final int statusCode = response.status().intValue(); if (logger.isDebugEnabled()) { logger.debug("Responding with HttpResponse code '{}'", statusCode); logger.debug("Responding with Entity: {}", response.entity()); } httpResponseFuture.complete(response); stop(); }
private Route handleSudoCountThingsPerRequest(final RequestContext ctx, final SudoCountThings command) { final CompletableFuture<HttpResponse> httpResponseFuture = new CompletableFuture<>(); Source.single(command) .to(Sink.actorRef(createHttpPerRequestActor(ctx, httpResponseFuture), HttpRequestActor.COMPLETE_MESSAGE)) .run(materializer); final CompletionStage<HttpResponse> allThingsCountHttpResponse = Source.fromCompletionStage(httpResponseFuture) .flatMapConcat(httpResponse -> httpResponse.entity().getDataBytes()) .fold(ByteString.empty(), ByteString::concat) .map(ByteString::utf8String) .map(Integer::valueOf) .map(count -> JsonObject.newBuilder().set("allThingsCount", count).build()) .map(jsonObject -> HttpResponse.create() .withEntity(ContentTypes.APPLICATION_JSON, ByteString.fromString(jsonObject.toString())) .withStatus(HttpStatusCode.OK.toInt())) .runWith(Sink.head(), materializer); return completeWithFuture(allThingsCountHttpResponse); }