use of io.helidon.common.reactive.Multi in project helidon by oracle.
the class JacksonBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
context.contentType(contentType);
AtomicBoolean first = new AtomicBoolean(true);
JacksonBodyWriter.ObjectToChunks objectToChunks = new JacksonBodyWriter.ObjectToChunks(objectMapper, context.charset());
return Multi.create(publisher).flatMap(objectToChunks).flatMap(it -> {
if (first.getAndSet(false)) {
// first record, do not prepend a comma
return Multi.just(DataChunk.create(ARRAY_JSON_BEGIN_BYTES), it);
} else {
// any subsequent record starts with a comma
return Multi.just(DataChunk.create(COMMA_BYTES), it);
}
}).onCompleteResume(DataChunk.create(ARRAY_JSON_END_BYTES));
}
use of io.helidon.common.reactive.Multi in project helidon by oracle.
the class JsonpStreamWriterTest method write.
private JsonArray write(Multi<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WRITER.write(publisher, type, CONTEXT).map(DataChunk::bytes).forEach(it -> {
try {
baos.write(it);
} catch (IOException ignored) {
// ignored
}
}).await();
return JSON_PARSER.createReader(new ByteArrayInputStream(baos.toByteArray())).readArray();
}
use of io.helidon.common.reactive.Multi in project helidon by oracle.
the class JsonpBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
context.contentType(contentType);
// we do not have join operator
AtomicBoolean first = new AtomicBoolean(true);
JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
return Multi.create(publisher).map(jsonToChunks).flatMap(it -> {
if (first.getAndSet(false)) {
// first record, do not prepend a comma
return Multi.just(DataChunk.create(ARRAY_JSON_BEGIN_BYTES), it);
} else {
// any subsequent record starts with a comma
return Multi.just(DataChunk.create(COMMA_BYTES), it);
}
}).onCompleteResume(DataChunk.create(ARRAY_JSON_END_BYTES));
}
use of io.helidon.common.reactive.Multi in project helidon by oracle.
the class JsonbNdBodyStreamWriter method write.
@Override
public Multi<DataChunk> write(Flow.Publisher<?> publisher, GenericType<?> type, MessageBodyWriterContext context) {
MediaType contentType = MediaType.APPLICATION_X_NDJSON;
context.contentType(contentType);
AtomicBoolean first = new AtomicBoolean(true);
return Multi.create(publisher).map(object -> DataChunk.create(jsonb.toJson(object).getBytes(StandardCharsets.UTF_8))).flatMap(dataChunk -> {
if (first.getAndSet(false)) {
return Single.just(dataChunk);
} else {
return Multi.just(DataChunk.create(NL), dataChunk);
}
});
}
use of io.helidon.common.reactive.Multi in project helidon by oracle.
the class JdbcStatementQuery method doExecute.
private Multi<DbRow> doExecute(DbClientServiceContext dbContext, Connection connection, CompletableFuture<Void> statementFuture, CompletableFuture<Long> queryFuture) {
// all below must run in an executor service, as it is blocking
CompletableFuture<Multi<DbRow>> result = new CompletableFuture<>();
executorService().submit(() -> {
PreparedStatement statement;
try {
// first try block is to create a statement
statement = super.build(connection, dbContext);
} catch (Exception e) {
result.completeExceptionally(e);
statementFuture.completeExceptionally(e);
queryFuture.completeExceptionally(e);
return;
}
try {
ResultSet rs = statement.executeQuery();
// at this moment we have a DbRows
statementFuture.complete(null);
result.complete(processResultSet(executorService(), dbMapperManager(), mapperManager(), queryFuture, rs));
} catch (Throwable e) {
LOGGER.log(Level.FINEST, String.format("Failed to execute query %s: %s", statement.toString(), e.getMessage()), e);
result.completeExceptionally(e);
statementFuture.completeExceptionally(e);
}
});
return Single.create(result).flatMap(Function.identity());
}
Aggregations