use of io.helidon.common.http.DataChunk in project helidon by oracle.
the class ClientMain method saveResponseToFile.
static Single<Void> saveResponseToFile(WebClient webClient) {
// We have to create file subscriber first. This subscriber will save the content of the response to the file.
Path file = Paths.get("test.txt");
try {
Files.deleteIfExists(file);
Files.createFile(file);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Downloading server response to file: " + file);
return webClient.get().request().map(WebClientResponse::content).flatMapSingle(content -> content.map(DataChunk::data).flatMapIterable(Arrays::asList).to(IoMulti.writeToFile(file).build())).peek(path -> System.out.println("Download complete!"));
}
use of io.helidon.common.http.DataChunk in project helidon by oracle.
the class WebClientRequestBuilderImpl method invoke.
private Single<WebClientResponse> invoke(Flow.Publisher<DataChunk> requestEntity) {
finalUri = prepareFinalURI();
if (requestId == null) {
requestId = REQUEST_NUMBER.incrementAndGet();
}
// LOGGER.finest(() -> "(client reqID: " + requestId + ") Request final URI: " + uri);
CompletableFuture<WebClientServiceRequest> sent = new CompletableFuture<>();
CompletableFuture<WebClientServiceResponse> responseReceived = new CompletableFuture<>();
CompletableFuture<WebClientServiceResponse> complete = new CompletableFuture<>();
WebClientServiceRequest completedRequest = new WebClientServiceRequestImpl(this, sent, responseReceived, complete);
CompletionStage<WebClientServiceRequest> rcs = CompletableFuture.completedFuture(completedRequest);
for (WebClientService service : services) {
rcs = rcs.thenCompose(service::request).thenApply(servReq -> {
finalUri = recreateURI(servReq);
return servReq;
});
}
Single<WebClientResponse> single = Single.create(rcs.thenCompose(serviceRequest -> {
URI requestUri = relativizeNoProxy(finalUri, proxy, configuration.relativeUris());
requestId = serviceRequest.requestId();
HttpHeaders headers = toNettyHttpHeaders();
DefaultHttpRequest request = new DefaultHttpRequest(toNettyHttpVersion(httpVersion), toNettyMethod(method), requestUri.toASCIIString(), headers);
boolean keepAlive = HttpUtil.isKeepAlive(request);
requestConfiguration = RequestConfiguration.builder(finalUri).update(configuration).followRedirects(followRedirects).clientServiceRequest(serviceRequest).readerContext(readerContext).writerContext(writerContext).connectTimeout(connectTimeout).readTimeout(readTimeout).services(services).context(context).proxy(proxy).keepAlive(keepAlive).requestId(requestId).build();
WebClientRequestImpl clientRequest = new WebClientRequestImpl(this);
CompletableFuture<WebClientResponse> result = new CompletableFuture<>();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventGroup).channel(NioSocketChannel.class).handler(new NettyClientInitializer(requestConfiguration)).option(ChannelOption.SO_KEEPALIVE, keepAlive).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout.toMillis());
ChannelFuture channelFuture = keepAlive ? obtainChannelFuture(requestConfiguration, bootstrap) : bootstrap.connect(finalUri.getHost(), finalUri.getPort());
channelFuture.addListener((ChannelFutureListener) future -> {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest(() -> "(client reqID: " + requestId + ") " + "Channel hashcode -> " + channelFuture.channel().hashCode());
}
channelFuture.channel().attr(REQUEST).set(clientRequest);
channelFuture.channel().attr(RESPONSE_RECEIVED).set(false);
channelFuture.channel().attr(RECEIVED).set(responseReceived);
channelFuture.channel().attr(COMPLETED).set(complete);
channelFuture.channel().attr(WILL_CLOSE).set(!keepAlive);
channelFuture.channel().attr(RESULT).set(result);
channelFuture.channel().attr(REQUEST_ID).set(requestId);
Throwable cause = future.cause();
if (null == cause) {
RequestContentSubscriber requestContentSubscriber = new RequestContentSubscriber(request, channelFuture.channel(), result, sent, allowChunkedEncoding);
requestEntity.subscribe(requestContentSubscriber);
} else {
sent.completeExceptionally(cause);
responseReceived.completeExceptionally(cause);
complete.completeExceptionally(cause);
result.completeExceptionally(new WebClientException(finalUri.toString(), cause));
}
});
return result;
}));
return wrapWithContext(single);
}
use of io.helidon.common.http.DataChunk in project helidon by oracle.
the class JsonContentReaderTest method simpleJsonArray.
@Test
public void simpleJsonArray() throws Exception {
Publisher<DataChunk> chunks = Multi.singleton("[ \"val\" ]").map(s -> DataChunk.create(s.getBytes()));
CompletionStage<? extends JsonArray> stage = JsonpSupport.reader().read(chunks, GenericType.create(JsonArray.class), CONTEXT).toStage();
JsonArray array = stage.toCompletableFuture().get(10, TimeUnit.SECONDS);
assertThat(array.getString(0), Is.is("val"));
}
use of io.helidon.common.http.DataChunk in project helidon by oracle.
the class JsonContentReaderTest method incompatibleTypes.
@Test
public void incompatibleTypes() throws Exception {
Publisher<DataChunk> chunks = Multi.singleton("{ \"p\" : \"val\" }").map(s -> DataChunk.create(s.getBytes()));
CompletionStage<? extends JsonArray> stage = JsonpSupport.reader().read(chunks, GenericType.create(JsonArray.class), CONTEXT).toStage();
try {
JsonArray array = stage.thenApply(o -> {
fail("Shouldn't occur because of JSON exception!");
return o;
}).toCompletableFuture().get(10, TimeUnit.SECONDS);
fail("Should have failed because an expected array is actually an object: " + array);
} catch (ExecutionException e) {
assertThat(e.getCause(), IsInstanceOf.instanceOf(JsonException.class));
}
}
use of io.helidon.common.http.DataChunk in project helidon by oracle.
the class JsonContentReaderTest method simpleJsonObject.
@Test
public void simpleJsonObject() throws Exception {
Publisher<DataChunk> chunks = Multi.singleton("{ \"p\" : \"val\" }").map(s -> DataChunk.create(s.getBytes()));
CompletionStage<? extends JsonObject> stage = JsonpSupport.reader().read(chunks, GenericType.create(JsonObject.class), CONTEXT).toStage();
JsonObject jsonObject = stage.toCompletableFuture().get(10, TimeUnit.SECONDS);
assertThat(jsonObject.getJsonString("p").getString(), Is.is("val"));
}
Aggregations