Search in sources :

Example 1 with JsonStructureToChunks

use of io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks 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 2 with JsonStructureToChunks

use of io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks in project helidon by oracle.

the class JsonpNdBodyStreamWriter method write.

@Override
public Multi<DataChunk> write(Flow.Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
    MediaType contentType = context.contentType().or(() -> findMediaType(context)).orElse(MediaType.APPLICATION_X_NDJSON);
    context.contentType(contentType);
    JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
    AtomicBoolean first = new AtomicBoolean(true);
    return Multi.create(publisher).map(jsonToChunks).flatMap(dataChunk -> {
        if (first.getAndSet(false)) {
            return Single.just(dataChunk);
        } else {
            return Multi.just(DataChunk.create(NL), dataChunk);
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)

Example 3 with JsonStructureToChunks

use of io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks in project helidon by oracle.

the class JsonpEsBodyStreamWriter method write.

@Override
public Multi<DataChunk> write(Flow.Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
    MediaType contentType = context.contentType().or(() -> findMediaType(context)).orElse(TEXT_EVENT_STREAM_JSON);
    context.contentType(contentType);
    JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
    return Multi.create(publisher).map(jsonToChunks).flatMap(dataChunk -> Multi.just(DataChunk.create(DATA), dataChunk, DataChunk.create(NL)));
}
Also used : MediaType(io.helidon.common.http.MediaType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)

Aggregations

MediaType (io.helidon.common.http.MediaType)3 JsonStructureToChunks (io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 GenericType (io.helidon.common.GenericType)1 DataChunk (io.helidon.common.http.DataChunk)1 Multi (io.helidon.common.reactive.Multi)1 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)1 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)1 JsonStructure (jakarta.json.JsonStructure)1 JsonWriterFactory (jakarta.json.JsonWriterFactory)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Publisher (java.util.concurrent.Flow.Publisher)1