Search in sources :

Example 1 with Multi

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));
}
Also used : Objects(java.util.Objects) Flow(java.util.concurrent.Flow) DataChunk(io.helidon.common.http.DataChunk) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) GenericType(io.helidon.common.GenericType) StandardCharsets(java.nio.charset.StandardCharsets) Multi(io.helidon.common.reactive.Multi) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType)

Example 2 with Multi

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();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonReaderFactory(jakarta.json.JsonReaderFactory) DataChunk(io.helidon.common.http.DataChunk) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) IOException(java.io.IOException) HashParameters(io.helidon.common.http.HashParameters) GenericType(io.helidon.common.GenericType) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) Json(jakarta.json.Json) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) Test(org.junit.jupiter.api.Test) JsonStructure(jakarta.json.JsonStructure) ByteArrayInputStream(java.io.ByteArrayInputStream) MessageBodyOperator(io.helidon.media.common.MessageBodyOperator) Map(java.util.Map) JsonObject(jakarta.json.JsonObject) Assertions.assertAll(org.junit.jupiter.api.Assertions.assertAll) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) JsonArray(jakarta.json.JsonArray) Multi(io.helidon.common.reactive.Multi) ByteArrayInputStream(java.io.ByteArrayInputStream) DataChunk(io.helidon.common.http.DataChunk) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with Multi

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));
}
Also used : JsonWriterFactory(jakarta.json.JsonWriterFactory) JsonStructure(jakarta.json.JsonStructure) Publisher(java.util.concurrent.Flow.Publisher) DataChunk(io.helidon.common.http.DataChunk) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) GenericType(io.helidon.common.GenericType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks) StandardCharsets(java.nio.charset.StandardCharsets) Multi(io.helidon.common.reactive.Multi) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)

Example 4 with Multi

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);
        }
    });
}
Also used : DataChunk(io.helidon.common.http.DataChunk) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) GenericType(io.helidon.common.GenericType) StandardCharsets(java.nio.charset.StandardCharsets) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) Objects(java.util.Objects) Jsonb(jakarta.json.bind.Jsonb) Flow(java.util.concurrent.Flow) Optional(java.util.Optional) Single(io.helidon.common.reactive.Single) Multi(io.helidon.common.reactive.Multi) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType)

Example 5 with Multi

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());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ResultSet(java.sql.ResultSet) Multi(io.helidon.common.reactive.Multi) PreparedStatement(java.sql.PreparedStatement) TimeoutException(java.util.concurrent.TimeoutException) MapperException(io.helidon.common.mapper.MapperException) SQLException(java.sql.SQLException) CancellationException(java.util.concurrent.CancellationException)

Aggregations

Multi (io.helidon.common.reactive.Multi)11 DataChunk (io.helidon.common.http.DataChunk)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 GenericType (io.helidon.common.GenericType)5 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)5 Flow (java.util.concurrent.Flow)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)5 Test (org.junit.jupiter.api.Test)5 MediaType (io.helidon.common.http.MediaType)4 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)4 IOException (java.io.IOException)4 Json (jakarta.json.Json)3 JsonStructure (jakarta.json.JsonStructure)3 StandardCharsets (java.nio.charset.StandardCharsets)3 List (java.util.List)3 CompletionStage (java.util.concurrent.CompletionStage)3 Single (io.helidon.common.reactive.Single)2 Config (io.helidon.config.Config)2 DbRow (io.helidon.dbclient.DbRow)2 CONFIG (io.helidon.tests.integration.dbclient.common.AbstractIT.CONFIG)2